-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLF_03b_frequentist_occurrence.R
More file actions
executable file
·232 lines (201 loc) · 11.5 KB
/
Copy pathLF_03b_frequentist_occurrence.R
File metadata and controls
executable file
·232 lines (201 loc) · 11.5 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
#' ---
#' title: "LF_03b_frequentist_occurrence.R"
#' output: word_document
#' ---
#'
## DATA NEEDED:
# Data_ModelDF.rds - Model dataframe with 624696 obs of 24 variables, created in LF_01_data_handling.R
# Data_03b_PR_f_oc.rds - Taxonomy for the occurrence species, created in lines 439 - 450 of this script
# Set up
print("This is the frequentist occurrence model script")
setwd("~/landuse_climate_lifeform")
## create "not in" operator
'%nin%' = Negate('%in%')
#'
## ----setup, include=FALSE---------------------------------------------------------------------------------------------------------------
#install.packages(c("tidyverse", "lme4","optimx", "glmmTMB", "MuMIn", "effects"))
library(tidyverse)
library(lme4)
library(optimx)
library(glmmTMB)
library(wec)
## create "not in" operator
'%nin%' = Negate('%in%')
## create logit transformation function
logitTransform <- function(x) { log(x/(1-x)) }
## read in and handle data------------------------------------
if(!exists("ModelDF")) {
if(file.exists("Data_ModelDF.rds")) {
try(ModelDF <- readRDS("Data_ModelDF.rds")) }
else source("LF_01_data_handling.R")
} ## 02/09/2020 624696 obs of 24 vars, unique, continuous vars are scaled
## handle model dataframe to get species levels in the right format
levels(ModelDF$Best_guess_binomial) <- gsub(" ", "_", levels(ModelDF$Best_guess_binomial))
mydata <- ModelDF
mydata$animal <- mydata$Best_guess_binomial
## get taxomonic data for all species
PR_oc <- readRDS("Data_03b_PR_f_oc.rds")
mydata <- droplevels(merge(mydata, PR_oc, by = "Best_guess_binomial",all.x = TRUE))
## ----oc_null----------------------------------------------------------------------------------------------------------------------------
# oc_null <- glmmTMB(pres_abs ~ 1 +
# (1|Best_guess_binomial) +
# (1|SS) +
# (1|SSS) +
# (1|SSBS),
# ziformula= ~ Predominant_habitat + raunk_lf,
# family = binomial,
# control = glmmTMBControl(optCtrl = list(iter.max = 1000, eval.max = 1000),
# profile = FALSE, collect = FALSE),
# data = mydata)
#
# if(exists("oc_null")) {
# try(saveRDS(oc_null, "f_oc_null.rds"))
# } else warning("oc_null failed to run")
#' ## ----oc_maximal--------------------------------------------------------------------------------------------
# oc_maximal_zi <- glmmTMB(pres_abs ~ Predominant_habitat*raunk_lf +
# Species_richness +
# map +
# map_var +
# mat +
# mat_var +
# Species_richness:raunk_lf +
# map_var:raunk_lf +
# map:raunk_lf +
# mat_var:raunk_lf +
# mat:raunk_lf +
# (1|Best_guess_binomial) +
# (1|SS), # +
# # (1|SSS) +
# # (1|SSBS),
# ziformula= ~ Predominant_habitat + raunk_lf,
# family = binomial,
# control = glmmTMBControl(optCtrl = list(iter.max = 10000, eval.max = 10000),
# profile = FALSE, collect = FALSE),
# data = mydata)
#
# if(exists("oc_maximal_zi")) {
# try(saveRDS(oc_maximal_zi, "f_oc_maximal_zi.rds"))
# } else warning("oc_maximal_zi failed to run")
## ----oc_maximal_wec--------------------------------------------------------------------------------------------
## set up for weighted effects coding
print("configure contrasts for model a")
## main effects
mydata$Predominant_habitat.wec <- factor(mydata$Predominant_habitat)
contrasts(mydata$Predominant_habitat.wec) <- contr.wec(mydata$Predominant_habitat, "Urban")
mydata$raunk_lf.wec <- factor(mydata$raunk_lf)
contrasts(mydata$raunk_lf.wec) <- contr.wec(mydata$raunk_lf, "therophyte")
## interactions
mydata$hab_raunk_interaction <- mydata$Predominant_habitat
mydata$hab_raunk_interaction <- wec.interact(mydata$Predominant_habitat.wec, mydata$raunk_lf.wec)
mydata$map_raunk_interaction <- wec.interact(mydata$raunk_lf.wec, mydata$map)
mydata$map_var_raunk_interaction <- wec.interact(mydata$raunk_lf.wec, mydata$map_var)
mydata$mat_raunk_interaction <- wec.interact(mydata$raunk_lf.wec, mydata$mat)
mydata$mat_var_raunk_interaction <- wec.interact(mydata$raunk_lf.wec, mydata$mat_var)
mydata$spp_raunk_interaction <- wec.interact(mydata$raunk_lf.wec, mydata$Species_richness)
print("start running model a")
oc_wec_int_maximal_zi_1_nested_no_U_T <- glmmTMB(pres_abs ~ Predominant_habitat.wec + raunk_lf.wec + hab_raunk_interaction +
Species_richness +
map +
map_var +
mat +
mat_var +
spp_raunk_interaction +
map_raunk_interaction +
map_var_raunk_interaction +
mat_raunk_interaction +
mat_var_raunk_interaction +
(1|Best_guess_binomial) +
(1|SS) +
(1|Class/Order/Family/Genus),
ziformula= ~ 1,
family = binomial,
control = glmmTMBControl(optCtrl = list(iter.max = 10000, eval.max = 10000),
profile = FALSE, collect = FALSE),
data = mydata)
if(exists("oc_wec_int_maximal_zi_1_nested_no_U_T")) {
try(saveRDS(oc_wec_int_maximal_zi_1_nested_no_U_T, "oc_wec_int_maximal_zi_1_nested_no_U_T.rds"))
} else warning("oc_wec_int_maximal_zi_1_nested_no_U_T failed to run")
print("ran model omitting Urban or therophyte, now running model omitting Primary forest and phanerophte")
print("configure contrasts for model b")
## main effects
mydata$Predominant_habitat.wec <- factor(mydata$Predominant_habitat)
contrasts(mydata$Predominant_habitat.wec) <- contr.wec(mydata$Predominant_habitat, "Primary forest")
mydata$raunk_lf.wec <- factor(mydata$raunk_lf)
contrasts(mydata$raunk_lf.wec) <- contr.wec(mydata$raunk_lf, "phanerophyte")
## interactions
mydata$hab_raunk_interaction <- mydata$Predominant_habitat
mydata$hab_raunk_interaction <- wec.interact(mydata$Predominant_habitat.wec, mydata$raunk_lf.wec)
mydata$map_raunk_interaction <- wec.interact(mydata$raunk_lf.wec, mydata$map)
mydata$map_var_raunk_interaction <- wec.interact(mydata$raunk_lf.wec, mydata$map_var)
mydata$mat_raunk_interaction <- wec.interact(mydata$raunk_lf.wec, mydata$mat)
mydata$mat_var_raunk_interaction <- wec.interact(mydata$raunk_lf.wec, mydata$mat_var)
mydata$spp_raunk_interaction <- wec.interact(mydata$raunk_lf.wec, mydata$Species_richness)
print("start running model b")
oc_wec_int_maximal_zi_1_nested_no_PF_P <- glmmTMB(pres_abs ~ Predominant_habitat.wec + raunk_lf.wec + hab_raunk_interaction +
Species_richness +
map +
map_var +
mat +
mat_var +
spp_raunk_interaction +
map_raunk_interaction +
map_var_raunk_interaction +
mat_raunk_interaction +
mat_var_raunk_interaction +
(1|Best_guess_binomial) +
(1|SS) +
(1|Class/Order/Family/Genus),
ziformula= ~ 1,
family = binomial,
control = glmmTMBControl(optCtrl = list(iter.max = 10000, eval.max = 10000),
profile = FALSE, collect = FALSE),
data = mydata)
if(exists("oc_wec_int_maximal_zi_1_nested_no_PF_P")) {
try(saveRDS(oc_wec_int_maximal_zi_1_nested_no_PF_P, "oc_wec_int_maximal_zi_1_nested_no_PF_P.rds"))
} else warning("oc_wec_int_maximal_zi_1_nested_no_PF_P failed to run")
print("ran model omitting Primary forest and phanerophte, now running model omitting Pasture and cryptophyte")
print("configure contrasts for model c")
## main effects
mydata$Predominant_habitat.wec <- factor(mydata$Predominant_habitat)
contrasts(mydata$Predominant_habitat.wec) <- contr.wec(mydata$Predominant_habitat, "Pasture")
mydata$raunk_lf.wec <- factor(mydata$raunk_lf)
contrasts(mydata$raunk_lf.wec) <- contr.wec(mydata$raunk_lf, "cryptophyte")
## interactions
mydata$hab_raunk_interaction <- mydata$Predominant_habitat
mydata$hab_raunk_interaction <- wec.interact(mydata$Predominant_habitat.wec, mydata$raunk_lf.wec)
mydata$map_raunk_interaction <- wec.interact(mydata$raunk_lf.wec, mydata$map)
mydata$map_var_raunk_interaction <- wec.interact(mydata$raunk_lf.wec, mydata$map_var)
mydata$mat_raunk_interaction <- wec.interact(mydata$raunk_lf.wec, mydata$mat)
mydata$mat_var_raunk_interaction <- wec.interact(mydata$raunk_lf.wec, mydata$mat_var)
mydata$spp_raunk_interaction <- wec.interact(mydata$raunk_lf.wec, mydata$Species_richness)
print("start running model c")
oc_wec_int_maximal_zi_1_nested_no_P_C <- glmmTMB(pres_abs ~ Predominant_habitat.wec + raunk_lf.wec + hab_raunk_interaction +
Species_richness +
map +
map_var +
mat +
mat_var +
spp_raunk_interaction +
map_raunk_interaction +
map_var_raunk_interaction +
mat_raunk_interaction +
mat_var_raunk_interaction +
(1|Best_guess_binomial) +
(1|SS) +
(1|Class/Order/Family/Genus),
ziformula= ~ 1,
family = binomial,
control = glmmTMBControl(optCtrl = list(iter.max = 10000, eval.max = 10000),
profile = FALSE, collect = FALSE),
data = mydata)
if(exists("oc_wec_int_maximal_zi_1_nested_no_P_C")) {
try(saveRDS(oc_wec_int_maximal_zi_1_nested_no_P_C, "oc_wec_int_maximal_zi_1_nested_no_P_C.rds"))
} else warning("oc_wec_int_maximal_zi_1_nested_no_P_C failed to run")
print("end")
#'
#'
#'
#'
#'
#'
#'