R:多元线性回归模型和预测模型 [英] R: multiple linear regression model and prediction model
问题描述
从线性model1 = lm(temp~alt+sdist)
开始,我需要建立一个预测模型,在该模型中可以获取新数据并做出有关temp
的预测.
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.
我试图做这样的事情:
model2 = predict.lm(model1, newdata=newdataset)
但是,我不确定这是正确的方法.我想知道的是,这是否是对temp
进行预测的正确方法.当涉及到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)您可以使用predict
而不是predict.lm
,因为predict
会知道您的输入属于lm
类,并且会自动执行正确的操作.
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 newdataset
应该是具有与原始预测变量相同的变量的data.frame
-在这种情况下为alt
和sdist
.
2 The newdataset
should be a data.frame
with the same variables as your original predictors - in this case alt
and sdist
.
3)如果默认情况下使用read.table
导入数据,它将创建一个data.frame
.假设新数据具有名为alt
和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)完成此操作后,如果要检查预测,则可以执行以下操作
4) After you have done this if you want to check the predictions - you can do the following
summary(model1)
这将为您提供alt
和sdist
的截距和系数
NewDataSet [1,]
这应该为第一行提供alt
和sdist
值,您可以将括号中的1更改为所需的任何行.然后使用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.
最终使用
新预测[1]
以获得predict()
在第一行中给您的内容(或将1更改为其他任何行)
Finally use
NewPredictions[1]
to get what predict()
gave you for the first row (or change the 1 to any other row)
希望这一切都会解决.
这篇关于R:多元线性回归模型和预测模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!