# NOT RUN {
# Gamma regression with one binary covariate
# Simulated data
set.seed(1)
simx <- rbinom(1000, 1, 0.5)
mean0 <- 1.1
simy <- rgamma(1000, shape=2, scale=exp(log(mean0) + 0.2*simx))
mod <- glm(simy ~ simx, family=Gamma(link="log"))
# Check the parameter estimates are close to true
# values used for simulation
(shape <- 1 / summary(mod)$dispersion)
coef(mod)[2] # log mean ratio associated with covariate. true value 0.2
exp(coef(mod)[1] - log(shape)) # mean with x=0, true value 1.1
exp(coef(mod)[1] + coef(mod)[2] - log(shape)) # mean with x=1
focus_mean <- function(par, X, dispersion){
exp(X %*% par - log(1/dispersion))
}
X <- rbind("x0" = c(1,0), "x1" = c(1,1))
inds <- rbind("no_covariate"=c(1,0), "covariate"=c(1,1))
fic(mod, inds=inds, focus=focus_mean, X=X)
# The focus need not depend on X or the dispersion
focus_base_scale <- function(par, dispersion){
exp(par[1])
}
fic(mod, inds=inds, focus=focus_base_scale)
# ...equivalently,
focus_base_scale <- function(par){
exp(par[1])
}
fic(mod, inds=inds, focus=focus_base_scale)
# }
Run the code above in your browser using DataCamp Workspace