Skip to contents

Append time series to each other. Resolve overlap determines which of two ts class time series is reaching further and arranges the two series into first and second series accordingly. Both time series are concatenated to one if both series had the same frequency. Typically this function is used concatenate two series that have a certain overlap, but one series clearly starts earlier while the other lasts longer. If one series starts earlier and stops later, all elements of the shorter series will be inserted into the larger series, i.e. elements of the smaller series will replace the elements of the longer series. Usually ts2 is kept.

Usage

resolve_ts_overlap(ts1, ts2, keep_ts2 = T, tolerance = 0.001)

Arguments

ts1

ts time series, typically the older series

ts2

ts time series, typically the younger series

keep_ts2

logical should ts2 be kept? Defaults to TRUE.

tolerance

numeric when comparing min and max values with a index vector of a time series R runs in to trouble with precision handling, thus a tolerance needs to be set. Typically this does not need to be adjusted. E.g. 2010 != 2010.000. With the help of the tolerance parameter these two are equal.

Examples

ts1 <- ts(rnorm(100), start = c(1990, 1), frequency = 4)
ts2 <- ts(1:18, start = c(2000, 1), frequency = 4)
resolve_ts_overlap(ts1, ts2)
#>             Qtr1        Qtr2        Qtr3        Qtr4
#> 1990  1.07499582 -0.56290995  0.47332312 -1.13058978
#> 1991 -0.13733236  0.08488305  0.25708364  0.86766986
#> 1992 -0.38971857 -0.29849332 -1.39490259 -1.86814621
#> 1993  2.10751916 -1.53210279  0.73222547 -0.19092268
#> 1994  1.25827781 -0.84690600  2.54992321 -0.26758671
#> 1995  0.06122782 -1.20684023  0.39584202 -0.01311066
#> 1996 -0.48042500 -0.72661184  0.21488446  3.15126199
#> 1997  0.22096008  0.48148591 -0.23079310 -0.37785992
#> 1998 -0.31281147 -1.55296866  1.37852550 -0.72345741
#> 1999  0.87811608 -0.86148197 -0.09795389 -0.33224813
#> 2000  1.00000000  2.00000000  3.00000000  4.00000000
#> 2001  5.00000000  6.00000000  7.00000000  8.00000000
#> 2002  9.00000000 10.00000000 11.00000000 12.00000000
#> 2003 13.00000000 14.00000000 15.00000000 16.00000000
#> 2004 17.00000000 18.00000000  2.21641754  0.12863304
#> 2005  0.35711592  1.52103064 -0.73402025 -0.99077254
#> 2006  0.31407530  1.12806931  0.90894625  1.51542445
#> 2007 -2.24889172 -0.52226441  1.20416093 -1.02365916
#> 2008  2.06949160 -0.04434057 -0.63362897 -0.33998993
#> 2009  0.15964475  0.96339472 -0.46080936  1.38096741
#> 2010 -0.19914854 -1.61168433  1.39064189 -2.28407090
#> 2011  1.64638428  1.07493732 -0.35441575  2.18454362
#> 2012  0.04432351 -0.63436366  0.64567639  0.60339269
#> 2013  0.30021773  0.42698292  0.01617907  0.63636970
#> 2014 -0.67644185  0.53087576 -0.84549742 -0.68165114

# automatical detection of correction sequence!
ts1 <- ts(rnorm(90), start = c(1990, 1), frequency = 4)
ts2 <- ts(1:60, start = c(2000, 1), frequency = 4)
resolve_ts_overlap(ts1, ts2)
#>             Qtr1        Qtr2        Qtr3        Qtr4
#> 1990  0.23715806 -0.36271823 -0.13964926  0.76096814
#> 1991 -0.37169021 -1.91417388  1.39588121  0.06797950
#> 1992  1.31606491 -2.59226016  0.69476329 -1.04401681
#> 1993 -0.13623151 -1.13112968  0.73484829  0.66166584
#> 1994 -1.09846253  0.50327294 -0.35065686 -0.30109697
#> 1995 -0.26623575 -1.29049713  0.03958001 -0.48245272
#> 1996 -0.66890939  1.13854216  0.19503770  0.39482715
#> 1997  0.19961277 -2.11065490  1.47803534 -1.27944778
#> 1998  0.18099566  0.86626534  0.61436684  1.10108159
#> 1999 -0.37106953  1.17084593  0.28372441 -0.12422234
#> 2000  1.00000000  2.00000000  3.00000000  4.00000000
#> 2001  5.00000000  6.00000000  7.00000000  8.00000000
#> 2002  9.00000000 10.00000000 11.00000000 12.00000000
#> 2003 13.00000000 14.00000000 15.00000000 16.00000000
#> 2004 17.00000000 18.00000000 19.00000000 20.00000000
#> 2005 21.00000000 22.00000000 23.00000000 24.00000000
#> 2006 25.00000000 26.00000000 27.00000000 28.00000000
#> 2007 29.00000000 30.00000000 31.00000000 32.00000000
#> 2008 33.00000000 34.00000000 35.00000000 36.00000000
#> 2009 37.00000000 38.00000000 39.00000000 40.00000000
#> 2010 41.00000000 42.00000000 43.00000000 44.00000000
#> 2011 45.00000000 46.00000000 47.00000000 48.00000000
#> 2012 49.00000000 50.00000000 51.00000000 52.00000000
#> 2013 53.00000000 54.00000000 55.00000000 56.00000000
#> 2014 57.00000000 58.00000000 59.00000000 60.00000000

