เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

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.

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Case Study: Analyzing City Time Series Data in R

ดูคอร์ส

คำแนะนำการฝึกหัด

  • Use na.approx() to fill the missing values in gdp_xts using linear interpolation. Save this new xts object as gdp_approx.
  • Plot your new xts object using plot.xts().
  • Query your new xts object for GDP in 1993.

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# 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
แก้ไขและรันโค้ด