Re: [R] still problems with predict!

From: Uwe Ligges <ligges_at_statistik.uni-dortmund.de>
Date: Sat 17 Jul 2004 - 02:00:13 EST

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)

Anne, predict cannot replace the data set properly, because you have specified fixed values in

   g <- lm(p1$y ~ p1$x1 + p1$x2)

Instead, use:

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

and all the stuff will work.

Please insert some blanks in your code to make it readable, and try to give reproducable examples by, e.g., setting the seed as in:

   set.seed(123)
   rm(list=ls())

   x1 <- rnorm(193)
   x2 <- runif(193, -5, 5)
   y  <- rnorm(193) + x1 + x2
   p1 <- data.frame(y = y, x1 = x1, x2 = x2)

   newdf <- data.frame(x1 = seq(min(p1$x1), max(p1$x1), length = 10),
                       x2 = rep(median(p1$x2), 10))

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

   pr1 <- predict(g, se.fit = TRUE)
   pr2 <- predict(g, newdata = newdf, se.fit = TRUE)

Uwe Ligges

> 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@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@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



R-help@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 Received on Sat Jul 17 02:10:39 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:55:03 EST