Merging time series MAN IP U L ATIN G TIME SE R IE S DATA W ITH - - PowerPoint PPT Presentation

merging time series
SMART_READER_LITE
LIVE PREVIEW

Merging time series MAN IP U L ATIN G TIME SE R IE S DATA W ITH - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Merging time series

MAN IP U L ATIN G TIME SE R IE S DATA W ITH XTS AN D ZOO IN R

Jerey Ryan

Creator of xts and quantmod

slide-2
SLIDE 2

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R

Introducing merge()

Combine series by column

cbind() and merge()

Database style joins on index (i.e. by time) Inner, outer, le and right joins

merge(..., fill = NA, join = "outer")

Fill argument handles missingness

slide-3
SLIDE 3

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R

# Default join = “outer” merge(x, y) x y 2016-08-09 1 2 2016-08-10 1 2 2016-08-11 1 NA 2016-08-12 NA 2 merge(x, y, join = "right", fill = na.locf) x y 2016-08-09 1 2 2016-08-10 1 2 2016-08-12 1 2 merge(x, y, join = "inner") x y 2016-08-09 1 2 2016-08-10 1 2

slide-4
SLIDE 4

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R

merge(x, c(2, 3, 4)) x c.2..3..4. 2016-08-09 1 2 2016-08-10 1 3 2016-08-11 1 4 merge(x, 3) x X3 2016-08-09 1 3 2016-08-10 1 3 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

slide-5
SLIDE 5

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R

Introducing rbind()

Combine series by row Rows are inserted in time order All rows in rbind() must have a time The number of columns must match

slide-6
SLIDE 6

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R

rbind() example

rbind(x, y) x 2016-08-09 1 2016-08-09 2 2016-08-10 1 2016-08-10 2 2016-08-11 1 2016-08-12 2 rbind(x, as.integer(y)) Error in try.xts(c(2L, 2L, 2L)): Error in as.xts.integer( x, ..., .RECLASS = TRUE) :

  • rder.by must be either

'names()' or otherwise specified

slide-7
SLIDE 7

Let's practice!

MAN IP U L ATIN G TIME SE R IE S DATA W ITH XTS AN D ZOO IN R

slide-8
SLIDE 8

Handling missingness

MAN IP U L ATIN G TIME SE R IE S DATA W ITH XTS AN D ZOO IN R

Jerey Ryan

Creator of xts and quantmod

slide-9
SLIDE 9

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R

Fill NAs with last observation

l.o.c.f. means “last observation carried forward”

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

slide-10
SLIDE 10

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R

Other NA options

Replace NAs na.fill(object, fill, ...) Remove NAs na.trim(object, ...) na.omit(object, ...) Interpolate NAs na.approx(object, ...)

slide-11
SLIDE 11

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R

NA replace and remove

na.fill(z, fill = -999) z Aug 09, 2016 1 Aug 10, 2016 -999 Aug 11, 2016 -999 Aug 12, 2016 4 Aug 13, 2016 -999 na.trim(z) z Aug 09, 2016 1 Aug 10, 2016 NA Aug 11, 2016 NA Aug 12, 2016 4 na.omit(z) z Aug 09, 2016 1 Aug 12, 2016 4

slide-12
SLIDE 12

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R

NA interpolation

na.approx() uses index

spacing to linearly approximate the missing values

x x Aug 09, 2016 1 Aug 11, 2016 NA Aug 12, 2016 4 na.approx(x) z Aug 09, 2016 1 Aug 11, 2016 3 Aug 12, 2016 4

slide-13
SLIDE 13

Let's practice!

MAN IP U L ATIN G TIME SE R IE S DATA W ITH XTS AN D ZOO IN R

slide-14
SLIDE 14

Lags and differences

MAN IP U L ATIN G TIME SE R IE S DATA W ITH XTS AN D ZOO IN R

Jerey Ryan

Creator of xts and quantmod

slide-15
SLIDE 15

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R

Seasonality and stationarity

Seasonality is a repeating paern Stationarity refers to some bound of the series These paerns are oen compared How get around misalignment of the series?

slide-16
SLIDE 16

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R

Lagging a time series

Used to align time series for comparisons

lag() will shi observations in time lag(x, k = 1, na.pad = TRUE, ...) k controls number of lags na.pad controls NA introduction

With xts, positive k shis values forward

slide-17
SLIDE 17

MANIPULATING TIME SERIES DATA WITH XTS AND ZOO IN R

Differencing series

Convert levels to changes (i.e. deltas)

diff(x,lag = 1, differences = 1, arithmetic = TRUE, log = FALSE, na.pad = TRUE, ...) lag controls which observations arithmetic vs. log calculations

slide-18
SLIDE 18

Let's practice!

MAN IP U L ATIN G TIME SE R IE S DATA W ITH XTS AN D ZOO IN R