2
$\begingroup$

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?

$\endgroup$
  • 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 Harrell Sep 22 '15 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$ – Tchotchke Sep 22 '15 at 19:27
3
$\begingroup$

If I understand well, you are talking about to use the function predict().

So you have the model:

LM <- lm(PSR ~ Area+Forests, data = Wetlands)

The function predict() provide this usage

pred <- pred(your_model, your_data_test)

but your_data_test have more variables than your model, right?

You can make a new_data_test using this,

new_data_test <- data.frame(your_data_test$variable1,your_data_test$variable2)

pred <- pred(yourmodel, new_data_test)

I suppose that this may be works well.

$\endgroup$
  • $\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$ – Apurv May 25 '17 at 13:15
0
$\begingroup$

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).

$\endgroup$
0
$\begingroup$

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.

$\endgroup$
  • $\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$ – Jim Feb 20 '18 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.