dat <- crowder.seeds
m1.glm <- m1.glmm <- m1.bb <- m1.hglm <- NA
# Graphic
require(lattice)
dotplot(germ/n~gen|extract, dat)
# GLM
m1.glm <- glm(cbind(germ,n-germ) ~ gen*extract,
data=dat, family=quasibinomial())
summary(m1.glm)
# GLMM. Assumes Gaussian random effects
library(MASS)
m1.glmm <- glmmPQL(cbind(germ, n-germ) ~ gen*extract, random= ~1|plate,
family=binomial(), data=dat)
summary(m1.glmm)
# AOD package
if(require(aod)){
m1.bb <- betabin(cbind(germ, n - germ) ~ gen * extract, ~ 1, data = dat)
summary(m1.bb)
}
# HGML package. Beta-binomial with beta-distributed random effects
if(require(hglm)){
m1.hglm <- hglm(fixed= germ/n ~ I(gen=="O75")*extract, weights=n, data=dat,
random=~1|plate, family=binomial(), rand.family=Beta(),
fix.disp=1)
}
# Compare coefficients
round(summary(m1.glm)$coef,2)
round(summary(m1.glmm)$tTable,2)
round(summary(m1.bb)@Coef,2)
round(summary(m1.hglm)$FixCoefMat,2)
# JAGS/BUGS. See http://mathstat.helsinki.fi/openbugs/Examples/Seeds.html
# Germination rate depends on p, which is a logit of a linear predictor
# based on genotype and extract, plus random deviation to intercept
require("rjags")
# To match the output on the BUGS web page, use: dat$gen=="O73".
# We use dat$gen=="O75" to compare with the parameterization above.
jdat =list(germ = dat$germ, n = dat$n,
root = as.numeric(dat$extract=="cucumber"),
gen = as.numeric(dat$gen=="O75"),
nobs = nrow(dat))
jinit = list(int = 0, genO75 = 0, extcuke = 0, g75ecuke = 0, tau = 10)
# Unlike BUGS, we use names THAT WE CAN ACTUALLY INTERPRET!
mod.bug = "
model {
for(i in 1:nobs) {
germ[i] ~ dbin(p[i], n[i])
b[i] ~ dnorm(0.0, tau)
logit(p[i]) <- int + genO75 * gen[i] + extcuke * root[i] +
g75ecuke * gen[i] * root[i] + b[i]
}
int ~ dnorm(0.0, 1.0E-6)
genO75 ~ dnorm(0.0, 1.0E-6)
extcuke ~ dnorm(0.0, 1.0E-6)
g75ecuke ~ dnorm(0.0, 1.0E-6)
tau ~ dgamma(0.001, 0.001)
sigma <- 1 / sqrt(tau)
}"
j1 <- jags.model(textConnection(mod.bug), data=jdat, inits=jinit, n.chains=1)
c1 <- coda.samples(j1, c("int","genO75","g75ecuke","extcuke","sigma"),
n.iter=20000)
summary(c1)
# Medians are very similar to estimates from hglm
# round(summary(m1.hglm)$FixCoefMat,2)
# Plot observed data with HPD intervals for germination probability
c2 <- coda.samples(j1, c("p"), n.iter=20000)
hpd <- HPDinterval(c2)[[1]]
med <- summary(c2, quantiles=.5)$quantiles
fit <- data.frame(med, hpd)
require("latticeExtra")
obs <- dotplot(1:21 ~ germ/n, dat, ylab="plate",
col=as.numeric(dat$gen), pch=substring(dat$extract,1))
obs + segplot(1:21 ~ lower + upper, data=fit, centers=med)Run the code above in your browser using DataLab