-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc12_02_cluster_kmean_bootstrap_global_slurm.R
97 lines (69 loc) · 2.82 KB
/
sc12_02_cluster_kmean_bootstrap_global_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 ---------------------------------------------------------------
usePackage("data.table")
usePackage("tidyverse")
usePackage("doMC")
usePackage("foreach")
usePackage("fpc")
# 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"))
# Define seed
rnum <- as.numeric(Sys.getenv(c("SEED")))
# Define number of starts
nstr <- as.numeric(Sys.getenv(c("NSTART")))
# Define number of cluster
ncl <- as.numeric(Sys.getenv(c("NCL")))
# Define number of boot samples
min_B <- as.numeric(Sys.getenv(c("MINB")))
max_B <- as.numeric(Sys.getenv(c("MAXB")))
# Directories -------------------------------------------------------------
# Define path
# Yale-Server
basin_path <- paste0("/my/working/directory/data/partitional_clustering/",
vset, "/basin_", cunit, "_", basinID)
k_path <- paste0(basin_path, "/kmeans")
stb_path <- paste0(basin_path, "/stability")
if(!dir.exists(stb_path)) dir.create(stb_path)
# Input data --------------------------------------------------------------
# Load scaled environmental variables
basin <- fread(paste0(basin_path, "/basin_envVar_sc_", cunit, "_", basinID,
"_", vset, ".csv"))
# Prepare data ------------------------------------------------------------
# Convert data table to matrix
data <- as.matrix(basin[,4:ncol(basin)])
# Cluster stability -------------------------------------------------------
n <- nrow(data)
boot <- min_B:max_B
registerDoMC(20)
bsamp <- foreach(i = 1:20) %dopar% {
sample(n, n, replace = TRUE)
}
bc1 <- foreach(i = 1:20) %dopar% {
mdata <- data[bsamp[[i]], ]
set.seed(rnum)
kmeans(mdata, centers = ncl, nstart=nstr, iter.max = 500)
}
# Name the lists
names(bc1) <- paste0("b=", boot)
names(bsamp) <- paste0("b=", boot)
# Save boot result --------------------------------------------------------
write_rds(bsamp, paste0(stb_path, "/boot_samples_", min_B, "_", max_B, "B_", cunit, "_",
basinID, "_", vset ,"_seed", rnum, "_nstart",
nstr, ".rds"))
write_rds(bc1, paste0(stb_path, "/kmean_boot_", min_B, "_", max_B, "B_", cunit, "_",
basinID, "_", vset ,"_seed", rnum, "_nstart",
nstr, ".rds"))
# Exit R ------------------------------------------------------------------
quit(save = "no")