set.seed(50)
# linear model, binary modifier
dat <- data.frame(y=runif(50), x1=runif(50), x2=runif(50),
z=rbinom(50, 1, 0.5), r=rbinom(50, 1, 0.5))
(qfit <- qgcomp.emm.glm.noboot(f=y ~ z + x1 + x2, emmvar="z",
expnms = c('x1', 'x2'), data=dat, q=2, family=gaussian()))
(qfitee <- qgcomp.emm.glm.ee(f=y ~ z + x1 + x2, emmvar="z",
expnms = c('x1', 'x2'), data=dat, q=2, family=gaussian()))
# logistic model, continuous modifier
dat2 <- data.frame(y=rbinom(50, 1, 0.5), x1=runif(50), x2=runif(50),
z=runif(50), r=rbinom(50, 1, 0.5))
(qfit2 <- qgcomp.emm.glm.noboot(f=y ~ z + x1 + x2, emmvar="z",
expnms = c('x1', 'x2'), data=dat2, q=2, family=binomial()))
(qfit2ee <- qgcomp.emm.glm.ee(f=y ~ z + x1 + x2, emmvar="z",
expnms = c('x1', 'x2'), data=dat2, q=2, family=binomial(), rr=FALSE))
# Note under rr = TRUE that the risk ratio will be reported in the MSM results
(qfit2eerr <- qgcomp.emm.glm.ee(f=y ~ z + x1 + x2, emmvar="z",
expnms = c('x1', 'x2'), data=dat2, q=2, family=binomial(), rr=TRUE))
# linear model, factor modifier
dat <- data.frame(y=runif(150), x1=runif(150), x2=runif(150),
r=rbinom(150, 1, 0.5), z=sample(c(1, 2, 3), 150, replace=TRUE))
#note need to declare factor
dat$zfact = as.factor(dat$z)
# this can fail if the model is unidentified (e.g. z and zfact are included in the model)
(qfit <- qgcomp.emm.glm.noboot(f=y ~ zfact + x1 + x2, emmvar="zfact",
expnms = c('x1', 'x2'), data=dat, q=2, family=gaussian()))
(qfitee <- qgcomp.emm.glm.ee(f=y ~ zfact + x1 + x2, emmvar="zfact",
expnms = c('x1', 'x2'), data=dat, q=2, family=gaussian()))
library(qgcomp)
# standard qgcomp model without interaction
(qfitee_noemm <- qgcomp.glm.ee(f=y ~ zfact + x1 + x2,
expnms = c('x1', 'x2'), data=dat, q=2, family=gaussian()))
qfitee_noemm$fit # underlying fit
# global test for interaction
anova(qfitee, qfitee_noemm)
# get stratified effect estimates:
getstrateffects(qfitee, emmval=1)
getstrateffects(qfitee, emmval=2)
getstrateffects(qfitee, emmval=3)
dat$rfact = as.factor(dat$r)
(qfiteer <- qgcomp.emm.glm.ee(f=y ~ zfact + x1 + x2 + r, emmvar="zfact",
expnms = c('x1', 'x2'), data=dat, q=2, family=gaussian()))
# factor as a confounder, also works if the modifier is not in the model
(qfiteerr2 <- qgcomp.emm.glm.ee(f=y ~ x1 + x2 + rfact, emmvar="zfact",
expnms = c('x1', 'x2'), data=dat, q=2, family=gaussian()))
getstrateffects(qfiteerr2, emmval=2)
getjointeffects(qfiteerr2, emmval=2)
modelbound(qfiteerr2, emmval=2)
pointwisebound(qfiteerr2, emmval=2)
(qfitee <- qgcomp.glm.ee(f=y ~ zfact + x1 + x2, emmvar="zfact",
expnms = c('x1', 'x2'), data=dat, q=10, degree=2, family=gaussian()))
(qfitee <- qgcomp.emm.glm.ee(f=y ~ zfact + x1 + x2, emmvar="zfact",
expnms = c('x1', 'x2'), data=dat, q=10, degree=2, family=gaussian()))
Run the code above in your browser using DataLab