# NOT RUN {
# Normal linear prediction:
# Generate single-model Wald and model-averaged MATA-Wald 95% confidence intervals
#
# Data 'y', covariates 'x1' and 'x2', all vectors of length 'n'.
# 'y' taken to have a normal distribution.
# 'x1' specifies treatment/group (factor).
# 'x2' a continuous covariate.
#
# Take the quantity of interest (theta) as the predicted response
# (expectation of y) when x1=1 (second group/treatment), and x2=15.
set.seed(0)
n = 20 # 'n' is assumed to be even
x1 = c(rep(0,n/2), rep(1,n/2)) # two groups: x1=0, and x1=1
x2 = rnorm(n, mean=10, sd=3)
y = rnorm(n, mean = 3*x1 + 0.1*x2) # data generation
x1 = factor(x1)
m1 = glm(y ~ x1) # using 'glm' provides AIC values.
m2 = glm(y ~ x1 + x2) # using 'lm' doesn't.
aic = c(m1$aic, m2$aic)
delta.aic = aic - min(aic)
model.weights = exp(-0.5*delta.aic) / sum(exp(-0.5*delta.aic))
residual.dfs = c(m1$df.residual, m2$df.residual)
p1 = predict(m1, se=TRUE, newdata=list(x1=factor(1), x2=15))
p2 = predict(m2, se=TRUE, newdata=list(x1=factor(1), x2=15))
theta.hats = c(p1$fit, p2$fit)
se.theta.hats = c(p1$se.fit, p2$se.fit)
# AIC model weights
model.weights
# 95% Wald confidence interval for theta (under Model 1)
theta.hats[1] + c(-1,1)*qt(0.975, residual.dfs[1])*se.theta.hats[1]
# 95% Wald confidence interval for theta (under Model 2)
theta.hats[2] + c(-1,1)*qt(0.975, residual.dfs[2])*se.theta.hats[2]
# 95% MATA-Wald confidence interval for theta:
mata.wald(theta.hats, se.theta.hats, model.weights, mata.t = TRUE, residual.dfs = residual.dfs)
# Plot the model-averaged confidence density and distribution functions
# on the interval [2, 7]
thetas <- seq(2, 7, by = 0.1)
dens <- dmata.wald(thetas, theta.hats, se.theta.hats, model.weights,
mata.t = TRUE, residual.dfs = residual.dfs)
dists <- pmata.wald(thetas, theta.hats, se.theta.hats, model.weights,
mata.t = TRUE, residual.dfs = residual.dfs)
par(mfrow = c(2,1))
plot(thetas, dens, type = 'l', main = 'Model-Averaged Confidence Density')
plot(thetas, dists, type = 'l', main = 'Model-Averaged Confidence Distribution')
# }
Run the code above in your browser using DataLab