R: multiple linear regression model and prediction model

前端 未结 1 1349
青春惊慌失措
青春惊慌失措 2020-12-12 07:16

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

相关标签:
1条回答
  • 2020-12-12 07:41

    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.

    0 讨论(0)
提交回复
热议问题