简体   繁体   中英

Getting slot from an S4 object

I would like to get all p.values that are under the 5% level.

I tried to use the tseries package to implement the kpss test.

> 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 

However, I cannot access the p.value object. I tried str(resKpss[1]) and got:

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"

I also tried the $ operator, but I get an error that this is an S4 method.

Any suggestions what I am doing wrong?

There's no S4 object here to get a slot from.

resKpss is a ordinary R list because its the output from lapply . To get an element of a list you use double-square brackets: resKpss[[1]] and resKpss[[2]] for example.

Each of those is then a list. To get a component of a list by name you use dollar signs - the at-sign is used to get parts of an S4 class. So:

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

Your sapply just needs a dollar sign instead of an at-sign:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM