-
Notifications
You must be signed in to change notification settings - Fork 1
/
Run_GAM_predict.R
183 lines (149 loc) · 5.01 KB
/
Run_GAM_predict.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#### Load packages ####
library(mgcv)
library(readr)
library(sspm)
library(tidyr)
library(arrow)
library(foreach)
library(doParallel)
library(parallelly)
library(ranger)
library(tidyverse)
library(kableExtra)
library(dplyr)
#### 0. Package installation for cluster ####
# # Package names
# packages <- c("readr", "tidyr", "foreach", "doParallel", "parallelly","ranger","tidyverse","kableExtra","arrow")
#
# # Install packages not yet installed
# installed_packages <- packages %in% rownames(installed.packages())
# if (any(installed_packages == FALSE)) {
# install.packages(packages[!installed_packages])
# }
Size= as.numeric(Sys.getenv('SIZE')) # Get size of Landscape
#### 1. Create and Start Cluster ####
#create the cluster
# n.cores <- parallelly::availableCores()/2
# For windows
n.cores <- as.numeric(Sys.getenv('OMP_NUM_THREADS'))
my.cluster <- parallel::makeCluster(
n.cores,
type = "PSOCK"
)
#check cluster definition (optional)
print(my.cluster)
#register it to be used by %dopar%
doParallel::registerDoParallel(cl = my.cluster)
#check if it is registered (optional)
foreach::getDoParRegistered()
#how many workers are available? (optional)
foreach::getDoParWorkers()
# memory.limit(n.cores*4000)
memory.limit(300000)
Sys.time()
#### 2. Define grid size and number of years ####
start_year =1991
years = files <- as.numeric(length(list.files('sim/', pattern = ".",all.files = FALSE, recursive = TRUE, full.names = TRUE)))
size = Size
trawl_data <- readr::read_table("PB_fall.dat")
trawl_data$year_f <- factor(trawl_data$year)
#Filtering
# trawl_data <- trawl_data[trawl_data$year %in% 1991:2000,]
dat_grid_x_y <- as.data.frame(expand_grid(long=seq(0.5,size,by=1),
lat= seq(0.5,size,by=1)))
dat_grid_year <- c(start_year:(start_year+years-1))
#### testing ####
# years_trawl <- unique(trawl_data$year)
#
# trawl_data <- trawl_data[trawl_data$year %in% 1991:2000,]
#
# diff <- trawl_data-trawl_data_1
# sum(trawl_data$biomass)
# sum(trawl_data_1$biomass)
# nyears <- as.data.frame(table(trawl_data$year))
# hist(trawl_data$year, breaks = 501)
####
head(trawl_data)
gc()
trawl_data$year_f <- factor(trawl_data$year)
# Sys.time()
# simple_gam <- bam((biomass/1000)~te(long, lat, year_f, bs= c("tp","re"), d = c(2,1)), family= "tw", data = trawl_data, method="REML")
# Sys.time()
#### Create models ####
simple_gam <<- list()
#define number of data frames to split into
split=5
n_chunk <- length(dat_grid_year)/split
#split data frame into groups per year
split_data <- trawl_data %>%
group_split(year)
# for (n_chunk in 1:n_chunk) {
# print(n_chunk)
# chunk <- split_data[(((n_chunk-1)*split)+1):(n_chunk*split)] %>% reduce(full_join)
# simple_gam[[n_chunk]] <- bam((biomass/1000)~te(long, lat, year_f, bs= c("tp","re"), d = c(2,1)), family= "tw", data = chunk, method="REML")
# }
#
# Sys.time()
# split_gams <- function(years_gam=trawl_data$year_f,dat_grid_year=dat_grid_year,split=10){
# #split data frame into n equal-sized data frames
# split_data <- trawl_data %>%
# group_split(year)
# for (n_chunk in 1:n_chunk) {
# print(n_chunk)
# chunk <- split_data[(((n_chunk-1)*split)+1):(n_chunk*split)] %>% reduce(full_join)
# simple_gam[[n_chunk]] <- bam((biomass/1000)~te(long, lat, year_f, bs= c("tp","re"), d = c(2,1)), family= "tw", data = chunk, method="REML")
# }
#
library(tictoc)
tictoc::tic()
# Sys.time()
simple_gam <- foreach(
n_chunk = 1:n_chunk,
.packages = c('mgcv','dplyr','purrr')
) %dopar% {
print(n_chunk)
chunk <- split_data[(((n_chunk-1)*split)+1):(n_chunk*split)] %>% reduce(full_join)
simple_gam[[n_chunk]] <- bam((biomass/1000)~te(long, lat, year, bs= c("tp","tp"), d = c(2,1)), family= "tw", data = chunk, method="REML")
gc()
return(simple_gam[[n_chunk]])
}
tictoc::toc()
# Sys.time()
#### Get PRedicted biomass + CI function ####
Get_biomass_Ci_write <- function(fit,dat_per_year=dplyr::bind_cols(dat_grid_x_y,year_f=as.factor(year))){
year=year
sims <- sspm:::produce_sims(simple_gam[[1]], dat_per_year, 1000)
sims <- exp(sims)
sims_total <- apply(sims, MARGIN = 2, FUN = "sum")
sims_point <- mean(sims_total)
alpha = 0.05
sims_CI <- quantile(sims_total, prob = c(alpha/2, 1-alpha/2))
output <- data.frame(year=year,point_est = sims_point, lower = sims_CI[1], upper = sims_CI[2])
write_parquet(output,paste0('Results/model_', year))
return(output)
}
#### 3. Run GAM Predictions ####
Sys.time()
x <- foreach(
year = dat_grid_year,
.packages = c('mgcv','dplyr','sspm','arrow')
) %dopar% {
for(gam in simple_gam){
if(as.factor(year) %in% unique(gam$model$year_f)){
Get_biomass_Ci_write(fit=gam)
# return(list(year=year,years=unique(gam$model$year_f)))
}
gc()
}
}
Sys.time()
#### Stop Cluster ####
parallel::stopCluster(cl = my.cluster)
#
#
# dat_per_year <- dplyr::bind_cols(dat_grid_x_y,year=year) %>%
# dplyr::mutate(fit_simple_gam = predict.bam(simple_gam,type = "response", newdata = .))
#
# sum(dat_per_year$fit_simple_gam)
# Predictions_summary[1]
# read_parquet('Result/model')