How to do it...

The imputeTS package is incredibly powerful and easy to use; it can be used to impute missing values for time series. In this recipe, we will load a dataset containing biodiesel production figures for Argentina (this is a monthly dataset from 2008 to 2018 containing monthly production values):

  1. First, we load the imputeTS package and the necessary dataset:
library(imputeTS) 
library(ggplot2)
biodisel_prod = read.csv("./biodiesel.csv")
biodisel_prod$indice_tiempo = as.Date(biodisel_prod$indice_tiempo,"%Y-%m-%d")
  1. We will remove six observations and use the Kalman filter approach to replace the missing values. There are multiple ways of imputing these missing values:
biodisel_prod$indice_tiempo = as.Date(biodisel_prod$indice_tiempo,"%Y-%m-%d") 
biodisel_prod_removed = biodisel_prod
biodisel_prod_removed[c(30,60,90,100,109,120),2] <- NA
biodisel_prod_removed = na.kalman(biodisel_prod_removed)
  1. We now plot the results. In order to do that, we will get the dates for the missing values (those will be used to create vertical lines in the plot).We will plot two lines; a red one containing the series with the imputed values, and another one in black containing the real values. As it can be seen here, the imputation makes a lot of sense, and the imputed values are not distant from from the actual values (we are painting the points that have been removed in gray):
miss_lines = biodisel_prod_removed[c(30,60,90,100,109,120),1] 
plot(biodisel_prod_removed,type="l",col="red",lwd=6,xlab="Time",ylab="biodiesel_production")
abline(v = miss_lines[1], untf = FALSE,col="gray")
abline(v = miss_lines[2], untf = FALSE,col="gray")
abline(v = miss_lines[3], untf = FALSE,col="gray")
abline(v = miss_lines[4], untf = FALSE,col="gray")
abline(v = miss_lines[5], untf = FALSE,col="gray")
abline(v = miss_lines[6], untf = FALSE,col="gray")
lines(biodisel_prod,type="l",pch=15)

The following output shows the true time series in black, and the imputed time series in red:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.251.128