annotate dennis-gam-initial-functions.R @ 0:b416a363a2d5 draft default tip

planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
author ecology
date Thu, 20 Jun 2019 04:03:31 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
1 ### R-Script Adapted from script provided by the CEH, UK BY: Reto Schmucki [ reto.schmucki@mail.mcgill.ca]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
2 ### DATE: 14 July 2014 function to run two stage model in DENNIS et al. 2013
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
3
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
4
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
5 .onAttach <- function(libname, pkgname) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
6 packageStartupMessage(" The regionalGAM package that is no longer maintained, \n use the new rbms package instead. \n
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
7 devtools::install_github(\"RetoSchmucki/rbms\", force=TRUE)")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
8 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
9
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
10
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
11 #' year_day_func Function
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
12 #' This function generate the full sequence of days, months and include the observation to that file.
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
13 #' @param sp_data A data.frame with your observation.
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
14 #' @keywords year days
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
15 #' @export
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
16 #' @author Reto Schmucki
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
17 #' @examples
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
18 #' year_day_func()
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
19
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
20
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
21 # FUNCTIONS
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
22
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
23 year_day_func = function(sp_data) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
24
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
25 year <- unique(sp_data$YEAR)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
26
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
27 origin.d <- paste(year, "01-01", sep = "-")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
28 if ((year%%4 == 0) & ((year%%100 != 0) | (year%%400 == 0))) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
29 nday <- 366
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
30 } else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
31 nday <- 365
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
32 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
33
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
34 date.serie <- as.POSIXlt(seq(as.Date(origin.d), length = nday, by = "day"), format = "%Y-%m-%d")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
35
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
36 dayno <- as.numeric(julian(date.serie, origin = as.Date(origin.d)) + 1)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
37 month <- as.numeric(strftime(date.serie, format = "%m"))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
38 week <- as.numeric(strftime(date.serie, format = "%W"))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
39 week_day <- as.numeric(strftime(date.serie, format = "%u"))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
40 day <- as.numeric(strftime(date.serie, format = "%d"))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
41
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
42 site_list <- sp_data[!duplicated(sp_data$SITE), c("SITE")]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
43
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
44 all_day_site <- data.frame(SPECIES = sp_data$SPECIES[1], SITE = rep(site_list, rep(nday, length(site_list))),
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
45 YEAR = sp_data$YEAR[1], MONTH = month, WEEK = week, DAY = day, DAY_WEEK = week_day, DAYNO = dayno,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
46 COUNT = NA)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
47
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
48 count_index <- match(paste(sp_data$SITE, sp_data$DAYNO, sep = "_"), paste(all_day_site$SITE, all_day_site$DAYNO,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
49 sep = "_"))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
50 all_day_site$COUNT[count_index] <- sp_data$COUNT
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
51 site_count_length <- aggregate(sp_data$COUNT, by = list(sp_data$SITE), function(x) list(1:length(x)))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
52 names(site_count_length$x) <- as.character(site_count_length$Group.1)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
53 site_countno <- utils::stack(site_count_length$x)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
54 all_day_site$COUNTNO <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
55 all_day_site$COUNTNO[count_index] <- site_countno$values # add count number to ease extraction of single count
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
56
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
57 # Add zero to close observation season two weeks before and after the first and last
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
58 first_obs <- min(all_day_site$DAYNO[!is.na(all_day_site$COUNT)])
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
59 last_obs <- max(all_day_site$DAYNO[!is.na(all_day_site$COUNT)])
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
60
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
61 closing_season <- c((first_obs - 11):(first_obs - 7), (last_obs + 7):(last_obs + 11))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
62
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
63 # If closing season is before day 1 or day 365, simply set the first and last 5 days to 0
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
64 if (min(closing_season) < 1)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
65 closing_season[1:5] <- c(1:5)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
66 if (max(closing_season) > nday)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
67 closing_season[6:10] <- c((nday - 4):nday)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
68
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
69 all_day_site$COUNT[all_day_site$DAYNO %in% closing_season] <- 0
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
70 all_day_site$ANCHOR <- 0
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
71 all_day_site$ANCHOR[all_day_site$DAYNO %in% closing_season] <- 1
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
72
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
73 all_day_site <- all_day_site[order(all_day_site$SITE, all_day_site$DAYNO), ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
74
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
75 return(all_day_site)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
76 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
77
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
78
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
79 #' trap_area Function
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
80 #'
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
81 #' This function compute the area under the curve using the trapezoid method.
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
82 #' @param x A vector or a two columns matrix.
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
83 #' @param y A vector, Default is NULL
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
84 #' @keywords trapezoid
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
85 #' @export
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
86 #' @examples
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
87 #' trap_area()
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
88
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
89
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
90 trap_area = function(x, y = NULL) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
91 # If y is null and x has multiple columns then set y to x[,2] and x to x[,1]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
92 if (is.null(y)) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
93 if (length(dim(x)) == 2) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
94 y = x[, 2]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
95 x = x[, 1]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
96 } else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
97 stop("ERROR: need to either specifiy both x and y or supply a two column data.frame/matrix to x")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
98 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
99 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
100
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
101 # Check x and y are same length
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
102 if (length(x) != length(y)) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
103 stop("ERROR: x and y need to be the same length")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
104 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
105
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
106 # Need to exclude any pairs that are NA for either x or y
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
107 rm_inds = which(is.na(x) | is.na(y))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
108 if (length(rm_inds) > 0) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
109 x = x[-rm_inds]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
110 y = y[-rm_inds]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
111 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
112
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
113 # Determine values of trapezoids under curve Get inds
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
114 inds = 1:(length(x) - 1)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
115 # Determine area using trapezoidal rule Area = ( (b1 + b2)/2 ) * h where b1 and b2 are lengths of bases
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
116 # (the parallel sides) and h is the height (the perpendicular distance between two bases)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
117 areas = ((y[inds] + y[inds + 1])/2) * diff(x)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
118
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
119 # total area is sum of all trapezoid areas
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
120 tot_area = sum(areas)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
121
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
122 # Return total area
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
123 return(tot_area)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
124 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
125
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
126
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
127 #' trap_index Function
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
128 #'
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
129 #' This function compute the area under the curve (Abundance Index) across species, sites and years
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
130 #' @param sp_data A data.frame containing species count data generated from the year_day_func()
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
131 #' @param y A vector, Default is NULL
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
132 #' @keywords Abundance index
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
133 #' @export
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
134 #' @examples
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
135 #' trap_index()
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
136
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
137
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
138
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
139 trap_index = function(sp_data, data_col = "IMP", time_col = "DAYNO", by_col = c("SPECIES", "SITE", "YEAR")) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
140
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
141 # Build output data.frame
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
142 out_obj = unique(sp_data[, by_col])
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
143 # Set row.names to be equal to collapsing of output rows (will be unique, you need them to make uploading
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
144 # values back to data.frame will be easier)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
145 row.names(out_obj) = apply(out_obj, 1, paste, collapse = "_")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
146
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
147 # Using this row.names from out_obj above as index in by function to loop through values all unique combs
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
148 # of by_cols and fit trap_area to data
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
149 ind_dat = by(sp_data[, c(time_col, data_col)], apply(sp_data[, by_col], 1, paste, collapse = "_"), trap_area)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
150
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
151 # Add this data to output object
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
152 out_obj[names(ind_dat), "SINDEX"] = round(ind_dat/7, 1)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
153
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
154 # Set row.names to defaults
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
155 row.names(out_obj) = NULL
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
156
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
157 # Return output object
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
158 return(out_obj)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
159 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
160
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
161
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
162 #' flight_curve Function
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
163 #' This function compute the flight curve across and years
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
164 #' @param your_dataset A data.frame containing original species count data. The data format is a csv (comma "," separated) with 6 columns with headers, namely "species","transect_id","visit_year","visit_month","visit_day","sp_count"
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
165 #' @keywords standardize flight curve
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
166 #' @export
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
167 #' @examples
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
168 #' flight_curve()
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
169
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
170
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
171 flight_curve <- function(your_dataset, GamFamily = 'nb', MinVisit = 2, MinOccur = 1) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
172
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
173 if("mgcv" %in% installed.packages() == "FALSE") {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
174 print("mgcv package is not installed.")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
175 x <- readline("Do you want to install it? Y/N")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
176 if (x == 'Y') {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
177 install.packages("mgcv")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
178 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
179 if (x == 'N') {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
180 stop("flight curve can not be computed without the mgcv package, sorry")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
181 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
182 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
183
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
184 flight_pheno <- data.frame()
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
185
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
186 your_dataset$DAYNO <- strptime(paste(your_dataset$DAY, your_dataset$MONTH,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
187 your_dataset$YEAR, sep = "/"), "%d/%m/%Y")$yday + 1
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
188 dataset <- your_dataset[, c("SPECIES", "SITE", "YEAR", "MONTH",
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
189 "DAY", "DAYNO", "COUNT")]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
190 sample_year <- unique(dataset$YEAR)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
191 sample_year <- sample_year[order(sample_year)]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
192
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
193 if (length(sample_year) >1 ) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
194 for (y in sample_year) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
195 dataset_y <- dataset[dataset$YEAR == y, ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
196
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
197 # subset sites with enough visit and occurence
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
198 occ <- aggregate(dataset_y$COUNT, by = list(SITE = dataset_y$SITE), function(x) sum(x > 0))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
199 vis <- aggregate(dataset_y$COUNT, by = list(SITE = dataset_y$SITE), function(x) length(x))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
200 dataset_y <- dataset_y[dataset_y$SITE %in% occ$SITE[occ$x >= MinOccur], ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
201 dataset_y <- dataset_y[dataset_y$SITE %in% vis$SITE[vis$x >= MinVisit], ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
202 nsite <- length(unique(dataset_y$SITE))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
203 if (nsite < 1) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
204 print(paste("No sites with sufficient visits and occurence, MinOccur:", MinOccur, " MinVisit: ", MinVisit, " for " , dataset$SPECIES[1],"at year", y))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
205 next
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
206 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
207 # Determine missing days and add to dataset
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
208 sp_data_all <- year_day_func(dataset_y)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
209 if (nsite > 200) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
210 sp_data_all <- sp_data_all[as.character(sp_data_all$SITE) %in% as.character(unique(dataset_y$SITE)[sample(1:nsite,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
211 200, replace = F)]), ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
212 sp_data_all <- sp_data_all
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
213 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
214 sp_data_all$trimDAYNO <- sp_data_all$DAYNO - min(sp_data_all$DAYNO) + 1
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
215 print(paste("Fitting the GAM for",as.character(sp_data_all$SPECIES[1]),"and year",y,"with",length(unique(sp_data_all$SITE)),"sites :",Sys.time()))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
216 if(length(unique(sp_data_all$SITE))>1){
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
217 gam_obj_site <- try(mgcv::gam(COUNT ~ s(trimDAYNO, bs = "cr") + as.factor(SITE) -1,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
218 data = sp_data_all, family = GamFamily), silent = TRUE)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
219 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
220 else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
221 gam_obj_site <- try(mgcv::gam(COUNT ~ s(trimDAYNO, bs = "cr") -1,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
222 data = sp_data_all, family = GamFamily), silent = TRUE)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
223 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
224 # Give a second try if the GAM does not converge the first time
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
225 if (class(gam_obj_site)[1] == "try-error") {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
226 # Determine missing days and add to dataset
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
227 sp_data_all <- year_day_func(dataset_y)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
228 if (nsite > 200) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
229 sp_data_all <- sp_data_all[as.character(sp_data_all$SITE) %in% as.character(unique(dataset_y$SITE)[sample(1:nsite,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
230 200, replace = F)]), ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
231 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
232 else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
233 sp_data_all <- sp_data_all
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
234 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
235 sp_data_all$trimDAYNO <- sp_data_all$DAYNO - min(sp_data_all$DAYNO) + 1
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
236 print(paste("Fitting the GAM for",sp_data_all$SPECIES[1],"at year", y,"with",length(unique(sp_data_all$SITE)),"sites :",Sys.time(),"second try"))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
237 if(length(unique(sp_data_all$SITE))>1){
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
238 gam_obj_site <- try(mgcv::gam(COUNT ~ s(trimDAYNO, bs = "cr") + as.factor(SITE) -1,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
239 data = sp_data_all, family = GamFamily), silent = TRUE)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
240 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
241 else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
242 gam_obj_site <- try(mgcv::gam(COUNT ~ s(trimDAYNO, bs = "cr") -1,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
243 data = sp_data_all, family = GamFamily), silent = TRUE)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
244 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
245 if (class(gam_obj_site)[1] == "try-error") {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
246 print(paste("Error in fitting the flight period for",sp_data_all$SPECIES[1],"at year", y,"no convergence after two trial"))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
247 sp_data_all[, "FITTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
248 sp_data_all[, "COUNT_IMPUTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
249 sp_data_all[is.na(sp_data_all$COUNT), "COUNT_IMPUTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
250 sp_data_all[, "NM"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
251 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
252 else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
253 # Generate a list of values for all days from the additive model and use
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
254 # these value to fill the missing observations
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
255 sp_data_all[, "FITTED"] <- mgcv::predict.gam(gam_obj_site, newdata = sp_data_all[,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
256 c("trimDAYNO", "SITE")], type = "response")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
257 # force zeros at the beginning end end of the year
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
258 sp_data_all[sp_data_all$trimDAYNO < 60, "FITTED"] <- 0
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
259 sp_data_all[sp_data_all$trimDAYNO > 305, "FITTED"] <- 0
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
260 # if infinite number in predict replace with NA.
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
261 if(sum(is.infinite(sp_data_all[, "FITTED"]))>0){
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
262 print(paste("Error in the flight period for",sp_data_all$SPECIES[1],"at year", y,"weird predicted values"))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
263 sp_data_all[, "FITTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
264 sp_data_all[, "COUNT_IMPUTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
265 sp_data_all[is.na(sp_data_all$COUNT), "COUNT_IMPUTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
266 sp_data_all[, "NM"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
267 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
268 else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
269 sp_data_all[, "COUNT_IMPUTED"] <- sp_data_all$COUNT
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
270 sp_data_all[is.na(sp_data_all$COUNT), "COUNT_IMPUTED"] <- sp_data_all$FITTED[is.na(sp_data_all$COUNT)]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
271 # Define the flight curve from the fitted values and append them over
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
272 # years (this is one flight curve per year for all site)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
273 site_sums <- aggregate(sp_data_all$FITTED, by = list(SITE = sp_data_all$SITE),
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
274 FUN = sum)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
275 # Rename sum column
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
276 names(site_sums)[names(site_sums) == "x"] <- "SITE_YR_FSUM"
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
277 # Add data to sp_data data.frame (ensure merge does not sort the data!)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
278 sp_data_all = merge(sp_data_all, site_sums, by <- c("SITE"),
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
279 all = TRUE, sort = FALSE)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
280 # Calculate normalized values
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
281 sp_data_all[, "NM"] <- sp_data_all$FITTED/sp_data_all$SITE_YR_FSUM
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
282 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
283 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
284 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
285 else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
286 # Generate a list of values for all days from the additive model and use
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
287 # these value to fill the missing observations
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
288 sp_data_all[, "FITTED"] <- mgcv::predict.gam(gam_obj_site, newdata = sp_data_all[,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
289 c("trimDAYNO", "SITE")], type = "response")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
290 # force zeros at the beginning end end of the year
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
291 sp_data_all[sp_data_all$trimDAYNO < 60, "FITTED"] <- 0
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
292 sp_data_all[sp_data_all$trimDAYNO > 305, "FITTED"] <- 0
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
293 # if infinite number in predict replace with NA.
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
294 if(sum(is.infinite(sp_data_all[, "FITTED"]))>0){
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
295 print(paste("Error in the flight period for",sp_data_all$SPECIES[1],"at year", y,"weird predicted values"))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
296 sp_data_all[, "FITTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
297 sp_data_all[, "COUNT_IMPUTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
298 sp_data_all[is.na(sp_data_all$COUNT), "COUNT_IMPUTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
299 sp_data_all[, "NM"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
300 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
301 else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
302 sp_data_all[, "COUNT_IMPUTED"] <- sp_data_all$COUNT
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
303 sp_data_all[is.na(sp_data_all$COUNT), "COUNT_IMPUTED"] <- sp_data_all$FITTED[is.na(sp_data_all$COUNT)]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
304 # Define the flight curve from the fitted values and append them over
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
305 # years (this is one flight curve per year for all site)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
306 site_sums = aggregate(sp_data_all$FITTED, by = list(SITE = sp_data_all$SITE),
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
307 FUN = sum)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
308 # Rename sum column
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
309 names(site_sums)[names(site_sums) == "x"] = "SITE_YR_FSUM"
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
310 # Add data to sp_data data.frame (ensure merge does not sort the data!)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
311 sp_data_all = merge(sp_data_all, site_sums, by = c("SITE"), all = TRUE,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
312 sort = FALSE)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
313 # Calculate normalized values
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
314 sp_data_all[, "NM"] = sp_data_all$FITTED/sp_data_all$SITE_YR_FSUM
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
315 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
316 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
317 sp_data_filled <- sp_data_all
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
318 flight_curve <- data.frame(species = sp_data_filled$SPECIES, year = sp_data_filled$YEAR,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
319 week = sp_data_filled$WEEK, DAYNO = sp_data_filled$DAYNO, DAYNO_adj = sp_data_filled$trimDAYNO,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
320 nm = sp_data_filled$NM)[!duplicated(paste(sp_data_filled$YEAR,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
321 sp_data_filled$DAYNO, sep = "_")), ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
322 flight_curve <- flight_curve[order(flight_curve$DAYNO), ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
323 # bind if exist else create
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
324 if (is.na(flight_curve$nm[1])) next()
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
325
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
326 flight_pheno <- rbind(flight_pheno, flight_curve)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
327
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
328 } # end of year loop
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
329 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
330 else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
331 y <- unique(dataset$YEAR)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
332 dataset_y <- dataset[dataset$YEAR == y, ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
333 # subset sites with enough visit and occurence
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
334 occ <- aggregate(dataset_y$COUNT, by = list(SITE = dataset_y$SITE), function(x) sum(x > 0))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
335 vis <- aggregate(dataset_y$COUNT, by = list(SITE = dataset_y$SITE), function(x) length(x))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
336 dataset_y <- dataset_y[dataset_y$SITE %in% occ$SITE[occ$x >= MinOccur], ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
337 dataset_y <- dataset_y[dataset_y$SITE %in% vis$SITE[vis$x >= MinVisit], ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
338 nsite <- length(unique(dataset_y$SITE))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
339 if (nsite < 1) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
340 stop(paste("No sites with sufficient visits and occurence, MinOccur:", MinOccur, " MinVisit: ", MinVisit, " for " ,dataset$SPECIES[1],"at year", y))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
341 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
342 # Determine missing days and add to dataset
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
343 sp_data_all <- year_day_func(dataset_y)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
344 if (nsite > 200) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
345 sp_data_all <- sp_data_all[as.character(sp_data_all$SITE) %in% as.character(unique(dataset_y$SITE)[sample(1:nsite,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
346 200, replace = F)]), ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
347 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
348 else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
349 sp_data_all <- sp_data_all
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
350 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
351 sp_data_all$trimDAYNO <- sp_data_all$DAYNO - min(sp_data_all$DAYNO) + 1
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
352 print(paste("Fitting the GAM for",sp_data_all$SPECIES[1],"at year", y,":",Sys.time()))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
353 if(length(unique(sp_data_all$SITE))>1){
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
354 gam_obj_site <- try(mgcv::gam(COUNT ~ s(trimDAYNO, bs = "cr") + as.factor(SITE) -1,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
355 data = sp_data_all, family = GamFamily), silent = TRUE)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
356 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
357 else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
358 gam_obj_site <- try(mgcv::gam(COUNT ~ s(trimDAYNO, bs = "cr") -1,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
359 data = sp_data_all, family = GamFamily), silent = TRUE)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
360 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
361 # Give a second try if the GAM does not converge the first time
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
362 if (class(gam_obj_site)[1] == "try-error") {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
363 # Determine missing days and add to dataset
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
364 sp_data_all <- year_day_func(dataset_y)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
365 if (nsite > 200) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
366 sp_data_all <- sp_data_all[as.character(sp_data_all$SITE) %in% as.character(unique(dataset_y$SITE)[sample(1:nsite,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
367 200, replace = F)]), ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
368 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
369 else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
370 sp_data_all <- sp_data_all
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
371 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
372 sp_data_all$trimDAYNO <- sp_data_all$DAYNO - min(sp_data_all$DAYNO) + 1
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
373 print(paste("Fitting the GAM for",sp_data_all$SPECIES[1],"at year", y,"with",length(unique(sp_data_all$SITE)),"sites :",Sys.time(),"second try"))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
374 if(length(unique(sp_data_all$SITE))>1){
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
375 gam_obj_site <- try(mgcv::bam(COUNT ~ s(trimDAYNO, bs = "cr") + as.factor(SITE) - 1,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
376 data = sp_data_all, family = GamFamily), silent = TRUE)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
377 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
378 else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
379 gam_obj_site <- try(mgcv::gam(COUNT ~ s(trimDAYNO, bs = "cr") -1,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
380 data = sp_data_all, family = GamFamily), silent = TRUE)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
381 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
382 if (class(gam_obj_site)[1] == "try-error") {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
383 print(paste("Error in fitting the flight period for",sp_data_all$SPECIES[1],"at year", y,"no convergence after two trial"))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
384 sp_data_all[, "FITTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
385 sp_data_all[, "COUNT_IMPUTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
386 sp_data_all[is.na(sp_data_all$COUNT), "COUNT_IMPUTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
387 sp_data_all[, "NM"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
388 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
389 else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
390 # Generate a list of values for all days from the additive model and use
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
391 # these value to fill the missing observations
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
392 sp_data_all[, "FITTED"] <- mgcv::predict.gam(gam_obj_site, newdata = sp_data_all[,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
393 c("trimDAYNO", "SITE")], type = "response")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
394 # force zeros at the beginning end end of the year
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
395 sp_data_all[sp_data_all$trimDAYNO < 60, "FITTED"] <- 0
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
396 sp_data_all[sp_data_all$trimDAYNO > 305, "FITTED"] <- 0
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
397 # if infinite number in predict replace with NA.
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
398 if(sum(is.infinite(sp_data_all[, "FITTED"]))>0){
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
399 print(paste("Error in the flight period for",sp_data_all$SPECIES[1],"at year", y,"weird predicted values"))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
400 sp_data_all[, "FITTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
401 sp_data_all[, "COUNT_IMPUTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
402 sp_data_all[is.na(sp_data_all$COUNT), "COUNT_IMPUTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
403 sp_data_all[, "NM"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
404 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
405 else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
406 sp_data_all[, "COUNT_IMPUTED"] <- sp_data_all$COUNT
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
407 sp_data_all[is.na(sp_data_all$COUNT), "COUNT_IMPUTED"] <- sp_data_all$FITTED[is.na(sp_data_all$COUNT)]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
408 # Define the flight curve from the fitted values and append them over
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
409 # years (this is one flight curve per year for all site)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
410 site_sums <- aggregate(sp_data_all$FITTED, by = list(SITE = sp_data_all$SITE),
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
411 FUN = sum)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
412 # Rename sum column
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
413 names(site_sums)[names(site_sums) == "x"] <- "SITE_YR_FSUM"
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
414 # Add data to sp_data data.frame (ensure merge does not sort the data!)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
415 sp_data_all = merge(sp_data_all, site_sums, by <- c("SITE"),
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
416 all = TRUE, sort = FALSE)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
417 # Calculate normalized values
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
418 sp_data_all[, "NM"] <- sp_data_all$FITTED/sp_data_all$SITE_YR_FSUM
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
419 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
420 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
421 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
422 else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
423 # Generate a list of values for all days from the additive model and use
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
424 # these value to fill the missing observations
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
425 sp_data_all[, "FITTED"] <- mgcv::predict.gam(gam_obj_site, newdata = sp_data_all[,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
426 c("trimDAYNO", "SITE")], type = "response")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
427 # force zeros at the beginning end end of the year
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
428 sp_data_all[sp_data_all$trimDAYNO < 60, "FITTED"] <- 0
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
429 sp_data_all[sp_data_all$trimDAYNO > 305, "FITTED"] <- 0
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
430 # if infinite number in predict replace with NA.
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
431 if(sum(is.infinite(sp_data_all[, "FITTED"]))>0){
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
432 print(paste("Error in the flight period for",sp_data_all$SPECIES[1],"at year", y,"weird predicted values"))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
433 sp_data_all[, "FITTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
434 sp_data_all[, "COUNT_IMPUTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
435 sp_data_all[is.na(sp_data_all$COUNT), "COUNT_IMPUTED"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
436 sp_data_all[, "NM"] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
437 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
438 else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
439 sp_data_all[, "COUNT_IMPUTED"] <- sp_data_all$COUNT
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
440 sp_data_all[is.na(sp_data_all$COUNT), "COUNT_IMPUTED"] <- sp_data_all$FITTED[is.na(sp_data_all$COUNT)]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
441 # Define the flight curve from the fitted values and append them over
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
442 # years (this is one flight curve per year for all site)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
443 site_sums = aggregate(sp_data_all$FITTED, by = list(SITE = sp_data_all$SITE),
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
444 FUN = sum)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
445 # Rename sum column
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
446 names(site_sums)[names(site_sums) == "x"] = "SITE_YR_FSUM"
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
447 # Add data to sp_data data.frame (ensure merge does not sort the data!)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
448 sp_data_all = merge(sp_data_all, site_sums, by = c("SITE"), all = TRUE,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
449 sort = FALSE)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
450 # Calculate normalized values
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
451 sp_data_all[, "NM"] = sp_data_all$FITTED/sp_data_all$SITE_YR_FSUM
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
452 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
453 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
454 sp_data_filled <- sp_data_all
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
455 flight_curve <- data.frame(species = sp_data_filled$SPECIES, year = sp_data_filled$YEAR,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
456 week = sp_data_filled$WEEK, DAYNO = sp_data_filled$DAYNO, DAYNO_adj = sp_data_filled$trimDAYNO,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
457 nm = sp_data_filled$NM)[!duplicated(paste(sp_data_filled$YEAR,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
458 sp_data_filled$DAYNO, sep = "_")), ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
459 flight_curve <- flight_curve[order(flight_curve$DAYNO), ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
460
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
461 flight_pheno <- rbind(flight_pheno, flight_curve)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
462
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
463 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
464 return(flight_pheno)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
465 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
466
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
467
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
468 #' abundance_index Function
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
469 #'
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
470 #' This function compute the Abundance Index across sites and years from your dataset and the regional flight curve
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
471 #' @param your_dataset A data.frame containing original species count data. The data format is a csv (comma "," separated) with 6 columns with headers, namely "species","transect_id","visit_year","visit_month","visit_day","sp_count"
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
472 #' @param flight_pheno A data.frame for the regional flight curve computed with the function flight_curve
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
473 #' @keywords standardize flight curve
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
474 #' @export
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
475 #' @examples
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
476 #' abundance_index()
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
477
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
478 abundance_index <- function(your_dataset,flight_pheno) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
479
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
480 your_dataset$DAYNO <- strptime(paste(your_dataset$DAY, your_dataset$MONTH,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
481 your_dataset$YEAR, sep = "/"), "%d/%m/%Y")$yday + 1
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
482
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
483 dataset <- your_dataset[, c("SPECIES", "SITE", "YEAR", "MONTH",
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
484 "DAY", "DAYNO", "COUNT")]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
485
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
486 sample_year <- unique(dataset$YEAR)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
487 sample_year <- sample_year[order(sample_year)]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
488
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
489
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
490 if (length(sample_year)>1){
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
491
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
492 for (y in sample_year) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
493
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
494 year_pheno <- flight_pheno[flight_pheno$year == y, ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
495
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
496 dataset_y <- dataset[dataset$YEAR == y, ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
497
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
498 sp_data_site <- year_day_func(dataset_y)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
499 sp_data_site$trimDAYNO <- sp_data_site$DAYNO - min(sp_data_site$DAYNO) + 1
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
500
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
501 sp_data_site <- merge(sp_data_site, year_pheno[, c("DAYNO", "nm")],
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
502 by = c("DAYNO"), all.x = TRUE, sort = FALSE)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
503
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
504 # compute proportion of the flight curve sampled due to missing visits
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
505 pro_missing_count <- data.frame(SITE = sp_data_site$SITE, WEEK = sp_data_site$WEEK,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
506 NM = sp_data_site$nm, COUNT = sp_data_site$COUNT, ANCHOR = sp_data_site$ANCHOR)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
507 pro_missing_count$site_week <- paste(pro_missing_count$SITE, pro_missing_count$WEEK,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
508 sep = "_")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
509 siteweeknocount <- aggregate(pro_missing_count$COUNT, by = list(pro_missing_count$site_week),
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
510 function(x) sum(!is.na(x)) == 0)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
511 pro_missing_count <- pro_missing_count[pro_missing_count$site_week %in%
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
512 siteweeknocount$Group.1[siteweeknocount$x == TRUE], ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
513 pro_count_agg <- aggregate(pro_missing_count$NM, by = list(pro_missing_count$SITE),
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
514 function(x) 1 - sum(x, na.rm = T))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
515 names(pro_count_agg) <- c("SITE", "PROP_PHENO_SAMPLED")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
516
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
517 # remove samples outside the monitoring window
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
518 sp_data_site$COUNT[sp_data_site$nm==0] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
519
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
520 # Compute the regional GAM index
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
521
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
522 if(length(unique(sp_data_site$SITE))>1){
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
523 glm_obj_site <- glm(COUNT ~ factor(SITE) + offset(log(nm)) - 1, data = sp_data_site,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
524 family = quasipoisson(link = "log"), control = list(maxit = 100))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
525 } else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
526 glm_obj_site <- glm(COUNT ~ offset(log(nm)) - 1, data = sp_data_site,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
527 family = quasipoisson(link = "log"), control = list(maxit = 100))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
528 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
529
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
530 sp_data_site[, "FITTED"] <- predict.glm(glm_obj_site, newdata = sp_data_site,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
531 type = "response")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
532 sp_data_site[, "COUNT_IMPUTED"] <- sp_data_site$COUNT
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
533 sp_data_site[is.na(sp_data_site$COUNT), "COUNT_IMPUTED"] <- sp_data_site$FITTED[is.na(sp_data_site$COUNT)]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
534
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
535 ## add fitted value for missing mid-week data
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
536 sp_data_site <- sp_data_site[!paste(sp_data_site$DAY_WEEK, sp_data_site$COUNT) %in%
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
537 c("1 NA", "2 NA", "3 NA", "5 NA", "6 NA", "7 NA"), ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
538
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
539 ## remove all added mid-week values for weeks with real count
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
540 ## (observation)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
541 sp_data_site$site_week <- paste(sp_data_site$SITE, sp_data_site$WEEK,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
542 sep = "_")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
543 siteweekcount <- aggregate(sp_data_site$COUNT, by = list(sp_data_site$site_week),
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
544 function(x) sum(!is.na(x)) > 0)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
545 sp_data_site <- sp_data_site[!(is.na(sp_data_site$COUNT) & (sp_data_site$site_week %in%
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
546 siteweekcount$Group.1[siteweekcount$x == TRUE])), names(sp_data_site) !=
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
547 "site_week"]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
548
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
549 ## Compute the regional GAM index
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
550 print(paste("Compute index for",sp_data_site$SPECIES[1],"at year", y,"for",length(unique(sp_data_site$SITE)),"sites:",Sys.time()))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
551 regional_gam_index <- trap_index(sp_data_site, data_col = "COUNT_IMPUTED",
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
552 time_col = "DAYNO", by_col = c("SPECIES", "SITE", "YEAR"))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
553
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
554 cumu_index <- merge(regional_gam_index, pro_count_agg, by = c("SITE"),
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
555 all.x = TRUE, sort = FALSE)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
556 names(cumu_index) <- c("SITE", "SPECIES", "YEAR", "regional_gam", "prop_pheno_sampled")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
557
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
558 cumu_index <- cumu_index[order(cumu_index$SITE), ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
559
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
560 # bind if exist else create
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
561 if ("cumullated_indices" %in% ls()) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
562 cumullated_indices <- rbind(cumullated_indices, cumu_index)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
563 } else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
564 cumullated_indices <- cumu_index
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
565 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
566
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
567 } # end of year loop
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
568
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
569 } else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
570
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
571 y <- unique(dataset$YEAR)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
572 year_pheno <- flight_pheno[flight_pheno$year == y, ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
573
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
574 dataset_y <- dataset[dataset$YEAR == y, ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
575
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
576 sp_data_site <- year_day_func(dataset_y)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
577 sp_data_site$trimDAYNO <- sp_data_site$DAYNO - min(sp_data_site$DAYNO) + 1
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
578
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
579 sp_data_site <- merge(sp_data_site, year_pheno[, c("DAYNO", "nm")],
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
580 by = c("DAYNO"), all.x = TRUE, sort = FALSE)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
581
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
582 # compute proportion of the flight curve sampled due to missing visits
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
583 pro_missing_count <- data.frame(SITE = sp_data_site$SITE, WEEK = sp_data_site$WEEK,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
584 NM = sp_data_site$nm, COUNT = sp_data_site$COUNT, ANCHOR = sp_data_site$ANCHOR)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
585 pro_missing_count$site_week <- paste(pro_missing_count$SITE, pro_missing_count$WEEK,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
586 sep = "_")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
587 siteweeknocount <- aggregate(pro_missing_count$COUNT, by = list(pro_missing_count$site_week),
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
588 function(x) sum(!is.na(x)) == 0)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
589 pro_missing_count <- pro_missing_count[pro_missing_count$site_week %in%
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
590 siteweeknocount$Group.1[siteweeknocount$x == TRUE], ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
591 pro_count_agg <- aggregate(pro_missing_count$NM, by = list(pro_missing_count$SITE),
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
592 function(x) 1 - sum(x, na.rm = T))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
593 names(pro_count_agg) <- c("SITE", "PROP_PHENO_SAMPLED")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
594
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
595 # remove samples outside the monitoring window
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
596 sp_data_site$COUNT[sp_data_site$nm==0] <- NA
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
597
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
598 # Compute the regional GAM index
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
599 if(length(unique(sp_data_site$SITE))>1){
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
600 glm_obj_site <- glm(COUNT ~ factor(SITE) + offset(log(nm)) - 1, data = sp_data_site,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
601 family = quasipoisson(link = "log"), control = list(maxit = 100))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
602 } else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
603 glm_obj_site <- glm(COUNT ~ offset(log(nm)) - 1, data = sp_data_site,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
604 family = quasipoisson(link = "log"), control = list(maxit = 100))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
605 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
606
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
607 sp_data_site[, "FITTED"] <- predict.glm(glm_obj_site, newdata = sp_data_site,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
608 type = "response")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
609 sp_data_site[, "COUNT_IMPUTED"] <- sp_data_site$COUNT
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
610 sp_data_site[is.na(sp_data_site$COUNT), "COUNT_IMPUTED"] <- sp_data_site$FITTED[is.na(sp_data_site$COUNT)]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
611
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
612 # add fitted value for missing mid-week data
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
613 sp_data_site <- sp_data_site[!paste(sp_data_site$DAY_WEEK, sp_data_site$COUNT) %in%
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
614 c("1 NA", "2 NA", "3 NA", "5 NA", "6 NA", "7 NA"), ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
615
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
616 # remove all added mid-week values for weeks with real count
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
617 # (observation)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
618 sp_data_site$site_week <- paste(sp_data_site$SITE, sp_data_site$WEEK,
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
619 sep = "_")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
620 siteweekcount <- aggregate(sp_data_site$COUNT, by = list(sp_data_site$site_week),
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
621 function(x) sum(!is.na(x)) > 0)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
622 sp_data_site <- sp_data_site[!(is.na(sp_data_site$COUNT) & (sp_data_site$site_week %in%
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
623 siteweekcount$Group.1[siteweekcount$x == TRUE])), names(sp_data_site) !=
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
624 "site_week"]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
625
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
626 # Compute the regional gam index
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
627 print(paste("Compute index for",sp_data_site$SPECIES[1],"at year", y,"for",length(unique(sp_data_site$SITE)),"sites:",Sys.time()))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
628 regional_gam_index <- trap_index(sp_data_site, data_col = "COUNT_IMPUTED",
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
629 time_col = "DAYNO", by_col = c("SPECIES", "SITE", "YEAR"))
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
630
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
631 cumu_index <- merge(regional_gam_index, pro_count_agg, by = c("SITE"),
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
632 all.x = TRUE, sort = FALSE)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
633 names(cumu_index) <- c("SITE", "SPECIES", "YEAR", "regional_gam", "prop_pheno_sampled")
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
634
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
635 cumu_index <- cumu_index[order(cumu_index$SITE), ]
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
636
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
637 # bind if exist else create
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
638 if ("cumullated_indices" %in% ls()) {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
639 cumullated_indices <- rbind(cumullated_indices, cumu_index)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
640 } else {
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
641 cumullated_indices <- cumu_index
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
642 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
643
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
644 }
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
645
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
646 return(cumullated_indices)
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
647
b416a363a2d5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/regionalgam commit ffe42225fff8992501b743ebe2c78e50fddc4a4e
ecology
parents:
diff changeset
648 }