开始使用免费开始使用

多种变量类型的模型法插补

您已经成功编写了一个函数,用逻辑回归并从条件分布中抽样来进行插补。这个统计方法已经相当高级了!在本练习中,您将把目前学到的基于模型的插补方法结合起来,对 tao 数据中的不同类型变量进行插补。

您的任务是像上一章那样按变量迭代,并插补两个变量:

  • is_hot,一个由 air_temp 派生出的二元变量;当 air_temp 大于等于 26 度时取 1,否则取 0;
  • humidity,一个您已经很熟悉的连续变量。

您需要同时使用之前学过的线性回归函数,以及您自己编写的逻辑回归函数。开始吧!

本练习是课程的一部分

R 中的缺失值填补处理

查看课程

练习说明

  • 在最初缺失的位置将 is_hot 设为 NA
  • 使用您的函数 impute_logreg(),以 sea_surface_temp 为唯一自变量,通过逻辑回归is_hot 进行插补。
  • 在最初缺失的位置将 humidity 设为 NA
  • 使用线性回归,以 sea_surface_tempair_temp 为自变量,对 humidity 进行插补。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Initialize missing values with hot-deck
tao_imp <- hotdeck(tao)

# Create boolean masks for where is_hot and humidity are missing
missing_is_hot <- tao_imp$is_hot_imp
missing_humidity <- tao_imp$humidity_imp

for (i in 1:3) {
  # Set is_hot to NA in places where it was originally missing and re-impute it
  ___ <- NA
  tao_imp <- ___(tao_imp, ___ ~ ___)
  # Set humidity to NA in places where it was originally missing and re-impute it
  ___ <- NA
  tao_imp <- ___(tao_imp, ___ ~ sea_surface_temp + ___)
}
编辑并运行代码