使用'lappy'为我的数据帧应用'ar'(自回归模型)返回'numeric(0)`?

使用'lappy'为我的数据帧应用'ar'(自回归模型)返回'numeric(0)`?,r,time-series,lapply,R,Time Series,Lapply,我正在使用一个包含所有数字数据的data.frame。我想计算每列的一阶自回归系数。我选择应用函数来执行任务,并定义了如下函数: return.ar <- function(vec){ return(as.numeric(ar(vec)$ar)) } structure(list(Year = c(1929, 1930, 1931, 1932, 1933, 1934), C.Growth = c(0.94774902516838, 0.989078396169958, 0.911

我正在使用一个包含所有数字数据的data.frame。我想计算每列的一阶自回归系数。我选择应用函数来执行任务,并定义了如下函数:

return.ar <- function(vec){
   return(as.numeric(ar(vec)$ar))
}
structure(list(Year = c(1929, 1930, 1931, 1932, 1933, 1934), 
C.Growth = c(0.94774902516838, 0.989078396169958, 0.911586749357132, 
0.996183522774413, 1.08170234030149, 1.05797659377887), S.Return = c(-19.7068321696574, 
-31.0834309393085, -45.2864376593084, -9.42504715968666, 
57.0992131145999, 4.05781718258972), Rf = c(4.79316783034255, 
2.58656906069154, 1.24356234069162, 0.954952840313344, 0.199213114599945, 
0.147817182589718), Inflation = c(-0.0531678303425544, -0.15656906069154, 
-0.15356234069162, -0.00495284031334435, 0.100786885400055, 
0.0321828174102824), Mkt.ret = c(-14.9668321696574, -28.6534309393085, 
-44.1964376593084, -8.47504715968666, 57.3992131145999, 4.23781718258972
), SL = c(-45.2568321696575, -35.1134309393085, -41.1864376593084, 
-5.28504715968666, 166.0392131146, 34.1378171825897), SM = c(-30.7368321696574, 
-31.9034309393085, -48.5364376593084, -8.94504715968666, 
118.7092131146, 19.7578171825897), SH = c(-36.7568321696575, 
-45.1834309393085, -51.5364376593084, 2.78495284031334, 125.7792131146, 
7.95781718258972), LL = c(-19.6968321696574, -26.2734309393085, 
-36.2264376593084, -7.31504715968666, 44.1492131145999, 10.6978171825897
), LM = c(0.673167830342554, -29.2434309393085, -59.9864376593084, 
-16.7150471596867, 89.4692131145999, -2.93218281741028), 
LH = c(-4.35683216965745, -43.1934309393085, -57.7364376593084, 
-4.30504715968666, 114.7092131146, -21.8421828174103)), .Names =   c("Year", 
"C.Growth", "S.Return", "Rf", "Inflation", "Mkt.ret", "SL", "SM", 
"SH", "LL", "LM", "LH"), row.names = c(NA, 6L), class = "data.frame")
lapply(df_return[col.names], function (x) ar(x, order.max = 5)$order)
我希望得到一个ar系数的向量。但是我得到了一个列表,其中所有的系数都放在第一个元素中,如下所示

lapply(df_return[,col.names],return.ar)
$C.Growth
[1]  0.35629140 -0.07671252 -0.08699333 -0.27404355  0.21448342
[6] -0.19049197  0.06610908 -0.23077602

$Mkt.ret
numeric(0)

$SL
numeric(0)

$SM
numeric(0)

$SH
numeric(0)

$LL
numeric(0)

$LM
numeric(0)

$LH
numeric(0)
我不明白发生了什么事

dput(head(df_return))
的输出如下所示:

return.ar <- function(vec){
   return(as.numeric(ar(vec)$ar))
}
structure(list(Year = c(1929, 1930, 1931, 1932, 1933, 1934), 
C.Growth = c(0.94774902516838, 0.989078396169958, 0.911586749357132, 
0.996183522774413, 1.08170234030149, 1.05797659377887), S.Return = c(-19.7068321696574, 
-31.0834309393085, -45.2864376593084, -9.42504715968666, 
57.0992131145999, 4.05781718258972), Rf = c(4.79316783034255, 
2.58656906069154, 1.24356234069162, 0.954952840313344, 0.199213114599945, 
0.147817182589718), Inflation = c(-0.0531678303425544, -0.15656906069154, 
-0.15356234069162, -0.00495284031334435, 0.100786885400055, 
0.0321828174102824), Mkt.ret = c(-14.9668321696574, -28.6534309393085, 
-44.1964376593084, -8.47504715968666, 57.3992131145999, 4.23781718258972
), SL = c(-45.2568321696575, -35.1134309393085, -41.1864376593084, 
-5.28504715968666, 166.0392131146, 34.1378171825897), SM = c(-30.7368321696574, 
-31.9034309393085, -48.5364376593084, -8.94504715968666, 
118.7092131146, 19.7578171825897), SH = c(-36.7568321696575, 
-45.1834309393085, -51.5364376593084, 2.78495284031334, 125.7792131146, 
7.95781718258972), LL = c(-19.6968321696574, -26.2734309393085, 
-36.2264376593084, -7.31504715968666, 44.1492131145999, 10.6978171825897
), LM = c(0.673167830342554, -29.2434309393085, -59.9864376593084, 
-16.7150471596867, 89.4692131145999, -2.93218281741028), 
LH = c(-4.35683216965745, -43.1934309393085, -57.7364376593084, 
-4.30504715968666, 114.7092131146, -21.8421828174103)), .Names =   c("Year", 
"C.Growth", "S.Return", "Rf", "Inflation", "Mkt.ret", "SL", "SM", 
"SH", "LL", "LM", "LH"), row.names = c(NA, 6L), class = "data.frame")
lapply(df_return[col.names], function (x) ar(x, order.max = 5)$order)

