从S4对象获取插槽

时间:2015-07-11 08:54:27

标签: r statistics

我希望得到所有低于5%的p.values。

我尝试使用tseries包来实现kpss测试。

> dput(datSel)
structure(list(oenb_dependent = c(142.8163942, 143.5711365, 145.3485827, 
142.0577145, 139.4326176, 140.1236581, 138.6560282, 136.405036, 
133.9337229, 133.8785538, 132.0608441, 130.0866307, 120.1320237, 
119.6368882, 114.3312943, 117.5084111, 114.4960017, 112.9124518, 
112.8185478, 112.3047916, 106.632639, 106.2107158, 106.8455028, 
106.3879556, 104.3451786, 102.9085952, 101.0967783, 101.7858278, 
101.0749044, 102.6441976, 102.0666152, 100, 97.14084104, 97.49972913, 
96.91453836, 96.05132443, 94.98057971, 92.78373451, 92.67526281, 
91.82430571, 91.4153859, 89.51740671, 89.01587176, 84.62259911, 
91.48598494, 89.12053042, 90.02364352, 90.92496121, 89.42963565, 
91.93886583, 88.83918306, 90.39513509, 87.54571761, 91.3386451, 
87.7836994, 91.79178376, 87.56903138, 87.77875755, 89.29938784, 
90.88084014), gdp = c(17703.7, 17599.8, 17328.2, 17044, 17078.3, 
16872.3, 16619.2, 16502.4, 16332.5, 16268.9, 16094.7, 15956.5, 
15785.3, 15587.1, 15460.9, 15238.4, 15230.2, 15057.7, 14888.6, 
14681.1, 14566.5, 14384.1, 14340.4, 14383.9, 14549.9, 14843, 
14813, 14668.4, 14685.3, 14569.7, 14422.3, 14233.2, 14066.4, 
13908.5, 13799.8, 13648.9, 13381.6, 13205.4, 12974.1, 12813.7, 
12562.2, 12367.7, 12181.4, 11988.4, 11816.8, 11625.1, 11370.7, 
11230.1, 11103.8, 11037.1, 10934.8, 10834.4, 10701.3, 10639.5, 
10638.4, 10508.1, 10472.3, 10357.4, 10278.3, 10031), employ = c(71.0619, 
70.9383, 71.162, 71.138, 71.2286, 71.5095, 71.565, 71.3246, 71.4963, 
71.3738, 71.4276, 71.3065, 71.0246, 71.3244, 71.0619, 70.9811, 
71.2149, 70.8342, 70.5568, 70.5444, 70.3286, 70.179, 70.2555, 
70.5103, 70.8038, 70.6748, 70.9769, 70.6988, 70.2125, 70.1661, 
69.6284, 69.5613, 68.9837, 68.8606, 68.4223, 67.963, 67.6293, 
67.5905, 67.1857, 67.1248, 66.7075, 66.5857, 66.4303, 66.2826, 
68.7514, 68.8897, 69.0824, 68.9718, 68.7927, 68.6387, 68.8053, 
68.7286, 68.4141, 68.2357, 68.4785, 68.4171, 68.4782, 68.3978, 
68.5344, 68.4772)), .Names = c("oenb_dependent", "gdp", "employ"
), row.names = c(NA, -60L), class = "data.frame")
> resKpss <- lapply(datSel,function(x){ kpss.test(x,"Trend") })
> cv.kpss <- sapply(resKpss, function(x){ x@p.value < 0.05 } )
Error in FUN(X[[1L]], ...) : 
  trying to get slot "p.value" from an object (class "htest") that is not an S4 object 

但是,我无法访问p.value对象。我试过str(resKpss[1])并得到了:

List of 1
 $ variName1:List of 5
  ..$ statistic: Named num 0.693
  .. ..- attr(*, "names")= chr "KPSS Trend"
  ..$ parameter: Named num 1
  .. ..- attr(*, "names")= chr "Truncation lag parameter"
  ..$ p.value  : num 0.01
  ..$ method   : chr "KPSS Test for Trend Stationarity"
  ..$ data.name: chr "x"
  ..- attr(*, "class")= chr "htest"

我也尝试了$运算符,但是我收到一个错误,即这是S4方法。

有什么建议我做错了吗?

1 个答案:

答案 0 :(得分:2)

这里没有S4对象来获取插槽。

resKpss是普通的R列表,因为它是lapply的输出。要获取列表元素,请使用双方括号:例如resKpss[[1]]resKpss[[2]]

然后每个都是一个列表。要按名称获取列表的组件,请使用美元符号 - 符号用于获取S4类的一部分。所以:

resKpss[[1]]$p.value
[1] 0.01

您的sapply只需要一个美元符号而不是一个符号:

> sapply(resKpss, function(x){ x$p.value < 0.05 } )
oenb_dependent            gdp         employ 
          TRUE           TRUE           TRUE