时间序列和预测
Time Series and Forecasting
我的原始数据是日期,A:z。我需要将每个 column/vector A:z 作为独立的 ts() 时间序列。所以我可以 运行 auto.armia 和每个 Vector 上的预测函数。我可以成功地使用 seq_along 在我的全局环境中创建单独的 df A:Z 。我现在的麻烦是遍历每个 df 并将它们变成时间序列,然后用 auto.armia 遍历每个 df 和预测函数。最终结果应该是一个 df,在我指定的时间(1 年或 5 年后),每个 A:Z 的点预测我想将下一个周期数的预测标准设置为变量。
structure(list(YEAR = c(2001, 2002, 2003, 2004, 2005, 2006),
A = c(0, 0, 0, 2003, 0, 0), B = c(0, 0, 0, 2004, 0, 0), C = c(0,
0, 0, 2005, 0, 0), D = c(0, 0, 0, 2006, 0, 0), E = c(0, 0,
0, 2007, 0, 0), F = c(0, 0, 0, 2008, 0, 0), G = c(0, 0, 0,
2009, 0, 2310593.63), H = c(0, 0, 0, 2010, 0, 949885.17),
I = c(0, 0, 0, 2011, 51939.35, 755167.32), J = c(0, 0, 0,
2012, 200485.83, 0), K = c(0, 0, 0, 2013, 340741.25, 0),
L = c(0, 0, 0, 2014, 692627.39, 0), M = c(0, 0, 0, 2015,
498738.38, 13228.06), N = c(0, 0, 0, 2016, 727855.33, 151441.77
), O = c(0, 0, 0, 2017, 1197076.02, 108188.58), P = c(0,
0, 0, 2018, 558267.98, 0), Q = c(0, 0, 0, 2019, 631624.18,
0), R = c(0, 0, 0, 2020, 1348869.22, 0), S = c(0, 0, 0, 2021,
1206861.95, 0), T = c(0, 0, 0, 2022, 0, 0), U = c(0, 0, 0,
2023, 0, 0), V = c(0, 0, 0, 2024, 0, 0), W = c("0", "0",
"0", "Grand Total", "7455086.88", "4288504.53"), X = c(0,
0, 0, 2011, 51939.35, 755167.32), Y = c(0, 0, 0, 2012, 200485.83,
0), Z = c(0, 0, 0, 2013, 340741.25, 0)), row.names = c(NA,
6L), class = "data.frame")
以下是为每一列生成单独时间序列的一些选项:
如果你上面的结构是data
,那么你可以做下面的
选项 1:设置为 data.table 并将 ts()
应用于每一列 A-Z
library(data.table)
setDT(data)
dt_as_ts = data[, lapply(.SD, ts, start=2001, end=2006), .SDcols=c(2:27)]
这将 return 一个 data.table,其中每一列都是 class "ts"
的一个对象。
输出:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
<ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts>
1: 0 0 0 0 0 0 0 0.0 0.00 0.0 0.0 0.0 0.00 0.0 0.0 0 0.0 0 0 0 0 0 0 0.00 0.0 0.0
2: 0 0 0 0 0 0 0 0.0 0.00 0.0 0.0 0.0 0.00 0.0 0.0 0 0.0 0 0 0 0 0 0 0.00 0.0 0.0
3: 0 0 0 0 0 0 0 0.0 0.00 0.0 0.0 0.0 0.00 0.0 0.0 0 0.0 0 0 0 0 0 0 0.00 0.0 0.0
4: 2003 2004 2005 2006 2007 2008 2009 2010.0 2011.00 2012.0 2013.0 2014.0 2015.00 2016.0 2017.0 2018 2019.0 2020 2021 2022 2023 2024 Grand Total 2011.00 2012.0 2013.0
5: 0 0 0 0 0 0 0 0.0 51939.35 200485.8 340741.2 692627.4 498738.38 727855.3 1197076.0 558268 631624.2 1348869 1206862 0 0 0 7455086.88 51939.35 200485.8 340741.2
6: 0 0 0 0 0 0 2310594 949885.2 755167.32 0.0 0.0 0.0 13228.06 151441.8 108188.6 0 0.0 0 0 0 0 0 4288504.53 755167.32 0.0 0.0
您可以预测其中每一个的下一个值(W 除外,它不是数字),如下所示:
t(dt_of_ts[,lapply(.SD, function(x) predict(arima(x))), .SDcols=-23])
选项 2:只需将 ts()
直接应用于感兴趣的列
或者,您可以像这样将整个数据作为矩阵提供给 ts()
,不包括第一列,年份
data_as_ts=ts(data[,-1], start=2001, end=2006)
data_as_ts
Time Series:
Start = 2001
End = 2006
Frequency = 1
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
2001 0 0 0 0 0 0 0 0.0 0.00 0.0 0.0 0.0 0.00 0.0 0.0 0 0.0 0 0 0 0 0 1 0.00 0.0 0.0
2002 0 0 0 0 0 0 0 0.0 0.00 0.0 0.0 0.0 0.00 0.0 0.0 0 0.0 0 0 0 0 0 1 0.00 0.0 0.0
2003 0 0 0 0 0 0 0 0.0 0.00 0.0 0.0 0.0 0.00 0.0 0.0 0 0.0 0 0 0 0 0 1 0.00 0.0 0.0
2004 2003 2004 2005 2006 2007 2008 2009 2010.0 2011.00 2012.0 2013.0 2014.0 2015.00 2016.0 2017.0 2018 2019.0 2020 2021 2022 2023 2024 4 2011.00 2012.0 2013.0
2005 0 0 0 0 0 0 0 0.0 51939.35 200485.8 340741.2 692627.4 498738.38 727855.3 1197076.0 558268 631624.2 1348869 1206862 0 0 0 3 51939.35 200485.8 340741.2
2006 0 0 0 0 0 0 2310594 949885.2 755167.32 0.0 0.0 0.0 13228.06 151441.8 108188.6 0 0.0 0 0 0 0 0 2 755167.32 0.0 0.0
这将 return class 的对象:"mts" "ts" "matrix"
,每一列都是 class“ts”的对象。例如,class(data_as_ts[,4])
returns "ts"
请注意 W 列是如何转换为数字的。
您可以使用 apply
获取每列的预测值
apply(data_as_ts,2,function(x) predict(arima(x)))
选项 3:将 data
拆分为单独的框架,并使用 lapply()
到 return ts
个对象的列表
最后,如果你想按列拆分框架,并有一个单独的 ts 对象列表,你可以这样做:
list_of_ts = lapply(split(melt(setDT(data)[,!c("W")], id="YEAR"), by="variable"),
function(x) ts(x$value, start=2001, end=2006)
)
输出(前三个元素)
$A
Time Series:
Start = 2001
End = 2006
Frequency = 1
[1] 0 0 0 2003 0 0
$B
Time Series:
Start = 2001
End = 2006
Frequency = 1
[1] 0 0 0 2004 0 0
$C
Time Series:
Start = 2001
End = 2006
Frequency = 1
[1] 0 0 0 2005 0 0
同样,您可以使用 lapply
获得列表中每个项目的下一个预测
lapply(list_of_ts,function(x) predict(arima(x)))
我的原始数据是日期,A:z。我需要将每个 column/vector A:z 作为独立的 ts() 时间序列。所以我可以 运行 auto.armia 和每个 Vector 上的预测函数。我可以成功地使用 seq_along 在我的全局环境中创建单独的 df A:Z 。我现在的麻烦是遍历每个 df 并将它们变成时间序列,然后用 auto.armia 遍历每个 df 和预测函数。最终结果应该是一个 df,在我指定的时间(1 年或 5 年后),每个 A:Z 的点预测我想将下一个周期数的预测标准设置为变量。
structure(list(YEAR = c(2001, 2002, 2003, 2004, 2005, 2006),
A = c(0, 0, 0, 2003, 0, 0), B = c(0, 0, 0, 2004, 0, 0), C = c(0,
0, 0, 2005, 0, 0), D = c(0, 0, 0, 2006, 0, 0), E = c(0, 0,
0, 2007, 0, 0), F = c(0, 0, 0, 2008, 0, 0), G = c(0, 0, 0,
2009, 0, 2310593.63), H = c(0, 0, 0, 2010, 0, 949885.17),
I = c(0, 0, 0, 2011, 51939.35, 755167.32), J = c(0, 0, 0,
2012, 200485.83, 0), K = c(0, 0, 0, 2013, 340741.25, 0),
L = c(0, 0, 0, 2014, 692627.39, 0), M = c(0, 0, 0, 2015,
498738.38, 13228.06), N = c(0, 0, 0, 2016, 727855.33, 151441.77
), O = c(0, 0, 0, 2017, 1197076.02, 108188.58), P = c(0,
0, 0, 2018, 558267.98, 0), Q = c(0, 0, 0, 2019, 631624.18,
0), R = c(0, 0, 0, 2020, 1348869.22, 0), S = c(0, 0, 0, 2021,
1206861.95, 0), T = c(0, 0, 0, 2022, 0, 0), U = c(0, 0, 0,
2023, 0, 0), V = c(0, 0, 0, 2024, 0, 0), W = c("0", "0",
"0", "Grand Total", "7455086.88", "4288504.53"), X = c(0,
0, 0, 2011, 51939.35, 755167.32), Y = c(0, 0, 0, 2012, 200485.83,
0), Z = c(0, 0, 0, 2013, 340741.25, 0)), row.names = c(NA,
6L), class = "data.frame")
以下是为每一列生成单独时间序列的一些选项:
如果你上面的结构是data
,那么你可以做下面的
选项 1:设置为 data.table 并将 ts()
应用于每一列 A-Z
library(data.table)
setDT(data)
dt_as_ts = data[, lapply(.SD, ts, start=2001, end=2006), .SDcols=c(2:27)]
这将 return 一个 data.table,其中每一列都是 class "ts"
的一个对象。
输出:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
<ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts> <ts>
1: 0 0 0 0 0 0 0 0.0 0.00 0.0 0.0 0.0 0.00 0.0 0.0 0 0.0 0 0 0 0 0 0 0.00 0.0 0.0
2: 0 0 0 0 0 0 0 0.0 0.00 0.0 0.0 0.0 0.00 0.0 0.0 0 0.0 0 0 0 0 0 0 0.00 0.0 0.0
3: 0 0 0 0 0 0 0 0.0 0.00 0.0 0.0 0.0 0.00 0.0 0.0 0 0.0 0 0 0 0 0 0 0.00 0.0 0.0
4: 2003 2004 2005 2006 2007 2008 2009 2010.0 2011.00 2012.0 2013.0 2014.0 2015.00 2016.0 2017.0 2018 2019.0 2020 2021 2022 2023 2024 Grand Total 2011.00 2012.0 2013.0
5: 0 0 0 0 0 0 0 0.0 51939.35 200485.8 340741.2 692627.4 498738.38 727855.3 1197076.0 558268 631624.2 1348869 1206862 0 0 0 7455086.88 51939.35 200485.8 340741.2
6: 0 0 0 0 0 0 2310594 949885.2 755167.32 0.0 0.0 0.0 13228.06 151441.8 108188.6 0 0.0 0 0 0 0 0 4288504.53 755167.32 0.0 0.0
您可以预测其中每一个的下一个值(W 除外,它不是数字),如下所示:
t(dt_of_ts[,lapply(.SD, function(x) predict(arima(x))), .SDcols=-23])
选项 2:只需将 ts()
直接应用于感兴趣的列
或者,您可以像这样将整个数据作为矩阵提供给 ts()
,不包括第一列,年份
data_as_ts=ts(data[,-1], start=2001, end=2006)
data_as_ts
Time Series:
Start = 2001
End = 2006
Frequency = 1
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
2001 0 0 0 0 0 0 0 0.0 0.00 0.0 0.0 0.0 0.00 0.0 0.0 0 0.0 0 0 0 0 0 1 0.00 0.0 0.0
2002 0 0 0 0 0 0 0 0.0 0.00 0.0 0.0 0.0 0.00 0.0 0.0 0 0.0 0 0 0 0 0 1 0.00 0.0 0.0
2003 0 0 0 0 0 0 0 0.0 0.00 0.0 0.0 0.0 0.00 0.0 0.0 0 0.0 0 0 0 0 0 1 0.00 0.0 0.0
2004 2003 2004 2005 2006 2007 2008 2009 2010.0 2011.00 2012.0 2013.0 2014.0 2015.00 2016.0 2017.0 2018 2019.0 2020 2021 2022 2023 2024 4 2011.00 2012.0 2013.0
2005 0 0 0 0 0 0 0 0.0 51939.35 200485.8 340741.2 692627.4 498738.38 727855.3 1197076.0 558268 631624.2 1348869 1206862 0 0 0 3 51939.35 200485.8 340741.2
2006 0 0 0 0 0 0 2310594 949885.2 755167.32 0.0 0.0 0.0 13228.06 151441.8 108188.6 0 0.0 0 0 0 0 0 2 755167.32 0.0 0.0
这将 return class 的对象:"mts" "ts" "matrix"
,每一列都是 class“ts”的对象。例如,class(data_as_ts[,4])
returns "ts"
请注意 W 列是如何转换为数字的。
您可以使用 apply
获取每列的预测值
apply(data_as_ts,2,function(x) predict(arima(x)))
选项 3:将 data
拆分为单独的框架,并使用 lapply()
到 return ts
个对象的列表
最后,如果你想按列拆分框架,并有一个单独的 ts 对象列表,你可以这样做:
list_of_ts = lapply(split(melt(setDT(data)[,!c("W")], id="YEAR"), by="variable"),
function(x) ts(x$value, start=2001, end=2006)
)
输出(前三个元素)
$A
Time Series:
Start = 2001
End = 2006
Frequency = 1
[1] 0 0 0 2003 0 0
$B
Time Series:
Start = 2001
End = 2006
Frequency = 1
[1] 0 0 0 2004 0 0
$C
Time Series:
Start = 2001
End = 2006
Frequency = 1
[1] 0 0 0 2005 0 0
同样,您可以使用 lapply
获得列表中每个项目的下一个预测
lapply(list_of_ts,function(x) predict(arima(x)))