函数gammamixEM(包mixtools)的密度图形输出

时间:2016-08-08 14:08:35

标签: r gamma-distribution

我正在使用包gammamixEM中的mixtools函数。如何在函数normalmixEM中返回密度的图形输出(即plot(...,which=2)中的第二个图)?

更新

以下是函数gammamixEM的{​​{3}}:

x <- c(rgamma(200, shape = 0.2, scale = 14), rgamma(200, 
     shape = 32, scale = 10), rgamma(200, shape = 5, scale = 6))
out <- gammamixEM(x, lambda = c(1, 1, 1)/3, verb = TRUE)

以下是函数normalmixEM的{​​{3}}:

data(faithful)
attach(faithful)
out <- normalmixEM(waiting, arbvar = FALSE, epsilon = 1e-03)
plot(out, which=2)

reproducible example

我想从函数gammamixEM获得密度的图形输出。

1 个答案:

答案 0 :(得分:1)

你走了。

out <- normalmixEM(waiting, arbvar = FALSE, epsilon = 1e-03)

x          <- out 
whichplots <- 2
density = 2 %in% whichplots
loglik  = 1 %in% whichplots

def.par    <- par(ask=(loglik + density > 1), "mar") # only ask and mar are changed
mix.object <- x


k    <- ncol(mix.object$posterior)
x    <- sort(mix.object$x)
a    <- hist(x, plot = FALSE)
maxy <- max(max(a$density), .3989*mix.object$lambda/mix.object$sigma)

我只需要深入研究plot.mixEM

的源代码

所以,现在用gammamixEM执行此操作:

x <- c(rgamma(200, shape = 0.2, scale = 14), rgamma(200, 
                                                    shape = 32, scale = 10), rgamma(200, shape = 5, scale = 6))
gammamixEM.out <- gammamixEM(x, lambda = c(1, 1, 1)/3, verb = TRUE)



mix.object <- gammamixEM.out

k    <- ncol(mix.object$posterior)
x    <- sort(mix.object$x)
a    <- hist(x, plot = FALSE)
maxy <- max(max(a$density), .3989*mix.object$lambda/mix.object$sigma)

main2 <- "Density Curves"
xlab2 <- "Data" 
col2  <- 2:(k+1) 

hist(x, prob = TRUE, main = main2, xlab = xlab2, 
     ylim = c(0,maxy))


for (i in 1:k) {
  lines(x, mix.object$lambda[i] * 
          dnorm(x, 
                sd = sd(x)))
}

enter image description here

我认为,如果你想添加标签,流畅的线条等,那么继续这个例子应该非常简单。这里是plot.mixEM函数的source