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.
Bài tập này là một phần của khóa học
Case Study: Analyzing City Time Series Data in R
Hướng dẫn bài tập
- Use
na.approx()to fill the missing values ingdp_xtsusing 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.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
# 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