Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R多元一步预测和精度_R_Time Series_Prediction - Fatal编程技术网

R多元一步预测和精度

R多元一步预测和精度,r,time-series,prediction,R,Time Series,Prediction,使用R,我想比较两个预测模型的RMSE(均方根误差)。第一个模型使用1966年至2000年的估计值预测2001年,然后使用1966年至2001年的估计值预测2002年,依此类推,直到2015年。第二个模型使用1991年至2000年的估计数预测2001年,然后使用1992年至2001年的估计数预测2002年,依此类推,直至2015年。这个问题真的难住我了,我真的很感激任何帮助 DF <- data.frame(YEAR=1966:2015, TEMP=rnorm(50), PRESSURE=

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

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)
DF您可以循环它:

方法1:

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)  

pred1这里是另一个解决方案,其中模拟在函数中。
此解决方案的目的是方便地修改模型规范

例如,如果您想试用范围为15年而不是10年的
model2
,只需修改函数中的输入(
range=15
)。这也让你有可能做一个感光度分析

compare_models <- function(DF, start = 1966, end = 2000, range = 10)
{
  require(hydroGOF)
  for (i in (end+1):tail(DF$YEAR)[6])
  {
   # model1
    lmod_1 = lm(TEMP ~ PRESSURE + RAINFALL, data = DF[DF$YEAR >= start & DF$YEAR < i,])
    DF$model1_sim[DF$YEAR == i] <- predict(lmod_1, newdata = DF[DF$YEAR == i,])
    # model2
    lmod_2 = lm(TEMP ~ PRESSURE + RAINFALL, data = DF[DF$YEAR >= i-range & DF$YEAR < i,])
    DF$model2_sim[DF$YEAR == i] <- predict(lmod_2, newdata = DF[DF$YEAR == i,])
  }
  return(DF)
} 
以及用于寻找模型预测的简单模拟/观察图:

# melting data for plot
output_melt = melt(output[,c("TEMP", "model1_sim", "model2_sim")], id = "TEMP")
# Plot
ggplot(output_melt, aes(x = TEMP, y = value, color = variable)) + 
theme_bw() + geom_point() + geom_abline(slope = 1, intercept = 0) + 
xlim(-2,2) + ylim(-2,2) + xlab("Measured") + ylab("Simulated")

这里还有另一个解决方案:

year <- 2000
time.frame <- 35


train.models <- function(year, time.frame) {
   predictions <- sapply(year:(max(df$YEAR)-1), 
          function(year) {
             lmod <- lm(TEMP ~ PRESSURE + RAINFALL, DF,
                        subset = with(DF, YEAR %in% (year - time.frame + 1):year))

             pred <- predict(lmod, newdata = DF[DF$YEAR == (year + 1),])
             names(pred) <- year + 1
             return (pred)
          })

   return (predictions)
}

models1 <- train.models(2000, 35)
models2 <- train.models(2001, 10)


rmse(models1 - DF$TEMP[DF$YEAR %in% names(models1)])
rmse(models2 - DF$TEMP[DF$YEAR %in% names(models2)])

year运行那里的代码时出现了什么问题?我的代码只是一个普通的回归模型。它不做任何超前预测。@Brian Goyggin,我有同样的问题,使用autoarima对预测进行子集划分,但如果我想使用第一个1966年(1966-2000年)预测2001年、2002年、2015年,但我也想使用三年(1966、1967、1968年)以类似的方式预测,五年、七年(截至2000年)预测2001年至2015年,9年比2000年增长。如果我想从1开始每隔两年使用一次,即(1,3,5,7,…)如何使用。
# melting data for plot
output_melt = melt(output[,c("TEMP", "model1_sim", "model2_sim")], id = "TEMP")
# Plot
ggplot(output_melt, aes(x = TEMP, y = value, color = variable)) + 
theme_bw() + geom_point() + geom_abline(slope = 1, intercept = 0) + 
xlim(-2,2) + ylim(-2,2) + xlab("Measured") + ylab("Simulated")
year <- 2000
time.frame <- 35


train.models <- function(year, time.frame) {
   predictions <- sapply(year:(max(df$YEAR)-1), 
          function(year) {
             lmod <- lm(TEMP ~ PRESSURE + RAINFALL, DF,
                        subset = with(DF, YEAR %in% (year - time.frame + 1):year))

             pred <- predict(lmod, newdata = DF[DF$YEAR == (year + 1),])
             names(pred) <- year + 1
             return (pred)
          })

   return (predictions)
}

models1 <- train.models(2000, 35)
models2 <- train.models(2001, 10)


rmse(models1 - DF$TEMP[DF$YEAR %in% names(models1)])
rmse(models2 - DF$TEMP[DF$YEAR %in% names(models2)])