Definição do problema

library(cmpreg)
data(nematodes)
str(nematodes)
## 'data.frame':    94 obs. of  5 variables:
##  $ cult: Factor w/ 19 levels "A","B","C","D",..: 1 1 1 1 1 2 2 2 2 2 ...
##  $ mfr : num  10.52 11.03 6.42 8.16 4.48 ...
##  $ vol : int  40 40 40 40 40 40 40 40 40 40 ...
##  $ nema: int  4 5 3 4 3 2 2 2 2 2 ...
##  $ off : num  0.263 0.276 0.161 0.204 0.112 ...
## help(nematodes, h = "html")
##======================================================================
## Análise de dados de nematóides
xyplot(nema ~ off, groups = cult,
       type = c("g", "p"),
       data = nematodes,
       auto.key = list(
           title = "Linhagens",
           cex.title = 1,
           corner = c(0.9, 0.9)
       ))

Duas estruturas para modelagem serão consideradas * Modelar somente o efeito aleatório das linhagens, * Modelar somente o efeito fixo de off e aleatório das linhagens.

Modelos em competição

  1. Modelo misto Poisson (ajustado pela maximização da verossimilhança)
  2. Modelo misto COM-Poisson (ajustado pela maximização da verossimilhança)
  3. Modelo misto Poisson (sob o paradigma Bayesiano)
    • INLA (Integrated Nested Laplace Aproximation)
    • Amostragem MCMC (usando o jags)
  4. Modelo misto Gamma-Count (sob o paradigma Bayesiano - INLA)

Abordagem de Máxima Verossimilhança

Modelo misto Poisson

##----------------------------------------------------------------------
## Com a Poisson
library(lme4)
mle0 <- glmer(nema ~ (1|cult), data = nematodes,
              family = poisson)
mle1 <- glmer(nema ~ log(off) + (1|cult), data = nematodes,
              family = poisson)
##-------------------------------------------
## Estimativas dos parametros
## Para o modelo sem efeito fixo
tmle0 <- rbind(sigma = c(sqrt(VarCorr(mle0)$cult), NA, NA),
               summary(mle0)$coef[, -4])
rownames(tmle0)[2] <- c("(Intercept)")
tmle0
##             Estimate Std. Error  z value
## sigma       0.759144         NA       NA
## (Intercept) 1.760503  0.1808152 9.736474
## Para o modelo com efeito fixo
tmle1 <- rbind(sigma = c(sqrt(VarCorr(mle1)$cult), NA, NA),
               summary(mle1)$coef[, -4])
tmle1
##              Estimate Std. Error  z value
## sigma       0.7310786         NA       NA
## (Intercept) 2.1562623 0.23278215 9.263005
## log(off)    0.1620083 0.06397819 2.532242
## Predição dos efeitos aleatórios
ranef.mle0 <- data.frame(ef = ranef(mle0)$cult[, 1], m = "mle0")
ranef.mle1 <- data.frame(ef = ranef(mle1)$cult[, 1], m = "mle1")
ranef.mle <- rbind(ranef.mle0, ranef.mle1)

densityplot(~ef, groups = m,
            data = ranef.mle,
            axis = axis.grid,
            auto.key = TRUE)

Modelo misto COM-Poisson

##----------------------------------------------------------------------
## Com a COM-Poisson
## library(cmpreg)
## cmp0 <- cmp(nema ~ (1|cult), data = nematodes, sumto = 50)
## cmp1 <- cmp(nema ~ log(off) + (1|cult), data = nematodes,
##             sumto = 50)
load("mixedcmp_models.rda")
## Estimativas dos parametros
## Para o modelo sem efeito fixo
tcmp0 <- rbind(sigma = c(exp(coef(cmp0)[2]), NA, NA),
               summary(cmp0)@coef[-2, -4])
colnames(tcmp0) <- colnames(summary(cmp0)@coef[, -4])
tcmp0
##              Estimate Std. Error   z value
## sigma       0.8749274         NA        NA
## phi         0.1528742  0.1792980 0.8526264
## (Intercept) 2.0683427  0.4408113 4.6921272
## Para o modelo com efeito fixo
tcmp1 <- rbind(sigma = c(exp(coef(cmp1)[2]), NA, NA),
               summary(cmp1)@coef[-2, -4])
colnames(tcmp1) <- colnames(summary(cmp1)@coef[, -4])
tcmp1
##              Estimate Std. Error  z value
## sigma       0.9161159         NA       NA
## phi         0.2409883 0.17683814 1.362762
## (Intercept) 2.7523952 0.56492336 4.872157
## log(off)    0.1982931 0.07807116 2.539902
## Predição dos efeitos aleatórios
ranef.cmp0 <- data.frame(ef = mixedcmp.ranef(cmp0), m = "cmp0")
ranef.cmp1 <- data.frame(ef = mixedcmp.ranef(cmp1), m = "cmp1")
ranef.cmp <- rbind(ranef.cmp0, ranef.cmp1)

densityplot(~ef, groups = m,
            data = ranef.cmp,
            axis = axis.grid,
            auto.key = TRUE)

Abordagem Bayesiana com INLA

Modelo misto Poisson

##----------------------------------------------------------------------
## Com a poisson
library(INLA)
inla0 <- inla(nema ~ 1 + f(cult, model = "iid"),
              data = nematodes, family = "poisson")
inla1 <- inla(nema ~ log(off) + f(cult, model = "iid"),
              data = nematodes, family = "poisson")
## Estimativas dos parametros
## Para o modelo sem efeito fixo
tinla0 <- rbind(c(sqrt(1/summary(inla0)$hyperpar[1]), rep(NA, 6)),
                summary(inla0)$fixed)
colnames(tinla0) <- colnames(summary(inla0)$fixed)
rownames(tinla0)[1] <- "sigma"
tinla0
##             mean     sd     0.025quant 0.5quant 0.975quant mode   kld
## sigma       0.729325 NA     NA         NA       NA         NA     NA 
## (Intercept) 1.7634   0.1793 1.4051     1.7647   2.1139     1.7671 0
## Para o modelo com efeito de off
tinla1 <- rbind(c(sqrt(1/summary(inla1)$hyperpar[1]), rep(NA, 6)),
                summary(inla1)$fixed)
colnames(tinla1) <- colnames(summary(inla1)$fixed)
rownames(tinla1)[1] <- "sigma"
tinla1
##             mean      sd     0.025quant 0.5quant 0.975quant mode   kld
## sigma       0.7018797 NA     NA         NA       NA         NA     NA 
## (Intercept) 2.1629    0.2321 1.7015     2.1646   2.6147     2.1678 0  
## log(off)    0.1632    0.0643 0.0374     0.163    0.2898     0.1626 0
## Predição para os efeitos aleatórios
ranef.inla0 <- data.frame(ef = inla0$summary.random$cult$mean,
                          m = "inla0")
ranef.inla1 <- data.frame(ef = inla1$summary.random$cult$mean,
                          m = "inla1")
ranef.inla <- rbind(ranef.inla0, ranef.inla1)

densityplot(~ef, groups = m,
            data = ranef.inla,
            axis = axis.grid,
            auto.key = TRUE)

Modelo misto Gamma-Count

##----------------------------------------------------------------------
## Com a Gamma-Count
inlag0 <- inla(nema ~ 1 + f(cult, model = "iid"),
               data = nematodes, family = "gammacount")
inlag1 <- inla(nema ~ log(off) + f(cult, model = "iid"),
               data = nematodes, family = "gammacount")
## Estimativas dos parametros
## Para o modelo sem efeito fixo
tinlag0 <- rbind(c(sqrt(1/summary(inlag0)$hyperpar[2, 1]), rep(NA, 6)),
                 c(summary(inlag0)$hyperpar[2, 1], rep(NA, 6)),
                 summary(inlag0)$fixed)
colnames(tinlag0) <- colnames(summary(inlag0)$fixed)
rownames(tinlag0) <- c("sigma", "alpha", "(Intercept)")
tinlag0
##                  mean     sd 0.025quant 0.5quant 0.975quant   mode kld
## sigma       0.7262411     NA         NA       NA         NA     NA  NA
## alpha       1.8960000     NA         NA       NA         NA     NA  NA
## (Intercept) 1.7718000 0.1803     1.4084   1.7737     2.1239 1.7774   0
## Para o modelo com efeito de off
tinlag1 <- rbind(c(sqrt(1/summary(inlag1)$hyperpar[2, 1]), rep(NA, 6)),
                 c(summary(inlag1)$hyperpar[2, 1], rep(NA, 6)),
                 summary(inlag1)$fixed)
colnames(tinlag1) <- colnames(summary(inlag1)$fixed)
rownames(tinlag1) <- c("sigma", "alpha", "(Intercept)", "log(off)")
tinlag1
##                  mean     sd 0.025quant 0.5quant 0.975quant   mode kld
## sigma       0.6989248     NA         NA       NA         NA     NA  NA
## alpha       2.0471000     NA         NA       NA         NA     NA  NA
## (Intercept) 2.1668000 0.2251     1.7161   2.1691     2.6041 2.1735   0
## log(off)    0.1598000 0.0598     0.0433   0.1593     0.2785 0.1585   0
## Predição para os efeitos aleatórios
ranef.inlag0 <- data.frame(ef = inlag0$summary.random$cult$mean,
                          m = "inlag0")
ranef.inlag1 <- data.frame(ef = inlag1$summary.random$cult$mean,
                          m = "inlag1")
ranef.inlag <- rbind(ranef.inlag0, ranef.inlag1)

densityplot(~ef, groups = m,
            data = ranef.inlag,
            axis = axis.grid,
            auto.key = TRUE)

Abordagem Bayesiana por simulação

Modelo misto Poisson

Abaixo define-se o modelo conforme sintaxe do JAGS

##======================================================================
## Por amostragem MCMC
library(rjags)

##-------------------------------------------
## Com a Poisson
cat(
" model {
    ## Log-verossimilhanca
    for (i in 1:m) {
      u[i] ~ dnorm(0, tau.e)
    }
    for (j in 1:n) {
      log(mu[j]) <- b0 + b1 * cov[j] + u[ind[j]]
      y[j] ~ dpois(mu[j])
    }
    ## Prioris
    b0 ~ dnorm(0, 0.0001)
    b1 ~ dnorm(0, 0.0001)
    tau.e ~ dgamma(0.001, 0.001)
    sigma <- sqrt(1/tau.e)
}
", file = "poisRE.modelo")
##-------------------------------------------
## Modelo sem efeito de covariavel
data0 <- with(
    nematodes,
    list("y" = nema,
         "cov" = rep(0, length(nema)),
         "n" = length(nema),
         "m" = length(unique(cult)),
         "ind" = as.numeric(cult))
)

jagsmodel0 <- jags.model(
    "poisRE.modelo",
    data = data0,
    n.chains = 3,
    n.adapt = 1000
)

amostra0 <- coda.samples(
    jagsmodel0, c("b0", "sigma"),
    n.iter = 10000, thin = 10,
    n.chains = 3,
    n.adapt = 1000)

Avaliação das cadeias

xyplot(amostra0, axis = axis.grid, aspect = "fill")

densityplot(amostra0, axis = axis.grid, aspect = "fill")

acfplot(amostra0, type = "h", axis = axis.grid, aspect = "fill")

Predição dos efeitos

##-------------------------------------------
## Predição dos efeitos fixos e aleatórios
amostra0 <- coda.samples(
    jagsmodel0, c("b0", "sigma", "u"),
    n.iter = 10000, thin = 10,
    n.chains = 3,
    n.adapt = 1000)

amostra0 <- as.mcmc(do.call(rbind, amostra0))
##-------------------------------------------
## Resumos da posteriori
resumo0 <- summary(amostra0)$statistics
ranef.jags0 <- data.frame(ef = resumo0[-(1:3), 1], m = "jags0")

