1. Learn
  2. /
  3. Courses
  4. /
  5. Manipulating Time Series Data with xts and zoo in R

Exercise

NA interpolation using na.approx()

On occasion, a simple carry forward approach to missingness isn't appropriate. It may be that a series is missing an observation due to a higher frequency sampling than the generating process. You might also encounter an observation that is in error, yet expected to be somewhere between the values of its neighboring observations.

These are scenarios where interpolation is useful. zoo provides a powerful tool to do this. Based on simple linear interpolation between points, implemented with na.approx() the data points are approximated using the distance between the index values. In other words, the estimated value is linear in time.

For this exercise, you'll use a smaller xts version of the Box and Jenkin's AirPassengers data set that ships with R. We've removed a few months of data to illustrate various fill techniques.

One takeaway, aside from getting a feel for the functions, is to see how various fill techniques impact your data, and especially how it will impact your understanding of it.

Caveat Emptor!

The AirPassengers data set is available in your workspace as AirPass.

Instructions

100 XP
  • Fill in missing months in AirPass with linear interpolation using na.approx().