【问题标题】:Plotting model comparison statistics in R在 R 中绘制模型比较统计信息
【发布时间】:2016-10-17 14:52:02
【问题描述】:

我将几个数据框组合成一个数据框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
  • 有没有更好的方法来比较这些模型?例如,我可以为每一列分别绘制。

【问题讨论】:

  • 请随时提出更好的问题标题
  • ...并添加一个可重现的示例,此帖子对社区具有更多价值...
  • 我已经指定了正在使用的每一个变量,以及我无法理解的确切功能。这个问题本来可以是明确而具体的。我真的不能放弃代码,在这个社区或其他任何地方复制(或直接引用)不是我的。
  • 您真的不必提供您的实际代码。但是您可以只提供一个可重现的示例(例如,在答案中如下所示),其中包含您刚刚编写的一些随机数据。
  • @Aayush 是否有一些随机代码可以在 iris top secret 之类的数据集上估算缺失值?我的意思是:关于 SO 有很好的问题,但没有很好的问题:how do I ask a good questionhow to provide a minimal reproducible example in R

标签: r plot model-comparison


【解决方案1】:

使用 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 喜欢“长”数据,我认为您的数据是“宽”的。

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

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

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

【讨论】:

    【解决方案2】:

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

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

    问题是我无法确定 R 使用的是哪个 densityplot()

    问题的其他部分保持开放。

    【讨论】:

      【解决方案3】:

      从@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"))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-22
        • 1970-01-01
        • 2021-01-27
        相关资源
        最近更新 更多