使用 simputation 進行資料插補
R 中有許多做插補的套件。我們將專注使用 simputation 套件,它提供了簡潔而強大的介面來執行插補。
建立一個好的插補模型相當重要,但主題也很複雜——打造良好的插補模型所需的功夫,和建立良好的統計模型一樣多。在本課程中,我們將著重於如何評估插補結果。
首先,我們要看看 impute_lm() 函式,它會依照你指定的線性模型來插補數值。
在這個練習中,我們會把前面學過的評估技巧,應用到用 impute_lm() 插補後的資料,之後再在後續單元基於這個方法繼續延伸。
本練習屬於課程
在 R 中處理遺漏值
練習說明
使用 oceanbuoys 資料集:
- 使用
wind_ew與wind_ns來插補humidity,並用add_label_shadow()追蹤遺漏值。 - 繪製
air_temp_c與humidity的插補值,分別放在 x 與 y 軸,並以any_missing()著色。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Impute humidity and air temperature using wind_ew and wind_ns, and track missing values
ocean_imp_lm_wind <- ___ %>%
bind_shadow() %>%
impute_lm(air_temp_c ~ wind_ew + wind_ns) %>%
impute_lm(___ ~ ___ + ___) %>%
add_label_shadow()
# Plot the imputed values for air_temp_c and humidity, colored by missingness
ggplot(___,
aes(x = ___, y = ___, color = any_missing)) +
geom_point()