执行与追踪插补

在 R 中处理缺失数据

Nicholas Tierney

Statistician

课程概览

用插补理解数据结构

可视化与探索插补值

  • 为探索缺失而插补
  • 追踪缺失值
  • 将插补值与数据对比可视化
在 R 中处理缺失数据

用插补理解数据结构

impute_below(c(5,6,7,NA,9,10))
5.00000  6.00000  7.00000  4.40271  9.00000 10.00000
在 R 中处理缺失数据

impute_below

  • impute_below_if():
impute_below_if(data, is.numeric)
  • impute_below_at():
impute_below_at(data, vars(var1,var2))
  • impute_below_all():
impute_below_all(data)
在 R 中处理缺失数据

追踪缺失值

df
# A tibble: 6 x 1
   var1
  <dbl>
1     5
2     6
3     7
4    NA
5     9
6    10
impute_below_all(df)
# A tibble: 6 x 1
   var1
  <dbl>
1  5   
2  6   
3  7   
4  4.40
5  9   
6 10
在 R 中处理缺失数据

追踪缺失值

bind_shadow(df)
# A tibble: 6 x 2
   var1 var1_NA
  <dbl> <fct>  
1  5    !NA    
2  6    !NA    
3  7    !NA    
4  NA    NA     
5  9    !NA    
6 10    !NA
bind_shadow(df) %>% impute_below_all()
# A tibble: 6 x 2
   var1 var1_NA
  <dbl> <fct>  
1  5    !NA    
2  6    !NA    
3  7    !NA    
4  4.40 NA     
5  9    !NA    
6 10    !NA
在 R 中处理缺失数据

用直方图将插补值与数据对比可视化

aq_imp <- airquality %>%
   bind_shadow() %>% 
   impute_below_all()

ggplot(aq_imp,
       aes(x = Ozone,
           fill = Ozone_NA)) + 
   geom_histogram()

在 R 中处理缺失数据

用分面将插补值与数据对比可视化

ggplot(aq_imp,
       aes(x = Ozone,
           fill = Ozone_NA)) + 
  geom_histogram() + 
  facet_wrap(~ Month)

在 R 中处理缺失数据

用分面可视化插补值

ggplot(aq_imp,
       aes(x = Ozone,
           fill = Ozone_NA)) + 
  geom_histogram() + 
  facet_wrap(~ Solar.R_NA)

在 R 中处理缺失数据

用散点图将插补值与数据对比可视化

aq_imp <- airquality %>%
  bind_shadow() %>% 
  add_label_shadow() %>%
  impute_below_all()

ggplot(aq_imp,
       aes(x = Ozone,
           y = Solar.R,
           color = any_missing)) + 
  geom_point()

在 R 中处理缺失数据

让我们练习吧!

在 R 中处理缺失数据

Preparing Video For Download...