在 R 中绘制模型比较统计数据

Plotting model comparison statistics in R

我将几个数据框组合成一个数据框 dfc,第五列称为 model,指定哪个模型用于插补。我想通过按 model.

分组来绘制分布

dfc 看起来像:(1000 行,5 列)

X1        X2        X3        X4      model
1500000 400000    0.542      7.521    actual
250000  32000     2.623     11.423   missForest
...

我使用下面的线条来绘制:

library(lattice)
densityplot(X1 + X2 + X3 + X4, group = dfc$model)

给予:

请注意 X1 <- dfc$X1(同样)

我的问题是:

使用 ggplot 的快速密度图。

library(ggplot2)
library(reshape2)
a <- rnorm(50)
b <- runif(50, min = -5, max = 5)
c <- rchisq(50, 2)

data <- data.frame(rnorm = a, runif = b, rchisq = c)
data <- melt(data) #from reshape2 package

ggplot(data) + geom_density(aes(value, color = variable)) + 
               geom_jitter(aes(value, 0, color = variable), alpha = 0.5, height = 0.02 ) 

备注:我添加了 reshape2 包,因为 ggplot 喜欢 "long" 数据,我认为你的是 "wide".

单独绘制每一列将像这样工作:

ggplot(data) + geom_density(aes(value, color = variable)) 
             + geom_point(aes(value, 0, color = variable))  
             + facet_grid(.~variable)

这里的颜色可能是多余的,但您可以删除 color 参数。

我所要做的就是设置一个参数:

densityplot(X1 + X2 + X3 + X4, group = dfc$model, auto.key = TRUE) 给出了想要的情节

问题是我无法弄清楚 densityplot() R 使用的是哪个。

问题的其他部分仍未解决。

从@alex 复制的数据

library(ggplot2)
library(reshape2)
a <- rnorm(50)
b <- runif(50, min = -5, max = 5)
c <- rchisq(50, 2)

dat <- data.frame(Hmisc = a, MICE = b, missForest = c)
dat <- melt(dat)

library(lattice) # using lattice package 
densityplot(~value,dat,groups = variable,auto.key = T)

个别地块

densityplot(~value|variable,dat,groups = variable,auto.key = T,scales=list(relation="free"))