问题
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.?
回答1:
I am putting everything from the comments into this answer.
1) You can use predict
rather than predict.lm
as predict
will know your input is of class lm
and do the right thing automatically.
2 The newdataset
should be a data.frame
with the same variables as your original predictors - in this case alt
and sdist
.
3) If you are bringing in you data using read.table
by default it will create a data.frame
. This assumes that the new data has columns named alt
and sdist
Then you can do:
NewDataSet<-read.table(whatever)
NewPredictions<- predict(model1, newdata=NewDatSet)
4) After you have done this if you want to check the predictions - you can do the following
summary(model1)
This will give you the intercept and the coefficients for alt
and sdist
NewDataSet[1,]
This should give you the alt
and sdist
values for the first row, you can change the 1 in the bracket to be any row you want. Then use the information from summary(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.
来源:https://stackoverflow.com/questions/22612382/r-multiple-linear-regression-model-and-prediction-model