简体   繁体   English

R:多元线性回归模型和预测模型

[英]R: multiple linear regression model and prediction model

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. 从线性模型model1 = lm(temp~alt+sdist)我需要开发一个预测模型,其中将有新数据出现并且将对temp进行预测。

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 . 我想知道的是,如果这是正确的预测temp Also I am a bit confused when it comes to the newdataset . newdataset我也有点困惑。 Which values should be filled in etc.? 应该填写哪些值?

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. 1)您可以使用predict ,而不是predict.lm作为predict会知道你的输入级的lm和自动做正确的事。

2 The newdataset should be a data.frame with the same variables as your original predictors - in this case alt and sdist . 2 newdataset应该是一个data.frame ,其变量与原始预测变量相同 - 在本例中为altsdist

3) If you are bringing in you data using read.table by default it will create a data.frame . 3)如果您使用read.table默认引入数据,它将创建一个data.frame This assumes that the new data has columns named alt and sdist Then you can do: 这假设新数据具有名为altsdist列然后您可以执行以下操作:

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 4)完成此操作后,如果要检查预测 - 可以执行以下操作

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. 这将为您提供截距和altsdist NewDataSet [1,]的系数。这应该为您提供第一行的altsdist值,您可以将括号中的1更改为您想要的任何行。 Then use the information from summary(model1) to calculate what the predicted value should be using any method that you trust. 然后使用summary(model1)的信息来计算使用您信任的任何方法的预测值。

Finally use NewPredictions[1] to get what predict() gave you for the first row (or change the 1 to any other row) 最后使用NewPredictions [1]来获取predict()为第一行提供的内容(或将1更改为任何其他行)

Hopefully that should all work out. 希望这一切都能解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM