R 高斯分布和伽马分布的混合
我正在寻找R中的一些脚本/包(Python也可以),以从高斯分布和伽马分布的混合中找出组件分布参数。到目前为止,我一直使用 R包“mixtools”将数据建模为高斯混合,但我认为它可以更好地用Gamma+Gaussian建模R 高斯分布和伽马分布的混合,r,gaussian,gamma-distribution,mixture-model,R,Gaussian,Gamma Distribution,Mixture Model,我正在寻找R中的一些脚本/包(Python也可以),以从高斯分布和伽马分布的混合中找出组件分布参数。到目前为止,我一直使用 R包“mixtools”将数据建模为高斯混合,但我认为它可以更好地用Gamma+Gaussian建模 谢谢这里有一种可能性: 定义实用程序功能: rnormgammamix <- function(n,shape,rate,mean,sd,prob) { ifelse(runif(n)<prob, rgamma(n,shape,ra
谢谢这里有一种可能性: 定义实用程序功能:
rnormgammamix <- function(n,shape,rate,mean,sd,prob) {
ifelse(runif(n)<prob,
rgamma(n,shape,rate),
rnorm(n,mean,sd))
}
?这听起来像是一个,在这种情况下可能是一个选择。@Andrie,我想你是说“tweedie”
dnormgammamix <- function(x,shape,rate,mean,sd,prob,log=FALSE) {
r <- prob*dgamma(x,shape,rate)+(1-prob)*dnorm(x,mean,sd)
if (log) log(r) else r
}
set.seed(101)
r <- rnormgammamix(1000,1.5,2,3,2,0.5)
d <- data.frame(r)
library("bbmle")
m1 <- mle2(r~dnormgammamix(exp(logshape),exp(lograte),mean,exp(logsd),
plogis(logitprob)),
data=d,
start=list(logshape=0,lograte=0,mean=0,logsd=0,logitprob=0))
cc <- coef(m1)
png("normgam.png")
par(bty="l",las=1)
hist(r,breaks=100,col="gray",freq=FALSE)
rvec <- seq(-2,8,length=101)
pred <- with(as.list(cc),
dnormgammamix(rvec,exp(logshape),exp(lograte),mean,
exp(logsd),plogis(logitprob)))
lines(rvec,pred,col=2,lwd=2)
true <- dnormgammamix(rvec,1.5,2,3,2,0.5)
lines(rvec,true,col=4,lwd=2)
dev.off()
tcc <- with(as.list(cc),
c(shape=exp(logshape),
rate=exp(lograte),
mean=mean,
sd=exp(logsd),
prob=plogis(logitprob)))
cbind(tcc,c(1.5,2,3,2,0.5))
library("MASS")
ff <- fitdistr(r,dnormgammamix,
start=list(shape=1,rate=1,mean=0,sd=1,prob=0.5))
cbind(tcc,ff$estimate,c(1.5,2,3,2,0.5))
ff2 <- fitdistr(r,dnormgammamix,
start=list(shape=1.5,rate=2,mean=3,sd=2,prob=0.5))
-logLik(ff2) ## 1725.994
-logLik(ff) ## 1755.458