-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_generator.R
484 lines (397 loc) · 15.6 KB
/
matrix_generator.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# load packages -----------------------------------------------------------
library(tidyverse)
library(here)
library(popbio)
# load data ---------------------------------------------------------------
# Pull Survey Data from the `HeliconiaSurveys` repository
urlfile_plants<-("https://raw.githubusercontent.com/BrunaLab/HeliconiaSurveys/master/data/survey_archive/HDP_survey.csv")
ha_plants<-read_csv(url(urlfile_plants))
# write_csv(ha_plants, here("manuscript_files",
# "MetadataS1", "data_downloaded","ha_plants.csv"))
urlfile_plots<-"https://raw.githubusercontent.com/BrunaLab/HeliconiaSurveys/master/data/survey_archive/HDP_plots.csv"
ha_plots<-read_csv(url(urlfile_plots))
# write_csv(ha_plots,here("manuscript_files",
# "MetadataS1", "data_downloaded","ha_plots.csv"))
# This is to save the info on the version of the data sets used in the paper
urlfile_v_plots<-"https://raw.githubusercontent.com/BrunaLab/HeliconiaSurveys/master/data/survey_archive/HDP_plots_version_info.txt"
version_plots<-read_lines(url(urlfile_v_plots))
urlfile_v_survey<-"https://raw.githubusercontent.com/BrunaLab/HeliconiaSurveys/master/data/survey_archive/HDP_survey_version_info.txt"
version_survey<-read_lines(url(urlfile_v_survey))
version_survey$file<-"survey"
names(version_survey)<-c("version", "date", "file")
# version_plots$file<-"plots"
# names(version_plots)<-c("version", "date", "file")
# versions_used<-bind_rows(as_tibble(version_survey),
# as_tibble(version_plots)) %>%
# relocate(file,.before=1)
# write_csv(versions_used,here("manuscript_files",
# "MetadataS1", "data_downloaded","versions_for_ms.csv"))
# Pull Data on Flowering from the `Hacuminata_leaf_shoot_height_data` repository
# these data were for a pilot study on self-fertilization and are useful records of number fof flowers, fruits, etc.
# urlfile_reprodata2<-paste0("https://raw.githubusercontent.com/BrunaLab/Hacuminata_leaf_shoot_height_data/master/data_clean/ha_size_data_1998_cor.csv")
# ha_repro2<-read_csv(url(urlfile_reprodata2))
# create transition matrices ----------------------------------------------
# ha_plants_wide<-ha_plants %>% select(-treefall_status) %>%
# pivot_wider(names_from = year, values_from = c(shts:tag_number))
# Put the data in a format from which you can create transition matrices
matrix_data_compiler <- function(ha_plants) {
matrix_data<-tibble(plot_id=ha_plants$plot_id,
plant_id=ha_plants$plant_id,
year=ha_plants$year,
sdlg=ha_plants$recorded_sdlg,
stage=ha_plants$shts,
infl=ha_plants$infl,
yr2 = lead(ha_plants$year),
fate = lead(ha_plants$shts),
status=lead(ha_plants$census_status)) %>%
arrange(desc(sdlg),plot_id,year,stage,fate,status)
matrix_data
# reduce to 7 post-seedling size classes
matrix_data<-matrix_data %>%
mutate(fate = case_when(
fate > 6 ~ 7,
# is.na(stage) ~ "NA",
.default = as.numeric(fate))) %>%
mutate(stage = case_when(
stage > 6 ~ 7,
# is.na(stage) ~ "NA",
.default = as.numeric(stage))) %>%
mutate(stage = as.character(stage),
sdlg = as.character(sdlg)) %>%
mutate(stage = case_when(
sdlg == "TRUE" ~ "sdlg",
# is.na(stage) ~ "NA",
.default = as.character(stage))) %>%
mutate(fate = case_when(
status == "dead" ~ "dead",
# is.na(stage) ~ "NA",
.default = as.character(fate))) %>%
mutate(infl=replace_na(infl,0))
return(matrix_data)
}
matrix_data<-matrix_data_compiler(ha_plants)
# calculate the Fertility matrix ------------------------------------------
# OVERALL: all plots and years combined ----------------------------------------
# How many infloresences in each plot in each year?
infl<-matrix_data %>% tally(infl)%>% rename(tot_infl=n)
infl$plot_id<-"all combined"
# how many seedlings appear in each plot in each year?
sdlg<-matrix_data %>% tally(sdlg==TRUE) %>% rename(tot_sdlgs=n)
sdlg$plot_id<-"all combined"
# The number of seedlings is based on the no. of infl. in the PREVIOUS year
repro<-left_join(infl,sdlg) %>%
mutate(tot_sdlgs=lead(tot_sdlgs)) %>%
mutate(sds_per_infl=tot_sdlgs/tot_infl) %>%
mutate(sds_per_infl=round(sds_per_infl,2)) %>%
ungroup()
# %>%
# select(year,plot_id,sds_per_infl)
str(repro)
# There are some plots x years with no seedlings or no flowering plants,
# resulting in NaN or InF in seedlings per infl. Replace those values with zero
repro<-repro %>% replace_na(list(sds_per_infl=0)) %>%
mutate(sds_per_infl=case_when(
is.nan(sds_per_infl)~0,
.default = as.numeric(sds_per_infl))) %>%
mutate(sds_per_infl=case_when(
is.infinite(sds_per_infl)~0,
.default = as.numeric(sds_per_infl)))
# Add the reproductive data to the matrix data and muliply number of infl
# by the number of seedlings per infl. to get value for how many
# seedlings in subsequent year
matrix_data$plot_id<-"all combined"
matrix_data<-left_join(matrix_data,repro) %>%
relocate(sds_per_infl,.after=infl) %>%
mutate(seedling=infl*sds_per_infl) %>%
relocate(seedling,.after=sds_per_infl)
# Create a transition matrix for each plot
plots<-sort(unique(matrix_data$plot_id))
years<-sort(unique(matrix_data$year))
trans01<-matrix_data %>%
filter(stage!=0) %>%
filter(fate!=0) %>%
select(plot_id,
plant_id,
year,
stage,
infl,
fate,
seedling) %>%
mutate(stage=as.factor(stage)) %>%
mutate(stage=fct_relevel(stage,"sdlg","1","2","3","4","5","6","7")) %>%
mutate(fate=as.factor(fate)) %>%
mutate(fate=fct_relevel(fate,"1","2","3","4","5","6","7","dead")) %>%
mutate(seedling=replace_na(seedling,0))
levels(trans01$stage)
levels(trans01$fate)
tf <- table(trans01$fate, trans01$stage)
T.mat_all <- prop.table(tf, 2)
T.mat<-T.mat_all[1:7,]
F.mat <- T.mat * 0
F.mat[1, ] <- tapply(trans01$seedling, trans01$stage, mean)
T.mat<-rbind(F.mat[1, ],T.mat)
rownames(T.mat)<-c("sdlg","1","2","3","4","5","6","7")
# stages <- unique(matrix_data$stage)
# fates <- unique(matrix_data$fate)
n <- c(5,24,23,21,13,5,2,3)
p <- pop.projection(T.mat, n, 50)
p$lambda
eigT.mat <- eigen.analysis(T.mat)
eigT.mat$lambda1
fundamental.matrix(T.mat)
eta<-fundamental.matrix(T.mat)
eta$N
eta_overall <- eta$meaneta
names(eta_overall)<-c("sdlg","shts_1","shts_2","shts_3","shts_4","shts_5","shts_6","shts_7")
eta_overall<-as.data.frame(eta_overall)
eta_overall$stage <- rownames(eta_overall)
eta_var_overall <- eta$vareta
names(eta_var_overall)<-c("sdlg","shts_1","shts_2","shts_3","shts_4","shts_5","shts_6","shts_7")
eta_var_overall<-as.data.frame(eta_var_overall)
eta_var_overall$stage <- rownames(eta_var_overall)
write_csv(eta_overall, "./output/eta_overall.csv")
write_csv(eta_var_overall, "./output/eta_var_overall.csv")
# eta
# var_eta
# eta_long<-pivot_longer(eta,sdlg:shts_7,names_to = "category") %>% rename(mean_years=value)
# eta_var_long<-pivot_longer(var_eta,sdlg:shts_7,names_to = "category") %>% rename(mean_years=value)
# plot-level T, all years combined ----------------------------------------
matrix_data<-matrix_data_compiler(ha_plants)
# How many infloresences in each plot in each year?
infl<-matrix_data %>% group_by(plot_id) %>% tally(infl)%>% rename(tot_infl=n)
# how many seedlings appear in each plot in each year?
sdlg<-matrix_data %>% group_by(plot_id) %>% tally(sdlg==TRUE) %>% rename(tot_sdlgs=n)
# The number of seedlings is based on the no. of infl. in the PREVIOUS year
repro<-left_join(infl,sdlg) %>%
mutate(tot_sdlgs=lead(tot_sdlgs)) %>%
mutate(sds_per_infl=tot_sdlgs/tot_infl) %>%
mutate(sds_per_infl=round(sds_per_infl,2)) %>%
ungroup()
# %>%
# select(year,plot_id,sds_per_infl)
str(repro)
# There are some plots x years with no seedlings or no flowering plants,
# resulting in NaN or InF in seedlings per infl. Replace those values with zero
repro<-repro %>% replace_na(list(sds_per_infl=0)) %>%
mutate(sds_per_infl=case_when(
is.nan(sds_per_infl)~0,
.default = as.numeric(sds_per_infl))) %>%
mutate(sds_per_infl=case_when(
is.infinite(sds_per_infl)~0,
.default = as.numeric(sds_per_infl)))
# Add the reproductive data to the matrix data and muliply number of infl
# by the number of seedlings per infl. to get value for how many
# seedlings in subsequent year
matrix_data<-left_join(matrix_data,repro) %>%
relocate(sds_per_infl,.after=infl) %>%
mutate(seedling=infl*sds_per_infl) %>%
relocate(seedling,.after=sds_per_infl)
# Create a transition matrix for each plot
plots<-sort(unique(matrix_data$plot_id))
years<-sort(unique(matrix_data$year))
data1 <- vector("list", nrow(as.data.frame(plots)))
data2 <- vector("list", nrow(as.data.frame(plots)))
for (i in seq_along(plots)) {
trans01<-matrix_data %>%
filter(plot_id==plots[i]) %>%
filter(stage!=0) %>%
filter(fate!=0) %>%
select(plot_id,
plant_id,year,
stage,
infl,
fate,
seedling) %>%
mutate(stage=as.factor(stage)) %>%
mutate(stage=fct_relevel(stage,"sdlg","1","2","3","4","5","6","7")) %>%
mutate(fate=as.factor(fate)) %>%
mutate(fate=fct_relevel(fate,"1","2","3","4","5","6","7","dead")) %>%
mutate(seedling=replace_na(seedling,0))
levels(trans01$stage)
levels(trans01$fate)
tf <- table(trans01$fate, trans01$stage)
T.mat_all <- prop.table(tf, 2)
T.mat<-T.mat_all[1:7,]
F.mat <- T.mat * 0
F.mat[1, ] <- tapply(trans01$seedling, trans01$stage, mean)
T.mat<-rbind(F.mat[1, ],T.mat)
rownames(T.mat)<-c("sdlg","1","2","3","4","5","6","7")
# stages <- unique(matrix_data$stage)
# fates <- unique(matrix_data$fate)
n <- c(5,24,23,21,13,5,2,3)
p <- pop.projection(T.mat, n, 1000)
p$lambda
eigT.mat <- eigen.analysis(T.mat)
eigT.mat$lambda1
fundamental.matrix(T.mat)
eta<-fundamental.matrix(T.mat)
eta$N
eta$meaneta
data1[i]<-as_tibble(eta$meaneta)
data2[i]<-as_tibble(eta$vareta)
}
plot_eta <- data.frame(t(sapply(data1,c)))
names(plot_eta)<-c("sdlg","shts_1","shts_2","shts_3","shts_4","shts_5","shts_6","shts_7")
plots<-as_tibble(plots) %>% rename(plot=value)
plot_eta<-bind_cols(plots,plot_eta)
plot_eta_var <- data.frame(t(sapply(data2,c)))
names(plot_eta_var)<-c("sdlg","shts_1","shts_2","shts_3","shts_4","shts_5","shts_6","shts_7")
plot_eta_var<-bind_cols(plots,plot_eta_var)
write_csv(plot_eta, "./output/plot_eta.csv")
write_csv(plot_eta_var, "./output/plot_eta_var.csv")
# eta
# var_eta
# eta_long<-pivot_longer(eta,sdlg:shts_7,names_to = "category") %>% rename(mean_years=value)
# eta_var_long<-pivot_longer(var_eta,sdlg:shts_7,names_to = "category") %>% rename(mean_years=value)
# # per-year per plot T ----------------------------------------
# matrix_data<-matrix_data_compiler(ha_plants)
# # How many infloresences in each year?
# infl<-matrix_data %>% group_by(year, plot_id) %>% tally(infl)%>% rename(tot_infl=n)
#
# # how many seedlings appear in each year?
# sdlg<-matrix_data %>% group_by(year, plot_id) %>% tally(sdlg==TRUE) %>% rename(tot_sdlgs=n)
#
# # The number of seedlings is based on the no. of infl. in the PREVIOUS year
# repro<-left_join(infl,sdlg) %>%
# mutate(tot_sdlgs=lead(tot_sdlgs)) %>%
# mutate(sds_per_infl=tot_sdlgs/tot_infl) %>%
# mutate(sds_per_infl=round(sds_per_infl,2)) %>%
# ungroup()
# # %>%
# # select(year,plot_id,sds_per_infl)
# str(repro)
#
# # There are some plots x years with no seedlings or no flowering plants,
# # resulting in NaN or InF in seedlings per infl. Replace those values with zero
# repro<-repro %>% replace_na(list(sds_per_infl=0)) %>%
# mutate(sds_per_infl=case_when(
# is.nan(sds_per_infl)~0,
# .default = as.numeric(sds_per_infl))) %>%
# mutate(sds_per_infl=case_when(
# is.infinite(sds_per_infl)~0,
# .default = as.numeric(sds_per_infl)))
#
# # Add the reproductive data to the matrix data and muliply number of infl
# # by the number of seedlings per infl. to get value for how many
# # seedlings in subsequent year
# matrix_data<-left_join(matrix_data,repro) %>%
# relocate(sds_per_infl,.after=infl) %>%
# mutate(seedling=infl*sds_per_infl) %>%
# relocate(seedling,.after=sds_per_infl)
#
# # Create a transition matrix for each year
# plots<-sort(unique(matrix_data$plot_id))
# years<-sort(unique(matrix_data$year))
# years<-years[2]:years[(length(years)-1)]
# #
# # data1 <- list(NA,NA,NA,NA)
# # data1 <- tibble("plot"=NA,"year"=NA,"eta"=as.list(NA),"eta_var"=as.list(NA))
# # data1 <- list("plot"=NA,"year"=NA,"eta"= NA,"eta_var"=NA)
# # data2 <- list("plot"=NA,"year"=NA,"eta"= NA,"eta_var"=NA)
# #
# # data3<-paste(data1, data1)
#
# #
# # data1 <- vector("list", nrow(as.data.frame(plots))*nrow(as.data.frame(years)))
# # data1 <- vector("list", nrow(as.data.frame(plots)))
# # data2 <- vector("list", nrow(as.data.frame(plots)))
# # data3 <- vector("list", nrow(as.data.frame(plots)))
# # data4 <- vector("list", nrow(as.data.frame(years)))
#
# # i=1
# # j=4
# str(matrix_data)
# for (i in seq_along(years)) {
# for (j in seq_along(plots)) {
# trans01<-matrix_data %>%
# filter(year==years[i]) %>%
# filter(plot_id==plots[j]) %>%
# filter(stage!=0) %>%
# filter(fate!=0) %>%
# select(plot_id,
# plant_id,
# year,
# stage,
# infl,
# fate,
# seedling) %>%
# mutate(stage=as.factor(stage)) %>%
# mutate(stage=fct_relevel(stage,"sdlg","1","2","3","4","5","6","7")) %>%
# mutate(fate=as.factor(fate)) %>%
# mutate(fate=fct_relevel(fate,"1","2","3","4","5","6","7","dead")) %>%
# mutate(seedling=replace_na(seedling,0))
#
# levels(trans01$stage)
# levels(trans01$fate)
#
# tf <- table(trans01$fate, trans01$stage)
# T.mat_all <- prop.table(tf, 2)
# T.mat<-T.mat_all[1:7,]
# F.mat <- T.mat * 0
# F.mat[1, ] <- tapply(trans01$seedling, trans01$stage, mean)
#
#
#
# T.mat<-rbind(F.mat[1, ],T.mat)
# rownames(T.mat)<-c("sdlg","1","2","3","4","5","6","7")
#
#
# stages <- unique(matrix_data$stage)
# fates <- unique(matrix_data$fate)
#
# n <- c(5,24,23,21,13,5,2,3)
# p <- pop.projection(T.mat, n, 1000)
# p$lambda
#
# eigT.mat <- eigen.analysis(T.mat)
# eigT.mat$lambda1
#
# fundamental.matrix(T.mat)
# eta<-fundamental.matrix(T.mat)
# # eta$N
# # eta$meaneta
# #
#
# data$eta<-eta$meaneta
# data$eta_var<-eta$vareta
# data<-as.data.frame(data)
# data$stage<-row.names(data)
# data$plot<-plots[j]
# data$year<-years[i]
#
# data2<-bind_rows(data2,data)
# }
# }
#
#
#
# plot_yr_eta <- data.frame(t(sapply(data1,c)))
# names(plot_yr_eta)<-c("sdlg","shts_1","shts_2","shts_3","shts_4","shts_5","shts_6","shts_7")
# plots <- unlist(data3)
# plots<-as_tibble(plots) %>% rename(plot=value)
# plot_yr_eta<-bind_cols(plots,plot_yr_eta)
#
# plot_yr_eta_var <- data.frame(t(sapply(data2,c)))
# names(plot_yr_eta_var)<-c("sdlg","shts_1","shts_2","shts_3","shts_4","shts_5","shts_6","shts_7")
# plot_yr_eta_var<-bind_cols(plots,plot_yr_eta_var)
#
# write_csv(plot_yr_eta, "./plot_yr_eta.csv")
# write_csv(plot_yr_eta_var, "./plot_yr_eta_var.csv")
#
#
#
# # How many infloresences in each plot in each year?
# infl<-matrix_data %>% group_by(year, plot_id) %>% tally(infl)%>% rename(tot_infl=n)
#
# # how many seedlings appear in each plot in each year?
# sdlg<-matrix_data %>% group_by(year, plot_id) %>% tally(sdlg==TRUE) %>% rename(tot_sdlgs=n)
#
# # The number of seedlings is based on the no. of infl. in the PREVIOUS year
# repro<-left_join(infl,sdlg) %>%
# mutate(tot_sdlgs=lead(tot_sdlgs)) %>%
# mutate(sds_per_infl=tot_sdlgs/tot_infl) %>%
# mutate(sds_per_infl=round(sds_per_infl,2)) %>%
# ungroup()
# # %>%
# # select(year,plot_id,sds_per_infl)
# str(repro)