-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunModels.R
139 lines (97 loc) · 3.62 KB
/
runModels.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
# Script 4: runModels.R
# Models for Pandemic State Space Model
# Predicting pest presence and introductions using the data provided to the
# [PoPS Global model of pest and pathogen spread](https://github.com/ncsu-landscape-dynamics/PoPS-Global)
# The "regression_data.csv" is generated using the [Statistical Model notebook]
# (https://github.com/arielsaffer/PoPS-Global-Ag-CaseStudy/blob/master/exploration/StatisticalModel.ipynb)
# You should have already run: workspace.R, data.R, models.R
# Code adapted from [Ecological Forecasting](https://github.com/EcoForecast/EF_Activities)
dev.off() # Reset plot space
palette("default")
# Which model do you want to run?
model = Binomial_Pandemic
# Options defined in models.R, e.g. Binomial_Pandemic, Temporal_Pandemic,
# Temporal_Pandemic_IntX, StateSpace_Pandemic, your own!
# Did you pick a static or dynamic model?
model_type = "static" # "static" or "dynamic"
nchain = 3
nruns = 5000
# 5000 - 15000 depending on complexity/convergence
# Select data
if (model_type =="static"){
dat = dat_static
} else if (model_type=="dynamic"){
dat = dat_dynamic
}
if (model == Verbatim_Pandemic) {
dat$pcap_orig <- mean(static_data[static_data$Origin==1|static_data$Destination==1,"P_Cap"])
dat$pcap_dest <- static_data$P_Cap
dat$sig_host <- sd(1-static_data$Host_Area)
dat$sig_clim <- sd(1-static_data$Climate_Max)
dat$beta <- 0.5
}
## Run model
j.model <- jags.model (file = textConnection(model),
data = dat,
# inits = inits, # Optional
n.chains = nchain)
## Sample output
if (model == Verbatim_Pandemic) {
parameters = c("alpha","lamda")
} else if (model == Binomial_Dist_Pandemic | model == StateSpace_Dist_Pandemic) {
parameters = c("b","a", "c")
} else{
parameters = c("d","b","a")
}
# Note: if you add model parameters, you may need to trace
# different variables
jags.out <- coda.samples (model = j.model,
variable.names = parameters,
n.iter = nruns)
## Plot: Review this plot for even mixing between the chains and
## parameter convergence (e.g. not multi-modal distributions)
# plot(jags.out)
## Plot: Review this plot to determine amount of burnout
## After how many samples is the diagnostic consistently under 1.05?
# BGR <- gelman.plot(jags.out)
## Set and remove burn-in
burnin = 2000
# 2000 is usually safe, but eyes are needed
# Could be 1500 - 5000
jags.burn <- window(jags.out,start=burnin)
## Review the summary of parameter values
summary <- summary(jags.burn)
print(summary)
## Convert to matrix
var.mat <- as.matrix(jags.burn)
## Review pairwise scatter plots & correlation
# pairs(var.mat)
#You might not want to see this if it's very big
## Sample output
cor(var.mat)
# Look at the credible intervals of the parameter estimates
# All parameters
MCMCplot(var.mat,
params = parameters,
ci = c(50, 90))
# In static model (*need to generalize to all models)
# Just trade
MCMCplot(var.mat,
params = "d",
ci = c(50, 90))
# Climate, host, and trade
MCMCplot(var.mat,
params = c("b","d"),
ci = c(50, 90))
# If everything worked okay here, you can go to...
# Next: simResults.R
# Additional plotting option: full distributions
data.frame(var.mat[,grepl("d",colnames(var.mat))]) %>%
gather(key="param", value="value") %>%
ggplot(aes(y = param, x = value)) +
stat_halfeye(.width = c(.90, .5)) +
theme_bw() +
xlim(0,22) +
ggtitle("Trade parameters") +
labs(y= "", x = "Regression Parameter Estimates") +
scale_y_discrete(labels=c(names(dat$origin),names(dat$bridge)))