Merging time series MAN IP U L ATIN G TIME SE R IE S DATA W ITH XTS AN D ZOO IN R Je � re y R y an Creator of x ts and q u antmod
Introd u cing merge () Combine series b y col u mn cbind() and merge() Database st y le joins on inde x ( i . e . b y time ) Inner , o u ter , le � and right joins merge(..., fill = NA, join = "outer") Fill arg u ment handles missingness MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R
# Default join = “outer” merge(x, y, join = "right", merge(x, y) fill = na.locf) x y x y 2016-08-09 1 2 2016-08-09 1 2 2016-08-10 1 2 2016-08-10 1 2 2016-08-11 1 NA 2016-08-12 1 2 2016-08-12 NA 2 merge(x, y, join = "inner") x y 2016-08-09 1 2 2016-08-10 1 2 MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R
merge(x, c(2, 3, 4)) merge(x, 3) x c.2..3..4. x X3 2016-08-09 1 2 2016-08-09 1 3 2016-08-10 1 3 2016-08-10 1 3 2016-08-11 1 4 2016-08-11 1 3 merge(x, as.Date(c("2016-08-14"))) x 2016-08-09 1 2016-08-10 1 2016-08-11 1 2016-08-14 NA MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R
Introd u cing rbind () Combine series b y ro w Ro w s are inserted in time order All ro w s in rbind() m u st ha v e a time The n u mber of col u mns m u st match MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R
rbind () e x ample rbind(x, y) rbind(x, as.integer(y)) x Error in try.xts(c(2L, 2L, 2L)): 2016-08-09 1 Error in as.xts.integer( 2016-08-09 2 x, ..., .RECLASS = TRUE) : 2016-08-10 1 order.by must be either 2016-08-10 2 'names()' or otherwise 2016-08-11 1 specified 2016-08-12 2 MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R
Let ' s practice ! MAN IP U L ATIN G TIME SE R IE S DATA W ITH XTS AN D ZOO IN R
Handling missingness MAN IP U L ATIN G TIME SE R IE S DATA W ITH XTS AN D ZOO IN R Je � re y R y an Creator of x ts and q u antmod
Fill NAs w ith last obser v ation l . o . c . f . means “ last obser v ation carried for w ard ” na.locf(object, na.rm = TRUE, fromLast = FALSE, maxgap = Inf) cbind(z, na.locf(z), na.locf(z, fromLast = TRUE)) z z.1 z.2 2016-08-09 1 1 1 2016-08-10 NA 1 4 2016-08-11 NA 1 4 2016-08-12 4 4 4 MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R
Other NA options Replace NAs na.fill(object, fill, ...) Remo v e NAs na.trim(object, ...) na.omit(object, ...) Interpolate NAs na.approx(object, ...) MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R
NA replace and remo v e na.fill(z, fill = -999) na.trim(z) z z Aug 09, 2016 1 Aug 09, 2016 1 Aug 10, 2016 -999 Aug 10, 2016 NA Aug 11, 2016 -999 Aug 11, 2016 NA Aug 12, 2016 4 Aug 12, 2016 4 Aug 13, 2016 -999 na.omit(z) z Aug 09, 2016 1 Aug 12, 2016 4 MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R
NA interpolation na.approx() u ses inde x x spacing to linearl y x appro x imate the missing Aug 09, 2016 1 v al u es Aug 11, 2016 NA Aug 12, 2016 4 na.approx(x) z Aug 09, 2016 1 Aug 11, 2016 3 Aug 12, 2016 4 MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R
Let ' s practice ! MAN IP U L ATIN G TIME SE R IE S DATA W ITH XTS AN D ZOO IN R
Lags and differences MAN IP U L ATIN G TIME SE R IE S DATA W ITH XTS AN D ZOO IN R Je � re y R y an Creator of x ts and q u antmod
Seasonalit y and stationarit y Seasonalit y is a repeating pa � ern Stationarit y refers to some bo u nd of the series These pa � erns are o � en compared Ho w get aro u nd misalignment of the series ? MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R
Lagging a time series Used to align time series for comparisons lag() w ill shi � obser v ations in time lag(x, k = 1, na.pad = TRUE, ...) k controls n u mber of lags na.pad controls NA introd u ction With x ts , positi v e k shi � s v al u es for w ard MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R
Differencing series Con v ert le v els to changes ( i . e . deltas ) diff(x,lag = 1, differences = 1, arithmetic = TRUE, log = FALSE, na.pad = TRUE, ...) lag controls w hich obser v ations arithmetic v s . log calc u lations MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R
Let ' s practice ! MAN IP U L ATIN G TIME SE R IE S DATA W ITH XTS AN D ZOO IN R
Recommend
More recommend