Home > database >  Combined outputs from breakpoints?
Combined outputs from breakpoints?

Time:01-01

Let’s consider the eu_stocks dataset (available in R by default as EuStockMarkets).

     library(strucchange)
     eu_stocks <- EuStockMarkets
     hh=eu_stocks[,1]
     jj=breakpoints(hh~ 1)
     breakdates(jj)

I would like to do this for the all columns of eu_stocks and combined the result of breakdates(jj) under the name of the column.

output:

   DAX    SMI
 1993     1992    
 1995     1993
 1996     1995
 1997     1996
  NA      1997

CodePudding user response:

We may create a function

f1 <- function(x) {
       jj <- breakpoints(x ~ 1)
       return(breakdates(jj))
 }

Then loop over the columns and apply the function, make the length same by appending NA at the end for shorter lengths and convert to a matrix

lst1 <- lapply(seq_len(ncol(eu_stocks)), \(i) f1(eu_stocks[,i]) )
names(lst1) <- colnames(eu_stocks)
mx <- max(lengths(lst1))
data.frame(lapply(lst1, `length<-`, mx))
      DAX      SMI      CAC     FTSE
1 1993.592 1992.704 1993.565 1992.838
2 1995.377 1993.777 1994.685 1993.912
3 1996.450 1995.338 1996.319 1995.427
4 1997.523 1996.412 1997.500 1996.500
5       NA 1997.485       NA 1997.573
  •  Tags:  
  • r
  • Related