GMM 滚动估计对每个 windows 重复相同的值

GMM rolling estimation repeats same value to each windows

使用以下代码结构,我在滚动的基础上估计了 gmm。该代码似乎有效,但最后,每个 windows 的结果都是相同的,这是不应该的。

library(zoo)
library(gmm)
    > dput(ALLX)
structure(c(" 0.0028923077", " 0.0099160000", "-0.0042440000", 
" 0.0795800000", "-0.0350041667", "-0.2480500000", " 0.0042880000", 
"-0.1620888889", " 0.0637518519", " 0.0928178571", "-0.0974379310", 
"-0.1012612903", "-0.0141133333", " 0.0373500000", " 0.0420323529", 
" 0.0406323529", "-0.0479314286", " 0.0393514286", " 0.0134594595", 
"-0.0042162162", " 0.0282142857", "-0.0467717949", " 0.0271487179", 
" 0.0484756757", " 0.0388105263", "-0.0232324324", "-0.1534487179", 
"-0.0553538462", "-0.0127976190", " 0.0248613636", "-0.0069093023", 
" 0.0016234043", "-0.0004617021", "-0.0747617021", " 0.0362914894", 
" 0.0773816327", "-0.0038936170", " 0.0876829787", "-0.0198040000", 
"-0.0078372549", " 0.0343978723", " 0.0350833333", "-0.0382480000", 
" 0.0824346939", "-0.0280615385", "-0.0312228070", "-0.1020928571", 
"-0.1011178571", " 0.0460186441", " 0.0407403509", "-0.0200607143", 
" 0.1692310345", " 0.1254927273", "-0.0012947368", " 0.0410393443", 
"-0.0060650000", " 0.1175457627", "-0.0219952381", " 0.0538709677", 
" 0.1302564516", " 0.0177306452", " 0.0465440678", "-0.0318777778", 
"-0.0027257576", "-0.0808714286", " 0.0753223881", " 0.0421338235", 
" 0.0192617647", " 0.0645835821", " 0.0178743243", " 0.0917106667", 
"-0.0504720000", "-0.0605917808", " 0.0670115942", " 0.0841644737", 
"-0.0862937500", " 0.0583458333", " 0.0341283951", " 0.0363376623", 
"-0.0072297619", " 0.0697188235", " 0.0708900000", " 0.0964263736", 
" 0.0265102273", "-0.0310302326", " 0.0419810000", "-0.0719272727", 
" 0.0243521739", "-0.0945943925", " 0.0339018868", " 0.0440350000", 
" 0.0503681818", " 0.0629381356", "-0.0510975000", " 0.0249760684", 
" 0.0827214876", " 0.0892224806", "-0.0168313043", " 0.0291585366", 
" 0.0273647541", "-0.0975674242", "-0.0746062016", " 0.0238595238", 
"-0.0278552000", "-0.0925223077", " 0.0659656934", " 0.0456731343", 
" 0.0466132450", "-0.0135568493", " 0.0414873016", " 0.0340628205", 
" 0.0267903846", "-0.0494906667", " 0.0180333333", " 0.0387062500", 
"-0.0283124183", " 0.0306898810", " 0.0886619048", "-0.0198162921", 
"-0.0154431818", "-0.0838285714", " 0.0658326531", "-0.0614076503", 
" 0.0424984211", "-0.0170115000", " 0.0205202970", " 0.0596765258", 
"-0.0538638298", "-0.0453000000", "-0.0493330317", " 0.0545885714", 
" 0.1193183168", "-0.0335423810", " 0.0599138393", " 0.0207066964", 
" 0.0554773756", " 0.0606587156", " 0.0448098039", "-0.0354082569", 
" 0.0057316038", " 0.0019272727", " 0.0214976303", "-0.0108309179", 
" 0.0166442211", " 0.0028019512", "-0.0548807910", " 0.0297556962", 
"-0.0161016854", " 0.0100739726", " 0.0538203704", " 0.0304168724", 
" 0.0246617647", " 0.0294436975", "-0.0192055300", "-0.0221397906", 
" 0.0429194215", "-0.1356025974", " 0.0155836364", " 0.0421339806", 
" 0.0193581152", "-0.0316510526", " 0.0171187192", " 0.0299633484", 
" 0.0136308017", "-0.0803899225", "-0.0462198413", "-0.0337478469", 
"-0.0044057018", " 0.0297085185", " 0.0273300000", "-0.0814074689", 
" 0.0194720183", "-0.0640064885", " 0.0194782979", " 0.0047418868", 
"-0.0393941634", " 0.0119372093", "-0.0092268116", "-0.0221629956", 
" 0.0803489130", "-0.0976939502", "-0.0391824701", "-0.0672646154", 
"-0.0124784247", "-0.1038085627", "-0.0105592476", "-0.0279538710", 
" 0.0196077181", " 0.0338205788", "-0.0189602996", "-0.0860247273", 
"-0.0478760714", "-0.0510344720", " 0.0275029586", " 0.0428622291", 
" 0.0166286232", "-0.0288036101", "-0.0180476923", " 0.0393909483", 
"-0.0641453901", "-0.0841982143", " 0.0047675900", "-0.0240106312", 
"-0.0700313793", " 0.0733418994", "-0.0533325905", "-0.0331509589", 
" 0.0489809399", "-0.0736352078", " 0.0240526646", "-0.0939243553", 
"-0.0042405995", "-0.0387828042", "-0.0470409756", "-0.0931115294", 
"-0.0910497738", " 0.0013111111", " 0.0348456633", " 0.0705459082", 
" 0.0758425000", "-0.1478202222", " 0.0717040179", " 0.0569382114", 
"-0.0945091623", " 0.1044561713", " 0.0293398396", "-0.1272769802", 
" 0.0325193750", " 0.0277184066", "-0.0341782369", "-0.0250598160", 
" 0.0189187500", "-0.0074535385", "-0.0359813953", " 0.0160150115", 
" 0.0476877551", "-0.1018837398", "-0.0427113689", " 0.0191961637", 
" 0.1059326403", "-0.169583118", "-0.116332577", "-0.129824912", 
"-0.124963142", "-0.126441587", "-0.225621177", "-0.168195296", 
"-0.338720204", "-0.317270673", "-0.273857581", " 0.035586249", 
"-0.092702051", "-0.064176332", "-0.153796722", "-0.325390751", 
"-0.274011376", "-0.227546299", "-0.365942549", " 0.085455103", 
"-0.010836766", "-0.217781081", " 0.003511111", "-0.096351701", 
"-0.036061785", "-0.082746305", "-0.058446435", "-0.029538576", 
"-0.061935687", "-0.099415678", "-0.159079368", " 0.005994670", 
"-0.120082806", " 0.014296796", " 0.015944490", "-0.178096580", 
"-0.196765343", "-0.149854935", "-0.182080042", "-0.047670592", 
"-0.064468455", "-0.087561840", "-0.090476667", "-0.181043278", 
"-0.085486903", "-0.090858829", "-0.066936975", "-0.015848709", 
"-0.110468333", "-0.153506248", "-0.044522802", "-0.080978652", 
"-0.159889171", "-0.154480172", "-0.205349921", "-0.103695059", 
"-0.138147377", "-0.145655249", "-0.040561855", "-0.126550923", 
"-0.108670753", "-0.072749279", " 0.007390436", "-0.158405424", 
"-0.194249538", "-0.249071040", "-0.063031677", "-0.137200770", 
"-0.005985255", "-0.061970616", "-0.001665978", "-0.125325668", 
"-0.135089529", "-0.030677383", "-0.022131183", "-0.027605680", 
"-0.048131926", "-0.009713796", "-0.024840051", "-0.003947993", 
"-0.028356052", " 0.008669359", "-0.065582513", "-0.018108408", 
"-0.089593204", " 0.016014273", "-0.222439074", "-0.002558520", 
"-0.003835706", "-0.084480896", "-0.011204725", "-0.009045822", 
" 0.039202318", "-0.017543767", "-0.044229046", "-0.064026845", 
" 0.019091399", "-0.146065442", "-0.082462079", "-0.076968274", 
" 0.010287979", " 0.048663646", "-0.052517617", "-0.078097749", 
"-0.058839773", " 0.020560811", " 0.022542800", "-0.049323346", 
"-0.136855394", " 0.051979118", "-0.049670745", "-0.269486125", 
"-0.016167840", "-0.073386011", "-0.138936808", "-0.053387474", 
" 0.010016301", "-0.068842589", "-0.074879318", "-0.072413842", 
"-0.101225355", "-0.003558611", "-0.041926566", "-0.148669593", 
"-0.009603558", "-0.233703786", "-0.041852318", " 0.041637969", 
"-0.135528966", "-0.107569064", "-0.209429496", "-0.196268700", 
"-0.046792696", "-0.087288872", "-0.034059584", "-0.174947640", 
"-0.138459757", "-0.122212061", "-0.124686515", "-0.391344971", 
"-0.216096655", " 0.052018432", "-0.019510655", "-0.139205691", 
"-0.125329712", " 0.141051207", " 0.049742832", " 0.026519711", 
"-0.110162918", " 0.055812668", " 0.068929681", "-0.039230413", 
"-0.041629101", "-0.055834264", "-0.006038571", "-0.159107391", 
" 0.070929013", " 0.004192821", "-0.120316632", "-0.130435927", 
"-0.026905200", " 0.003387477", "-0.011266511", " 0.057113748", 
" 0.000171649", "-0.046263896", "-0.020481623", "-0.105748694", 
"-0.051830004", " 0.015952694", "-0.104439047", "-0.048152805", 
"-0.043645953", "-0.160393736", "-0.074220347", "-0.059452923", 
"-0.025244806", "-0.092688259", "-0.003549789", " 0.023238113", 
"-0.022835595", "-0.089331999", "-0.063785474", "-0.099384857", 
"-0.053629441", " 0.056101995", "-0.075942152", "-0.056728587", 
"-0.036623971", "-0.003294702", "-0.022135444", "-0.059247766", 
"-0.068374619", "-0.078938524", "-0.031597308", "-0.156975052", 
"-0.026702652", " 0.001761469", "-0.031652647", "-0.057892629", 
"-0.061797364", "-0.112400246", "-0.031780546", "-0.066364666", 
"-0.027673000", " 0.005256987", "-0.025408318", "-0.059037925", 
"-0.049307603", "-0.039687409", "-0.091508111", "-0.144818522", 
" 0.002830555", "-0.148057711", "-0.039918341", "-0.001476170", 
"-0.109996768", "-0.014149004", "-0.064867451", "-0.134710810", 
"-0.074557220", "-0.116546886", "-0.160253490", " 0.038980675", 
"-0.156042254", "-0.247471967", "-0.099724931", "-0.025823112", 
" 0.034017819", "-0.021862462", "-0.105484344", "-0.043692340", 
"-0.032831220", " 0.000531434", "-0.138783395", "-0.080691278", 
"-0.101847518", "-0.011818825", "-0.021092363", "-0.063934848", 
"-0.083217974"), .Dim = c(240L, 2L), .Dimnames = list(c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", 
"36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", 
"47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", 
"58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", 
"69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", 
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", 
"91", "92", "93", "94", "95", "96", "97", "98", "99", "100", 
"101", "102", "103", "104", "105", "106", "107", "108", "109", 
"110", "111", "112", "113", "114", "115", "116", "117", "118", 
"119", "120", "121", "122", "123", "124", "125", "126", "127", 
"128", "129", "130", "131", "132", "133", "134", "135", "136", 
"137", "138", "139", "140", "141", "142", "143", "144", "145", 
"146", "147", "148", "149", "150", "151", "152", "153", "154", 
"155", "156", "157", "158", "159", "160", "161", "162", "163", 
"164", "165", "166", "167", "168", "169", "170", "171", "172", 
"173", "174", "175", "176", "177", "178", "179", "180", "181", 
"182", "183", "184", "185", "186", "187", "188", "189", "190", 
"191", "192", "193", "194", "195", "196", "197", "198", "199", 
"200", "201", "202", "203", "204", "205", "206", "207", "208", 
"209", "210", "211", "212", "213", "214", "215", "216", "217", 
"218", "219", "220", "221", "222", "223", "224", "225", "226", 
"227", "228", "229", "230", "231", "232", "233", "234", "235", 
"236", "237", "238", "239", "240"), c("DE", "rmrf_local")), index = 1:240, class = c("zooreg", 
"zoo"), frequency = 1)

