8
votes

I have a data frame containing a time series of monthly data, with some missing values.

dates <- seq(
  as.Date("2010-01-01"), as.Date("2017-12-01"), "1 month"
)
n_dates <- length(dates)
dates <- dates[runif(n_dates) < 0.5]
time_data <- data.frame(
  date = dates,
  value = rnorm(length(dates))
)
##          date      value
## 1  2010-02-01  1.3625419
## 2  2010-06-01  0.1512481
## etc.

In order do be able to make use of time series forecasting functionality in, e.g., forecast, I'd like to convert this to a ts object.

The dumb way to do this is to create a regular set of monthly dates over the whole time period, then left join back to the original data.

library(dplyr)
first_date <- min(time_data$date)
last_date <- max(time_data$date)
full_dates <- data.frame(
  date = seq(first_date, last_date, "1 month")
)
extended_time_data <- left_join(full_dates, time_data, by = "date")
##          date      value
## 1  2010-02-01  1.3625419
## 2  2010-03-01         NA
## etc.

Now I can create the time series using ts().

library(lubridate)
time_series <- ts(
  extended_time_data$value, 
  start = c(year(first_date), month(first_date)),
  frequency = 12
)

For such a simple task, this is long-winded and pretty gross.

I also looked into first converting to xts, and using a convertor from the timetk package, but nothing jumped out at me as an easier way.

This question is a dupe of How to create time series with missing datetime values, but the answer there was even fuzzier.

How do I create a ts object from a time series with missing values?

3
What is n_dates Try with expand time_data %>% expand(date = seq(min(date),max(date), by = "1 month"), select(., everything(), -date))akrun
I think n_dates should be length(seq( as.Date("2010-01-01"), as.Date("2017-12-01"), "1 month" ))Chriss Paul
You're also missing the lubridate package to access year / month etc in your example code.thelatemail

3 Answers

7
votes

Instead of using the left_join an easier option is complete, convert it to a tsibble object which is now compatible with the forecast package functions

library(tidyverse)
library(tsibble)
time_data %>% 
  complete(date = seq(min(date), max(date), by = "1 month"), 
        fill = list(value = NA)) %>%
  as_tsibble(index = date)


# A tsibble: 94 x 2 [1D]
#   date         value
#   <date>       <dbl>
# 1 2010-02-01   1.02 
# 2 2010-03-01  NA    
# 3 2010-04-01  NA    
# 4 2010-05-01   1.75 
# 5 2010-06-01  NA    
# 6 2010-07-01  NA    
# 7 2010-08-01  -0.233
# 8 2010-09-01  NA    
# 9 2010-10-01  NA    
#10 2010-11-01  -0.987
# ... with 84 more rows

As mentioned above, it is compatible withe forecast functions

library(fable)
time_data %>% 
   complete(date = seq(min(date), max(date), by = "1 month"), 
         fill = list(value = 0)) %>% 
   as_tsibble(index = date) %>%
   ETS(value) %>% 
   forecast %>%
   autoplot

NOTE: Here, the missing values are imputed as 0.

enter image description here

It can be imputed with the previous non-NA value with fill

time_data %>% 
   complete(date = seq(min(date), max(date), by = "1 month")) %>% 
   fill(value) %>% 
   as_tsibble(index = date) %>% 
   ETS(value) %>%
   forecast %>%
   autoplot

data

n_dates <- 3
9
votes

Using the input data frame defined in the Note at the end, convert it to a zoo object with index of class yearmon. Then as.ts will convert it to ts.

library(zoo)

z <- read.zoo(DF, FUN = as.yearmon)
as.ts(z)
##      Jan Feb Mar Apr May Jun Jul Aug
## 2000   1  NA  NA   2   3  NA   4   5

If you prefer to express it in terms of pipes:

library(magrittr)
library(zoo)

DF %>% read.zoo(FUN = as.yearmon) %>% as.ts

If desired, interpolate the values in the time series using na.locf (last occurrence carried forward), na.approx (linear interpolation), na.spline, na.StructTS (seasonal Kalman filter) or other zoo NA filling function. e.g.

library(forecast)

DF %>% read.zoo(FUN = as.yearmon) %>% as.ts %>% na.spline %>% forecast

Note

The data in the question is not reproducible because random numbers are used without set.seed and n_dates is undefined. Below we define a data frame DF reproducibly for purposes of example.

library(zoo)

dates <- as.Date(as.yearmon("2000-01") + c(0, 3, 4, 6, 7)/12)
DF <- data.frame(dates, values = seq_along(dates))

giving:

> DF
       dates values
1 2000-01-01      1
2 2000-04-01      2
3 2000-05-01      3
4 2000-07-01      4
5 2000-08-01      5
0
votes

A base option and using set.seed(789) before running your data generation

temp <- which(full_dates$date%in%time_data$date)
full_dates$new[temp] <- time_data$value
head(full_dates, 20)

         date         new
1  2010-02-01  0.62589399
2  2010-03-01  0.98117664
3  2010-04-01          NA
4  2010-05-01 -0.04770986
5  2010-06-01 -1.51961483
6  2010-07-01          NA
7  2010-08-01  0.79493644
8  2010-09-01 -0.14423251
9  2010-10-01 -0.70649791
10 2010-11-01  0.61071247
11 2010-12-01          NA
12 2011-01-01  1.08506164
13 2011-02-01 -0.71134925
14 2011-03-01  1.15628805
15 2011-04-01  1.23556280
16 2011-05-01 -0.32245531
17 2011-06-01          NA
18 2011-07-01          NA
19 2011-08-01  0.73277540
20 2011-09-01 -0.28752883

or same result but using data.table

setDT(full_dates)[temp, new:= time_data$value]

Now to xts

xts::xts(full_dates[,-1], order.by = full_dates$date,  frequency = 12 )