I am an undergrad student and I'm super new to R! I have a data set that I have split into a training and test set. I obtained a multiple regression model from my training set, and now I want to use it to predict my test data. My dependent variable is Plant Species Richness (PSR), and my original data set had 4 independent variables (Area, AdjacentWetlands, Roads, and Forest) but my model is only using Area and Forest: LM<-lm(PSR~Area+Forest, data=Wetlands)
. How do I use this model to predict PSR in my test set? And then how do I assess whether it is a good prediction or not?
-
1$\begingroup$ Split-sample validation takes an amazingly large sample size to work in the sense of providing almost the same answer if you were to repeat the single random split and re-do all model building and all validation calculations. What is your total sample size? $\endgroup$– Frank HarrellCommented Sep 22, 2015 at 19:07
-
1$\begingroup$ If you're interested in learning about machine learning and R, I'd highly recommend checking out An Introduction to Statistical Learning, which is a great book that is freely available as a pdf. It has many examples in R, including cross validation (Chapter 5) $\endgroup$– TchotchkeCommented Sep 22, 2015 at 19:27
3 Answers
Firstly, get your model:
LM <- lm(PSR ~ Area+Forests, data = Wetlands)
Make sure all data values are correct.
The function predict()
does the calculation:
pred <- pred(your_model, your_data_test)
Your issue seems that your_data_test
have more variables than your model, right?
So you can slice your_data_test
and put into a new_data_test
by using
new_data_test <- data.frame(your_data_test$variable1,your_data_test$variable2)
and then
pred <- pred(yourmodel, new_data_test)
I suppose should be work for you.
-
$\begingroup$ I tried your solution on a similar dataset, but this gives a warning: " 'newdata' had 15600 rows but variables found have 1164007 rows " Also the created predict vector is not of 15600 length as it should be, but of 1164007 length. $\endgroup$– ApurvCommented May 25, 2017 at 13:15
-
$\begingroup$ Please check your data and try to catch any inconsistency in the values. $\endgroup$ Commented Nov 28, 2019 at 17:25
Actually, you don't need to create new_data_set. Instead, simply use your_data_test to get pred since your_model (LM) restricts to its entries (2 variables as mentioned above).
You can use print(summary(LM)) to access the accuracy of your model. You'll also get to know of other vitals like r^2 value, p-value of each predictor which helps you decide on a model.
-
$\begingroup$ this is true and generally helpful, but the OP asks "How do I use this model to predict PSR in my test set? And then how do I assess whether it is a good prediction or not?" $\endgroup$– JimCommented Feb 20, 2018 at 9:33