[R] Returning NA from lm
Neil Beddoe
Neil.Beddoe at mbamfunds.com
Fri Jan 23 18:11:44 CET 2009
If anyone's interested, I got round it by doing:
tryCatch(lm(data~model,na.action=na.fail),error=function(err){NA})
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Neil Beddoe
Sent: 23 January 2009 15:40
To: 'Prof Brian Ripley'
Cc: r-help at r-project.org
Subject: Re: [R] Returning NA from lm
I tried that I'm afraid. I still get values for groups with missing data. I think na.exclude just ignores the NAs but I want it to return NA. Where one of the values is NA.
-----Original Message-----
From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
Sent: 23 January 2009 13:04
To: Neil Beddoe
Cc: r-help at r-project.org
Subject: Re: [R] Returning NA from lm
See ?na.exclude
On Fri, 23 Jan 2009, Neil Beddoe wrote:
> Hi.
>
> I need to apply run a regression analysis for groups of data of fixed length:100 As, 100 Bs, 100 Cs etc.
>
> eg
>
> x
> Key Value
> A 1
> A 21.2
> A 4
> A 6.5
> ...repeat 96 times with differing values of A
> B 1
> B 2.3
> B NA
> B 6.5
> ...repeat 96 times with differing values of B etc
>
> I run these against a linear model using tapply(data$Value,
> data$Key,FUN=regr,100) where
> regr<-function(x,w)
> {
> #run the model against the last w values of x
> lm((x[length(x)-w):length(x)]~myModel(w))
> }
> In the results, I want to return NA for any Key group where one or more of the values is NA. If I run the above I get a regression structure ignoring the missing values and returning values for data that contains NA. Using na.action=na.fail or na.action=NULL causes the whole tapply function to fail and I get nothing. Is there a way I can get lm to return NA if any of the values in the data are NA but valid numbers for complete data?
>
> I realise that I could remove the groups with NAs but I'm running the regressions over multiple time periods and most of the data groups will have a full complement of data for at least some of these periods. It becomes a pain to manage NAs if I do that.
>
> Sorry if the above is a little unclear.
>
> Thanks
>
> Neil
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
.
This message is intended only for the use of the person(s) to whom it is addressed. It may contain information which is privileged and confidential. Accordingly any unauthorised use is strictly prohibited. If you are not the intended recipient, please contact the sender as soon as possible.
It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction, unless specifically agreed otherwise. All market prices, data and other information are not warranted as to completeness or accuracy and are subject to change without notice. Any opinions or advice contained in this Internet email are subject to the terms and conditions expressed in any applicable governing Marble Bar Asset Management LLP's terms and conditions of business or client agreement letter. Any comments or statements made herein do not necessarily reflect those of Marble Bar Asset Management LLP.
Marble Bar Asset Management LLP is regulated and authorised by the FSA.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
.
This message is intended only for the use of the person(s) to whom it is addressed. It may contain information which is privileged and confidential. Accordingly any unauthorised use is strictly prohibited. If you are not the intended recipient, please contact the sender as soon as possible.
It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction, unless specifically agreed otherwise. All market prices, data and other information are not warranted as to completeness or accuracy and are subject to change without notice. Any opinions or advice contained in this Internet email are subject to the terms and conditions expressed in any applicable governing Marble Bar Asset Management LLP's terms and conditions of business or client agreement letter. Any comments or statements made herein do not necessarily reflect those of Marble Bar Asset Management LLP.
Marble Bar Asset Management LLP is regulated and authorised by the FSA.
More information about the R-help
mailing list