<- list(
inv_links identity = function(eta) eta,
inverse = function(eta) 1/eta,
log = exp)
<- function(inv_link){
get_data <- pmin(pmax(0, rnorm(n, 2)), 4)
x <- pmin(pmax(0, rnorm(n, 1)), 4)
z <- 1 + x + 2*z
eta data.frame(
x=x,
z=z,
eta=eta,
mu=inv_link(eta),
y=rgamma(n, shape=1, scale=inv_link(eta))
)
}
<- function(x, I) (x >I[1]) && (x< I[2]) is.in.confint
Simulate a glm
with exponential distribution such that
\[\operatorname{y} \sim \operatorname{exp}(\operatorname{h}(\alpha + 1\operatorname{x} + 2\operatorname{z}))\]
with the \(h\) being the inverse of the link functions:
- identity
- inverse
- log
Setup
Simulation
For each link function: 1. Plot an example (green line is the true expectation and red line is a smoothing spline) 2. Simulate coverage of true parameter for x
(should be 95%)
<- 100 # sample size
n <- 500 # number of simulations to test the confidence interval
nsim set.seed(123)
for (i in seq_along(inv_links)){
<- get_data(inv_links[[i]])
D
plot(y ~ mu, main=names(inv_links)[i], data=D)
# Test with SmoothingSplines if indeed: E(y|mu) = mu
with(D, lines(smooth.spline(mu, y), col='red'))
abline(0,1, col='green')
# see if the conf-interval for beta_x is valid. i.e.:
# repeat it nsim times and see if 1 is in our confidence interval
<- mcreplicate::mc_replicate(nsim,{
R try({
<- get_data(inv_links[[i]])
D <- summary(f <- glm(
s ~ x + z,
y data = D,
family = Gamma(link = names(inv_links)[i]),
mustart = mu)); s
suppressMessages(is.in.confint(1, confint(f, "x")))
silent = TRUE)
},
})<- mean(as.logical(R), na.rm=TRUE)
coverage cat(sprintf("Coverage for %s: %f\n", names(inv_links)[i], coverage))
}
Coverage for identity: 0.940299
Coverage for inverse: 0.946058
Coverage for log: 0.956000
Results
For all link functions, the coverage is about 95%