-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
282 lines (197 loc) · 10.4 KB
/
README.Rmd
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
---
date: "`r format(Sys.time(), '%d/%m, %Y')`"
output:
bookdown::github_document2:
fig_caption: true
toc: true
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
message = F,
warning = F
)
```
# AICcPermanova
<!-- badges: start -->
<!-- badges: end -->
The goal of AICcPermanova is to evaluate the best models for plant communities and bacterial communities in Denmark in order to do that we require the following packages
```{r LoadPackages}
library(ampvis2)
library(readxl)
library(tidyverse)
library(AICcPermanova)
```
# Vegetation models
## Presence-absence data
### Generation of all possible models
After that we read in the datasets for environmental layers and generate all possible models to fit, in this case we will limit ourselves to only using at most one variable per ten observations, in this case that means up to 5 variables per model. The code for generating all possible models can be expanded bellow
<details style=\"margin-bottom:10px;\">
<summary>Model generator</summary>
```{r ModelGenerator, cache = T}
METADATAS <- list.files(pattern = "PERMANOVA_VEGETATION_", full.names = T) |>
purrr::map(read_excel) |>
purrr::reduce(full_join) |>
janitor::clean_names()
AllForms <- AICcPermanova::make_models(vars = c("p_h_water", "ec", "water_content", "wr", "ammonium", "nitrat_nitrit",
"oc_beregnet", "clay", "fine_silt", "coarse_silt_sand", "shannon_bak",
"finesilt_and_clay", "dexter_n")
, ncores = 21, k = floor(nrow(METADATAS)/10))
saveRDS(AllForms, "AllForms.rds")
openxlsx::write.xlsx(AllForms, "AllForms.xlsx")
```
</details>
This generate up to `r prettyNum(nrow(AllForms), big.mark = ",")` models to evaluate, which can be downloaded as an excel file [here](https://github.com/Sustainscapes/AICcPermanova/raw/master/AllForms.xlsx) an rds [here](https://github.com/Sustainscapes/AICcPermanova/blob/master/AllForms.rds).
## Avoiding colinearity
Now before the model fitting, we will filter out models with high degree of multicolinearity using VIF with 5 as a cut off
<details style=\"margin-bottom:10px;\">
<summary>VIF filter</summary>
```{r colinearityfilter3, cache =TRUE}
Filtered <- AICcPermanova::filter_vif(all_forms = AllForms , env_data = METADATAS, ncores = 21)
```
</details>
This is then narrowed down to `r prettyNum(nrow(Filtered), big.mark = ",")` models to evaluate
### Model fitting
Then in the following code each model is fitted and AICc is calculated to order it
<details style=\"margin-bottom:10px;\">
<summary>Model fitting code</summary>
```{r ModelFitting, cache = T}
vegetation_data = read_excel("Presence_absence_vegetation_AC_Danielsen.xlsx")%>%
janitor::clean_names()
vegetation_data_no_ID = subset(vegetation_data, select = -plot)
FS <- AICcPermanova::fit_models(all_forms = Filtered,
env_data = METADATAS,
veg_data = vegetation_data_no_ID,
ncores = 21,
method = "jaccard",
logfile = "new.txt",
strata = "area"
)
saveRDS(FS, "FS.rds")
openxlsx::write.xlsx(FS, "FS.xlsx")
```
</details>
As seen in table \@ref(tab:SummaryPlantPA) there are `r AICcPermanova::select_models(readRDS("FS.rds")) |> nrow()` models within 2 AICc where the max VIF is lower or equal than 6 of each other, you can see there how many times a variable has been selected
```{r SummaryPlantPA, echo = F}
GlobalSummary <- AICcPermanova::select_models(readRDS("FS.rds")) |> AICcPermanova::akaike_adjusted_rsq()
knitr::kable(GlobalSummary, digits = 3, caption = "Number of selected models were variables are present and their Akaike Weighted R squared for the Marginal effect of the terms", format.args = list(big.mark = ','))
```
Now we can see the top models that have a delta AICc within 2 of the best model in table \@ref(tab:BestPLantModels) if expanded
<details style=\"margin-bottom:10px;\">
<summary>Show table of selected models for vegetation presence absence</summary>
```{r BestPLantModels, echo=FALSE}
Fs <- readRDS("FS.rds")
Fs |> AICcPermanova::select_models() |>
knitr::kable(digits = 3, caption = "Best models for vegetation presence absence", format.args = list(big.mark = ','))
```
</details>
## Abundance data
Since the environmental variables are the same, we don´t need to do any of the filtering or model generation again, only the model fitting, based on the filtered data.frame
### Model fitting
Then in the following code each model is fitted and AICc is calculated to order it
<details style=\"margin-bottom:10px;\">
<summary>Model fitting code for vegetation abundance</summary>
```{r ModelFittingVegAbund, cache = T}
vegetation_data = read_excel("Pinpoint-data til ordination_minus_Lønstrup.xlsx")%>%
janitor::clean_names()
# Remocing the first column (ID) in vegetation-dataset and cheking there are no NAs
vegetation_data_no_ID = vegetation_data
Fs <- AICcPermanova::fit_models(all_forms = Filtered,
env_data = METADATAS,
veg_data = vegetation_data_no_ID,
ncores = 21,
method = "bray",
logfile = "PlantAbund.txt",
strata = "area"
)
saveRDS(Fs, "FSVegAbund.rds")
Fs <- Fs %>% arrange(AICc)
saveRDS(Fs, "FSVegAbund.rds")
openxlsx::write.xlsx(Fs, "FSVegAbund.xlsx")
```
</details>
As seen in table \@ref(tab:SummaryVegAbund) there are `r AICcPermanova::select_models(readRDS("FSVegAbund.rds")) |> nrow()` models within 2 AICc of each other, you can see there how many times a variable has been selected
```{r SummaryVegAbund, echo = F}
GlobalSummary <- AICcPermanova::select_models(readRDS("FSVegAbund.rds")) |> AICcPermanova::akaike_adjusted_rsq()
knitr::kable(GlobalSummary, digits = 3, caption = "Number of selected models were variables are present and their Akaike Weighted R squared for the Marginal effect of the terms", format.args = list(big.mark = ','))
```
Now we can see the top models that have a delta AICc within 2 of the best model in table \@ref(tab:BestVegAbundModels) if expanded
<details style=\"margin-bottom:10px;\">
<summary>Show table of selected models for vegetation presence absence</summary>
```{r BestVegAbundModels, echo=FALSE}
Fs <- readRDS("FSVegAbund.rds")
Fs |> AICcPermanova::select_models() |>
knitr::kable(digits = 3, caption = "Best models for vegetation abundance", format.args = list(big.mark = ','))
```
</details>
# Baterial models
## Abundance data
### Generation of all possible models
We read in the datasets for environmental layers and generate all possible models to fit, in this case we will limit ourselves to only using at most one variable per ten observations, in this case that means up to 5 variables per model. The code for generating all possible models can be expanded bellow
<details style=\"margin-bottom:10px;\">
<summary>Model generator bacterial abundance</summary>
```{r ModelGeneratorBacterialAbundance, cache = T}
METADATAS <- list.files(pattern = "PERMANOVA_BACTERIA_", full.names = T) |>
purrr::map(read_excel) |>
purrr::reduce(full_join) |>
janitor::clean_names()
AllForms <- AICcPermanova::make_models(vars = c("p_h_water", "ec",
"water_content", "wr", "ammonium", "nitrat_nitrit", "oc_beregnet",
"clay", "fine_silt", "coarse_silt_sand", "shannon_veg", "finesilt_and_clay",
"dexter_n")
, ncores = 21, k = floor(nrow(METADATAS)/10))
saveRDS(AllForms, "AllFormsBacterialAbund.rds")
openxlsx::write.xlsx(AllForms, "AllFormsBacterialAbund.xlsx")
```
</details>
This generate up to `r prettyNum(nrow(AllForms), big.mark = ",")` models to evaluate, which can be downloaded as an excel file [here](https://github.com/Sustainscapes/AICcPermanova/raw/master/AllFormsBacterialAbund.xlsx) an rds [here](https://github.com/Sustainscapes/AICcPermanova/blob/master/AllFormsBacterialAbund.rds).
## Avoiding colinearity
Now before the model fitting, we will filter out models with high degree of multicolinearity using VIF with 5 as a cut off
<details style=\"margin-bottom:10px;\">
<summary>VIF filter</summary>
```{r colinearityfilter2, cache =TRUE}
Filtered <- AICcPermanova::filter_vif(all_forms = AllForms , env_data = METADATAS, ncores = 21)
```
</details>
This is then narrowed down to `r prettyNum(nrow(Filtered), big.mark = ",")` models to evaluate
### Model fitting
Then in the following code each model is fitted and AICc is calculated to order it
<details style=\"margin-bottom:10px;\">
<summary>Model fitting code bacterial abund</summary>
```{r ModelFittingBacterialABund, cache = T}
d <- amp_load(
otutable = "AC_otutale_new.txt",
metadata = "Metadata_mix-samples_AC_Danielsen_final.xlsx")
bac.data.subset = amp_subset_taxa(d, "d__Bacteria")
bacterial_data = amp_subset_samples(d, Investigator == "AC")
bacterial_data = as.data.frame(t(bacterial_data$abund)) %>%
janitor::clean_names()
METADATAS <- METADATAS[match(rownames(bacterial_data), METADATAS$seq_id),]
Fs <- AICcPermanova::fit_models(all_forms = Filtered,
env_data = METADATAS,
veg_data = bacterial_data,
ncores = 21,
method = "bray",
logfile = "BactLog.txt",
strata = "area"
)
saveRDS(Fs, "FSBacterialAbund.rds")
openxlsx::write.xlsx(Fs, "FSBacterialAbund.xlsx")
```
</details>
As seen in table \@ref(tab:SummaryBacterialAbund) there are `r AICcPermanova::select_models(readRDS("FSBacterialAbund.rds")) |> nrow()` models within 2 AICc of each other, you can see there how many times a variable has been selected
```{r SummaryBacterialAbund, echo = F}
GlobalSummary <- AICcPermanova::select_models(readRDS("FSBacterialAbund.rds")) |> AICcPermanova::akaike_adjusted_rsq()
knitr::kable(GlobalSummary, digits = 3, caption = "Number of selected models were variables are present and their Akaike Weighted R squared for the Marginal effect of the terms", format.args = list(big.mark = ','))
```
Now we can see the top models that have a delta AICc within 2 of the best model in table \@ref(tab:BestBacterialAbundModels) if expanded
<details style=\"margin-bottom:10px;\">
<summary>Show table of selected models for vegetation presence absence</summary>
```{r BestBacterialAbundModels, echo=FALSE}
Fs <- readRDS("FSBacterialAbund.rds")
Fs |> AICcPermanova::select_models() |>
knitr::kable(digits = 3, caption = "Best models for bacterial abundance", format.args = list(big.mark = ','))
```
</details>