accessing elements within lists in purrr

I have wrote a function to conduct required unit root tests for all the variables. However ZA (ur.za) from "urca" package has additional value of breakpoint which I would like to access. I want to get the value of this break point for all the variables and printed to a tibble. I did something like this which gives me the test statistic of all the variables for all the tests. I want break point of ZA test also in this table.'
`

library(tidyverse)
library(urca)
library(data.table)
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last
#> The following object is masked from 'package:purrr':
#> 
#>     transpose


data<-data.table::data.table(
                                check.names = FALSE,
                            `CALENDAR YEAR` = c("1996Q2",
                                                "1996Q3","1996Q4","1997Q1",
                                                "1997Q2","1997Q3"),
                     `Remittance.Million $` = c(2641,2738,
                                                3020,4036,2989,3646),
             `GDP_mp (2011).Billion Rs...6` = c(7731.66414685866,7128.91661549548,
                                                8874.80258510475,9006.00426674344,
                                                7983.75766925485,7473.97783433195),
                  `Oil Price.US$ per Barel` = c(19.4766666666667,20.5433333333333,23.16,
                                                21.17,18.0533333333333,
                                                18.5233333333333),
                        `Exchange rate.US$` = c(34.744,
                                                35.6429666666666,35.7370333333333,
                                                35.8758333333333,
                                                35.8130666666666,36.0296666666666),
                         `REER.Trade based` = c(97.23,96.42,
                                                96.5133333333333,97.99,100.73,
                                                102.743333333333),
                                 NEER....10 = c(103.543333333333,100.61,100.9,
                                                102.883333333333,105.116666666666,
                                                107.023333333333),
                            `Libor.3-month` = c(5.51972409836066,5.59651461538462,
                                                5.53228796875,5.56570245901639,
                                                5.83054380952381,5.73185092307692),
                        `T-bill.14-90 days` = c(12.4385,
                                                9.163933333,8.209533333,
                                                7.595233333,5.335466667,6.168966667),
            `IR_differential.tbill - lobor` = c(6.91877590163934,3.56741871761538,
                                                2.67724536425,2.02953087398361,
                                                -0.495077142523809,0.437115743923077),
  `US GDP.Billions of Chained 2012 Dollars` = c(10998.322,
                                                11096.976,11212.205,11284.587,
                                                11472.137,11615.636),
               `US_unemployment rate....15` = c(5.5,
                                                5.26666666666667,5.33333333333333,
                                                5.23333333333333,5,
                                                4.86666666666667),
                          nasdaq_comp....16 = c(1187.32682539683,1140.71453125,1261.168125,
                                                1326.07508196721,1331.64578125,
                                                1594.9971875),
                           BSE_sensex....17 = c(3746.19409166666,3500.61706666666,
                                                3040.91729333333,3541.66354533333,
                                                3807.98063033333,4159.068092)
)

#function to calculate unit root
unit_root<-apply(data[,-1],2,function(x){
        return(
                list(
                        ur.df(x, type = "drift",selectlags = c("AIC")),
                        ur.df(x, type = "trend",selectlags = c("AIC")),
                        ur.df(x, type = "none",selectlags = c("AIC")),
                        summary(ur.pp(x,type = "Z-tau",model = "constant")),
                        summary(ur.pp(x,type = "Z-tau",model = "trend")),
                        ur.za(x,model = "both"),
                        ur.za(x,model = "trend"),
                        ur.za(x,model = "intercept")
                )
        )
}) %>% unlist()

test_stat_levels<-unit_root %>% map_dfr(.,~data.table("Test Statistic"=round(.x@teststat,3),model=.x@model,
                                                      test=.x@test.name),.id="Variable")

Created on 2020-08-19 by the reprex package (v0.3.0)
`

it would be helpful if you gave an example of this breakpoint stuff, maybe just for one variable

library(tidyverse)
library(urca)
library(data.table)
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last
#> The following object is masked from 'package:purrr':
#> 
#>     transpose


data<-data.table::data.table(
        check.names = FALSE,
        `CALENDAR YEAR` = c("1996Q2",
                            "1996Q3","1996Q4","1997Q1",
                            "1997Q2","1997Q3"),
        `Remittance.Million $` = c(2641,2738,
                                   3020,4036,2989,3646),
        `GDP_mp (2011).Billion Rs...6` = c(7731.66414685866,7128.91661549548,
                                           8874.80258510475,9006.00426674344,
                                           7983.75766925485,7473.97783433195),
        `Oil Price.US$ per Barel` = c(19.4766666666667,20.5433333333333,23.16,
                                      21.17,18.0533333333333,
                                      18.5233333333333),
        `Exchange rate.US$` = c(34.744,
                                35.6429666666666,35.7370333333333,
                                35.8758333333333,
                                35.8130666666666,36.0296666666666),
        `REER.Trade based` = c(97.23,96.42,
                               96.5133333333333,97.99,100.73,
                               102.743333333333),
        NEER....10 = c(103.543333333333,100.61,100.9,
                       102.883333333333,105.116666666666,
                       107.023333333333),
        `Libor.3-month` = c(5.51972409836066,5.59651461538462,
                            5.53228796875,5.56570245901639,
                            5.83054380952381,5.73185092307692),
        `T-bill.14-90 days` = c(12.4385,
                                9.163933333,8.209533333,
                                7.595233333,5.335466667,6.168966667),
        `IR_differential.tbill - lobor` = c(6.91877590163934,3.56741871761538,
                                            2.67724536425,2.02953087398361,
                                            -0.495077142523809,0.437115743923077),
        `US GDP.Billions of Chained 2012 Dollars` = c(10998.322,
                                                      11096.976,11212.205,11284.587,
                                                      11472.137,11615.636),
        `US_unemployment rate....15` = c(5.5,
                                         5.26666666666667,5.33333333333333,
                                         5.23333333333333,5,
                                         4.86666666666667),
        nasdaq_comp....16 = c(1187.32682539683,1140.71453125,1261.168125,
                              1326.07508196721,1331.64578125,
                              1594.9971875),
        BSE_sensex....17 = c(3746.19409166666,3500.61706666666,
                             3040.91729333333,3541.66354533333,
                             3807.98063033333,4159.068092)
)

#function to calculate unit root
unit_root<-apply(data[,-1],2,function(x){
        return(
                list(
                        ur.df(x, type = "drift",selectlags = c("AIC")),
                        ur.df(x, type = "trend",selectlags = c("AIC")),
                        ur.df(x, type = "none",selectlags = c("AIC")),
                        summary(ur.pp(x,type = "Z-tau",model = "constant")),
                        summary(ur.pp(x,type = "Z-tau",model = "trend")),
                        ur.za(x,model = "both"),
                        ur.za(x,model = "trend"),
                        ur.za(x,model = "intercept")
                )
        )
}) %>% unlist()

test_stat_levels<-unit_root %>% map_dfr(.,~data.table("Test Statistic"=round(.x@teststat,3),model=.x@model,
                                                      test=.x@test.name),.id="Variable")
k<-ur.za(data[,2],model = "both")
#> Error in ur.za(data[, 2], model = "both"): 
#> Insufficient number of obeservations.
k@bpoint    # gives the break point , but this is just the number not the date
#> Error in eval(expr, envir, enclos): object 'k' not found
data$`CALENDAR YEAR`[k@bpoint] # to get the corresponding break date
#> Error in eval(expr, envir, enclos): object 'k' not found

Created on 2020-08-19 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.