我需要获得不同对的格兰杰检验结果。
这是提供所需结果(p_value
)的函数,并且工作正常。
myFunction<-function(x,y)
{
test<-grangertest(x~y,data=df)
return(test$`Pr(>F)`[2])
}
此功能运行良好,例如在这种情况下:
myFunction(df$X48df886f9,df$X0deb4b6a8)
现在,我准备所有可能的组合:
list_1<-list(names(df))
list_2<-list(names(df))
combinations<-expand.grid(list_1,list_2)
为了使其具有可重复性,我为您选择了前6个变量
> df[1:50,1:6]
target X48df886f9 X0deb4b6a8 X34b15f335 a8cb14b00 X2f0771a37
1 17.45310 0 0 0 0 0
2 13.30469 0 0 0 0 0
3 16.11810 0 0 0 0 0
4 14.50866 0 0 0 0 0
5 16.48274 0 0 0 0 0
6 14.84513 0 0 0 0 0
7 12.00763 0 0 0 0 0
8 13.30469 0 0 0 0 0
9 13.79429 0 0 0 0 0
10 13.03898 0 0 0 0 0
11 13.91082 0 0 0 0 0
12 16.58810 0 0 0 0 0
13 12.77706 0 0 0 0 0
14 15.76142 0 0 0 0 0
15 11.51294 0 0 0 0 0
16 13.59237 0 0 0 0 0
17 12.20608 0 0 0 0 0
18 15.09644 0 0 0 0 0
19 14.84012 0 0 0 0 0
20 10.59666 0 0 0 0 0
21 14.28551 0 0 0 0 0
22 14.99928 0 0 0 0 0
23 15.63034 0 0 0 0 0
24 16.51593 0 0 0 0 0
25 13.63319 0 0 0 0 0
26 12.89922 0 0 0 0 0
27 15.68731 0 0 0 0 0
28 14.34614 0 0 0 0 0
29 13.74652 0 0 0 0 0
30 15.20181 0 0 0 0 0
31 11.53274 0 0 0 0 0
32 16.81124 0 0 0 0 0
33 15.35596 0 0 0 0 0
34 16.81124 0 0 0 0 0
35 13.69898 0 0 0 0 0
36 15.60727 0 0 0 0 0
37 16.81124 0 0 0 0 0
38 14.50866 0 0 0 0 0
39 16.70588 0 0 0 0 0
40 15.67181 0 0 0 0 0
41 16.81124 0 0 0 0 0
42 16.11810 0 0 0 0 0
43 15.42495 0 0 0 0 0
44 16.01274 0 0 0 0 0
45 17.36334 0 0 0 0 0
46 15.20181 0 0 0 0 0
47 14.31629 0 0 0 0 0
48 15.31959 0 0 0 0 0
49 16.39573 0 0 0 0 0
50 16.11810 0 0 0 0 0
当我想对所有可能的组合应用测试时,我将mapply用作:
res_matrix <- mapply(myFunction,df[,which(names(df) %in% c(combinations$Var1))],df[,which(names(df) %in% c(combinations$Var2))])
但是它返回空结果:
> res_matrix
named list()
我该如何解决这个问题?
答案 0 :(得分:0)
这是一个命题,如果需要,请要求澄清/修改。我没有grangertest
,所以我输入了一个伪函数。
data <- data.frame(target=runif(11),
col1=0, col2=0, col3=0,
col4=0, col5=0)
grangertest <- function(x,y){ # fake test
length(x) * length(y)
}
outer(colnames(data), colnames(data), # pairwise operation
FUN=Vectorize(function(a,b) grangertest(x=data[,a], y=data[,b])))