为什么 R 中的插补函数会出现此错误?

Why is this error happening with the imputation function in R?

我正在尝试根据此示例进行插补:impute example

data(airquality)
summary(airquality)

airq = airquality
ind = sample(nrow(airq), 10)
airq$Wind[ind] = NA
airq$Wind = cut(airq$Wind, c(0,8,16,24))
summary(airq)

imp = impute(airq, classes = list(integer = imputeMean(), factor = imputeMode()),
  dummy.classes = "integer")

这给了我一个警告:

Warning message:
In mean.default(sort(x, partial = half + 0L:1L)[half + 0L:1L]) :
  argument is not numeric or logical: returning NA

但是,当我尝试查看返回的数据帧时,我得到:

> head(imp, 10)
Error in x[..., drop = drop] : incorrect number of dimensions

> head(imp$data, 10)
NULL

desc 给出:

> imp$desc
NULL

我最初是使用我的实际数据完成上述操作的,但遇到了这些错误,所以我尝试了上面的示例来进行完整性检查。

我已经在 Windows 中从 RStudio 和命令行界面尝试了这个,在示例和我的实际数据上都得到了相同的结果。另外,尝试使用版本 3.63 和 4.03,再次得到相同的结果。

我也在 Ubuntu 上对两个全新安装进行了尝试,结果相同。

有趣的是,当我执行 names 时,虚拟变量不存在:

> names(imp)
[1] "Ozone"   "Solar.R" "Wind"    "Temp"    "Month"   "Day"

str(imp) 给出:

> str(imp)
Classes ‘impute’ and 'data.frame':      153 obs. of  6 variables:
 $ Ozone  : num  41 36 12 18 NA 28 23 19 8 NA ...
 $ Solar.R: num  190 118 149 313 NA NA 299 99 19 194 ...
 $ Wind   : Factor w/ 3 levels "(0,8]","(8,16]",..: 1 1 2 NA 2 2 2 2 3 2 ...
 $ Temp   : int  67 72 74 62 56 66 65 59 61 69 ...
 $ Month  : int  5 5 5 5 5 5 5 5 5 5 ...
 $ Day    : int  1 2 3 4 5 6 7 8 9 10 ...
 - attr(*, "imputed")= int [1:54] 5 NA NA NA NA NA NA NA NA NA ...

并查看应该进行插补的列之一:

> head(imp$Solar.R)
[1] 190 118 149 313  NA  NA

(我的实际数据将 NA 替换为全 0,即使它应该是列平均值)

更新:我刚刚在我的本地机器 运行 MacOS 上测试了这个并且得到了完全相同的错误。

我明白了。我使用的是 Hmisc 库中 impute 的任何版本。使用 mlr 成功了。

> imp$desc
Imputation description
Target: 
Features: 6; Imputed: 6
impute.new.levels: TRUE
recode.factor.levels: TRUE
dummy.type: factor