[R] still problems with predict!
Sundar Dorai-Raj
sundar.dorai-raj at PDF.COM
Fri Jul 16 17:55:43 CEST 2004
Anne wrote:
> Hi all,
>
> I still have problems with the predict function by setting up the values on
> which I want to predict
>
> ie:
> original df: p1 (193 obs) variates y x1 x2
>
> rm(list=ls())
> x1<-rnorm(193)
> x2<-runif(193,-5,5)
> y<-rnorm(193)+x1+x2
> p1<-as.data.frame(cbind(y,x1,x2))
> p1
> y x1 x2
> 1 -0.6056448 -0.1113607 -0.5859728
> 2 -4.2841793 -1.0432688 -3.3116807
> ......
> 192 -1.3228239 1.0263013 -2.7801324
> 193 1.8736683 1.0480632 0.4746959
>
> newdf<-data.frame(x1= seq(min( p1$x1),max( p1$x1),length=10),
> x2=rep(median( p1$x2),10) )
> pr<-predict(g<-lm(p1$y~p1$x1+p1$x2) ,newdf, se.fit = TRUE)
>
> newdf
> x1 x2
> 1 -2.3844149 -0.2594991
> 2 -1.8388635 -0.2594991
> ...
> 9 1.9799963 -0.2594991
> 10 2.5255477 -0.2594991
>
> pr$fit
> 1 -0.6766906
> 2 -4.4198864
> .....
> 192 -1.6531906
> 193 1.6395442
>
> so apparently the predict() function did not take up the new data.frame
>
>
> I looked up with conflicts() to see if I had masked objects in the search
> path potentially causing this problem
> but found none
>
>
>
Hi Anne,
predict is working properly (though not as you expected). It's not
evaluating your newdf because it has no columns called p1$x1 or p1$x2.
Try this instead:
pr <- predict(g <- lm(y ~ x1 + x2, p1), newdf, se.fit = TRUE)
> str(pr)
List of 4
$ fit : Named num [1:10] -2.365 -1.865 -1.366 -0.867 -0.367 ...
..- attr(*, "names")= chr [1:10] "1" "2" "3" "4" ...
$ se.fit : Named num [1:10] 0.1751 0.1424 0.1120 0.0868 0.0723 ...
..- attr(*, "names")= chr [1:10] "1" "2" "3" "4" ...
$ df : int 190
$ residual.scale: num 0.987
--sundar
More information about the R-help
mailing list