-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7 - model - presence.R
219 lines (180 loc) · 5.83 KB
/
7 - model - presence.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# Set up environment -----------------------------------------------------------
## Libraries
"lme4" %>=>% libInstall %=>% library(.., char = T)
"car" %>=>% libInstall %=>% library(.., char = T)
## Helper functions
"/crossvalidation.R" %=>% paste0(getwd(), ..) %=>% source
"/goftest.R" %=>% paste0(getwd(), ..) %=>% source
"/fidelity.R" %=>% paste0(getwd(), ..) %=>% source
"/prediction.R" %=>% paste0(getwd(), ..) %=>% source
"/animation.R" %=>% paste0(getwd(), ..) %=>% source
"/selection.R" %=>% paste0(getwd(), ..) %=>% source
"/simulation.R" %=>% paste0(getwd(), ..) %=>% source
"/threeD.R" %=>% paste0(getwd(), ..) %=>% source
## Message
message("***\n",
"The steps in this sripts are not automated. I tried several automated\n",
"ways, but they all had drawbacks. Go through starting model then factor\n",
"reduction. If some factors can be removed, go back, rinse and repeat.\n",
"***")
# Import data ------------------------------------------------------------------
## Load saved R objects
"/Data/data.rda" %=>% paste0(getwd(), ..) %=>% load(.., envir = globalenv())
"/Data/formula.rda" %=>% paste0(getwd(), ..) %=>% load(.., envir = globalenv())
## Change reference levels for genetype
dataBinom = within(dataBinom, (gene = relevel(gene, ref = "mut")))
tedataBinom = within(tedataBinom, (gene = relevel(gene, ref = "mut")))
# Actaul modeling --------------------------------------------------------------
bForm = presence ~ week + dis + gene +
Temp.60cm.mean + Humidity.mean + Rain.sum +
gene:week + gene:dis + gene:Humidity.mean +
gene:Temp.60cm.mean +
(1 | year / rep / id) + (1 | year : dir)
## Generate model
fit = glmer(
formula = bForm,
data = dataBinom,
family = binomial(link = "logit"),
control = glmerControl(optimizer = "bobyqa", optCtrl = list(maxfun = 2e5))
)
## Save the model as R object
c("fit") %=>%
save(list = .., file = paste0(getwd(), "/Data/model.rda"))
## Load the model for easy access
# "/Data/model.rda" %=>% paste0(getwd(), ..) %=>% load(.., envir = globalenv())
## Model summary
summary(fit)
## Factor reduction ------------------------------------------------------------
## Wald's Chi Sq
options(contrasts = c("contr.sum", "contr.poly"))
Anova(fit, type = 2)
Anova(fit, type = 3)
## Drop1 method
drop1(fit)
# Likelihood ratio test --------------------------------------------------------
modelSelection(fit, nullVar = "id")
##Godness of fit ---------------------------------------------------------------
gofTest(fit)
fidelity(fit) %=>%
ggsave(filename = paste0(getwd(), "/Output/fidelity.png"),
.., width = 10, device = "png", height = 8, units = "in", dpi = 300)
# Cross validation -------------------------------------------------------------
xVal(fit, extdata = tedataBinom)
# Ratio of velocity ------------------------------------------------------------
est = x ->> fixef(fit)[x]
rate = round(
((est("week") + est("week:genewt")) / est("week")) *
(est("dis") / (est("dis") + est("dis:genewt")))
, 2)
cat("XopJ2+ is", rate, "times faster than XopJ2-.")
# Prediction -------------------------------------------------------------------
## Tile output
expand.grid(
week = 0:12,
dis = 0:6,
gene = c("mut", "wt")
) %=>%
dispersal(
fit,
..,
spp = "X. perforans",
gene = "XopJ2"
) %=>%
ggsave(filename = paste0(getwd(), "/Output/prediction.pdf"),
.., width = 18, device = cairo_pdf, height = 8, units = "in", dpi = 300)
## Confidence intervals
expand.grid(
week = 0:12,
dis = 2,
gene = c("mut", "wt")
) %=>%
dispersalPlus(
fit,
..,
spp = "X. perforans",
gene = "XopJ2"
) %=>%
ggsave(filename = paste0(getwd(), "/Output/confint.pdf"),
.., width = 18, device = cairo_pdf, height = 8, units = "in", dpi = 300)
## Multi-line output
expand.grid(
week = 0:10,
dis = seq(2, 8, 2),
gene = c("mut", "wt")
) %=>%
dispersal2(
fit,
..,
spp = "X. perforans",
gene = "XopJ2"
) %=>%
ggsave(filename = paste0(getwd(), "/Output/revival.pdf"),
.., width = 12, device = cairo_pdf, height = 8, units = "in", dpi = 300)
## 3D surfaceplot
expand.grid(
week = 0:10,
dis = 0:6,
gene = c("wt","mut")
) %=>%
plot3d(
fit,
..,
pos = c("c", "m"),
spp = "X. perforans",
gene = "XopJ2"
)
# Animation --------------------------------------------------------------------
list(
grid = c(15, 15),
week = seq(0, 10.9, 0.1),
gene = c("mut", "wt")
) %=>%
animation(
fit,
..,
# out = "Output/animation.gif",
pos = c("c", "m"),
spp = "X. perforans",
gene = "XopJ2"
)
# Field area simulation --------------------------------------------------------
list(
grid = c(15, 15),
week = c(0, 3, 6, 9),
gene = c("mut", "wt")
) %=>%
animFrames(
fit,
..,
pos = c("c", "m"),
spp = "X. perforans",
gene = "XopJ2"
) %=>%
ggsave(filename = paste0(getwd(), "/Output/anim-frames.pdf"),
.., width = 15, device = cairo_pdf, height = 8, units = "in", dpi = 300)
# Distribution -----------------------------------------------------------------
list(
grid = c(15, 15),
week = c(0, 10),
gene = c("mut", "wt")
) %=>%
distribution(
fit,
..,
pos = c("c", "m"),
spp = "X. perforans",
gene = "XopJ2"
) %=>%
ggsave(filename = paste0(getwd(), "/Output/distribution.pdf"),
.., width = 5, device = cairo_pdf, height = 8, units = "in", dpi = 300)
# Simulation -------------------------------------------------------------------
simulField(
ne = 1e8,
p0 = 1e-7,
nrep = 100,
ngen = 14,
w = c(9.13, 1),
gene = "xopJ2"
) %!=>%
ggsave(filename = paste0(getwd(), "/Output/simulation.pdf"),
.., width = 15, device = cairo_pdf, height = 8, units = "in", dpi = 300)