r - Why doesn't predict like the dimensions of my newdata? -


i want perform multiple regression in r , make predictions based on trained model. below example code 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 2-variate regression model trained 5 samples, want make prediction test data point first variate 3 , second variate 5. warning above code saying 'newdata' had 1 rows variable(s) found have 5 rows. how can correct above code? below code works fine give variables separately model formula. since have hundreds of variates, have give them in matrix since unfeasible 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!

the easiest way past issue of matching variable names matrix of covariates newdata data.frame column names put input data data.frame 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 combine price , predictors data.frame such named same newdata data.frame. use . in formula mean "all other columns" don't have specify them explicitly.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -