annotate graph_link_var.r @ 0:e5552099d0e3 draft

"planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
author ecology
date Tue, 27 Jul 2021 16:57:02 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
1 #Rscript
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
2
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
3 ################################################
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
4 ## Link between variables and themselves ##
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
5 ################################################
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
6
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
7 #####Packages : ggplot2
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
8 # Cowplot
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
9 # Car
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
10 # faraway
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
11 # dplyr
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
12 # GGally
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
13 # FactoMiner
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
14 # factoextra
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
15 # ggcorrplot
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
16
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
17 #####Load arguments
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
18
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
19 args <- commandArgs(trailingOnly = TRUE)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
20
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
21 if (length(args) == 0) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
22 stop("This tool needs at least one argument")
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
23 }else{
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
24 table <- args[1]
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
25 hr <- args[2]
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
26 colli <- as.logical(args[3])
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
27 vif <- as.logical(args[4])
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
28 pca <- as.logical(args[5])
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
29 interr <- as.logical(args[6])
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
30 auto <- as.logical(args[7])
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
31 spe <- as.numeric(args[8])
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
32 col <- as.numeric(strsplit(args[9], ",")[[1]])
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
33 var <- as.numeric(args[10])
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
34 var2 <- as.numeric(args[11])
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
35 var4 <- as.numeric(args[12])
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
36 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
37
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
38 if (hr == "false") {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
39 hr <- FALSE
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
40 }else{
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
41 hr <- TRUE
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
42 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
43
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
44 if (length(col) == 1) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
45 stop("Please select two or more numerical columns")
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
46 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
47
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
48 #####Import data
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
49 data <- read.table(table, sep = "\t", dec = ".", header = hr, fill = TRUE, encoding = "UTF-8")
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
50 if (vif | pca) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
51 data_active <- data[col]
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
52 #Define the active individuals and the active variables for the PCA
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
53 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
54
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
55 if (colli | interr) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
56 colspe <- colnames(data)[spe]
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
57 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
58
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
59 if (colli) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
60 data_num <- data[col]
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
61 data_num$species <- data[, spe]
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
62 data_num <- data_num[grep("^$", data_num$spe, invert = TRUE), ]
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
63 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
64
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
65 if (interr | auto) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
66 colvar <- colnames(data)[var]
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
67 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
68
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
69 if (interr) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
70 colvar2 <- colnames(data)[var2]
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
71 colvar4 <- colnames(data)[var4]
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
72 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
73
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
74 #####Your analysis
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
75
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
76 ####Independence of the observations####
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
77
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
78 acf_tb <- function(data, var) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
79 obj <- acf(data[, var], na.action = na.pass)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
80 return(obj)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
81 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
82
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
83 acf_df <- function(data, var) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
84
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
85 tb <- data.frame(acf = acf_tb(data, var)$acf, lag = acf_tb(data, var)$lag)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
86
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
87 return(tb) # Lag: intervalle temporel entre mesures, fréquence à laquelle on mesure l'auto corrélation.
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
88 # ACF: indépendance temporelle
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
89 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
90
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
91 autocorr <- function(var1, var2) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
92 cat("\nACF\n", var2$acf, file = "acf.txt", fill = 1, append = TRUE)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
93 graph <- ggplot2::ggplot() +
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
94 ggplot2::geom_bar(ggplot2::aes(x = var2$lag, y = var2$acf), stat = "identity", position = "identity", fill = "midnightblue") +
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
95 ggplot2::geom_hline(mapping = ggplot2::aes(yintercept = qnorm((1 + 0.95) / 2) / sqrt(var1$n.used)),
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
96 linetype = "dashed") + # calcul interval de confiance à 95% sans correction du bruit blanc.
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
97 ggplot2::geom_hline(mapping = ggplot2::aes(yintercept = -qnorm((1 + 0.95) / 2) / sqrt(var1$n.used)), linetype = "dashed") + ggplot2::labs(title = "Autocorrelation") + ggplot2::xlab("lag") + ggplot2::ylab("acf")
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
98 ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5))
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
99
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
100 ggplot2::ggsave("autocorrelation.png", graph)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
101 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
102
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
103 ####Interractions####
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
104
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
105 graph <- function(data, var1, var2, var3) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
106 graph <- ggplot2::ggplot(data, ggplot2::aes_string(x = var1, y = var2, group = var3, color = var3)) +
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
107 ggplot2::geom_point() +
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
108 ggplot2::geom_smooth(method = lm, se = FALSE) +
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
109 ggplot2::theme(plot.title = ggplot2::element_text(color = "black", size = 12, face = "bold"))
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
110
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
111 return(graph)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
112 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
113
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
114 # Put multiple panels
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
115 interraction <- function(data, var1, var2, var3, var4) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
116 cat("\nSpecies\n", spe, file = "Species.txt", fill = 1, append = TRUE)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
117 if (mult1) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
118 for (spe in unique(data[, var3])) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
119 data_cut <- data[data[, var3] == spe, ]
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
120 mult_graph <- graph(data_cut, var1, var2, var3) + ggplot2::facet_grid(cols = ggplot2::vars(data_cut[, var4]), scales = "free") +
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
121 cowplot::background_grid(major = "xy", minor = "none") +
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
122 cowplot::panel_border() + ggplot2::ggtitle("Interactions")
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
123
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
124 ggplot2::ggsave(paste("interaction_of_", spe, ".png"), mult_graph, width = 10, height = 7)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
125 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
126 }else{
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
127 mult_graph <- graph(data, var1, var2, var3) + ggplot2::facet_grid(rows = ggplot2::vars(data[, var3]), cols = ggplot2::vars(data[, var4]), scales = "free") +
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
128 cowplot::background_grid(major = "xy", minor = "none") +
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
129 cowplot::panel_border() + ggplot2::ggtitle("Interactions")
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
130
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
131 ggplot2::ggsave("interraction.png", mult_graph)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
132 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
133 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
134
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
135 ####Collinearity among covariates####
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
136 # Create the plots
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
137
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
138 coli <- function(data, var) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
139 if (mult2) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
140 cat("\nThere is not enough data on these species they appear too few times in the tabular-file\n", file = "Data.txt", fill = 1, append = TRUE)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
141 for (spe in unique(data$species)) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
142 nb_spe <- sum(data$species == spe)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
143 if (nb_spe <= 2) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
144 cat(spe, file = "Data.txt", fill = 1, append = TRUE)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
145 }else{
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
146 data_cut <- data[data$species == spe, ]
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
147 nb <- ncol(data_cut)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
148 data_num <- data_cut[, -nb]
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
149 graph <- GGally::ggpairs(data_num, ggplot2::aes(color = data_cut$species),
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
150 lower = list(continuous = "points"), axisLabels = "internal")
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
151
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
152 ggplot2::ggsave(paste0("collinarity_of_", spe, ".png"), graph, width = 20, height = 15)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
153 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
154 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
155
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
156 }else{
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
157 nb <- ncol(data)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
158 data_cut <- data[, -nb]
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
159 graph <- GGally::ggpairs(data_cut, ggplot2::aes(color = data[, var]),
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
160 lower = list(continuous = "points"), axisLabels = "internal") +
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
161 ggplot2::scale_colour_manual(values = c("#00AFBB", "#E7B800", "#FC4E07")) +
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
162 ggplot2::scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
163
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
164 ggplot2::ggsave("collinarity.png", graph)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
165 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
166 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
167
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
168 ####PCA method####
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
169
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
170 plot_pca <- function(data) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
171 #Correlation circle
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
172 graph_corr <- factoextra::fviz_pca_var(active_data(data), col.var = "cos2",
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
173 gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
174 repel = TRUE #Avoid text overlap
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
175 )
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
176 ggplot2::ggsave("Pca_circle.png", graph_corr)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
177 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
178
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
179 plot_qual <- function(data) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
180 #PCA results for variables
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
181 var <- factoextra::get_pca_var(active_data(data))
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
182
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
183 #representation quality
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
184 graph_quality <- ggcorrplot::ggcorrplot(var$cos2[!apply(var$cos2, 1, anyNA), ], method = "circle",
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
185 ggtheme = ggplot2::theme_gray,
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
186 colors = c("#00AFBB", "#E7B800", "#FC4E07"))
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
187
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
188 ggplot2::ggsave("Pca_quality.png", graph_quality)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
189 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
190
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
191 #### Variance inflation factor ####
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
192
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
193 myvif <- function(mod) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
194 v <- vcov(mod)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
195 assign <- attributes(model.matrix(mod))$assign
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
196 if (names(coefficients(mod)[1]) == "(Intercept)") {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
197 v <- v[-1, -1]
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
198 assign <- assign[-1]
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
199 } else warning("No intercept: vifs may not be sensible.")
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
200 terms <- labels(terms(mod))
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
201 n_terms <- length(terms)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
202 if (n_terms < 2) stop("The model contains fewer than 2 terms")
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
203 if (length(assign) > dim(v)[1]) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
204 diag(tmp_cor) <- 0
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
205 if (any(tmp_cor == 1.0)) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
206 return("Sample size is too small, 100% collinearity is present")
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
207 } else {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
208 return("Sample size is too small")
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
209 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
210 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
211 r <- cov2cor(v)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
212 detr <- det(r)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
213 result <- matrix(0, n_terms, 3)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
214 rownames(result) <- terms
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
215 colnames(result) <- c("GVIF", "Df", "GVIF^(1/2Df)")
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
216 for (term in 1:n_terms) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
217 subs <- which(assign == term)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
218 result[term, 1] <- det(as.matrix(r[subs, subs])) * det(as.matrix(r[-subs, -subs])) / detr
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
219 result[term, 2] <- length(subs)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
220 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
221 if (all(result[, 2] == 1)) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
222 result <- data.frame(GVIF = result[, 1])
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
223 } else {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
224 result[, 3] <- result[, 1] ^ (1 / (2 * result[, 2]))
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
225 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
226 invisible(result)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
227 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
228 corvif1 <- function(dataz) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
229 dataz <- as.data.frame(dataz)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
230 #correlation part
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
231 tmp_cor <- cor(dataz, use = "complete.obs")
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
232 return(tmp_cor)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
233 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
234 corvif2 <- function(dataz) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
235 dataz <- as.data.frame(dataz)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
236 #vif part
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
237 form <- formula(paste("fooy ~ ", paste(strsplit(names(dataz), " "), collapse = " + ")))
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
238 dataz <- data.frame(fooy = 1, dataz)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
239 lm_mod <- lm(form, dataz)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
240
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
241 return(myvif(lm_mod))
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
242 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
243
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
244 #Autocorrelation
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
245 if (auto) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
246 obj1 <- acf_tb(data, var = colvar)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
247 obj2 <- acf_df(data, var = colvar)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
248 autocorr(var1 = obj1, var2 = obj2)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
249 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
250
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
251 if (interr) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
252 #Interractions
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
253 mult1 <- ifelse(length(unique(data[, colspe])) <= 6, FALSE, TRUE)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
254 interraction(data, var1 = colvar, var2 = colvar2, var3 = colspe, var4 = colvar4)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
255 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
256
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
257 #Collinearity
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
258 if (colli) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
259 mult2 <- ifelse(length(unique(data[, spe])) < 3, FALSE, TRUE)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
260 coli(data = data_num, var = spe)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
261 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
262
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
263 #PCA
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
264 if (pca) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
265 active_data <- function(data) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
266 #Calcul of PCA for the active data
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
267 res_pca <- FactoMineR::PCA(data, graph = FALSE)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
268
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
269 return(res_pca)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
270 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
271
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
272 #eigenvalue
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
273 eig_val <- capture.output(factoextra::get_eigenvalue(active_data(data_active)))
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
274
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
275 cat("\nwrite table with eigenvalue. \n--> \"", paste(eig_val, "\"\n", sep = ""), file = "valeurs.txt", sep = "", append = TRUE)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
276
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
277 plot_pca(data_active)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
278 plot_qual(data_active)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
279 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
280
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
281 #VIF
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
282 if (vif) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
283 #Compute 2 tables#
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
284 tb_corr <- as.data.frame(corvif1(dataz = data_active))
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
285 tb_corr <- cbind(x = rownames(tb_corr), tb_corr)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
286 tb_vif <- corvif2(dataz = data_active)
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
287
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
288 write.table(tb_corr, "corr.tabular", row.names = FALSE, quote = FALSE, sep = "\t", dec = ".", fileEncoding = "UTF-8")
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
289
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
290 if (all(is.na(tb_vif))) {
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
291 tb_vif <- NULL
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
292 cat("Vif couldn't be calculated, selected data isn't correlated")
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
293 }else{
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
294 write.table(tb_vif, "vif.tabular", row.names = FALSE, quote = FALSE, sep = "\t", dec = ".", fileEncoding = "UTF-8")
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
295 }
e5552099d0e3 "planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
ecology
parents:
diff changeset
296 }