对R的MICE中的每个估算数据集执行操作 [英] Perform operation on each imputed dataset in R's MICE
问题描述
如何从R的软件包mice
中对类mids
的对象中的每个估算数据集执行操作(如子集或添加计算列)?我希望结果仍然是mids
对象.
How can I perform an operation (like subsetting or adding a calculated column) on each imputed dataset in an object of class mids
from R's package mice
? I would like the result to still be a mids
object.
示例
library(mice)
data(nhanes)
# create imputed datasets
imput = mice(nhanes)
估算的数据集存储为列表列表
The imputed datasets are stored as a list of lists
imput$imp
其中仅存在针对给定变量进行插补的观测行.
where there are rows only for the observations with imputation for the given variable.
原始(不完整)数据集存储在此处:
The original (incomplete) dataset is stored here:
imput$data
例如,如何在每个估算数据集中创建一个以chl/2
计算的新变量,从而产生一个新的mids
对象?
For example, how would I create a new variable calculated as chl/2
in each of the imputed datasets, yielding a new mids
object?
推荐答案
另一个选择是在插补之前计算变量并对其进行限制.
Another option is to calculate the variables before the imputation and place restrictions on them.
library(mice)
# Create the additional variable - this will have missing
nhanes$extra <- nhanes$chl / 2
# Change the method of imputation for extra, so that it always equals chl/2
# change the predictor matrix so only chl predicts extra
ini <- mice(nhanes, max = 0, print = FALSE)
meth <- ini$meth
meth["extra"] <- "~I(chl/2)"
pred <- ini$pred # extra isnt used to predict
pred[ "extra", "chl"] <- 1
# Imputations
imput <- mice(nhanes, seed=1, pred = pred, meth = meth, print = FALSE)
在老鼠中有一些例子:R中的链式方程进行多元插补
There are examples in mice: Multivariate Imputation by Chained Equations in R
这篇关于对R的MICE中的每个估算数据集执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!