resumo0[1:3, ]
##             Mean        SD    Naive SE Time-series SE
## b0     1.7588183 0.1966604 0.003590511    0.010455028
## sigma  0.8177363 0.1582447 0.002889140    0.003172241
## u[1]  -0.4162237 0.2844491 0.005193306    0.010261947
HPDinterval(amostra0)[1:3, ]
##            lower     upper
## b0     1.3741071 2.1423279
## sigma  0.5439982 1.1260357
## u[1]  -0.9337815 0.1732196
##-------------------------------------------
## Modelo com efeito de covariavel
data1 <- with(
    nematodes,
    list("y" = nema,
         "cov" = log(off),
         "n" = length(nema),
         "m" = length(unique(cult)),
         "ind" = as.numeric(cult))
)

jagsmodel1 <- jags.model(
    "poisRE.modelo",
    data = data1,
    n.chains = 3,
    n.adapt = 1000
)

amostra1 <- coda.samples(
    jagsmodel1, c("b0", "b1", "sigma"),
    n.iter = 15000, thin = 15,
    n.chains = 3,
    n.adapt = 1000)
##-------------------------------------------
## Avaliação das cadeias
xyplot(amostra1, axis = axis.grid, aspect = "fill")

densityplot(amostra1, axis = axis.grid, aspect = "fill")

acfplot(amostra1, type = "h", axis = axis.grid, aspect = "fill")

##-------------------------------------------
## Predição dos efeitos fixos e aleatórios
amostra1 <- coda.samples(
    jagsmodel1, c("b0", "b1", "sigma", "u"),
    n.iter = 15000, thin = 15,
    n.chains = 3,
    n.adapt = 1000)

amostra1 <- as.mcmc(do.call(rbind, amostra1))
##-------------------------------------------
## Resumos da posteriori
resumo1 <- summary(amostra1)$statistics
ranef.jags1 <- data.frame(ef = resumo1[-(1:3), 1], m = "jags1")

resumo1[1:3, ]
##            Mean         SD    Naive SE Time-series SE
## b0    2.1624017 0.24134134 0.004406270    0.011765488
## b1    0.1613295 0.06378398 0.001164531    0.002198930
## sigma 0.7870339 0.15444255 0.002819722    0.002819722
HPDinterval(amostra1)[1:3, ]
##            lower     upper
## b0    1.68139162 2.6175509
## b1    0.03712581 0.2786109
## sigma 0.51791519 1.0893652
##-------------------------------------------
## Empilhando os efeitos aleatórios
ranef.jags <- rbind(ranef.jags0, ranef.jags1)

densityplot(~ef, groups = m,
            data = ranef.jags,
            axis = axis.grid,
            auto.key = TRUE)

Comparação

