[R] Predictably puzzled.

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Sat Nov 20 03:35:23 CET 2021


?predict.lm says:

"predict.lm produces predicted values, obtained by evaluating the
regression function in the frame newdata (which defaults to
model.frame(object)). "

model.frame(fit) is:
1   1.37095845 -0.30663859
2  -0.56469817 -1.78130843
4   0.63286260  1.21467470
6  -0.10612452 -0.43046913
7   1.51152200 -0.25726938
8  -0.09465904 -1.76316309
9   2.01842371  0.46009735
10 -0.06271410 -0.63999488
11  1.30486965  0.45545012
12  2.28664539  0.70483734
13 -1.38886070  1.03510352
16  0.63595040 -1.71700868
17 -0.28425292 -0.78445901
18 -2.65645542 -0.85090759
19 -2.44046693 -2.41420765
20  1.32011335  0.03612261

i.e. only the rows that are used to fit the model are present.

Cheers,
Bert


On Fri, Nov 19, 2021 at 6:12 PM Rolf Turner <r.turner using auckland.ac.nz> wrote:
>
>
> Consider the following toy example:
>
>     set.seed(42)
>     y <- rnorm(20)
>     x <- rnorm(20)
>     y[c(3,5,14,15)] <- NA
>     fit <- lm(y~x)
>     predict(fit)
>
> This for some reason, which escapes me, does not provide predicted
> values when the response/dependent variable is missing, despite
> there being no missing values in the predictor/independent variable.
>
> I can get predicted values for all values of x if I set
>
>     ddd <- data.frame(y=y,x=x)
>
> and execute
>
>     predict(fit,newdata=ddd)
>
> Note that y is (unnecessarily) included in ddd.  I thought that
> predict() might omit any rows of the data in which there are missing
> values, but not so.
>
> OK.  I have a workaround which gives me the predicted values that I
> want, but:
>
> (a) Why does predict() behave in this way?  It makes no sense to me,
> but I figure there *must* be a rationale.
>
> (b) Is there a way of getting predict() to behave as I would like, by
> specifying an appropriate value for na.action?  I could not find such
> an appropriate value.
>
> Thanks for any enlightenment.
>
> cheers,
>
> Rolf Turner
>
> --
> Honorary Research Fellow
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list