Replace missing data - II
Like most aspects of time series data manipulation, there are many ways to handle missingness. As you discovered in the previous exercise, both the locf
and nocb
approach require you to make certain assumptions about growth patterns in your data. While locf
is more conservative and nocb
is a more aggressive, both generate step-wise growth from missing data.
But what if you have reason to expect linear growth in your data? In this case, it may be more useful to use linear interpolation, which generates new values between the data on either end of the missing value weighted according to time.
In this exercise, you'll fill the missing values in your gdp_xts
data using the na.approx() command, which uses interpolation to estimate linear values in time.
This exercise is part of the course
Case Study: Analyzing City Time Series Data in R
Exercise instructions
- Use
na.approx()
to fill the missing values ingdp_xts
using linear interpolation. Save this new xts object asgdp_approx
. - Plot your new xts object using
plot.xts()
. - Query your new xts object for GDP in 1993.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Fill NAs in gdp_xts using linear approximation
gdp_approx <-
# Plot your new xts object
plot.xts(___, major.format = "%Y")
# Query for GDP in 1993 in gdp_approx