hello to all! I have a small problem wit predict() for lm Let's say I have predictors x1 and x2, response y I want to predict for a new ds say dn<-data.frame(x1= seq(min(x1),max(x1),length=10),x2=rep(median(x2),10)) predict(lm(y~x1+x2),dn,se.fit=T) Error message> Error: variables 'x1', 'x2' were specified differently from the fit(I looked in the help and found the example x <- rnorm(15) y <- x + rnorm(15) new <- data.frame(x = seq(-3, 3, 0.5)) predict(lm(y ~ x), new, se.fit = TRUE) where is the difference?) 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 -------------------------------------------------- [[alternative HTML version deleted]]
Hi On 16 Jul 2004 at 12:38, Anne wrote:> hello to all! > > I have a small problem wit predict() for lm > > Let's say I have predictors x1 and x2, response y > > I want to predict for a new ds say > dn<-data.frame(x1> seq(min(x1),max(x1),length=10),x2=rep(median(x2),10)) > > predict(lm(y~x1+x2),dn,se.fit=T) > > > Error message > > Error: variables 'x1', 'x2' were specified differently from the fitAFAIK predixt require to be fed by dataframe in which the variables has the same names as was in lm() call. Either call lm() with variables named "x1" and "x2" or build the dataframe "dn" with names identical as are names in lm() call Cheers Petr> > (I looked in the help and found the example > > x <- rnorm(15) > y <- x + rnorm(15) > new <- data.frame(x = seq(-3, 3, 0.5)) > predict(lm(y ~ x), new, se.fit = TRUE) > > where is the difference?) > > > 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 > -------------------------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.htmlPetr Pikal petr.pikal at precheza.cz
Hi Ann As I read your code mode closely, I suspect that there could be another variables x1 and x2 somewhere in your search path which led to the error. With fresh R session without any data this works as expected dold<-data.frame(x1=rnorm(10),x2=5*rnorm(10)) y<-1:10 predict(lm(y~x1+x2,dold),dn,se.fit=T) Cheers Petr On 16 Jul 2004 at 13:19, Petr Pikal wrote:> Hi > > On 16 Jul 2004 at 12:38, Anne wrote: > > > hello to all! > > > > I have a small problem wit predict() for lm > > > > Let's say I have predictors x1 and x2, response y > > > > I want to predict for a new ds say > > dn<-data.frame(x1> > seq(min(x1),max(x1),length=10),x2=rep(median(x2),10)) > > > > predict(lm(y~x1+x2),dn,se.fit=T) > > > > > > Error message > > > Error: variables 'x1', 'x2' were specified differently from the > > > fit > > AFAIK predixt require to be fed by dataframe in which the > variables has the same names as was in lm() call. Either call lm() > with variables named "x1" and "x2" or build the dataframe "dn" with > names identical as are names in lm() call > > Cheers > Petr > > > > > > (I looked in the help and found the example > > > > x <- rnorm(15) > > y <- x + rnorm(15) > > new <- data.frame(x = seq(-3, 3, 0.5)) > > predict(lm(y ~ x), new, se.fit = TRUE) > > > > where is the difference?) > > > > > > 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 > > -------------------------------------------------- > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > 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 > > Petr Pikal > petr.pikal at precheza.cz > > ______________________________________________ > 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.htmlPetr Pikal petr.pikal at precheza.cz
You wrote:> I have a small problem wit predict() for lm > > Let's say I have predictors x1 and x2, response y > > I want to predict for a new ds say > dn<-data.frame(x1= seq(min(x1),max(x1),length=10),x2=rep(median(x2),10)) > > predict(lm(y~x1+x2),dn,se.fit=T) > > > Error message > > Error: variables 'x1', 'x2' were specified differently from the fitI tried your example (with a made-up ``original'' x1 and x2, and y) and got a perfectly reasonable result, with no error message. Explicitly I did: > x1 <- runif(42) > x2 <- rnorm(42) > y <- 4 + 2*x1 + 3*x2 + rnorm(42) > dn <-data.frame(x1=seq(min(x1),max(x1),length=10), x2=rep(median(x2),10)) > predict(lm(y~x1+x2),dn,se.fit=T) No problema. Are you sure you did what you say you did? cheers, Rolf Turner rolf at math.unb.ca