forked from goldingn/simulation_entomo_baci
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsim_baci_data.R
157 lines (120 loc) · 3.66 KB
/
sim_baci_data.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
# data simulation code
library(greta)
library(tidyverse)
#Parameters
intervention_percent_reduction <- 27
study_avg_wt <- 2000
inter_village_sd <- 0.3
inter_household_sd <- 0.3
inter_month_sd <- 0.3
n_villages <- 8
n_households_per_village <- 11
data_template <- expand_grid(
village = seq_len(n_villages),
household = seq_len(n_households_per_village),
month = c(1:3, 13:15),
weight = NA
) %>%
mutate(
before_after = case_when(
month %in% 1:3 ~ "before",
month %in% 13:15 ~ "after"
),
arm = case_when(
village <= round(n_villages / 2) ~ "control",
.default = "intervention"
),
.before = "weight"
) %>%
mutate(
# make unique combinations of households and villages
household_village_combination = paste(village,
household,
sep = "-"),
unique_household = match(household_village_combination,
unique(household_village_combination)),
.after = "household"
) %>%
select(
-household_village_combination
)
village_avg_wt_diffs <- normal(
mean = 0,
sd = inter_village_sd,
dim = n_villages
)
unique_household_avg_wt_diffs <- normal(
mean = 0,
sd = inter_household_sd,
dim = n_households_per_village*n_villages
)
n_months <- 6
# # this is the version that does not end with a probability distribution
#
# month_household_avg_wt_diffs <- normal(
# mean = 0,
# sd = inter_month_sd,
# dim = n_months*n_villages*n_households_per_village
# )
#
#
# nonintervention_log_weights <- log(study_avg_wt) +
# village_avg_wt_diffs[data_template$village] +
# unique_household_avg_wt_diffs[data_template$unique_household] +
# month_household_avg_wt_diffs
#
# sims_old_way <- calculate(nonintervention_log_weights[50],
# nsim = 10000)
# hist(sims_old_way$`nonintervention_log_weights[50]`,
# breaks = 100)
intervention_multiplier <- 1 - intervention_percent_reduction / 100
# log_intervention_multiplier <- log(intervention_multiplier)
# create a dummy variable for whether there is an effect of the intervention
intervention_in_place <- as.numeric(
data_template$before_after == "after" &
data_template$arm == "intervention"
)
intervention_multiplier_vec <- (1 - intervention_in_place) +
intervention_in_place * intervention_multiplier
log_intervention_multiplier_vec <- log(intervention_multiplier_vec)
# calculate(log_intervention_multiplier_vec,
# values = list(
# intervention_percent_reduction = 10))
# this is the version that ends with a probability distribution (so that we can
# define a likelihood over some data)
nonintervention_log_weights_household <- log(study_avg_wt) +
village_avg_wt_diffs[data_template$village] +
unique_household_avg_wt_diffs[data_template$unique_household]
intervention_log_weights_household <- nonintervention_log_weights_household +
log_intervention_multiplier_vec
log_weights_observations <- normal(
mean = intervention_log_weights_household,
sd = inter_month_sd
)
m <- model(log_weights_observations)
plot(m )
simulated_log_weights <- calculate(log_weights_observations, nsim = 1)
simulated_weights <- exp(unlist(simulated_log_weights))
simdat <- data_template |>
mutate(
weight = simulated_weights
)
simdat %>%
mutate(
intervened = arm == "intervention" & before_after == "after",
before_after = factor(before_after,
levels = c("before", "after"))
) %>%
ggplot(
aes(
x = weight,
fill = intervened
)
) +
geom_histogram() +
facet_grid(arm ~ before_after) +
coord_flip()
write_csv(
x = simdat,
file = "ento_baci_data.csv"
)