### Data preparation
data(clinics)
climv <- clinics
(fitClinics <- HLfit(cbind(npos,nneg)~treatment+(1|clinic),
family=binomial(),data=clinics))
set.seed(123)
climv$np2 <- simulate(fitClinics, type="residual")
### fits
## Default fit without 'X2X' argument
(mvfit <- fitmv(
submodels=list(mod1=list(formula=cbind(npos,nneg)~treatment+(1|clinic),family=binomial()),
mod2=list(formula=np2~treatment+(1|clinic),
family=poisson(), fixed=list(lambda=c("1"=1)))),
data=climv))
## Fits with 'X2X' argument
# Suppose we want to fit the same intercept for the two submodels
# (there may be cases where this is meaningful, even if not here).
# The original fit has four coefficients corresponding to four columns
# of fixed-effect design matrix:
head(design_X <- model.matrix(mvfit))
# (Intercept)_1 treatment_1 (Intercept)_2 treatment_2
# [1,] 1 1 0 0
# ...
# where '_1' or '_2' identifies the submodel to which each coefficient belongs.
# The three coefficients of the intended model are (say)
# "(Intercept)" "treatment_1" "treatment_2"
# We build a matrix that relates the original 4 coefficients to these 3 ones:
X_4to3 <-
matrix(c(1,0,0,
0,1,0,
1,0,0,
0,0,1), nrow=4, ncol=3, byrow=TRUE,
dimnames=list(NULL, c("(Intercept)","treatment_1","treatment_2")))
# defined such that design_X %*% X_4to3 will be the design matrix
# for the intended model, and the single "(Intercept)" coefficient
# of the three-parameter model will operate as a shared estimate
# of the "(Intercept)_1" and "(Intercept)_2" coefficients
# of the original 4-coefficients model, as intended.
# To define such matrices, it is *strongly advised* to either fit
# the unconstrained model first, and to examine the structure
# of its model matrix (as shown above), or to use genX2X().
# The 'X2X' argument provides the matrix:
(mvfit3m <- fitmv(
submodels=list(mod1=list(formula=cbind(npos,nneg)~treatment+(1|clinic),family=binomial()),
mod2=list(formula=np2~treatment+(1|clinic),
family=poisson(), fixed=list(lambda=c("1"=1)))),
X2X = X_4to3,
data=climv))
# => the column names of 'X_4to3' are the fixed-effect names in all output.
# Alternatively, the 'X2X' argument provides a genX2X() call:
(mvfit3g <- fitmv(
submodels=list(mod1=list(formula=cbind(npos,nneg)~treatment+(1|clinic),family=binomial()),
mod2=list(formula=np2~treatment+(1|clinic),
family=poisson(), fixed=list(lambda=c("1"=1)))),
X2X = genX2X(list("(Intercept)"=c("(Intercept)_1","(Intercept)_2"))),
data=climv))
# => the last two fits are equivalent (although the order of the coefficients may differ).
# The internally produced 'X2X' matrix is that provided by
genX2X(list("(Intercept)"=c("(Intercept)_1","(Intercept)_2")),
names_ori=colnames(design_X) )
Run the code above in your browser using DataLab