R 老鼠的条纹图不显示分类变量

R 老鼠的条纹图不显示分类变量,r,imputation,r-mice,R,Imputation,R Mice,我使用R中的小鼠包进行多重插补。我只使用数值变量进行了几次插补,插补方法是预测平均值匹配,当我使用stripplot(imp)时,我可以看到所有变量的观察值和插补值 当我对分类变量和数值变量组合进行插补时,问题就出现了。然后,插补方法是数值变量的PMM,分类变量的逻辑回归。然后,stripplot只显示数值变量。使用下面的代码,我试图强制绘制带有2个值的分类变量edu: stripplot(imp,imp$edu) 条纹图(imp,名称(imp$edu)) 我得到了这个错误: stripplo

我使用R中的
小鼠
包进行多重插补。我只使用数值变量进行了几次插补,插补方法是预测平均值匹配,当我使用
stripplot(imp)
时,我可以看到所有变量的观察值和插补值

当我对分类变量和数值变量组合进行插补时,问题就出现了。然后,插补方法是数值变量的PMM,分类变量的逻辑回归。然后,
stripplot
只显示数值变量。使用下面的代码,我试图强制绘制带有2个值的分类变量
edu

stripplot(imp,imp$edu)
条纹图(imp,名称(imp$edu))
我得到了这个错误:

stripplot.mids(imp,imp$edu)中出错:无法填充扩展公式


有人知道我如何绘制数值变量和分类变量的观察值和插补值吗?

您可以尝试的一件事是将插补的
数据集
检索为data.frame,并使用正常的绘图功能。首先检索数据集,包括缺少值的原始数据集(imp是mice.mids对象,即运行mice的结果)

!包含is.na
,以避免绘制na条
var1
是要打印的变量。对于连续变量,可以创建密度图

ggplot(impL, aes(x = var2, colour = Imputed)) + geom_density()

要查看所有唯一插补,可以在aes括号内添加
group=.imp
。希望这有帮助

我刚刚遇到了一个类似的问题,所以我想我可以发布一个答案,实现您的目标,而无需提取插补数据

library(mice)

# Create dataset holding numerical and categorical data
a <- as.factor(rbinom(100, 1, 0.5))
b <- rnorm(100, 5, 1)
df <- cbind.data.frame(a, b)

# Randomly assign 10 NA values to each column
df$a[sample(length(df$a), 10)] <- NA
df$b[sample(length(df$b), 10)] <- NA

# Impute with ppm and logreg
init = mice(df, maxit=0)
meth = init$method
meth['a'] <- 'logreg'
imp <- mice(df, method = meth)

# This only plots b, the numerical
stripplot(imp)

# This plots both, as included below
stripplot(imp, a + b ~ .imp)
库(鼠标)
#创建包含数字和分类数据的数据集

谢谢你,聂克。但是,facetplot代码给出了一个错误:
“unexpected”,“in:ggplot(impL[which(!is.na(long_df$var1)),],aes(x=var1))+geom_bar(aes(y=…prop..,group=Imputed))+facet_wrap(Imputed~,“
…谢谢你的评论,我忘了在代码中添加
了facet_wrap(Imputed~,ncol=1,nrow=2)
这应该可以
ggplot(impL[which(!is.na(impL$var1)),],aes(x = var1)) + 
geom_bar(aes(y = ..prop.., group = Imputed)) + facet_wrap(Imputed ~ .,ncol=1,nrow=2)
ggplot(impL, aes(x = var2, colour = Imputed)) + geom_density()
library(mice)

# Create dataset holding numerical and categorical data
a <- as.factor(rbinom(100, 1, 0.5))
b <- rnorm(100, 5, 1)
df <- cbind.data.frame(a, b)

# Randomly assign 10 NA values to each column
df$a[sample(length(df$a), 10)] <- NA
df$b[sample(length(df$b), 10)] <- NA

# Impute with ppm and logreg
init = mice(df, maxit=0)
meth = init$method
meth['a'] <- 'logreg'
imp <- mice(df, method = meth)

# This only plots b, the numerical
stripplot(imp)

# This plots both, as included below
stripplot(imp, a + b ~ .imp)