Starting from a linear model1 = lm(temp~alt+sdist) i need to develop a prediction model, where new data will come in hand and predictions about temp will be made.

I have tried doing something like this:

model2 = predict.lm(model1, newdata=newdataset)

However, i am not sure this is the right way. What i would like to know here is, if this is the right way to go in order to make prediction about temp. Also i am a bit confused when it comes to the newdataset. Which values should be filled in etc.?


newdata should contain a column for each of your predictive variables, alt and sdist. (Any variables except the one you're predicting.) For example:

newdata = data.frame(alt = newAltVector, sdist = newSdistVector)
predictions = predict.lm(model, newdata)

predictions will then contain a fitted y value for each new x. In the below, black dots represent training data, and the blue dots represent predicted values.

Whether that's the right way to predict temp depends on how well a linear model approximates the relationship between variables. Try this intro to evaluating a linear model in R.

Linear regression with toy data

  • $\begingroup$ Well your answer helped a lot. Thanks for the link too. By the way how did you plotted the using predictor and predicted variables? $\endgroup$ – Murania Mar 24 '14 at 21:31
  • $\begingroup$ plot(x, y, xlab = "Predictor variable", ylab = "Predicted variable"); points(predictions, newdata$x, col = "blue") $\endgroup$ – Sean Easter Mar 24 '14 at 23:32

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.