annotate FunctExeCalcGLMGalaxy.r @ 6:b40fecef616b draft default tip

"planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 65ab5b6fe84871db0fe18244d805cea19a44e830"
author ecology
date Sat, 26 Jun 2021 07:20:12 +0000
parents 8db02073b671
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
1 #Rscript
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
2
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
3 #####################################################################################################################
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
4 #####################################################################################################################
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
5 ################################# Compute a Generalized Linear Model from your data #################################
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
6 #####################################################################################################################
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
7 #####################################################################################################################
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
8
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
9 ###################### Packages
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
10 suppressMessages(library(multcomp))
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
11 suppressMessages(library(DHARMa))
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
12 suppressMessages(library(glmmTMB)) ###Version: 0.2.3
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
13 suppressMessages(library(gap))
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
14
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
15
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
16 ###################### Load arguments and declaring variables
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
17
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
18 args <- commandArgs(trailingOnly = TRUE)
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
19
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
20 if (length(args) < 10) {
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
21 stop("At least 4 arguments must be supplied : \n- two input dataset files (.tabular) : metrics table and unitobs table \n- Interest variable field from metrics table \n- Response variable from unitobs table.", call. = FALSE) # if no args -> error and exit1
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
22
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
23 } else {
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
24 import_data <- args[1] ###### file name : metrics table
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
25 import_unitobs <- args[2] ###### file name : unitobs informations
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
26 colmetric <- as.numeric(args[3]) ###### Selected interest metric for GLM
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
27 list_fact <- strsplit(args [4], ",")[[1]] ###### Selected response factors for GLM
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
28 list_rand <- strsplit(args [5], ",")[[1]] ###### Selected randomized response factors for GLM
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
29 col_fact_ana <- args[6] ####### (optional) Selected splitting factors for GLMs
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
30 distrib <- args[7] ###### (optional) Selected distribution for GLM
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
31 glm_out <- args[8] ###### (Optional) GLM object as Rdata output ?
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
32 aggreg <- args[9] ###### Aggregation level of the data table
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
33 source(args[10]) ###### Import functions
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
34
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
35 }
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
36
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
37
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
38 #### d_ata must be a dataframe with at least 3 variables : unitobs representing location and year ("observation.unit"), species code ("species.code") and abundance ("number")
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
39
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
40
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
41 #Import des données / Import data
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
42 obs <- read.table(import_data, sep = "\t", dec = ".", header = TRUE, encoding = "UTF-8") #
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
43 obs[obs == -999] <- NA
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
44 metric <- colnames(obs)[colmetric]
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
45 tab_unitobs <- read.table(import_unitobs, sep = "\t", dec = ".", header = TRUE, encoding = "UTF-8")
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
46 tab_unitobs[tab_unitobs == -999] <- NA
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
47
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
48 if (col_fact_ana != "None") {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
49 fact_ana <- colnames(tab_unitobs)[as.numeric(col_fact_ana)]
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
50 if (class(tab_unitobs[fact_ana]) == "numeric" || fact_ana == "observation.unit") {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
51 stop("Wrong chosen separation factor : Analysis can't be separated by observation unit or numeric factor")
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
52 }
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
53 }else{
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
54 fact_ana <- col_fact_ana
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
55 }
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
56
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
57 vars_data1 <- NULL
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
58 err_msg_data1 <- "The input metrics dataset doesn't have the right format. It needs to have at least the following 2 variables :\n- observation.unit (or year and site)\n- numeric or integer metric\n"
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
59 check_file(obs, err_msg_data1, vars_data1, 2)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
60
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
61 vars_data2 <- c("observation.unit", list_fact, list_rand)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
62 err_msg_data2 <- "The input unitobs dataset doesn't have the right format. It needs to have at least the following 2 variables :\n- observation.unit (or year and site)\n- factors used in GLM (habitat, year and/or site)\n"
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
63 check_file(tab_unitobs, err_msg_data2, vars_data2[vars_data2 != "None"], 2)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
64
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
65 if (all(c(list_fact, list_rand) == "None")) {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
66 stop("GLM needs to have at least one response variable.")
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
67 }
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
68
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
69 if (list_fact[1] == "None" || all(is.element(list_fact, list_rand))) {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
70 stop("GLM can't have only random effects.")
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
71 }
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
72
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
73
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
74 ####################################################################################################
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
75 ######### Computing Generalized Linear Model ## Function : glm_community ############
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
76 ####################################################################################################
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
77
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
78 glm_community <- function(metrique, list_fact, list_rand, fact_ana, distrib, tab_metrics, tab_metrique, tab_unitobs, unitobs = "observation.unit", nb_name = "number") {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
79 ## Purpose: Monitoring steps for GLM on community data
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
80 ## ----------------------------------------------------------------------
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
81 ## Arguments: metrique : selected metric
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
82 ## list_fact : Factors for GLM
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
83 ## list_rand : Random factors for GLM
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
84 ## fact_ana : Separation factor for GLMs
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
85 ## distrib : selected distribution for model
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
86 ## tab_metrics : data table metrics
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
87 ## tab_metrique : data table's name
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
88 ## tab_unitobs : data table unitobs
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
89 ## ----------------------------------------------------------------------
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
90 ## Author: Yves Reecht, Date: 18 août 2010, 15:59 modified by Coline ROYAUX 04 june 2020
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
91
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
92 tmpd_ata <- tab_metrics
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
93
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
94 out_fact <- .GlobalEnv$organise_fact(list_rand = list_rand, list_fact = list_fact)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
95 resp_fact <- out_fact[[1]]
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
96 list_f <- out_fact[[2]]
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
97 list_fact <- out_fact[[3]]
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
98
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
99 ##Creating model's expression :
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
100 expr_lm <- eval(parse(text = paste(metrique, "~", resp_fact)))
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
101
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
102 ##Creating analysis table :
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
103
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
104 list_fact_tab <- c(list_fact, fact_ana)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
105 list_fact_tab <- list_fact_tab[list_fact_tab != "None"]
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
106
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
107 if (all(is.na(match(tmpd_ata[, unitobs], tab_unitobs[, unitobs])))) {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
108 stop("Observation units doesn't match in the two input tables")
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
109 }
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
110
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
111 if (! is.element("species.code", colnames(tmpd_ata))) {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
112 col <- c(unitobs, metrique)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
113 tmpd_ata <- cbind(tmpd_ata[, col], tab_unitobs[match(tmpd_ata[, unitobs], tab_unitobs[, unitobs]), list_fact_tab])
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
114 colnames(tmpd_ata) <- c(col, list_fact_tab)
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
115
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
116 for (i in list_fact_tab) {
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
117 switch(i,
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
118 tmpd_ata[, i] <- as.factor(tmpd_ata[, i]))
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
119 }
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
120 }else{
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
121 stop("Warning : wrong data frame, data frame should be aggregated by observation unit (year and site)")
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
122 }
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
123
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
124 ## Suppress unsed 'levels' :
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
125 tmpd_ata <- .GlobalEnv$drop_levels_f(tmpd_ata)
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
126
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
127 ## Automatic choice of distribution if none is selected by user :
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
128
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
129 chose_distrib <- .GlobalEnv$distrib_choice(distrib = distrib, metrique = metrique, data = tmpd_ata)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
130
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
131 if (fact_ana != "None" && nlevels(tmpd_ata[, fact_ana]) > 1) {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
132 ana_cut <- levels(tmpd_ata[, fact_ana])
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
133 }else{
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
134 ana_cut <- NULL
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
135 }
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
136
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
137 ##Create results table :
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
138 lev <- unlist(lapply(list_f, FUN = function(x) {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
139 levels(tmpd_ata[, x])
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
140 }))
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
141 row <- c("global", ana_cut)
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
142
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
143 if (is.element("year", list_f) && ! is.element("year", list_rand)) {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
144 tab_sum <- .GlobalEnv$create_res_table(list_rand = list_rand, list_fact = list_fact, row = row, lev = unlist(c("year", lev)), distrib = chose_distrib)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
145 }else{
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
146 tab_sum <- .GlobalEnv$create_res_table(list_rand = list_rand, list_fact = list_fact, row = row, lev = lev, distrib = chose_distrib)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
147 }
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
148 ### creating rate table
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
149 tab_rate <- data.frame(analysis = row, complete_plan = NA, balanced_plan = NA, NA_proportion_OK = NA, no_residual_dispersion = NA, uniform_residuals = NA, outliers_proportion_OK = NA, no_zero_inflation = NA, observation_factor_ratio_OK = NA, enough_levels_random_effect = NA, rate = NA)
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
150
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
151 ##plural analysis
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
152 for (cut in ana_cut) {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
153 cutd_ata <- tmpd_ata[grep(cut, tmpd_ata[, fact_ana]), ]
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
154 cutd_ata <- .GlobalEnv$drop_levels_f(cutd_ata)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
155
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
156 res <- ""
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
157 resy <- ""
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
158
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
159 if (list_rand[1] != "None") {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
160 res <- tryCatch(glmmTMB::glmmTMB(expr_lm, family = chose_distrib, data = cutd_ata), error = function(e) {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
161 })
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
162 if (is.element("year", list_f) && ! is.element("year", list_rand)) { #Model with year as continuous
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
163 cutd_ata$year <- as.numeric(cutd_ata$year)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
164 resy <- tryCatch(glmmTMB::glmmTMB(expr_lm, family = chose_distrib, data = cutd_ata), error = function(e) {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
165 })
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
166 cutd_ata$year <- as.factor(cutd_ata$year)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
167 }else{
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
168 resy <- ""
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
169 }
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
170
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
171 }else{
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
172 res <- tryCatch(glm(expr_lm, data = cutd_ata, family = chose_distrib), error = function(e) {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
173 })
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
174 if (is.element("year", list_f)) { #Model with year as continuous
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
175 cutd_ata$year <- as.numeric(cutd_ata$year)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
176 resy <- tryCatch(glm(expr_lm, family = chose_distrib, data = cutd_ata), error = function(e) {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
177 })
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
178 cutd_ata$year <- as.factor(cutd_ata$year)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
179 }else{
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
180 resy <- ""
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
181 }
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
182
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
183 }
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
184
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
185 ## Write results :
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
186 if (! is.null(res)) {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
187 file_save_glm_cut <- paste("GLM_", cut, ".Rdata", sep = "")
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
188 save(res, file = file_save_glm_cut)
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
189
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
190 tab_sum <- sorties_lm_f(obj_lm = res, obj_lmy = resy, tab_sum = tab_sum, metrique = metrique,
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
191 fact_ana = fact_ana, cut = cut, col_ana = "analysis", lev = lev, #modSel = iFactGraphSel, list_fact_sel = list_fact_sel,
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
192 list_fact = list_fact, list_rand = list_rand,
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
193 d_ata = cutd_ata)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
194
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
195 tab_rate[tab_rate[, "analysis"] == cut, c(2:11)] <- .GlobalEnv$note_glm_f(data = cutd_ata, obj_lm = res, metric = metrique, list_fact = list_fact, details = TRUE)
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
196
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
197 }else{
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
198 cat("\nCannot compute GLM for level", cut, "Check if one or more factor(s) have only one level, or try with another distribution for the model in advanced settings \n\n")
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
199 }
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
200
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
201 }
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
202
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
203 ## Global analysis :
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
204
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
205 res_g <- ""
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
206 res_gy <- ""
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
207
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
208 if (list_rand[1] != "None") {
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
209 res_g <- glmmTMB::glmmTMB(expr_lm, family = chose_distrib, data = tmpd_ata)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
210 if (is.element("year", list_fact) && ! is.element("year", list_rand)) { #Model with year as continuous
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
211 yr <- tmpd_ata$year
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
212 tmpd_ata$year <- as.numeric(tmpd_ata$year)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
213 res_gy <- glmmTMB::glmmTMB(expr_lm, family = chose_distrib, data = tmpd_ata)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
214 tmpd_ata$year <- as.factor(tmpd_ata$year)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
215 tmpd_ata$year <- yr
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
216 }else{
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
217 res_gy <- ""
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
218 }
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
219
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
220 }else{
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
221 res_g <- glm(expr_lm, data = tmpd_ata, family = chose_distrib)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
222 if (is.element("year", list_fact)) { #Model with year as continuous
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
223 yr <- tmpd_ata$year
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
224 tmpd_ata$year <- as.numeric(tmpd_ata$year)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
225 res_gy <- glm(expr_lm, family = chose_distrib, data = tmpd_ata)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
226 tmpd_ata$year <- yr
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
227 }else{
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
228 res_gy <- ""
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
229 }
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
230 }
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
231
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
232 ## write results :
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
233
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
234 save(res_g, file = "global_GLM.Rdata")
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
235
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
236 tab_sum <- sorties_lm_f(obj_lm = res_g, obj_lmy = res_gy, tab_sum = tab_sum, metrique = metrique,
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
237 fact_ana = fact_ana, cut = "global", col_ana = "analysis", lev = lev, #modSel = iFactGraphSel, list_fact_sel = list_fact_sel,
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
238 list_fact = list_fact, list_rand = list_rand,
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
239 d_ata = tmpd_ata)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
240
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
241 tab_rate[tab_rate[, "analysis"] == "global", c(2:11)] <- .GlobalEnv$note_glm_f(data = tmpd_ata, obj_lm = res_g, metric = metrique, list_fact = list_fact, details = TRUE)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
242 .GlobalEnv$note_glms_f(tab_rate = tab_rate, expr_lm = expr_lm, obj_lm = res_g, file_out = TRUE)
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
243
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
244 ## simple statistics and infos :
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
245 filename <- "GLMSummaryFull.txt"
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
246 info_stats_f(filename = filename, d_ata = tmpd_ata, agreg_level = aggreg, type = "stat",
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
247 metrique = metrique, fact_graph = fact_ana, #fact_graph_sel = modSel,
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
248 list_fact = list_fact)#, list_fact_sel = list_fact_sel)
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
249
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
250 tab_sum$separation <- fact_ana
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
251
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
252 return(tab_sum)
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
253
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
254 }
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
255
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
256 ################# Analysis
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
257
4
44b9069775ca "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 3df1978827a91be30e815dee2ed83a92862d1b1c"
ecology
parents: 0
diff changeset
258 tab <- glm_community(metrique = metric, list_fact = list_fact, list_rand = list_rand, fact_ana = fact_ana, distrib = distrib, tab_metrics = obs, tab_metrique = aggreg, tab_unitobs = tab_unitobs, nb_name = "number")
0
f0dc3958e65d "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
ecology
parents:
diff changeset
259
5
8db02073b671 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit cd938a05cb066b16f2518159fa0ff5e3ed8a2b2b"
ecology
parents: 4
diff changeset
260 write.table(tab, "GLMSummary.tabular", row.names = FALSE, quote = FALSE, sep = "\t", dec = ".", fileEncoding = "UTF-8")