Solved! Here is the solution in case it helps others:

The easiest way to get past the issue of matching up variable names from a
matrix of covariates to newdata data.frame column names is to put your
input data into a data.frame as well. Try this

price = c(10,18,18,11,17)
predictors = cbind(c(5,6,3,4,5),c(2,1,8,5,6))
indata<-data.frame(price,predictors=predictors)

predict(lm(price ~ ., indata), data.frame(predictors=matrix(c(3,5),nrow=1)))

 Here we combine price and predictors into a data.frame such that it will
be named the same say as the newdata data.frame. We use the . in the
formula to mean "all other columns" so we don't have to specify them
explicitly.


On 29 May 2014 13:38, Safiye Celik <safi...@gmail.com> wrote:

>  I want to perform a multiple regression in R and make predictions based
> on the trained model. Below is an example code I am using:
>
> price = c(10,18,18,11,17)
> predictors = cbind(c(5,6,3,4,5),c(2,1,8,5,6))
> predict(lm(price ~ predictors), data.frame(predictors=matrix(c(3,5),nrow=1)))
>
>  So, based on the 2-variate regression model trained by 5 samples, I want
> to make a prediction for the test data point where the first variate is 3
> and second variate is 5. But I get a warning from above code saying that 
> 'newdata'
> had 1 rows but variable(s) found have 5 rows. How can I correct above
> code? Below code works fine where I give the variables separately to the
> model formula. But since I will have hundreds of variates, I have to give
> them in a matrix since it would be unfeasible to append hundreds of columns
> using + sign.
>
> price = c(10,18,18,11,17)
> predictor1 = c(5,6,3,4,5)
> predictor2 = c(2,1,8,5,6)
> predict(lm(price ~ predictor1 + predictor2), 
> data.frame(predictor1=3,predictor2=5))
>
>  Thanks in advance!
>
> --
> -safiye
>



-- 
-safiye

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to