我试图在滚动窗口上运行'vrtest'包上的命令。我可以在一组数据上完美地运行它们,但是我很难使用'zoo'包中的rollapply函数。比方说,我的数据是一个数据帧x,其中变量是y,我想在时间上滚动10个观察值。我有30个观察,所以我应该有JR1,JR2和JS1的21个输出。因此,举例来说,如果我正在尝试进行滚动联合怀疑测试,那么我已经得到了最远的测试;
JWroll <- rollapply(x$y, 10, function(x){
x<- Joint.Wright(x$y, kvec)}
我知道我需要在括号内的某处定义'kvec',但我还没有让它工作。数据是;
10
10
8
5
10
2
8
2
10
2
8
8
10
10
8
5
10
8
8
12
10
7
8
7
8
1
8
5
10
2
非常感谢任何建议或帮助!
答案 0 :(得分:0)
rollapply
中的函数必须返回标量或普通向量,而不是列表。试试这个:
# input data
kvec <- c(2, 4, 6)
x <- c(10, 10, 8, 5, 10, 2, 8, 2, 10, 2, 8, 8, 10, 10, 8, 5, 10, 8,
8, 12, 10, 7, 8, 7, 8, 1, 8, 5, 10, 2)
library(zoo)
library(vrtest)
# the -1 removes the holding period from the output; unlist makes it a plain vector
rollapply(x, 10, function(x) unlist(Joint.Wright(x, kvec)[-1]) )
,并提供:
JR1 JR2 JS1
[1,] 1.5864093 1.5907354 3.042555
[2,] 2.1701906 2.1396224 3.042555
[3,] 2.5298221 2.4731367 3.042555
[4,] 2.1701906 2.1405101 3.042555
[5,] 1.1831651 1.1973584 3.042555
[6,] 0.9238698 0.9238698 3.042555
[7,] 1.0155461 1.0177885 3.042555
[8,] 1.4546477 1.4897304 3.042555
[9,] 1.2983318 1.2741745 3.042555
[10,] 1.1148843 1.1287839 3.042555
[11,] 1.2347644 1.1613745 3.042555
[12,] 1.0711498 1.0541897 3.042555
[13,] 1.1704861 1.1552450 3.042555
[14,] 1.1895470 1.1769137 3.042555
[15,] 0.9343208 0.9388534 3.042555
[16,] 0.9531326 0.9467291 3.042555
[17,] 0.8966971 0.8695012 3.042555
[18,] 0.7543456 0.7304734 3.042555
[19,] 0.8354976 0.7837317 3.042555
[20,] 1.0690090 0.9367724 3.042555
[21,] 2.5407608 2.3289587 3.042555