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.?
I am putting everything from the comments into this answer.
1) You can use
predict
rather thanpredict.lm
aspredict
will know your input is of classlm
and do the right thing automatically.2 The
newdataset
should be adata.frame
with the same variables as your original predictors - in this casealt
andsdist
.3) If you are bringing in you data using
read.table
by default it will create adata.frame
. This assumes that the new data has columns namedalt
andsdist
Then you can do:4) After you have done this if you want to check the predictions - you can do the following
This will give you the intercept and the coefficients for
alt
andsdist
NewDataSet[1,] This should give you thealt
andsdist
values for the first row, you can change the 1 in the bracket to be any row you want. Then use the information fromsummary(model1)
to calculate what the predicted value should be using any method that you trust.Finally use NewPredictions[1] to get what
predict()
gave you for the first row (or change the 1 to any other row)Hopefully that should all work out.