R多变量提前一步预测和准确性 [英] R multivariate one step ahead forecasts and accuracy

查看:39
本文介绍了R多变量提前一步预测和准确性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 R 我想比较两个预测模型的 RMSE(均方根误差).第一个模型使用 1966 年至 2000 年的估计值预测 2001 年,然后使用 1966 年至 2001 年的估计值预测 2002 年直到 2015 年.第二个模型使用 1991 年至 2000 年的估计值预测 2001 年,然后使用 1992 年至 2001 年的估计值预测 2002 年等等直到 2015 年.这个问题让我真的很难过,我真的很感激任何帮助.

Using R I would like to compare the RMSE (root mean square error) from two prediction models. The first model uses estimates from 1966 to 2000 to predict 2001 and then uses estimates from 1966 to 2001 to predict 2002 and so on up to 2015. The second model uses estimates from 1991 to 2000 to predict 2001 and then uses estimates from 1992 to 2001 to predict 2002 and so on up to 2015. This problem has me really stumped and I truly appreciate any help.

DF <- data.frame(YEAR=1966:2015, TEMP=rnorm(50), PRESSURE=rnorm(50), RAINFALL=rnorm(50))

lmod <- lm(TEMP ~ PRESSURE + RAINFALL, data = DF)

rmse <- function(error) sqrt(mean(error^2))

rmse(lmod$residuals)

推荐答案

你可以循环:

方法一:

pred1<-numeric(0)
rmse1<-numeric(0)

for(i in 1:15){

DF.train1<-DF[DF$YEAR < 2000+i,]
DF.test1<-DF[DF$YEAR == 2000+i,]
lmod1 <- lm(TEMP ~ PRESSURE + RAINFALL, data = DF.train1)
pred1[i]<- predict(lmod1, newdata = DF.test1)
rmse1[i]<-sqrt(mean((DF.test1$TEMP-pred1[i])^2))
}

pred1
rmse1  
mean(rmse1)  

方法二:

pred2<-numeric(0)
rmse2<-numeric(0)

for(i in 1:15){

DF.train2<-DF[DF$YEAR < 2000+i & DF$YEAR > 1989+i,]
DF.test2<-DF[DF$YEAR == 2000+i,]
lmod2 <- lm(TEMP ~ PRESSURE + RAINFALL, data = DF.train2)
pred2[i]<- predict(lmod2, newdata = DF.test2)
rmse2[i]<-sqrt(mean((DF.test2$TEMP-pred2[i])^2))
} 

pred2
rmse2  
mean(rmse2) 

比较 rmse1rmse2 的各个组件,以及它们各自的方法应该是有用的.向量 pred1pred2 包含各自方法对每年(2001-2015)的单独 TEMP 预测.

Comparing the individual components of rmse1 and rmse2, as well as their respective means should be useful. The vectors pred1 and pred2 contain the individual TEMP predictions for each year (2001-2015) for their respective methods.

现在应该可以工作了,方法 2 在滚动的 10 年间隔内训练.此外,我将 RMSE 视为 this 文章中为预测变量定义的 MSE 的平方根.

should be working now, and Method 2 trains on a rolling 10 year gap. Also, I take RMSE to be the square root of the MSE as defined for predictors in this article.

这篇关于R多变量提前一步预测和准确性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