Michael
2006-Jan-29 22:11 UTC
[R] SoS! How to predict new values using linear regression models?
Hi all, After trial and error by myself for a few hours, I decide to ask for your help. I have a training set which is a matrix of size 200 x 2, where the two columns denote each independent variable. I have 200 observations. ----------------- ss=data.frame(trainingSet); result=lm(trainingClass~ss$X1+ss$X2); ----------------- where trainingClass denotes the true classes of the training data. Now I want to apply the model to predict new data: -----------------> gg=predict(result, data.frame(X1=1, X2=2))Warning message: 'newdata' had 1 rows but variable(s) found have 200 rows ----------------- That's to say, I provide a new data which is one observation of 2 independent variables(1 row, two columns). I converted it into data frame. However, the R never gives me new predication value for this NEW ONE observation. Instead, it keeps giving me the above warning and keeps printing the fitted value for the 200 training samples... That's very bad. Please help me! [[alternative HTML version deleted]]
Gabor Grothendieck
2006-Jan-29 22:28 UTC
[R] SoS! How to predict new values using linear regression models?
Leaving aside the issue of whether linear regression is appropriate here, do it like this where I have used the builtin iris data frame since I don't have access to your ss: iris.lm <- lm(as.numeric(Species) ~ Sepal.Length + Sepal.Width, iris) predict(iris.lm, data.frame(Sepal.Length = 3, Sepal.Width = 2)) On 1/29/06, Michael <comtech.usa at gmail.com> wrote:> Hi all, > > After trial and error by myself for a few hours, I decide to ask for your > help. > > I have a training set which is a matrix of size 200 x 2, where the two > columns denote each independent variable. I have 200 observations. > > ----------------- > ss=data.frame(trainingSet); > result=lm(trainingClass~ss$X1+ss$X2); > ----------------- > > where trainingClass denotes the true classes of the training data. > > Now I want to apply the model to predict new data: > > ----------------- > > gg=predict(result, data.frame(X1=1, X2=2)) > Warning message: > 'newdata' had 1 rows but variable(s) found have 200 rows > ----------------- > > That's to say, I provide a new data which is one observation of 2 > independent variables(1 row, two columns). I converted it into data frame. > > However, the R never gives me new predication value for this NEW ONE > observation. Instead, it keeps giving me the above warning and keeps > printing the fitted value for the 200 training samples... > > That's very bad. > > Please help me! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Petr Pikal
2006-Jan-30 13:50 UTC
[R] SoS! How to predict new values using linear regression models?
Hi On 29 Jan 2006 at 17:28, Gabor Grothendieck wrote: Date sent: Sun, 29 Jan 2006 17:28:29 -0500 From: Gabor Grothendieck <ggrothendieck at gmail.com> To: Michael <comtech.usa at gmail.com> Copies to: R-help at stat.math.ethz.ch Subject: Re: [R] SoS! How to predict new values using linear regression models?> Leaving aside the issue of whether linear regression is appropriate > here, do it like this where I have used the builtin iris data frame > since I don't have access to your ss: > > iris.lm <- lm(as.numeric(Species) ~ Sepal.Length + Sepal.Width, iris) > predict(iris.lm, data.frame(Sepal.Length = 3, Sepal.Width = 2)) > > On 1/29/06, Michael <comtech.usa at gmail.com> wrote: > > Hi all, > > > > After trial and error by myself for a few hours, I decide to ask for > > your help. > > > > I have a training set which is a matrix of size 200 x 2, where the > > two columns denote each independent variable. I have 200 > > observations. > > > > ----------------- > > ss=data.frame(trainingSet); > > result=lm(trainingClass~ss$X1+ss$X2);^^^^ ^^^ As Gabor suggested, use data argument. result=lm(trainingClass~X1+X2, data=ss) and your predict shall work. HTH Petr> > ----------------- > > > > where trainingClass denotes the true classes of the training data. > > > > Now I want to apply the model to predict new data: > > > > ----------------- > > > gg=predict(result, data.frame(X1=1, X2=2)) > > Warning message: > > 'newdata' had 1 rows but variable(s) found have 200 rows > > ----------------- > > > > That's to say, I provide a new data which is one observation of 2 > > independent variables(1 row, two columns). I converted it into data > > frame. > > > > However, the R never gives me new predication value for this NEW ONE > > observation. Instead, it keeps giving me the above warning and keeps > > printing the fitted value for the 200 training samples... > > > > That's very bad. > > > > Please help me! > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > > http://www.R-project.org/posting-guide.html > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.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