This caught me learning R, and no doubt thousands of others. 

When would one ever want the results of fitted() or residuals() to NOT match 
the data frame rows which went into the model? Certainly making shrinking the 
results the default is not what 99% of user will want  if they need to access 
residuals: They will want a list of residuals padded with NAs to plot or use in 
further analyses in the originating data frame.

IMHO, the default na.action for lm() should be “na.exclude" (which, believe it 
or not, INcludes NAs in the results…)

Best, tim

On 14 Nov 2011, at 3:49 PM, William Dunlap wrote:
> If you want fitted() to return NAs in the positions where
> there were NA's in data, use na.action=na.exclude in your
> call to lm().  E.g.,
> 
>> z <- data.frame(y=1:5, x=c(1,NA,3,3,5))
>> fitted(lm(y~x, data=z))
>     1    3    4    5 
>  1.25 3.25 3.25 5.25 
>> fitted(lm(y~x, data=z, na.action=na.exclude))
>     1    2    3    4    5 
>  1.25   NA 3.25 3.25 5.25
> 
> I think the help file for model.matrix has more detail,
> including how to use options() to set the na.action for
> your R session.
> 
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com 
> 
>> -----Original Message-----
>> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
>> Behalf Of arunkumar1111
>> Sent: Monday, November 14, 2011 3:36 AM
>> To: r-help@r-project.org
>> Subject: Re: [R] help in fitted values in lm function
>> 
>> Yes there are few NA in the Data
>> 
>> --
>> View this message in context: 
>> http://r.789695.n4.nabble.com/help-in-fitted-values-in-lm-function-
>> tp4038642p4039207.html
>> Sent from the R help mailing list archive at Nabble.com.
>> 
>> ______________________________________________
>> R-help@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.
> 
> ______________________________________________
> R-help@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.

______________________________________________
R-help@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.

Reply via email to