LOCF 插补以及如何填充缺失的条目

问题描述

我正在处理以下数据集,我正在尝试填充 VISUAL52 变量的缺失条目,通过 LOCF 方法(最后一次观察结转)输入数据。

library(readr)
library(mice)
library(finalfit)
library(Hmisc)
library(lattice)
library(VIM)
library(rms)
library(zoo)

> hw3
# A tibble: 240 x 11
   treat LINE0 LOST4 LOST12 LOST24 LOST52 VISUAL0 VISUAL4 VISUAL12 VISUAL24 VISUAL52
   <fct> <dbl> <dbl>  <dbl>  <dbl>  <dbl>   <dbl>   <dbl>    <dbl>    <dbl>    <dbl>
 1 2        12     1      3     NA     NA      59      55       45       NA       NA
 2 2        13    -1      0      0      2      65      70       65       65       55
 3 1         8     0      1      6     NA      40      40       37       17       NA
 4 1        13     0      0      0      0      67      64       64       64       68
 5 2        14    NA     NA     NA     NA      70      NA       NA       NA       NA
 6 2        12     2      2      2      4      59      53       52       53       42
 7 1        13     0     -2     -1      0      64      68       74       72       65
 8 1         8     1      0      1      1      39      37       43       37       37
 9 2        12     1      2      1      1      59      58       49       54       58
10 1        10     0     -4     -4     NA      49      51       71       71       NA
# ... with 230 more rows

我不知道我做得好不好,但我尝试以这种方式描述每次处理的 VISUAL52 变量的样本量、均值和标准误差(请告诉我使用不同的函数会更好)。

numSummary(hw3[,"VISUAL52",drop=FALSE],groups=hw3$treat,statistics=c("mean","se(mean)","quantiles"),quantiles=c(0,.25,.5,.75,1))

binnedCounts(hw3[hw3$treat == '1',drop=FALSE])
# treat = 1

binnedCounts(hw3[hw3$treat == '2',drop=FALSE])
# treat = 2

然而,至于插补部分,我已经从数据表包中运行了函数 nafill(),但是我得到了在运行 complete() 函数之后你可能会看到的错误。

 library(data.table)
 imp_locf <-  nafill(hw3$VISUAL52,"locf",nan=NA)
 data_imputed <- complete(imp_locf)

*emphasized text*Error in UseMethod("complete_") : 
      no applicable method for 'complete_' applied to an object of class "c('double','numeric')"

我想知道为什么该函数会返回此错误,以及是否有人可能知道一些替代方法来使用 locf 方法估算数据并填充数据集中缺失的数据。

解决方法

如果你想在你的数据集上应用 locf,你可以使用 imputeTS 包。

contents: any

或者如果您只想将 LOCF 用于 VISUAL52 变量:

onDaySelected

另请记住,其他算法可能更适合您的数据。 imputeTS 提供多种功能,特别是用于时间序列插补 (more algorithms in imputeTS)。您似乎已经在使用的 mouse 包具有用于横截面数据的其他算法。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...