使用 R 从另一个 table 的线性模型进行预测
Predict with linear model from another table with R
大家好,我是 R 的新手,我需要知道如何使用 tables 的预测函数。
我的第一个 table 带有一些天气变量 (wind, temperature, pressure
) 和建筑物的 consumption
。我创建了一个线性模型:
mymodel<-lm(energyConsum ~ temperature + pressure+ wind, data=mytable)
我还有第二个 table 天气预报,变量值 temperature, wind and pressure
,我想预测 consumption
我知道我必须使用函数 predict()
,并设置 mymodel
,但我真的不知道如何创建包含所有预测的新列(通过线)。
干杯
我没有你的数据,但这里有一个 mtcars
的例子。
您可以使用 predict.lm
的第二个参数 newdata
来预测 data.frame。然后您可以将这些结果分配给新列。
示例:
train <- mtcars[1:20, ]
test <- mtcars[21:32,]
mymodel <- lm(mpg ~ cyl + disp + hp, data=train)
test$pred <- predict(mymodel, test)
大家好,我是 R 的新手,我需要知道如何使用 tables 的预测函数。
我的第一个 table 带有一些天气变量 (wind, temperature, pressure
) 和建筑物的 consumption
。我创建了一个线性模型:
mymodel<-lm(energyConsum ~ temperature + pressure+ wind, data=mytable)
我还有第二个 table 天气预报,变量值 temperature, wind and pressure
,我想预测 consumption
我知道我必须使用函数 predict()
,并设置 mymodel
,但我真的不知道如何创建包含所有预测的新列(通过线)。
干杯
我没有你的数据,但这里有一个 mtcars
的例子。
您可以使用 predict.lm
的第二个参数 newdata
来预测 data.frame。然后您可以将这些结果分配给新列。
示例:
train <- mtcars[1:20, ]
test <- mtcars[21:32,]
mymodel <- lm(mpg ~ cyl + disp + hp, data=train)
test$pred <- predict(mymodel, test)