stan_model_meta <- "
data {
int<lower=0> N; // number of studies
int<lower=0> K; // max number of estimator (estimator-sampling pairs)
// number of studies with estimator (estimator-sampling pair)
int<lower=0,upper=N> N1;
int<lower=0,upper=N> N2;
int<lower=0,upper=N> N3;
// ids of studies with specific estimator (estimator-sampling pair)
int<lower=0,upper=N> observed1[N1];
int<lower=0,upper=N> observed2[N2];
int<lower=0,upper=N> observed3[N3];
// parameter estimates
real<lower=0,upper=1> est1[N1];
real<lower=0,upper=1> est2[N2];
real<lower=0,upper=1> est3[N3];
// estimated standard errors of parameter estimates
real<lower=0> est1_sd[N1];
real<lower=0> est2_sd[N2];
real<lower=0> est3_sd[N3];
}
parameters {
// (additive) error factor for each estimator/estimator-sampling pair
real<lower=-1,upper=1> error[K];
// prevalence estimate for each study
vector<lower=0,upper=1>[N] alpha;
// need to add Sigma to allow for interdependence of errors across estimators
// or studies
}
model
target += normal_lpmf(est1 | error[1] + alpha[observed1], est1_sd);
target += normal_lpmf(est2 | error[2] + alpha[observed2], est2_sd);
target += normal_lpmf(est3 | error[3] + alpha[observed3], est3_sd);
}
"
get_meta_estimands <- function(data) {
data.frame(estimand_label = c(paste0("prevalence_", 1:N)),
estimand = c(data[,1]),
stringsAsFactors = FALSE)
}
get_meta_estimators = function(data) {
stan_data <- list(N = nrow(data),
K = (ncol(data)-1)/2)
for (k in 1:stan_data$K) {
stan_data[[ paste0("observed",k) ]] <-
which(!is.na(data[,(2 * k)]))
stan_data[[ paste0("N",k) ]] <-
length(stan_data[[paste0("observed",k)]])
stan_data[[ paste0("est",k) ]] <-
data[stan_data[[paste0("observed",k)]],(2 * k)]
stan_data[[ paste0("est",k,"_sd") ]] <-
data[stan_data[[paste0("observed",k)]],(1 + 2 * k)]
}
fit <-
rstan::stan(fit = stan_model_meta,
data = stan_data,
iter = 4000) %>%
extract
data.frame(estimator_label = c(paste0("prev_", 1:N)),
estimate = c(apply(fit$alpha, 2, mean)),
sd = c(apply(fit$alpha, 2, sd)),
estimand_label = c(paste0("hidden_prev", 1:N)),
big_Rhat = big_Rhat
)
}
pop_args <-
list(study_1 = study_1$pop,
study_2 = study_2$pop,
study_3 = study_3$pop,
study_4 = study_4$pop)
sample_args <-
list(study_1 = study_1$sample,
study_2 = study_2$sample,
study_3 = study_3$sample,
study_4 = study_4$sample)
study_estimators <-
list(study_1 = study_1$estimators,
study_2 = study_2$estimators,
study_3 = study_3$estimators,
study_4 = study_4$estimators)
study_estimands <-
list(study_1 = get_study_estimands,
study_2 = get_study_estimands,
study_3 = get_study_estimands,
study_4 = get_study_estimands)
study_populations <-
declare_population(handler = get_stduy_populations, handler_args = meta_pop_args)
study_samples <-
declare_sampling(handler = get_study_samples, handler_args = meta_sample_args)
study_estimands <-
declare_estimand(handler = get_study_estimands, handler_args = study_estimands)
study_estimators <-
declare_estimator(handler = get_study_estimators, handler_args = study_estimators)
meta_switch <-
declare_step(prep_study_estimators, handler_args = get_study_estimators)
meta_estimands <-
declare_estimand(handler = get_meta_estimands)
meta_estimators <-
declare_estimator(handler = get_meta_estimators)
meta_design <-
study_populations +
study_samples +
study_estimands +
study_estimators +
meta_switch +
meta_estimands +
meta_estimators