[R] still problems with predict!

Liaw, Andy andy_liaw at merck.com
Fri Jul 16 18:40:20 CEST 2004


One thing to etch into your brain:  Don't use constructs like

  lm(p1$y~p1$x1+p1$x2)

Instead, use:

  lm(y ~ x1 + x2, data=p1)

Doing the former is only asking for trouble when it comes time to do
prediction.  It will look for `p1$y' in the newdata, and of course, won't
find it there!

Andy


> From: Anne
> 
> 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
> 
> 
> 
> Thanks and a good week end! (I for one need it)
> Anne
> ----------------------------------------------------
> Anne Piotet
> Tel: +41 79 359 83 32 (mobile)
> Email: anne.piotet at m-td.com
> ---------------------------------------------------
> M-TD Modelling and Technology Development
> PSE-C
> CH-1015 Lausanne
> Switzerland
> Tel: +41 21 693 83 98
> Fax: +41 21 646 41 33
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list