This vignette shows how to fit measurement error and imputation
models using the inlamemi
package for a few different
simple simulated data sets. Note that although the data sets describe
realistic situations, they are all completely fictitious, and created
purely to illustrate how to fit models in different situations.
Simple example with missingness and two types of measurement error
Error types | Likelihood | Response | Covariate with error | Other covariate(s) |
---|---|---|---|---|
Berkson, classical, missing values | Gaussian |
This is a simple simulation with Berkson and classical error as well as missing data, to check that the package works as expected in that scenario.
Generating the data
set.seed(2024)
n <- 1000
# Covariate without error:
z <- rnorm(n, mean = 0, sd = 1)
# Berkson error:
u_b <- rnorm(n, sd = 1)
alpha.0 <- 1; alpha.z <- 2
r <- rnorm(n, mean = alpha.0 + alpha.z*z, sd = 1)
x <- r + u_b # Turn off Berkson by commenting out "+ u_b"
# Response:
beta.0 <- 1; beta.x <- 2; beta.z <- 2
y <- beta.0 + beta.x*x + beta.z*z + rnorm(n)
# Classical error:
u_c <- rnorm(n, sd = 1)
x_obs <- r + u_c
# Missingness:
m_pred <- -1.5 - 0.5*z # This gives a mean probability of missing of ca 0.2.
m_prob <- exp(m_pred)/(1 + exp(m_pred))
m_index <- as.logical(rbinom(n, 1, prob = m_prob)) # MAR
# m_index <- sample(1:n, 0.2*n, replace = FALSE) # MCAR
x_obs[m_index] <- NA
simple_data <- data.frame(y = y, x = x_obs, z = z)
Fitting the model
# Fit the model
simple_model <- fit_inlamemi(data = simple_data,
formula_moi = y ~ x + z,
formula_imp = x ~ z,
family_moi = "gaussian",
error_type = c("berkson", "classical"),
prior.prec.moi = c(10, 9), # Gamma(10, 9)
prior.prec.berkson = c(10, 9), # Gamma(10, 9)
prior.prec.classical = c(10, 9), # Gamma(10, 9)
prior.prec.imp = c(10, 9), # Gamma(10, 9)
prior.beta.error = c(0, 1/1000), # N(0, 10^3)
initial.prec.moi = 1,
initial.prec.berkson = 1,
initial.prec.classical = 1,
initial.prec.imp = 1)
summary(simple_model)
#> Formula for model of interest:
#> y ~ x + z
#>
#> Formula for imputation model:
#> x ~ z
#>
#> Error types:
#> [1] "berkson" "classical"
#>
#> Fixed effects for model of interest:
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> beta.0 1.038367 0.2188315 0.6194553 1.038938 1.453129 1.035553
#> beta.z 1.928720 0.3880856 1.2432347 1.924118 2.597072 1.927316
#>
#> Coefficient for variable with measurement error and/or missingness:
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> beta.x 1.974098 0.2000806 1.581592 1.973621 2.369381 1.971641
#>
#> Fixed effects for imputation model:
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> alpha.x.0 1.033079 0.05057932 0.9338512 1.033087 1.132261 1.033087
#> alpha.x.z 2.024710 0.05224108 1.9222957 2.024693 2.127224 2.024692
#>
#> Model hyperparameters (apart from beta.x):
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> Precision for model of interest 1.1259893 0.3674036 0.5572310 1.0751508 1.986992 0.9817414
#> Precision for x berkson model 1.1273265 0.3463917 0.5993735 1.0766969 1.949423 0.9817974
#> Precision for x classical model 0.9272704 0.1097689 0.7287398 0.9212726 1.160285 0.9102319
#> Precision for x imp model 0.9773233 0.1253106 0.7551711 0.9689568 1.247736 0.9516362
simple.truth <- tibble::tribble(
~"variable", ~"value",
"beta.x", beta.x,
"beta.z", beta.z,
# "beta.0", beta.0,
"alpha.x.z", alpha.z,
# "alpha.0", alpha.0
)
plot(simple_model, plot_intercepts = FALSE) +
geom_point(data = simple.truth, aes(x = value))
Missing data only
Error types | Likelihood | Response | Covariate with error | Other covariate(s) |
---|---|---|---|---|
Missing values | Gaussian |
In this example, we have missingness in one covariate, but no other measurement error, so this shows how to do simple imputation of a missing covariate in R-INLA.
Generating the data
set.seed(2024)
n <- 1000
# Covariate without missingness:
z <- rnorm(n, mean = 0, sd = 1)
# Covariate that will have missingness:
alpha.0 <- 1; alpha.z <- 2
x <- rnorm(n, mean = alpha.0 + alpha.z*z, sd = 1)
# Response:
beta.0 <- 1; beta.x <- 2; beta.z <- 2
y <- beta.0 + beta.x*x + beta.z*z + rnorm(n)
# Missingness:
m_pred <- -1.5 - 0.5*z # This gives a mean probability of missing of ca 0.2.
m_prob <- exp(m_pred)/(1 + exp(m_pred))
m_index <- as.logical(rbinom(n, 1, prob = m_prob)) # MAR
# m_index <- sample(1:n, 0.2*n, replace = FALSE) # MCAR
x_obs <- x
x_obs[m_index] <- NA
missing_data <- data.frame(y = y, x = x_obs, z = z)
Model without imputation
naive_model <- inla(formula = y ~ x + z, family = "gaussian", data = missing_data)
naive_model$summary.fixed
#> mean sd 0.025quant 0.5quant 0.975quant mode kld
#> (Intercept) 2.059952 0.07313492 1.9165113 2.059952 2.203392 2.059952 1.012936e-11
#> x 1.060038 0.04847764 0.9649586 1.060038 1.155118 1.060038 1.020428e-11
#> z 4.242952 0.09766027 4.0514099 4.242952 4.434494 4.242952 9.964362e-12
naive_model$summary.hyperpar
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> Precision for the Gaussian observations 0.3082829 0.01379352 0.2818484 0.308078 0.3359021 0.3076677
Model with imputation
missing_model <- fit_inlamemi(formula_moi = y ~ x + z,
formula_imp = x ~ z,
family_moi = "gaussian",
data = missing_data,
error_type = "missing",
prior.prec.moi = c(2, 1),
prior.prec.imp = c(2, 1),
prior.beta.error = c(0, 1/1000),
initial.prec.moi = 1,
initial.prec.imp = 1)
summary(missing_model)
#> Formula for model of interest:
#> y ~ x + z
#>
#> Formula for imputation model:
#> x ~ z
#>
#> Error types:
#> [1] "missing"
#>
#> Fixed effects for model of interest:
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> beta.0 0.973503 0.04846928 0.8797655 0.9733355 1.067906 0.973237
#> beta.z 1.943237 0.07613999 1.7991292 1.9423503 2.088858 1.941701
#>
#> Coefficient for variable with measurement error and/or missingness:
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> beta.x 2.024608 0.03441343 1.955974 2.024909 2.09147 2.026179
#>
#> Fixed effects for imputation model:
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> alpha.x.0 1.031089 0.03137387 0.9695648 1.031086 1.092633 1.031086
#> alpha.x.z 1.983403 0.03207566 1.9204914 1.983404 2.046312 1.983404
#>
#> Model hyperparameters (apart from beta.x):
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> Precision for model of interest 1.056206 0.05227668 0.9576852 1.054691 1.163469 1.0511931
#> Precision for x classical model 1.141023 0.35772335 0.6283785 1.079405 2.018753 0.9573468
#> Precision for x imp model 1.066437 0.04970986 0.9717735 1.065332 1.167454 1.0632390
missing_truth <- tibble::tribble(
~"variable", ~"value",
"beta.0", beta.0,
"beta.x", beta.x,
"beta.z", beta.z,
"alpha.x.0", alpha.0,
"alpha.x.z", alpha.z
)
plot(missing_model) +
geom_point(data = missing_truth, aes(x = value))
Random effect in the main model
Error types | Likelihood | Response | Covariate with error | Other covariate(s) |
---|---|---|---|---|
Classical | Gaussian | , random effect |
In this example, we simulate data that is grouped in such a way that it should be modelled with a random effect in the model of interest.
Generating the data
m <- 10 # number of groups
n <- 100 # number of observations per group
N <- m*n # total number of observations
sd_y <- 3 # sd for the noise
sd_w <- 2 # sd for random effect
sd_x <- 2 # sd for covariate without error
sd_u <- 1 # sd for measurement error
# Covariate without error
z <- rnorm(N, 0, 2)
# Covariate with error
x <- rnorm(N, 0, sd_x) # Independent of z, but can change that here
x_obs <- x + rnorm(N, 0, sd_u)
# Random effect
w_per_group <- rnorm(m, 0, sd_w)
w <- rep(w_per_group, each = n)
# Response
y <- 1 + 2*x + 2*z + w + rnorm(N, 0, sd_y)
reff_data <- data.frame(y = y, id = rep(1:m, each = n), x = x_obs, z = z)
Fitting the model
Firstly, if we ignored the measurement error, we might fit a model like this:
naive_model <- inla(y ~ x + z + f(id, model = "iid"),
data = reff_data,
family = "gaussian")
summary(naive_model)
#> Time used:
#> Pre = 1.63, Running = 0.753, Post = 0.0302, Total = 2.41
#> Fixed effects:
#> mean sd 0.025quant 0.5quant 0.975quant mode kld
#> (Intercept) -0.316 0.560 -1.429 -0.316 0.796 -0.316 0
#> x 1.682 0.051 1.582 1.682 1.782 1.682 0
#> z 2.022 0.058 1.908 2.022 2.136 2.022 0
#>
#> Random effects:
#> Name Model
#> id IID model
#>
#> Model hyperparameters:
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> Precision for the Gaussian observations 0.077 0.003 0.070 0.077 0.084 0.077
#> Precision for id 0.401 0.184 0.145 0.367 0.852 0.305
#>
#> Marginal log-Likelihood: -2756.06
#> is computed
#> Posterior summaries for the linear predictor and the fitted values are computed
#> (Posterior marginals needs also 'control.compute=list(return.marginals.predictor=TRUE)')
# curve(dgamma(x, shape = 1.5, rate = 2), to = 2)
reff_model <- fit_inlamemi(formula_moi = y ~ x + z +
f(id, model = "iid", hyper = list(prec = list(initial = -15, param = c(2, 2)))),
formula_imp = x ~ 1,
family_moi = "gaussian",
error_type = "classical",
data = reff_data,
initial.prec.moi = 1/4,
initial.prec.classical = 1,
initial.prec.imp = 1/4,
prior.prec.moi = c(1, 4),
prior.prec.classical = c(10, 10),
prior.prec.imp = c(1, 4),
prior.beta.error = c(0, 1/1000))
summary(reff_model)
#> Formula for model of interest:
#> y ~ x + z + f(id, model = "iid", hyper = list(prec = list(initial = -15,
#> param = c(2, 2))))
#>
#> Formula for imputation model:
#> x ~ 1
#>
#> Error types:
#> [1] "classical"
#>
#> Fixed effects for model of interest:
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> beta.0 -0.3672884 0.53978793 -1.440816 -0.3675593 0.7078623 -0.3675271
#> beta.z 2.0223168 0.05797559 1.908603 2.0223201 2.1360120 2.0223201
#>
#> Coefficient for variable with measurement error and/or missingness:
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> beta.x 2.154564 0.2170726 1.730389 2.15348 2.58508 2.148951
#>
#> Fixed effects for imputation model:
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> alpha.x.0 0.1022764 0.07115944 -0.03728399 0.1022764 0.2418368 0.1022764
#>
#> Model hyperparameters (apart from beta.x):
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> Precision for model of interest 0.1132594 0.02348445 0.07435727 0.1107971 0.1664097 0.1058989
#> Precision for x classical model 0.9607242 0.33903509 0.45749111 0.9072227 1.7743618 0.8090164
#> Precision for x imp model 0.2552421 0.02842044 0.20413553 0.2535861 0.3158959 0.2501341
#> Precision for id 0.4114624 0.16995927 0.16008511 0.3842442 0.8165514 0.3326511
reff.truth <- tibble::tribble(
~"variable", ~"value",
"beta.x", 2,
"beta.z", 2,
"beta.0", 1,
"alpha.x.0", 0
)
plot(reff_model) +
geom_point(data = reff.truth, aes(x = value))
Interaction effect with error variable
Error types | Likelihood | Response | Covariate with error | Other covariate(s) |
---|---|---|---|---|
Classical | Gaussian | categorical variable , interaction effect |
Interaction effects between the error prone variable and an (assumed)
error-free variable can also be used. The syntax is as normal, an
interaction effect between variables x
and s
would be specified in the formula for the model of interest as
x:s
.
Generating the data
set.seed(2024)
n <- 1000
# Covariate without error:
s <- c(rep(0, n/2), rep(1, n/2))
# Classical error:
x <- rnorm(n, mean = 0, sd = 2)
u_c <- rnorm(n, sd = 1)
x_obs <- x + u_c
# Response:
beta.0 <- 1; beta.x.s <- 2
y <- beta.0 + beta.x*x + beta.x.s*x_obs*s + rnorm(n)
interact_data <- data.frame(y = y, x = x_obs, z = z, s = s)
Fitting the model
interact_model <- fit_inlamemi(formula_moi = y ~ x:s,
formula_imp = x ~ 1,
family_moi = "gaussian",
data = interact_data,
error_type = "classical",
prior.beta.error = c(0, 0.01),
prior.prec.moi = c(10, 9),
prior.prec.classical = c(10, 9),
prior.prec.imp = c(10, 8),
initial.prec.moi = 1,
initial.prec.classical = 1,
initial.prec.imp = 2)
summary(interact_model)
#> Formula for model of interest:
#> y ~ x:s
#>
#> Formula for imputation model:
#> x ~ 1
#>
#> Error types:
#> [1] "classical"
#>
#> Fixed effects for model of interest:
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> beta.0 1.00808321 0.07959729 0.8519468 1.00808629 1.1642024 1.00808641
#> beta.s -0.08599863 0.13378884 -0.3484160 -0.08599385 0.1763915 -0.08599385
#>
#> Coefficient for variable with measurement error and/or missingness:
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> beta.sx 2.055727 0.06323594 1.931629 2.055591 2.180612 2.055030
#> beta.x 1.709473 0.04042264 1.629998 1.709437 1.789156 1.709289
#>
#> Fixed effects for imputation model:
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> alpha.x.0 0.03481148 0.06851461 -0.0995666 0.03481148 0.1691896 0.03481148
#>
#> Model hyperparameters (apart from beta.sx, beta.x):
#> mean sd 0.025quant 0.5quant 0.975quant mode
#> Precision for model of interest 0.4106093 0.03320783 0.3492889 0.4092161 0.4799530 0.4063318
#> Precision for x classical model 4.0998047 0.44010802 3.2958477 4.0784353 5.0265322 4.0401171
#> Precision for x imp model 0.2252679 0.01059211 0.2050960 0.2250328 0.2467911 0.2245931
plot(interact_model)
Logistic regression with classical error and missing data
Error types | Likelihood | Response | Covariate with error | Other covariate(s) |
---|---|---|---|---|
Classical, missing | Binomial |
Here we show how to fit a logistic regression model.
set.seed(1)
nn <- 1000
z <- rbinom(nn, 1, 0.5)
x <- rnorm(nn, 1-0.5*z, 1)
# Error
w <- x + rnorm(nn, 0, 1)
# Generating missing data, depending on z
eta <- -1 + z
prob_missing <- exp(eta)/(1 + exp(eta))
missing_index <- rbinom(nn, 1, prob_missing)
# Proportion missing:
sum(missing_index)/nn
#> [1] 0.393
# Replace the values in w by missing in case the index for missingness is =1:
w <- ifelse(missing_index==1, NA, w)
# Generate binomial response
eta <- x + z
prob <- exp(eta)/(1 + exp(eta))
y <- rbinom(nn, 1, prob)
data_binom <- data.frame(x = x, z = z, y = y, w = w)
Modeling error and missing data:
model_binom <- fit_inlamemi(data = data_binom,
formula_moi = y ~ w + z,
formula_imp = w ~ z,
formula_mis = m ~ z,
family_moi = "binomial",
error_type = c("classical","missing"),
prior.prec.classical = c(10, 9),
prior.prec.imp = c(10, 9),
prior.beta.error = c(0, 1/1000),
initial.prec.classical = 2,
initial.prec.imp = 1)
summary(model_binom)
plot(model_binom)
Poisson regression with classical error and missing data
Error types | Likelihood | Response | Covariate with error | Other covariate(s) |
---|---|---|---|---|
Classical, missing | Poisson |
Here we show how to fit a Poisson regression model, where the model of interest has a random intercept term.
set.seed(1)
nn <- 1000
z <- rbinom(nn, 1, 0.5)
x <- rnorm(nn, 1-0.5*z, 1)
# Error
w <- x + rnorm(nn, 0, 1)
# Generating missing data, depending on z
eta <- -1 + z
prob_missing <- exp(eta)/(1 + exp(eta))
missing_index <- rbinom(nn, 1, prob_missing)
# Proportion missing:
sum(missing_index)/nn
#> [1] 0.393
# Replace the values in w by missing in case the index for missingness is =1:
w <- ifelse(missing_index==1, NA, w)
# Random effect to include in the regression model:
re <- rep(rnorm(nn/20), each = 20)
# Linear predictor
eta <- x + z + re
# Generate Poisson response
y <- rpois(nn, exp(eta))
data_pois <- data.frame(x = x, z = z, y = y, w = w, id = rep(seq(1:50), each = 20))
Simple regression models with correct and error-prone variables to check the effect:
library(lme4)
summary(glmer(y ~ x + z + (1|id), data = data_pois, family="poisson"))$coef
summary(glmer(y ~ w + z + (1|id), data = data_pois, family="poisson"))$coef
Modeling error and missing data:
model_pois <- fit_inlamemi(data = data_pois,
formula_moi = y ~ w + z + f(id,model="iid"),
formula_imp = w ~ z,
formula_mis = m ~ z,
family_moi = "poisson",
error_type = c("classical","missing"),
prior.prec.classical = c(19, 9),
prior.prec.imp = c(10, 9),
prior.beta.error = c(0, 1/1000),
initial.prec.classical = 2,
initial.prec.imp = 1)
summary(model_pois)
plot(model_pois)