tmle0
##             Estimate Std. Error  z value
## sigma       0.759144         NA       NA
## (Intercept) 1.760503  0.1808152 9.736474
tcmp0
##              Estimate Std. Error   z value
## sigma       0.8749274         NA        NA
## phi         0.1528742  0.1792980 0.8526264
## (Intercept) 2.0683427  0.4408113 4.6921272
tinla0
##             mean     sd     0.025quant 0.5quant 0.975quant mode   kld
## sigma       0.729325 NA     NA         NA       NA         NA     NA 
## (Intercept) 1.7634   0.1793 1.4051     1.7647   2.1139     1.7671 0
tinlag0
##                  mean     sd 0.025quant 0.5quant 0.975quant   mode kld
## sigma       0.7262411     NA         NA       NA         NA     NA  NA
## alpha       1.8960000     NA         NA       NA         NA     NA  NA
## (Intercept) 1.7718000 0.1803     1.4084   1.7737     2.1239 1.7774   0
resumo0
##             Mean        SD    Naive SE Time-series SE
## b0     1.7588183 0.1966604 0.003590511    0.010455028
## sigma  0.8177363 0.1582447 0.002889140    0.003172241
## u[1]  -0.4162237 0.2844491 0.005193306    0.010261947
## u[2]  -0.9637514 0.3375637 0.006163042    0.010115228
## u[3]  -0.4186188 0.3049069 0.005566812    0.010426097
## u[4]  -0.4157892 0.2820474 0.005149458    0.010105954
## u[5]  -0.2345221 0.2759777 0.005038641    0.010612596
## u[6]  -0.6840160 0.3025901 0.005524513    0.010810981
## u[7]  -1.0375140 0.3412430 0.006230216    0.009723383
## u[8]  -1.2206177 0.3582616 0.006540933    0.010846117
## u[9]  -0.1987818 0.2734049 0.004991667    0.010531217
## u[10] -0.4061088 0.2875541 0.005249995    0.010414626
## u[11] -0.1660468 0.2723515 0.004972436    0.010755736
## u[12]  0.4116607 0.2416842 0.004412530    0.010277316
## u[13]  0.2467485 0.2527692 0.004614914    0.011376233
## u[14]  1.0841331 0.2208306 0.004031797    0.010694903
## u[15]  0.5327949 0.2398190 0.004378476    0.010744884
## u[16]  0.7943707 0.2319959 0.004235646    0.010999328
## u[17]  1.1908691 0.2169643 0.003961208    0.010432497
## u[18]  0.7948980 0.2251802 0.004111209    0.010635809
## u[19]  1.0825267 0.2182815 0.003985257    0.010701551
tmle1
##              Estimate Std. Error  z value
## sigma       0.7310786         NA       NA
## (Intercept) 2.1562623 0.23278215 9.263005
## log(off)    0.1620083 0.06397819 2.532242
tcmp1
##              Estimate Std. Error  z value
## sigma       0.9161159         NA       NA
## phi         0.2409883 0.17683814 1.362762
## (Intercept) 2.7523952 0.56492336 4.872157
## log(off)    0.1982931 0.07807116 2.539902
tinla1
##             mean      sd     0.025quant 0.5quant 0.975quant mode   kld
## sigma       0.7018797 NA     NA         NA       NA         NA     NA 
## (Intercept) 2.1629    0.2321 1.7015     2.1646   2.6147     2.1678 0  
## log(off)    0.1632    0.0643 0.0374     0.163    0.2898     0.1626 0
tinlag1
##                  mean     sd 0.025quant 0.5quant 0.975quant   mode kld
## sigma       0.6989248     NA         NA       NA         NA     NA  NA
## alpha       2.0471000     NA         NA       NA         NA     NA  NA
## (Intercept) 2.1668000 0.2251     1.7161   2.1691     2.6041 2.1735   0
## log(off)    0.1598000 0.0598     0.0433   0.1593     0.2785 0.1585   0
resumo1
##             Mean         SD    Naive SE Time-series SE
## b0     2.1624017 0.24134134 0.004406270    0.011765488
## b1     0.1613295 0.06378398 0.001164531    0.002198930
## sigma  0.7870339 0.15444255 0.002819722    0.002819722
## u[1]  -0.5395566 0.27873408 0.005088965    0.009066656
## u[2]  -0.9263252 0.32183310 0.005875842    0.008283940
## u[3]  -0.4491349 0.29382947 0.005364568    0.008813636
## u[4]  -0.4031997 0.28691589 0.005238343    0.009457460
## u[5]  -0.2545586 0.26399599 0.004819885    0.007509279
## u[6]  -0.6703322 0.30010689 0.005479177    0.008784288
## u[7]  -1.0082799 0.33303094 0.006080285    0.009482659
## u[8]  -1.1251695 0.36241744 0.006616807    0.007940801
## u[9]  -0.2136563 0.26412956 0.004822324    0.008618960
## u[10] -0.3650129 0.28670337 0.005234463    0.008332607
## u[11] -0.1161477 0.26147135 0.004773792    0.007814705
## u[12]  0.3730351 0.22910566 0.004182878    0.008130473
## u[13]  0.2645631 0.24565335 0.004484996    0.008553872
## u[14]  0.9830484 0.21815909 0.003983022    0.008544280
## u[15]  0.5108496 0.22955938 0.004191162    0.008584932
## u[16]  0.7494138 0.21721909 0.003965860    0.008079686
## u[17]  1.1445692 0.21035784 0.003840591    0.008120491
## u[18]  0.8216965 0.22093545 0.004033711    0.008199035
## u[19]  1.0277424 0.21477409 0.003921221    0.008779407