简体   繁体   English

使用小鼠 package 进行多重插补

[英]Multiple imputation using mice package

I'm not R user but I use R only to rum multiple imputation using mice package.我不是 R 用户,但我使用 R 仅使用小鼠 package 朗姆酒多重插补。 Since I updated R from version 3.6.3 to version 4.0.4, I encountered problem with the imputed data when I call it from R to STATA.由于我将 R 从版本 3.6.3 更新到版本 4.0.4,当我从 R 调用它到 STATA 时,我遇到了估算数据的问题。 The problem is when I run a survival analysis all subjects were treated as died (status=1) while the status variable includes (alive and died).问题是当我进行生存分析时,所有受试者都被视为死亡(状态=1),而状态变量包括(活着和死亡)。 I would be grateful if you please check my R code.请检查我的 R 代码,我将不胜感激。 I have named the variables for this platform as var1, var2, var3, var4.我已将此平台的变量命名为 var1、var2、var3、var4。 This is my R script这是我的 R 脚本

library (MASS)
library (lattice)
library(splines)
library(survival)
library (Rcpp)
library (mice)

#reading data 
hla=read.dta("R:/mypath")

attach(dataname)
names(dataname)

#create data frame 
dataframe=data.frame(var1, var2, var3, var4, na) 

names(dataframe)

#generate automatic prediction matrix
predmatrix=quickpred(dataframe)
predmatrix

#ImputationSettings
maxIter=20
imputations=10

#Imputation settings
#set timer on
ptm=proc.time()      

imp10=mice(dataframe,m=imputations,maxit=maxIter,
method=c(
"",#var1
"pmm",#var2
"pmm",#var3
"logreg",#var4
""),#na

predictorMatrix=predmatrix,seed=100)

#extract original and imputed data in long form
ImputedData=complete(imp10,action="long",include=TRUE)
#export data to Stata file
write.dta(ImputedData,"R:/mypath/data.dta")```

The syntax is correct, the problem was when calling the imputed data from R to STATA.语法是正确的,问题出在将推算数据从 R 调用到 STATA 时。 Recording and relabelling to some of the categorical variables are needed.需要记录和重新标记一些分类变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM