[R] Extracting slots from an object (e.g.: object produced by unit root test function "urdfTest")

David Winsemius dwinsemius at comcast.net
Fri Mar 13 05:52:24 CET 2015


On Mar 12, 2015, at 7:49 PM, jpm miao wrote:

> Thank you very much. 
> 
> Could we extract the p-value in the output of the ur.df function? Does there exist any unit root test function where the p-value can be extracted? Thanks!

If this were an S3 function, it would be a fairly simple operation to hack that code so that it actually delivered what was promised. Then you could use the code you originally attempted. It's not a very large function. In fact I see no reason why you couldn't extract the code you needed and make an S3 function. Or even just use `summary` on the first argument and pull the p-value from the returned object. Many possibilities.

-- 
David.


> 
> An example for ur.df function:
> 
> data(Raotbl3)
> attach(Raotbl3)
> lc.df <- ur.df(y=lc, lags=3, type='trend')
> summary(lc.df)
> 
> 2015-03-13 1:23 GMT+08:00 David Winsemius <dwinsemius at comcast.net>:
> 
> On Mar 12, 2015, at 1:04 AM, jpm miao wrote:
> 
> > Hi,
> >
> >   I run a statistical test function in the package "fUnitRoots" that
> > returns a S4 object but I am wondering how to extract the p-value, one of
> > the output elements.
> 
> > The document of the function "urdfTest":
> > .....
> > All tests return an object of class "fHTEST" with the following slots:
> >
> > @call
> > .....
> > @test
> > a list object which holds the output of the underlying test function.
> > @title
> > .....
> > The entries of the @test slot include the following components:
> >
> > $statistic
> > ......
> > $p.value
> > the p-value of the test.
> > .....
> > (end)
> >
> >   I store the result of the test test in an element of a list
> > (adf1["r3m"][[1]]), and I want to extract the p-value. I was expecting the
> > p-value via adf1["r3m"][[1]]@test$p.value but it gives only an error
> > message. Could someone tell me how to extract the p-value? Thanks!!!
> 
> If you look at the code by typing: fUnitRoots::urdfTest,  you should quickly see why you are seeing text output:
> 
> That function is doing the equivalent of a console screenscrape:
> 
> ...
> output = capture.output(summary(urca))[-(1:4)]
> ...
> # and then assigns a trimmed version of that result to the `test`- slot.
> 
> 
> So the documentation is misleading in suggesting that a list object is being returned in the `test` slot. It's just a character vestor.
> 
> 
> --
> >
> >> adf1["r3m"][[1]]<-urdfTest(dat[,i], lags = 1, type = "ct")
> >> adf1["r3m"][[1]]
> > [1] "  "
> > [2] "  Test regression trend "
> > [3] "  "
> > [4] "  Call:"
> > [5] "  lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)"
> > [6] "  "
> > [7] "  Residuals:"
> > [8] "      Min      1Q  Median      3Q     Max "
> > [9] "  -3.0785 -0.0485  0.0072  0.0627  3.5672 "
> > [10] "  "
> > [11] "  Coefficients:"
> > [12] "                Estimate Std. Error t value Pr(>|t|)    "
> > [13] "  (Intercept) -1.733e-02  9.529e-03  -1.818  0.06910 .  "
> > [14] "  z.lag.1     -7.060e-03  2.343e-03  -3.013  0.00261 ** "
> > [15] "  tt           5.299e-06  4.927e-06   1.076  0.28221    "
> > [16] "  z.diff.lag  -1.035e-01  1.859e-02  -5.569 2.81e-08 ***"
> > [17] "  ---"
> > [18] "  Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1"
> > [19] "  "
> > [20] "  Residual standard error: 0.2107 on 2864 degrees of freedom"
> > [21] "  Multiple R-squared:  0.01461,\tAdjusted R-squared:  0.01358 "
> > [22] "  F-statistic: 14.15 on 3 and 2864 DF,  p-value: 3.716e-09"
> > [23] "  "
> > [24] "  "
> > [25] "  Value of test-statistic is: -3.0134 3.0697 4.5828 "
> > [26] "  "
> > [27] "  Critical values for test statistics: "
> > [28] "        1pct  5pct 10pct"
> > [29] "  tau3 -3.96 -3.41 -3.12"
> > [30] "  phi2  6.09  4.68  4.03"
> > [31] "  phi3  8.27  6.25  5.34"
> >
> 
> That is being displayed as a multi-element text object. You instead need to show the code that actually created that object.
> 
> >> adf1["r3m"][[1]]@test
> > Error: trying to get slot "test" from an object of a basic class
> > ("character") with no slots
> >> adf1["r3m"][[1]]@test$p-value
> > Error: trying to get slot "test" from an object of a basic class
> > ("character") with no slots
> >
> >       [[alternative HTML version deleted]]
> 
> Please learn to post in palin text.
> 
> --
> 
> David Winsemius
> Alameda, CA, USA
> 
> 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list