-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_simu_twosided.R
153 lines (127 loc) · 5.99 KB
/
example_simu_twosided.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Example 4: two-sided selection models
## Simulate data
library(tidyverse)
library(brms)
library(rstan)
library(MCMCvis)
library(publipha)
library(RoBMA)
rstan_options(auto_write = TRUE)
set.seed(2021)
N_exp <- 60 # number of studies
theta_exp <- 0.3 # the true effect
tau_e_exp <- 0.15 # the standard deviation of the true effects for studies
SE_exp <- 0.2 # the standard deviation of the sampling distribution
# the probablity to publish the study based the p-value
w_publish <- c(0.2, 0.5, 1) # (0.10, 1] (0.05, 0.10] [0, 0.05]
alpha_e <- rnorm(N_exp, 0, tau_e_exp) # studies
df_simu_one <- tibble(
SE = extraDistr::rtnorm(N_exp, 0.1, SE_exp, a = 0)) %>%
mutate(g = rnorm(n(), theta_exp+alpha_e, SE), #
experiment = paste0("E",1:n()),
pvalue = round((1-pnorm(g, 0, SE))*2, 3), # still two-sided p-value
# pvalue = round(1-pnorm(g, 0, SE), 3), # one-sdied p-value (should not use this)
# p-value intervals
K = case_when(pvalue <= .05 ~ 3,
(pvalue > .05 & pvalue <= .1) ~ 2,
pvalue > .1 ~ 1),
published = extraDistr::rbern(n(), w_publish[K]))
df4 <- filter(df_simu_one, published==1)
df_simu_one %>%
group_by(K) %>%
summarize(mean(published), n())
df4 %>%
summarize(mean(g))
##################### Fit with brms #####################
### Fit the brm model (without publication bias)
# Note: brms still fit two-sided models
bf4 <- bf(g | se(SE) ~ 1 + (1|experiment))
brmfit_two <- brm(bf4, data = df4,
chains = 6, cores = 6, seed = 12,
control = list(adapt_delta = .9))
##################### Equivalent codes Set 1 ##################################
# -1.96, -1.65, 1.65 and 1.96
# Below functions essentially use two-sided p-value of 0.10 and 0.05 to set
# intervals for selection models.
## with custom Stan codes.
data_ls_one1 <- make_standata(bf4, data=df4)
data_ls_one1$alpha <- c(0.10, 0.05)
data_ls_one1$N_alpha <- length(data_ls_one1$alpha) # number of intervals
data_ls_one1$side <- 2
ex4_bias_one1 <- stan(file = 'stan_models/ma_bias_twosided.stan', # 'stan_bias.stan',
data = data_ls_one1,
iter = 4000, warmup = 2000,
chains = 6, cores = 6, seed = 12,
control = list(adapt_delta = .9))
MCMCsummary(ex4_bias_one1, params=c("b_Intercept", "omega", "cutoff_output"))
# pairs(ex4_bias_one, pars=c("omega", "b_Intercept"))
## With library(publipha)
# it seems that library(publipha) cannot fit two-sided selection models.
# Note: it seems that using gamma or dirichlet make differences.
## With library(RoMBA)
robma_1 <- RoBMA(d = df4$g, se = df4$SE,
priors_omega = list(prior(distribution = "two.sided",
parameters = list(alpha = c(1, 1, 1),
steps = c(.05, .10)),
prior_odds = 1)),
priors_mu_null = NULL,
priors_tau_null = NULL,
priors_omega_null = NULL)
robma_1$models
##################### Equivalent codes Set 2 ##################################
# -1.96, -1.65, 1.65 and 1.96
# Below functions essentially use two-sided p-value of 0.10 and 0.05 to set
# intervals for selection models. But only the positive effects are easier to
# be published.
## Fit the model with the publication bias (with one-sided)
data_ls_one2 <- make_standata(bf4, data=df4)
data_ls_one2$alpha <- c(.975, .95, 0.05, .025)
data_ls_one2$N_alpha <- length(data_ls_one2$alpha) # number of intervals
data_ls_one2$side <- 1
ex4_bias_one2 <- stan(file = 'stan_models/ma_bias_twosided.stan', # 'stan_bias.stan',
data = data_ls_one2,
chains = 6, cores = 6, seed = 12,
iter = 4000, warmup = 2000,
control = list(adapt_delta = .9))
MCMCsummary(ex4_bias_one2, params=c("b_Intercept", "omega", "cutoff_output"))
# pairs(ex4_bias_one, pars=c("omega", "b_Intercept"))
## with library(publipha)
psmafit2 <- psma(yi = df4$g, vi = df4$SE^2, alpha = c(0, 0.025, 0.05, .95, .975, 1),
chains = 6, cores = 6, seed = 12,
iter = 4000, warmup = 2000,
control = list(adapt_delta = .9))
MCMCsummary(psmafit2, params=c("theta0", "eta"))
## with Stan code modified from library(publipha)
psmafit_ls2 <- list(
N = data_ls_one2$N,
k = 4,
alpha = c(.975, .95, 0.05, 0.025),
yi = data_ls_one2$Y,
vi = data_ls_one2$se^2,
eta0 = c(1,1,1,1,1),
tau_prior = 1
)
# the following results should be the same as psmafit
psmafit2_stan <- stan(file = 'stan_models/psma_stan.stan', # 'stan_bias.stan',
data = psmafit_ls2,
iter = 4000, warmup = 2000,
chains = 6, cores = 6, seed = 12,
control = list(adapt_delta = .9))
MCMCsummary(psmafit2_stan, params=c("theta0", "eta", "cutoff_output"))
psmafit2_stan_gamma <- stan(file = 'stan_models/psma_stan_gamma_prior.stan', # 'stan_bias.stan',
data = psmafit_ls2,
iter = 4000, warmup = 2000,
chains = 6, cores = 6, seed = 12,
control = list(adapt_delta = .9))
MCMCsummary(psmafit2_stan_gamma, params=c("theta0", "eta", "cutoff_output"))
# Note: it seems that using gamma or dirichlet make differences.
## With library(RoMBA)
robma_2 <- RoBMA(d = df4$g, se = df4$SE,
priors_omega = list(prior(distribution = "one.sided",
parameters = list(alpha = c(1, 1, 1, 1, 1),
steps = c(.025, .05, .95, .975)),
prior_odds = 1)),
priors_mu_null = NULL,
priors_tau_null = NULL,
priors_omega_null = NULL)
robma_2$models