而我的代码如下:

DE <- as.matrix(ALLX$DE)
    p <- nrow(DE)
    rmrf_local <- as.matrix(ALLX$rmrf_local)
    h <- matrix(rmrf_local, p, 1)
    dolm <- function(x) gmm(DE ~ rmrf_local, x=h)
    #ALLX = as.zooreg(ALLX )

#rolling gmm estimate  

pp=rollapply(ALLX,width=24,by=1,FUN=dolm,  by.column = FALSE, align = "right")
pp=data.frame(pp)
pp$coefficients

在这一点上,我希望每个 217 windows 得到不同的结果,但我得到每个 windows 的重复相同值,如下面输出的最后三行所示,任何请帮忙?

.
.
.
[[215]]
 (Intercept)   rmrf_local 
-0.001226275 -0.032253645 

[[216]]
 (Intercept)   rmrf_local 
-0.001226275 -0.032253645 

[[217]]
 (Intercept)   rmrf_local 
-0.001226275 -0.032253645 

显然,zoo::rollapply 运行s 跨越一维向量或专门针对 data 参数:

data: the data to be used (representing a series of observations).

由于您需要 运行 在二维矩阵上建模,请考虑 lapply 在您的 window 长度为 24 的块中对矩阵长度进行索引。然后,每个迭代将数据传递到 gmm。此外,您原来的 ALLX 保留了字符值,因此需要使用 as.numeric 进行转换。否则会引发错误:二元运算符的非数字参数

gmm_list <- lapply(1:(length(ALLX$DE)-24), function(i) {
  tmp <- ALLX[i:(i+23)]
  DE <- as.matrix(as.numeric(tmp$DE))
  p <- nrow(DE)
  rmrf_local <- as.matrix(as.numeric(tmp$rmrf_local))
  h <- matrix(rmrf_local, p, 1)

  gmm(DE ~ rmrf_local, x=h)
})

# OUTPUTS ALL COEFFICIENTS             
lapply(gmm_list, "[[", "coefficients")

有几个问题:

  • ALLX 是字符,而显然我们需要数字数据
  • dolm 不依赖于它的参数,因此不能期望它为不同的输入提供不同的值

试试这个:

ALLX0 <- apply(ALLX, 2, as.numeric)

do_gmm <- function(X) {
  DE <- X[, "DE"]
  h <- rmrf_local <- X[, "rmrf_local"]
  coef(gmm(DE ~ rmrf_local, x = h))
}

r <- rollapplyr(ALLX0, 24, do_gmm, by.column = FALSE, fill = NA)

这给出了一个矩阵 r 具有与 ALLX 相同的维度,在开始时用 NA 填充它以实现这一点,但如果您不想要 NA,则删除 fill = NA .