# both series are of the same length use sequence of arguments.
ts1 <- ts(rnorm(100), start = c(1990, 1), frequency = 4)
ts2 <- ts(1:48, start = c(2003, 1), frequency = 4)
resolve_ts_overlap(ts1, ts2)
#>              Qtr1         Qtr2         Qtr3         Qtr4
#> 1990  0.448161982  1.080627129  0.710088195  1.052108208
#> 1991  0.969099141 -0.612065164 -0.388277237 -0.220564717
#> 1992  1.081428848 -0.247436991 -1.170782447  0.905195112
#> 1993  0.302591414 -0.572939736 -0.098992242 -2.307641112
#> 1994  0.838894994  1.327500612 -0.412948518  0.538962656
#> 1995  1.137273292 -0.006175048 -0.413406675 -1.244555744
#> 1996 -0.189813461 -0.232158812 -0.851538410  0.014622186
#> 1997  0.185678373  1.641547999  0.031051717  0.666718862
#> 1998  2.199621717  2.025922335 -2.058201368  1.429970731
#> 1999  0.344010785  0.358509203  0.038469268 -0.619369399
#> 2000  0.568921164 -1.140901758 -0.080893461 -0.676020061
#> 2001  1.079421091 -0.275567441 -1.337584230  1.440831422
#> 2002 -0.428264028 -0.917613468  1.883843707  0.307011283
#> 2003  1.000000000  2.000000000  3.000000000  4.000000000
#> 2004  5.000000000  6.000000000  7.000000000  8.000000000
#> 2005  9.000000000 10.000000000 11.000000000 12.000000000
#> 2006 13.000000000 14.000000000 15.000000000 16.000000000
#> 2007 17.000000000 18.000000000 19.000000000 20.000000000
#> 2008 21.000000000 22.000000000 23.000000000 24.000000000
#> 2009 25.000000000 26.000000000 27.000000000 28.000000000
#> 2010 29.000000000 30.000000000 31.000000000 32.000000000
#> 2011 33.000000000 34.000000000 35.000000000 36.000000000
#> 2012 37.000000000 38.000000000 39.000000000 40.000000000
#> 2013 41.000000000 42.000000000 43.000000000 44.000000000
#> 2014 45.000000000 46.000000000 47.000000000 48.000000000
ts1 <- ts(rnorm(101), start = c(1990, 1), frequency = 4)
ts2 <- ts(1:61, start = c(2000, 1), frequency = 4)
resolve_ts_overlap(ts1, ts2)
#>             Qtr1        Qtr2        Qtr3        Qtr4
#> 1990 -0.18129372 -1.32719748 -0.54565438  0.93310559
#> 1991  3.17553109 -0.06793149  0.09544166  0.26998457
#> 1992  0.99632482  0.74559793  0.59883125 -1.12278819
#> 1993 -0.44346396  0.96042299  0.82366842  0.10853195
#> 1994 -1.02132913  1.05567610  1.13204407  2.26413266
#> 1995 -1.66185419 -1.37590559  0.69176861  1.50343817
#> 1996  0.74105731  1.49445860 -0.06440401 -0.77839368
#> 1997  0.24260579 -0.51037225  0.76219346  1.73675137
#> 1998  0.50278433 -0.11788021 -0.24069547  0.82245862
#> 1999 -2.09134021 -0.46645726 -0.18637962 -0.11101719
#> 2000  1.00000000  2.00000000  3.00000000  4.00000000
#> 2001  5.00000000  6.00000000  7.00000000  8.00000000
#> 2002  9.00000000 10.00000000 11.00000000 12.00000000
#> 2003 13.00000000 14.00000000 15.00000000 16.00000000
#> 2004 17.00000000 18.00000000 19.00000000 20.00000000
#> 2005 21.00000000 22.00000000 23.00000000 24.00000000
#> 2006 25.00000000 26.00000000 27.00000000 28.00000000
#> 2007 29.00000000 30.00000000 31.00000000 32.00000000
#> 2008 33.00000000 34.00000000 35.00000000 36.00000000
#> 2009 37.00000000 38.00000000 39.00000000 40.00000000
#> 2010 41.00000000 42.00000000 43.00000000 44.00000000
#> 2011 45.00000000 46.00000000 47.00000000 48.00000000
#> 2012 49.00000000 50.00000000 51.00000000 52.00000000
#> 2013 53.00000000 54.00000000 55.00000000 56.00000000
#> 2014 57.00000000 58.00000000 59.00000000 60.00000000
#> 2015 61.00000000                                    
#' clearly dominatn ts2 series
ts1 <- ts(rnorm(50), start = c(1990, 1), frequency = 4)
ts2 <- ts(1:100, start = c(1990, 1), frequency = 4)
resolve_ts_overlap(ts1, ts2)
#>      Qtr1 Qtr2 Qtr3 Qtr4
#> 1990    1    2    3    4
#> 1991    5    6    7    8
#> 1992    9   10   11   12
#> 1993   13   14   15   16
#> 1994   17   18   19   20
#> 1995   21   22   23   24
#> 1996   25   26   27   28
#> 1997   29   30   31   32
#> 1998   33   34   35   36
#> 1999   37   38   39   40
#> 2000   41   42   43   44
#> 2001   45   46   47   48
#> 2002   49   50   51   52
#> 2003   53   54   55   56
#> 2004   57   58   59   60
#> 2005   61   62   63   64
#> 2006   65   66   67   68
#> 2007   69   70   71   72
#> 2008   73   74   75   76
#> 2009   77   78   79   80
#> 2010   81   82   83   84
#> 2011   85   86   87   88
#> 2012   89   90   91   92
#> 2013   93   94   95   96
#> 2014   97   98   99  100