Skip to content

help in fitted values in lm function

5 messages · arunkumar1111, PIKAL Petr, William Dunlap +1 more

#
Hi,

I have a data set of 10000 rows and ran a linear regression by using the
function lmres = lm(formula,data)

then got the fitted value by using the value fit=fitted(lmres).

but the number of rows in the fitted one is about 9548,

What could be the reason for reduction in the number of rows in the fitted
one 

--
View this message in context: http://r.789695.n4.nabble.com/help-in-fitted-values-in-lm-function-tp4038642p4038642.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi
fitted
Some NA values in your data?

Petr
http://www.R-project.org/posting-guide.html
#
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
#
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: