-
Notifications
You must be signed in to change notification settings - Fork 3
/
4_tables.R
219 lines (145 loc) · 6.92 KB
/
4_tables.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
# Generate tables for Extended Data
library(dplyr)
library(flextable)
library(officer)
# Define location of input and output -------------------------------------
# if not stated otherwise, load and save from/in repo folders
# Data prep input
if (!exists("path_input")) path_input = "data_prep/input/"
# Data prep output
if (!exists("path_output")) path_output = "data_prep/output/"
# Analysis outputs
if (!exists("path_mortality")) path_mortality = "out/mortality_models/"
if (!exists("path_meta")) path_meta = "out/meta_models/"
# Tables
if (!exists("path_tables")) path_tables = "out/tables/"
# create folder for tables
dir.create(path_tables)
# Load data ---------------------------------------------------------------
# select run
run = "main"
# create folder
dir.create(paste0(path_tables, run))
# mortality modeling results
load(paste0(path_mortality, run, "/global_mortality.Rdata"))
# sites
sites = sort(unique(sums_global$site))
# auxiliary information
Site_table = readxl::read_xlsx(paste0(path_input, "global_metainfo/plot_sites_information.xlsx"), sheet = 1)
Site_table = Site_table %>%
dplyr::filter(ID %in% sites)
# Extended Data Table 1 ---------------------------------------------------
# Variables:
# Site name, latitude, longitude, mean temperature, mean precipitation,
# plot size, number of censuses, census years
# generated based on Site_table
# with additional info from
# _tree_1_metainfo.Rdata (censuses)
# select columns
ext_data_table_1 = Site_table[, c("site", "lat", "long", "mat", "map", "size_ha", "ID")]
ext_data_table_1$size_ha[ext_data_table_1$ID == "ucsc"] = 6 # subset was used
# sort by full site name
ext_data_table_1 = ext_data_table_1[order(ext_data_table_1$site), ]
# round lat and long
ext_data_table_1$lat = format(round(ext_data_table_1$lat, digits=2), nsmall = 2)
ext_data_table_1$long = format(round(ext_data_table_1$long, digits=2), nsmall = 2)
# add number of censuses and census years from _tree_1_metainfo.Rdata
ext_data_table_1$n_censuses = NA
ext_data_table_1$census_years = NA
for (site in sites) {
load(paste0(path_output, "data_1_metainfo/", site, "_tree_1_metainfo.Rdata"))
ext_data_table_1$n_censuses[ext_data_table_1$ID == site] = length(tree)
get_year = function(x) names(sort(table(format(x$date, format="%Y")), decreasing = T))[1] # most common name per census
ext_data_table_1$census_years[ext_data_table_1$ID == site] = paste(unlist(lapply(tree, get_year)), collapse = ", ")
}
ext_data_table_1 = ext_data_table_1 %>%
select(-ID)
# Formatting
ext_data_table_1 %>%
flextable() %>%
set_table_properties(layout = "autofit") %>%
height_all(height = .21, part = "all") %>%
hrule(rule = "exact") %>%
font(fontname = "Arial", part = "all") %>%
fontsize(part = "all", size = 8) %>%
set_header_labels(site = "Site",
lat = "Latitude (°)",
long = "Longitude (°)",
mat = "Mean temperature (°C)",
map = "Mean precipitation (mm/yr)",
size_ha = "Plot size (ha)",
n_censuses = "N censuses",
census_years = "Census years") %>%
theme_booktabs() -> var
template <- system.file(package = "officer",
"doc_examples", "landscape.docx")
doc_1 <- read_docx(path = template)
doc_1 %>%
body_add_flextable(var) -> my_doc
print(my_doc, target = paste0(path_tables, run, "/extended_data_table_1.docx")) %>% invisible()
# Extended Data Table 2 ---------------------------------------------------
# Variables:
# number of observations, number of species, number of species in species groups (shrubs/trees).
# generated based on nsp_global
# with additional info from
# _tree_1_metainfo.Rdata (number of recorded trees)
# rAME_global (rare species group convergence)
# add number of recorded trees from _tree_1_metainfo.Rdata (full list of tree censuses)
# nsp_global$n_trees = NA
# for (site in sites) {
# load(paste0(path_output, "data_1_metainfo/", site, "_tree_1_metainfo.Rdata"))
# nsp_global$n_trees[nsp_global$site == site] = nrow(tree[[1]]) # take first census but all have the same number of entries
# }
# calculate various n
nsp_global %>%
mutate(nobs = ndead + nsurv) %>%
group_by(site) %>%
summarise(# n_trees = unique(n_trees),
n_mortality_observations = sum(nobs),
n_species = n(),
perc_species_fitted_individually = round(100*sum(!rare)/n_species, 1),
n_species_fitted_as_rare_trees = sum(rare_stature == "Rare_tree", na.rm = T),
n_species_fitted_as_rare_shrubs = sum(rare_stature == "Rare_shrub", na.rm = T),
perc_mortality = round(100*sum(ndead)/sum(nobs), 1)) -> ext_data_table_2
# replace n_rare with 0 if model not converged
rare_convergence = rAME_global[grepl("Rare", rAME_global$sp) & rAME_global$change == "equilibrium", c("site", "sp")]
for (site in sites) {
groups = c("Rare_shrub", "Rare_tree")
not_converged = groups[!groups %in% rare_convergence$sp[rare_convergence$site == site]]
if (any(not_converged == "Rare_shrub")) ext_data_table_2$n_species_fitted_as_rare_shrubs[ext_data_table_2$site == site] = 0
if (any(not_converged == "Rare_tree")) ext_data_table_2$n_species_fitted_as_rare_trees[ext_data_table_2$site == site] = 0
}
# replace with full site name
ext_data_table_2$site = Site_table$site[match(ext_data_table_2$site, Site_table$ID)]
ext_data_table_2 = ext_data_table_2[order(ext_data_table_2$site), ]
# Formatting
ext_data_table_2 %>%
flextable() %>%
set_table_properties(layout = "autofit") %>%
height_all(height = .21, part = "all") %>%
hrule(rule = "exact") %>%
font(fontname = "Arial", part = "all") %>%
fontsize(part = "all", size = 8) %>%
set_header_labels(site = "Site",
# n_trees = "N trees on entire plot",
n_mortality_observations = "N status observations",
n_species = "N species for mortality analyses",
perc_species_fitted_individually = "% species fitted individually",
n_species_fitted_as_rare_trees = "N species fitted as rare trees",
n_species_fitted_as_rare_shrubs = "N species fitted as rare shrubs",
perc_mortality = "% dead status observations") %>%
theme_booktabs() -> var
template <- system.file(package = "officer",
"doc_examples", "landscape.docx")
doc_2 <- read_docx(path = template)
doc_2 %>%
body_add_flextable(var) -> my_doc
print(my_doc, target = paste0(path_tables, run, "/extended_data_table_2.docx")) %>% invisible()
# For text ----------------------------------------------------------------
# global numbers (for text)
nsp_global %>%
mutate(nobs = ndead + nsurv) %>%
summarise(n_mortality_observations = sum(nobs),
n_species = n(),
perc_species_fitted_individually = round(100-100*sum(!rare)/n_species, 1),
perc_mortality = round(100*sum(ndead)/sum(nobs), 1))