Making irregular data regular

Importing and Managing Financial Data in R

Joshua Ulrich

Quantitative Analyst & quantmod Co-Author and Maintainer

Regular date-time sequences

  • Time observations are same distance apart
  • Create regular date-time sequences using seq() methods:
    • seq.Date()
    • seq.POSIXt() (POSIXct and POSIXlt)
from_date <- as.Date("2017-01-01")
to_date <- as.Date("2017-01-03")
date_seq <- seq(from = from_date, to = to_date, by = "day")
Importing and Managing Financial Data in R
  • start() first index value
  • end() last index value
regular_xts <- xts(seq_along(date_seq), order.by = date_seq)
start(regular_xts)
"2017-01-01"
end(regular_xts)
"2017-01-03"
seq(from = start(regular_xts), to = end(regular_xts), by = "day")
"2017-01-01" "2017-01-02" "2017-01-03"
Importing and Managing Financial Data in R

Zero-width xts objects

  • xts object with an index, no data
zero_width_xts <- xts(, order.by = date_seq)
zero_width_xts
Data:
numeric(0)
Index:
 Date[1:3], format: "2017-01-01" "2017-01-02" "2017-01-03"
str(zero_width_xts)
An 'xts' object of zero-width
Importing and Managing Financial Data in R

Creating regular from irregular data

  • Add observation at each date-time in regular sequence
  • NA in the result
Importing and Managing Financial Data in R

Merge irregular xts with regular zero-width xts

irregular
           Price
2017-01-02 20.01
2017-01-04 20.02
2017-01-10 20.05
date_seq <- seq(from = start(irregular),
                to = end(irregular),
                by = "day")
regular_xts <- xts(, date_seq)
Importing and Managing Financial Data in R

Merge irregular xts with regular zero-width xts

merge(irregular, regular_xts)
           Price
2017-01-02 20.01
2017-01-03    NA
2017-01-04 20.02
2017-01-05    NA
2017-01-06    NA
2017-01-07    NA
2017-01-08    NA
2017-01-09    NA
2017-01-10 20.05
Importing and Managing Financial Data in R

Filling missing values

merged_xts <- merge(irregular, regular_xts)

na.locf(merged_xts)
           Price
2017-01-02 20.01
2017-01-03 20.01
2017-01-04 20.02
2017-01-05 20.02
2017-01-06 20.02
2017-01-07 20.02
2017-01-08 20.02
2017-01-09 20.02
2017-01-10 20.05
Importing and Managing Financial Data in R

Filling missing values

merge(irregular, regular_xts, fill = na.locf)
           Price
2017-01-02 20.01
2017-01-03 20.01
2017-01-04 20.02
2017-01-05 20.02
2017-01-06 20.02
2017-01-07 20.02
2017-01-08 20.02
2017-01-09 20.02
2017-01-10 20.05
Importing and Managing Financial Data in R

Let's practice!

Importing and Managing Financial Data in R

Preparing Video For Download...