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