一旦包含了数据,诊断就变得容易了

ar
将根据AIC自动执行
p
的部分。您的一些列有强有力的证据表明是白噪声,因此
ar
选择了
p=0
,在这种情况下,
$ar
字段将是
数值(0)

我建议您也使用以下方法:

return.ar <- function(vec){
   return(as.numeric(ar(vec)$ar))
}
structure(list(Year = c(1929, 1930, 1931, 1932, 1933, 1934), 
C.Growth = c(0.94774902516838, 0.989078396169958, 0.911586749357132, 
0.996183522774413, 1.08170234030149, 1.05797659377887), S.Return = c(-19.7068321696574, 
-31.0834309393085, -45.2864376593084, -9.42504715968666, 
57.0992131145999, 4.05781718258972), Rf = c(4.79316783034255, 
2.58656906069154, 1.24356234069162, 0.954952840313344, 0.199213114599945, 
0.147817182589718), Inflation = c(-0.0531678303425544, -0.15656906069154, 
-0.15356234069162, -0.00495284031334435, 0.100786885400055, 
0.0321828174102824), Mkt.ret = c(-14.9668321696574, -28.6534309393085, 
-44.1964376593084, -8.47504715968666, 57.3992131145999, 4.23781718258972
), SL = c(-45.2568321696575, -35.1134309393085, -41.1864376593084, 
-5.28504715968666, 166.0392131146, 34.1378171825897), SM = c(-30.7368321696574, 
-31.9034309393085, -48.5364376593084, -8.94504715968666, 
118.7092131146, 19.7578171825897), SH = c(-36.7568321696575, 
-45.1834309393085, -51.5364376593084, 2.78495284031334, 125.7792131146, 
7.95781718258972), LL = c(-19.6968321696574, -26.2734309393085, 
-36.2264376593084, -7.31504715968666, 44.1492131145999, 10.6978171825897
), LM = c(0.673167830342554, -29.2434309393085, -59.9864376593084, 
-16.7150471596867, 89.4692131145999, -2.93218281741028), 
LH = c(-4.35683216965745, -43.1934309393085, -57.7364376593084, 
-4.30504715968666, 114.7092131146, -21.8421828174103)), .Names =   c("Year", 
"C.Growth", "S.Return", "Rf", "Inflation", "Mkt.ret", "SL", "SM", 
"SH", "LL", "LM", "LH"), row.names = c(NA, 6L), class = "data.frame")
lapply(df_return[col.names], function (x) ar(x, order.max = 5)$order)
或者更好:

fit_ar <- function(x) ar(x, order.max = 5)[c("order", "ar")]
lapply(df_return[col.names], fit_ar)
但不幸的是,你从中得不到任何有用的东西。注意,您使用了串联
c()
,因此无法判断哪一个系数对应于哪一列

lappy
与这种循环不同。你应使用:

ar.vec <- vector("list", length(col.names))
for (i in 1:length(col.names))
   ar.vec[[i]] <- return.ar(df_return[[ col.names[i] ]])

ar.vec Zheyuan Li我也试过,但返回的结果相同。我试过'lappy(df_return[col.names],return.ar'),但仍然得到相同的结果。col.names被定义为
c(c(“c.Growth”,“Mkt.ret”),names(df_return)[7:12])
。当然,我不仅仅是想告诉你它不起作用,我感谢你的帮助。我想这也是有道理的。然而,这里的问题是,所有ar系数都放在$C.增长部分。我已经为所有列尝试了显式for循环。我得到了相同的系数。我只是想知道为什么我使用apply得到的结果是所有这些都嵌套在$C.Growth部分中。您可以看到$C.Growth部分中有8个数值。