首页 > 解决方案 > 使用 R 在两年间隔内对时间序列进行子集化

问题描述

我想使用 autoarima 开发模型,并且我有 1970 年到 2015 年的时间序列数据。我只想看看它如何使用一年数据(1970 年)进行预测,然后预测 2005 年,使用三年数据(1970 年, 1971,1972) 预测 2005 年或 2006 年,使用五年数据 (1970, 1971,1972,1973, 1974 (训练集)...并以两年间隔继续 (如 1,3,5,7..年数据)直到 2015 年预测一年(测试集)。我在编码方面遇到困难,但我在 Stackoverflow 上有以下重要链接,我无法修改它。这个子集问题让我发疯,请帮助我出去了。我从 StackOverflow 获得了这个很好的来源,我想知道,但是我如何根据我的情况定制它? R 多变量提前一步预测和准确性

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) 

标签: r

解决方案


考虑在两对火车年份和测试年份之间使用Map(wrapper to ) 的元素循环以进行子集化:mapply

年份值

test_yrs <- lapply(c(2005:2015), function(i) c(2005:i) )

train_yrs <- lapply(seq(1,(2015-1970), by=2), function(i) 
     c(1970:2004)[c(1:i)] 
)[seq_along(test_yrs)]


train_yrs    

# [[1]]
# [1] 1970
# 
# [[2]]
# [1] 1970 1971 1972
# 
# [[3]]
# [1] 1970 1971 1972 1973 1974
# 
# [[4]]
# [1] 1970 1971 1972 1973 1974 1975 1976
# 
# [[5]]
# [1] 1970 1971 1972 1973 1974 1975 1976 1977 1978
# 
# [[6]]
# [1] 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
# 
# [[7]]
# [1] 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
# 
# [[8]]
# [1] 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
# 
# [[9]]
# [1] 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
# 
# [[10]]
# [1] 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
# 
# [[11]]
# [1] 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990


test_yrs

# [[1]]
# [1] 2005
# 
# [[2]]
# [1] 2005 2006
# 
# [[3]]
# [1] 2005 2006 2007
# 
# [[4]]
# [1] 2005 2006 2007 2008
# 
# [[5]]
# [1] 2005 2006 2007 2008 2009
# 
# [[6]]
# [1] 2005 2006 2007 2008 2009 2010
# 
# [[7]]
# [1] 2005 2006 2007 2008 2009 2010 2011
# 
# [[8]]
# [1] 2005 2006 2007 2008 2009 2010 2011 2012
# 
# [[9]]
# [1] 2005 2006 2007 2008 2009 2010 2011 2012 2013
# 
# [[10]]
# [1] 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
# 
# [[11]]
# [1] 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015

造型

model_func <- function(train_yrs, test_yrs) {

   train_df <- df[df$Year %in% train_yrs,]
   test_df <- df[df$Year %in% test_yrs,]

   mod <- lm(DepVar ~ IndepVar1 + IndepVar2 + IndepVar3, data = train_df)

   pred <- predict(mod, newdata = DF.test2),
   rmse <- sqrt(mean((test_df$DepVar - pred)^2))

   return(list(pred = pred, rmse = rmse))
}

results_list <- Map(model_func, train_yrs, test_yrs)

# ALL ELEMENTS
results_list

# ALL pred ELEMENTS
lapply(results_list, `[`, "pred")

# ALL rmse ELEMENTS
lapply(results_list, `[`, "rmse")    

# INDIVIDUAL ELEMENTS
results_list[[1]]$pred
results_list[[1]]$rmse

results_list[[2]]$pred
results_list[[2]]$rmse
...
results_list[[11]]$pred
results_list[[11]]$rmse

要跨多个数据帧运行上述过程,请将过程包装到一个方法中并lapply在数据帧列表中调用它。结果将嵌套在顶层

proc_df <- function(df) {
    # NEW PARAM
    model_func <- function(train_yrs, test_yrs, df) {    
       ...
    }

    # ADDED ARG
    results_list <- Map(model_func, train_yrs, test_yrs, MoreArgs=list(df))

    return(results_list)
}

dfs <- list(df1, df2, df3)
df_results_list <- lapply(dfs, proc_df)

# ALL ELEMENTS BY DATA FRAME
df_results_list[[1]]
df_results_list[[2]]
df_results_list[[3]]

推荐阅读