% \VignetteIndexEntry{Beta-blockers - Discrete Mixture Models} % \VignetteDepends{flexmix} %\VignetteEngine{knitr::knitr} %\VignetteEncoding{UTF-8} \documentclass[a4paper]{article} \title{Beta-blockers - Discrete Mixture Models} \begin{document} \maketitle <>= options(width=80) @ The data set "betablockers" is loaded from the package "flexmix". <>= library(flexmix) data(betablocker) @ <>= betablocker$Treatment <- as.factor(betablocker$Treatment) @ First a simple logit model is fitted with the only covariate "Treatment". <>= GlmT <- glm(cbind(Deaths, Total - Deaths) ~ Treatment, family = "binomial", data = betablocker) summary(GlmT) @ Now the logit model is extended by the factor "Center" which has 22 different values. The deviance reduces from 305.76 with 42 degrees of freedom to 23.62 with 21 degrees of freedom. <>= GlmTC <- glm(cbind(Deaths, Total - Deaths) ~ Treatment + as.factor(Center), family = "binomial", data = betablocker) summary(GlmTC) @ In the following two mixed models are fitted with Gauss--Hermite--Quadrature, so "glmmML" is needed. <>= library(glmmML) @ First the random intercept model with 4 quadrature points is fitted. <>= MixedGH4 <- glmmML(cbind(Deaths, Total - Deaths) ~ Treatment, cluster=Center, method = c("ghq"), n.points = 4, boot = 0, data=betablocker) summary(MixedGH4) @ Now we use 20 quadrature points but there is no big difference in coefficients. <>= MixedGH20 <- glmmML(cbind(Deaths, Total - Deaths) ~ Treatment, cluster=Center, method = c("ghq"), n.points = 20, boot = 0, data=betablocker) summary(MixedGH20) @ <>= set.seed(5) @ Finally we fit the discrete mixture models for which the function "stepFlexmix" is used. Here we use three components defined by option "k=3". <>= detach(package:glmmML) @ <>= MixFix3 <-stepFlexmix(cbind(Deaths, Total - Deaths) ~ 1 | Center, model = FLXMRglmfix(family = "binomial", fixed = ~ Treatment), k = 3, nrep = 5, data = betablocker) @ Typing the name of the fitted model yields the sizes of the three clusters. <>= MixFix3 @ The coefficients are printed by the command "parameters()". <>= parameters(MixFix3) @ The command "summary()" returns for example the estimated component weights and the BIC. The coefficients with standard errors and p--values can be found by "summary(refit())". <>= library(flexmix) @ <>= summary(MixFix3) summary(refit(MixFix3)) @ <>= set.seed(5) @ Finally the discrete mixture model with 4 components is fitted. <>= MixFix4 <-stepFlexmix(cbind(Deaths, Total - Deaths) ~ 1 | Center, model = FLXMRglmfix(family = "binomial", fixed = ~ Treatment), k = 4, nrep = 5, data = betablocker) @ <>= MixFix4 @ <>= parameters(MixFix4) @ <>= summary(MixFix4) summary(refit(MixFix4)) @ <>= detach(package:flexmix) @ \end{document}