Hi, To obtain estimates for some missing values in my data I fitted a linear regression and then used the command fitted(model) to get the fitted values from the model, but R doesn't return any values for the NA's. I can calculate the fitted values from the estimates obtained from the summary of model, but that's not very handy. Is there a way to include the missing values in the analysis and get fitted values for the NA's? I tried to use with the function na.action, but that didn't work. thanks Luca
Have you considered:
> set.seed(1)
> tstDF <- data.frame(x=1:4, y=c(NA, 2:4+rnorm(3)))
> fit <- lm(y~x, tstDF)
> predict(fit)
2 3 4
1.678441 2.573854 3.469266
> predict(fit, tstDF)
1 2 3 4
0.7830284 1.6784410 2.5738536 3.4692662
spencer graves
Luca Wacker wrote:
> Hi,
>
> To obtain estimates for some missing values in my data I fitted a
> linear regression and then used the command fitted(model) to get the
> fitted values from the model, but R doesn't return any values for the
> NA's. I can calculate the fitted values from the estimates obtained
> from the summary of model, but that's not very handy. Is there a way to
> include the missing values in the analysis and get fitted values for
> the NA's? I tried to use with the function na.action, but that
didn't
> work.
>
>
> thanks
>
> Luca
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
It depends on the na.action used in lm(), which defaults to na.omit. Here's an example:> x <- c(1:5, NA, 7:10) > y <- rnorm(x) > fitted(lm(y ~ x))1 2 3 4 5 7 8 0.68680104 0.47913875 0.27147646 0.06381417 -0.14384812 -0.55917271 -0.76683500 9 10 -0.97449729 -1.18215958> fitted(lm(y ~ x, na.action=na.exclude))1 2 3 4 5 6 7 0.68680104 0.47913875 0.27147646 0.06381417 -0.14384812 NA -0.55917271 8 9 10 -0.76683500 -0.97449729 -1.18215958 Andy> From: Luca Wacker > > Hi, > > To obtain estimates for some missing values in my data I fitted a > linear regression and then used the command fitted(model) to get the > fitted values from the model, but R doesn't return any values for the > NA's. I can calculate the fitted values from the estimates obtained > from the summary of model, but that's not very handy. Is > there a way to > include the missing values in the analysis and get fitted values for > the NA's? I tried to use with the function na.action, but that didn't > work. > > > thanks > > Luca > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >
I posted a bad example. The question would only make sense if NAs are all in the response. Here's try #2: I believe you can not get fitted values for cases where y is missing directly from the model object. You can, however, use predict(lm.object, newdata=mydata[!complete.cases(mydata)]) to get the predictions for those with missing y. Andy> From: Luca Wacker > > Hi, > > To obtain estimates for some missing values in my data I fitted a > linear regression and then used the command fitted(model) to get the > fitted values from the model, but R doesn't return any values for the > NA's. I can calculate the fitted values from the estimates obtained > from the summary of model, but that's not very handy. Is > there a way to > include the missing values in the analysis and get fitted values for > the NA's? I tried to use with the function na.action, but that didn't > work. > > > thanks > > Luca > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >