0

I am trying to capture certain parts of the output from the ur.ls function I got from the GitHub.

The code that I am using is:

my_test <- ur.ls(y=mydf$x, model = "break", breaks = 2, lags = maxlag, method = "GTOS",pn = 0.1, print.results = "print" )
print(my_test)

Which gives the following output:

[[1]]
[[1]]$`t-stat`
[1] -5.529869

[[1]]$`First break`
[1] 35

[[1]]$`Second break`
[1] 55

[[1]]$Runtime
Time difference of 0.07702468 mins


[[2]]

Call:
lm(formula = y.diff ~ 0 + lagmatrix(S.tilde, 1)[, -1] + datmat[, 
    2:(slag + 2)] + DTt.diff)

Residuals:
     Min       1Q   Median       3Q      Max 
-15.1301  -1.7312   0.0684   1.8045  10.3472 

Coefficients: (1 not defined because of singularities)
                                       Estimate Std. Error t value Pr(>|t|)    
lagmatrix(S.tilde, 1)[, -1]            -0.23512    0.04252  -5.530 5.71e-07 ***
datmat[, 2:(slag + 2)]NoLags                 NA         NA      NA       NA    
datmat[, 2:(slag + 2)]S.tilde.diff.l1   0.21258    0.09923   2.142 0.035797 *  
datmat[, 2:(slag + 2)]S.tilde.diff.l2   0.29349    0.10449   2.809 0.006509 ** 
datmat[, 2:(slag + 2)]S.tilde.diff.l3   0.45070    0.11026   4.088 0.000119 ***
datmat[, 2:(slag + 2)]S.tilde.diff.l4   0.21321    0.13451   1.585 0.117662    
datmat[, 2:(slag + 2)]S.tilde.diff.l5  -0.10950    0.11104  -0.986 0.327638    
datmat[, 2:(slag + 2)]S.tilde.diff.l6  -0.26316    0.11047  -2.382 0.020050 *  
DTt.diff1                              -3.48648    1.16726  -2.987 0.003934 ** 
DTt.diff2                             -11.06934    5.14490  -2.152 0.035044 *  
DTt.diff3                              11.16147    2.60165   4.290 5.87e-05 ***
DTt.diff4                               9.94658    5.91986   1.680 0.097575 .  
DTt.diff5                             -11.26274    2.10680  -5.346 1.17e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 4.34 on 67 degrees of freedom
  (7 observations deleted due to missingness)
Multiple R-squared:  0.6446,    Adjusted R-squared:  0.5809 
F-statistic: 10.13 on 12 and 67 DF,  p-value: 5.789e-11

I am trying to capture the following information from the output:

  1. t-statistics,
  2. the break positions
  3. estimate for lagmatrix(S.tilde, 1)[, -1]
  4. number of lags
  5. number of breaks

Of course, this could be done manually using:

tstat<- c(-5.529869)
break1<-c(35)
break2<-c(55)
estimate<-c(-0.23512)
nlag<- c(6)
nbreak<- c(2)

But an issue is that I need to do several scores of these tests, so this is not very practical.

I first tried to capture the coefficients with tidy using broom package which usually works pretty well, but in this case tidy package simply does not recognize the output. That is when I try to apply it I get:

tidy(my_test)
Error: No tidy method recognized for this list.

After that I managed to find an approach that sort of works but only for capturing the t-statitics:

tstat<- c(as.numeric(substring((capture.output(print(my_test))[3]),5)))

In principle I could apply this to other things I need to capture but problem is that everything in the printed output moves up or down depending on whether I use 1 or 2 breaks and in addition number of lags keeps changing. The only two things that do not change their place are t-stat at the beginning and first break. Position of everything else changes when there is switch from two to one break, or depending on the number of lags.

Consequently, I am looking for some way how the output from the above test could be dynamically captured even when the positions are changing. It does not necessarily need to be done by capture.output function - that was just only thing that I managed to make work.


dput for my_test:

list(list(`t-stat` = -5.52986939989444, `First break` = 35, `Second break` = 55, 
    Runtime = structure(0.243574384848277, class = "difftime", units = "mins")), 
    structure(list(call = lm(formula = y.diff ~ 0 + lagmatrix(S.tilde, 
        1)[, -1] + datmat[, 2:(slag + 2)] + DTt.diff), terms = y.diff ~ 
        0 + lagmatrix(S.tilde, 1)[, -1] + datmat[, 2:(slag + 
            2)] + DTt.diff, residuals = c(`8` = 0.872355573916085, 
    `9` = -0.697717191898609, `10` = -1.2862848943206, `11` = 0.320933377794575, 
    `12` = -2.40645214393101, `13` = 2.39038766132493, `14` = 1.153249843184, 
    `15` = -0.39800522090757, `16` = -0.217625238730097, `17` = -2.24998945123775, 
    `18` = -1.86235465631137, `19` = 1.52748936990644, `20` = 0.722105553225127, 
    `21` = 1.6896236096191, `22` = 1.21756654083507, `23` = -2.2737081032416, 
    `24` = -0.0555189856184707, `25` = -1.32745447769992, `26` = -2.69191851601456, 
    `27` = 0.450323241676394, `28` = -0.619946732648159, `29` = 0.0684171523027832, 
    `30` = 1.36501867693609, `31` = 7.01636358940376, `32` = 7.45606136222332, 
    `33` = -0.0603699574229863, `34` = -7.06134338379436, `35` = -3.04120659857061, 
    `36` = 1.87843876200867e-16, `37` = -1.56067141954618, `38` = -3.98995837788732, 
    `39` = -2.64034463279261, `40` = 2.46318877060907, `41` = -0.0946579433877879, 
    `42` = -0.00627968340149751, `43` = -1.12994022351662, `44` = 3.87981944634356, 
    `45` = 3.3557912879781, `46` = 9.63244802758174, `47` = 5.24100528509744, 
    `48` = -1.60009293145095, `49` = -5.2376819302277, `50` = -9.38754075959704, 
    `51` = 10.3471769906691, `52` = -12.1568213639348, `53` = 0.85133551132459, 
    `54` = 2.24952813266479, `55` = -0.216304186525884, `56` = -3.97245045306514e-16, 
    `57` = 2.71150736214547, `58` = -3.66810792780268, `59` = 0.358219488791779, 
    `60` = 1.56109333234247, `61` = -0.753216017616136, `62` = -15.1300849652179, 
    `63` = 4.61095601773964, `64` = 1.91940902601647, `65` = -3.09550436133278, 
    `66` = 2.49107250967601, `67` = 3.82885377877594, `68` = 0.787299241035634, 
    `69` = -5.65977223850224, `70` = -4.46105368830907, `71` = -2.00896053907376, 
    `72` = -0.983959541709144, `73` = 0.385499753844272, `74` = 1.38470803882371, 
    `75` = 0.341275340812954, `76` = 0.228639263368348, `77` = -4.17228901167752, 
    `78` = 1.63431085026858, `79` = 5.33398073977219, `80` = 4.03952947226404, 
    `81` = 3.63845224619818, `82` = 0.567342464096198, `83` = 3.13210340972586, 
    `84` = -0.906695505369889, `85` = 4.99623519164391, `86` = -3.1108437307305
    ), coefficients = structure(c(-0.23511569792016, 0.21258442578596, 
    0.293489227268745, 0.450696031637051, 0.213211609782706, 
    -0.109499991050521, -0.263162801329296, -3.48647692130263, 
    -11.0693444630996, 11.1614685510626, 9.94657879595707, -11.2627444589128, 
    0.0425174051894693, 0.0992264489202736, 0.104487280919139, 
    0.110259125401975, 0.134513214269294, 0.111044998102637, 
    0.110466070195529, 1.16726401701977, 5.1449023295093, 2.60164949654566, 
    5.91986223733132, 2.10680207458884, -5.52986939989444, 2.14241694728758, 
    2.80885122750846, 4.08760753356181, 1.5850607015892, -0.986086657854799, 
    -2.38229531351562, -2.98687946382877, -2.15151692960425, 
    4.29015075469706, 1.68020443672368, -5.34589584601145, 5.7066830928081e-07, 
    0.0357972680393423, 0.00650896518048846, 0.000118991853434715, 
    0.117661531503541, 0.327638306451416, 0.020050387349118, 
    0.00393444251065558, 0.0350440626131117, 5.87177198313913e-05, 
    0.0975747763082409, 1.16724779705709e-06), .Dim = c(12L, 
    4L), .Dimnames = list(c("lagmatrix(S.tilde, 1)[, -1]", "datmat[, 2:(slag + 2)]S.tilde.diff.l1", 
    "datmat[, 2:(slag + 2)]S.tilde.diff.l2", "datmat[, 2:(slag + 2)]S.tilde.diff.l3", 
    "datmat[, 2:(slag + 2)]S.tilde.diff.l4", "datmat[, 2:(slag + 2)]S.tilde.diff.l5", 
    "datmat[, 2:(slag + 2)]S.tilde.diff.l6", "DTt.diff1", "DTt.diff2", 
    "DTt.diff3", "DTt.diff4", "DTt.diff5"), c("Estimate", "Std. Error", 
    "t value", "Pr(>|t|)"))), aliased = c(`lagmatrix(S.tilde, 1)[, -1]` = FALSE, 
    `datmat[, 2:(slag + 2)]NoLags` = TRUE, `datmat[, 2:(slag + 2)]S.tilde.diff.l1` = FALSE, 
    `datmat[, 2:(slag + 2)]S.tilde.diff.l2` = FALSE, `datmat[, 2:(slag + 2)]S.tilde.diff.l3` = FALSE, 
    `datmat[, 2:(slag + 2)]S.tilde.diff.l4` = FALSE, `datmat[, 2:(slag + 2)]S.tilde.diff.l5` = FALSE, 
    `datmat[, 2:(slag + 2)]S.tilde.diff.l6` = FALSE, DTt.diff1 = FALSE, 
    DTt.diff2 = FALSE, DTt.diff3 = FALSE, DTt.diff4 = FALSE, 
    DTt.diff5 = FALSE), sigma = 4.34022853989968, df = c(12L, 
    67L, 13L), r.squared = 0.644573890970572, adj.r.squared = 0.580915483383212, 
        fstatistic = c(value = 10.1255107596904, numdf = 12, 
        dendf = 67), cov.unscaled = structure(c(9.59639922665142e-05, 
        2.20580058347817e-05, -1.86765939916722e-05, -6.97933563296721e-05, 
        -9.18368279866152e-05, -2.74027448794235e-05, -1.08445650048574e-06, 
        0.00166049760804861, 0.00505585030558691, -0.00497849874276068, 
        -0.00336957248783249, 0.00332058543538181, 2.20580058347817e-05, 
        0.000522672561463745, -0.000170134234480263, -0.000136299258433012, 
        -0.000156683869192449, 3.20741691986199e-05, 0.000109417994668808, 
        -0.000340545121006471, 0.000835502882418284, -0.00034262796834208, 
        -0.00311928203169194, 0.00137747915213809, -1.86765939916722e-05, 
        -0.000170134234480263, 0.000579564343400617, -0.000118662022760285, 
        -0.000113907761908382, -7.56369903448711e-05, -1.67418563277836e-05, 
        -0.000673822989020871, 0.00199348470539279, 0.00202854373119897, 
        -0.00443981226091342, -0.00134400501096897, -6.97933563296721e-05, 
        -0.000136299258433012, -0.000118662022760285, 0.000645362742765626, 
        -5.98565469825177e-05, -3.8229997405084e-05, -0.000146569197018866, 
        -0.00144027001250464, -0.00434693403705934, 0.004657498011182, 
        0.00200435070362651, -0.00352774302397917, -9.18368279866152e-05, 
        -0.000156683869192449, -0.000113907761908382, -5.98565469825177e-05, 
        0.000960516222555618, -0.000232668650784361, 2.61183442453487e-05, 
        -0.00216702563816879, -0.010485401200229, 0.00424329320154643, 
        0.0246863500668702, -0.00150724670465891, -2.74027448794235e-05, 
        3.20741691986199e-05, -7.56369903448711e-05, -3.8229997405084e-05, 
        -0.000232668650784361, 0.000654595183149194, -0.000227073038814052, 
        -0.00050839722994379, -0.00414193034888211, 0.0012164182953312, 
        -0.0101302132850535, -0.000174392386630977, -1.08445650048574e-06, 
        0.000109417994668808, -1.67418563277836e-05, -0.000146569197018866, 
        2.61183442453487e-05, -0.000227073038814052, 0.000647787572328266, 
        -0.000832648885600449, 0.00184942667154017, -0.00111837128659685, 
        0.0101221919879007, 0.00341776474899047, 0.00166049760804861, 
        -0.000340545121006471, -0.000673822989020871, -0.00144027001250464, 
        -0.00216702563816879, -0.00050839722994379, -0.000832648885600449, 
        0.0723290896245352, 0.099047414012332, -0.126717608042948, 
        -0.0879683442319499, 0.0467983613397516, 0.00505585030558691, 
        0.000835502882418284, 0.00199348470539279, -0.00434693403705934, 
        -0.010485401200229, -0.00414193034888211, 0.00184942667154017, 
        0.099047414012332, 1.40517065730678, -0.317384545601921, 
        -0.294134124076671, 0.205736990719251, -0.00497849874276068, 
        -0.00034262796834208, 0.00202854373119897, 0.004657498011182, 
        0.00424329320154643, 0.0012164182953312, -0.00111837128659685, 
        -0.126717608042948, -0.317384545601921, 0.359312541483163, 
        0.15130744081174, -0.232250025264234, -0.00336957248783249, 
        -0.00311928203169194, -0.00443981226091342, 0.00200435070362651, 
        0.0246863500668702, -0.0101302132850535, 0.0101221919879007, 
        -0.0879683442319499, -0.294134124076671, 0.15130744081174, 
        1.86036432914864, -0.0614605342855884, 0.00332058543538181, 
        0.00137747915213809, -0.00134400501096897, -0.00352774302397917, 
        -0.00150724670465891, -0.000174392386630977, 0.00341776474899047, 
        0.0467983613397516, 0.205736990719251, -0.232250025264234, 
        -0.0614605342855884, 0.235625493888645), .Dim = c(12L, 
        12L), .Dimnames = list(c("lagmatrix(S.tilde, 1)[, -1]", 
        "datmat[, 2:(slag + 2)]S.tilde.diff.l1", "datmat[, 2:(slag + 2)]S.tilde.diff.l2", 
        "datmat[, 2:(slag + 2)]S.tilde.diff.l3", "datmat[, 2:(slag + 2)]S.tilde.diff.l4", 
        "datmat[, 2:(slag + 2)]S.tilde.diff.l5", "datmat[, 2:(slag + 2)]S.tilde.diff.l6", 
        "DTt.diff1", "DTt.diff2", "DTt.diff3", "DTt.diff4", "DTt.diff5"
        ), c("lagmatrix(S.tilde, 1)[, -1]", "datmat[, 2:(slag + 2)]S.tilde.diff.l1", 
        "datmat[, 2:(slag + 2)]S.tilde.diff.l2", "datmat[, 2:(slag + 2)]S.tilde.diff.l3", 
        "datmat[, 2:(slag + 2)]S.tilde.diff.l4", "datmat[, 2:(slag + 2)]S.tilde.diff.l5", 
        "datmat[, 2:(slag + 2)]S.tilde.diff.l6", "DTt.diff1", 
        "DTt.diff2", "DTt.diff3", "DTt.diff4", "DTt.diff5"))), 
        na.action = structure(1:7, .Names = c("1", "2", "3", 
        "4", "5", "6", "7"), class = "omit")), class = "summary.lm"))
1muflon1
  • 209
  • 1
  • 7
  • Eww. Ugly. Learn to post library calls at a minimum when asking about obscure functions that are unlikely to be in the workspace of the average responder to SO. You are seeing the output of the print method for objects of whatever class is returned by `ur.ls`. You need to look at the actual structure and perhaps also the structure of what might be returned if there is a `summary method for that class. – IRTFM Oct 31 '20 at 01:00
  • @IRTFM sorry for that, yes it is very ugly problem. Could you please give me some reference for how to do proper library call. I usually dont deal with these obscure functions so I am not sure what the proper procedure is. – 1muflon1 Oct 31 '20 at 01:06
  • After looking at that GitHub page I've come to the conclusion that there is no package wrapper. and therefore probably no summary method. You will need to look at the structure of the object and go from there. That's a pretty basic skill so if it's beyond you then you should be studying tutorials. – IRTFM Oct 31 '20 at 01:27
  • 1
    What about ```my_test[[1L]][['t-stat']]```? – Cole Oct 31 '20 at 01:29
  • @Cole that works perfectly and it is more elegant solution for the t-stat than my original solution, but how can I apply it to the other things I am looking for? – 1muflon1 Oct 31 '20 at 01:41
  • You will need to subset ```my_test``` in similar ways. Note, ```nlag``` and ```nbreak``` seem to be already defined by what you passed to ```ur.ls```. I guess if you ```dput(my_test)``` I can come up with an answer. – Cole Oct 31 '20 at 01:45
  • @Cole the number of breaks is already defined so that is not problem in principle although I hoped there is some solution that could just check how it is defined based on output. But the lags actually are determined by the function I only specify max lag. Also, the solution that worked for t-stat also worked for first and second break I am just having trouble making it work for the rest. Also put dput as requested – 1muflon1 Oct 31 '20 at 01:54

1 Answers1

2

This is mainly a subsetting problem.

tstat = my_test[[1L]][['t-stat']]

## breaks ##
break_names = endsWith(names(my_test[[1L]]), 'break')
nbreak = sum(break_names)
for (i in seq_len(nbreak)) { ## note, I would assign breaks to a list: breaks = my_test[[1L]][break_names]
  assign(paste0('break', i), my_test[[1L]][[i + 1L]])
}

estimate = my_test[[2L]][['coefficients']][1L, 1L]

txt = row.names(my_test[[2L]][['coefficients']])
nlag = max(as.integer(regmatches(txt, regexpr("\\d*$", txt))), na.rm = TRUE)

The only uncertain part is the nlag. It is fragile right now as I am unsure of the rules that it would follow. But for this dataset, it matches OP request.

Cole
  • 11,130
  • 1
  • 9
  • 24
  • Thanks, by the way why do we have to use 2L with coefficients when with t-stat we can get the information with 1L (apologies in advance if this is basic question, I just try to understand what is being done here), I know that in R, L suffix denotes integer data type but what does changing 1L to 2L does/why is it necessary? – 1muflon1 Oct 31 '20 at 18:02
  • 1
    It is a list object. We need to subset the second element of the list to get the coefficient information. For simpler data.frames, see ```iris[[1]]``` vs ```iris[[2]``` which extracts vectors. – Cole Oct 31 '20 at 23:41