-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc01_correlation_envVar_slurm.R
97 lines (61 loc) · 3.07 KB
/
sc01_correlation_envVar_slurm.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Set working directory
setwd("/my/working/directory/data")
# Functions ---------------------------------------------------------------
# Function to check if the required packages are installed and to load the library
usePackage <- function(p){
if (!is.element(p, installed.packages()[,1])) install.packages(p, dep = TRUE)
library(p, character.only = TRUE)
}
# Libraries ---------------------------------------------------------------
# Load libraries
usePackage("data.table")
usePackage("tidyverse")
usePackage("corrr")
usePackage("lubridate")
# Select basin and dataset ------------------------------------------------
# Define computational unit
cunit <- Sys.getenv(c("CUNIT"))
# Define basin ID
basinID <- Sys.getenv(c("BID"))
# Define the set of variables
vset <- Sys.getenv(c("VSET"))
# Directories -------------------------------------------------------------
# Path to environmental variable dataset
basin_path <- paste0("/my/working/directory/data/partitional_clustering/",
vset, "/basin_", cunit, "_", basinID)
# Create paths for correlation output
corr_path <- paste0(basin_path, "/correlation")
if(!dir.exists(corr_path)) dir.create(corr_path)
# Input data --------------------------------------------------------------
basin_sc <- fread(paste0(basin_path, "/basin_envVar_sc_", cunit, "_",
basinID,".csv"))
# Correlation analysis ----------------------------------------------------
#t0 <- now()
#pear_corr_matrix <- correlate(basin_sc[,4:ncol(basin_sc)],
# use = 'pairwise.complete.obs',
# method = "pearson",
# diagonal = NA) # %>%
# rearrange() # rearrange by correlations
#t1 <- interval(t0, now()) %>%
# as.period() %>% round()
#print(paste0("Pearson's correlation finished in ", t1 ))
#pear_corr_greater_07 <- pear_corr_matrix %>%
# mutate_if(is.numeric, funs(replace(., abs(.)<=0.7, NA)))
#t0 <- now()
sman_corr_matrix <- correlate(basin_sc[,4:ncol(basin_sc)],
use = 'pairwise.complete.obs',
method = "spearman",
diagonal = NA) # %>%
# rearrange() # rearrange by correlations
t1 <- interval(t0, now()) %>%
as.period() %>% round()
print(paste0("Spearman rank correlation finished in ", t1 ))
sman_corr_greater_07 <- sman_corr_matrix %>%
mutate_if(is.numeric, funs(replace(., abs(.)<=0.7, NA)))
# Save output -------------------------------------------------------------
fwrite(pear_corr_matrix, paste0(corr_path, "/pearson_corr_", cunit, "_", basinID , "_", vset, ".csv"))
fwrite(pear_corr_greater_07, paste0(corr_path, "/pearson_greater_07_", cunit, "_", basinID , "_", vset, ".csv"))
fwrite(sman_corr_matrix, paste0(corr_path, "/spearman_corr_", cunit, "_", basinID , "_", vset, ".csv"))
fwrite(sman_corr_greater_07, paste0(corr_path, "/spearman_greater_07_", cunit, "_", basinID , "_", vset, ".csv"))
# Exit --------------------------------------------------------------------
quit(save = "no")