-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_weathergen_mcv.r
427 lines (354 loc) · 20.5 KB
/
run_weathergen_mcv.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#######DESCRIPTION###############################
#runs single-site stochastic weather generator
#with monthly covariates
#generates daily prcp, tmax, tmin, rhmax, rhmin
#windmax, windmin, and srad
#################################################
##load libraries
library(dplyr)
library(tidyr)
library(data.table)
library(boot)
library(foreach)
library(doParallel)
library(lubridate)
na.set = function(na.inds, dat){
inds.start = NULL
inds.start[1] = na.inds[1]
inds.end = NULL
k = 1
for(i in 2:length(na.inds)){
if(na.inds[i] - na.inds[i-1] > 1){
k = k + 1
inds.start[k] = na.inds[i]
inds.end[k - 1] = na.inds[i - 1]
}
}
inds.end[k] = na.inds[length(na.inds)]
for(i in 1:k){
dat[inds.start[i]:inds.end[i]] = mean(dat[inds.start[i] - 1], dat[inds.end[i] + 1])
}
return(dat)
}
regr_tab = function(reg_model, model_name){
regr_tab = data.frame(reg_model$coefficients)
colnames(regr_tab) = colnames(reg_model$coefficients)
regr_tab$Parameter = rownames(regr_tab)
regr_tab[ ,4] = ifelse(regr_tab[ ,4] < .001, "< 0.001",
ifelse(regr_tab[ ,4] < .01, "< 0.01",
round(regr_tab[ ,4], 3)))
# format the table
regr_tab$Model = model_name
return(regr_tab)
}
options(contrasts=c("contr.treatment","contr.poly"))
##user inputs
dir_dat = '/Users/danbroman/Documents/Heat Stress/data/'
dir_out = '/Users/danbroman/Documents/Heat Stress/data/'
run_name = 'wafr-heatstress'
covar = 'mcv'
sta_name_sel = 'OUAHIGOUYA'
nsim = 200
pred_flag = T
##process data
weather_data = readRDS(paste0(dir_dat, run_name, '.rda'))
weather_data_fl = filter(weather_data, name == sta_name_sel)
weather_data_fl = weather_data_fl %>% ungroup() %>% dplyr::arrange(date) %>% dplyr::mutate(prcpocc = ifelse(prcp > 0, 1, 0)) %>% data.table()
eind = nrow(weather_data_fl)
start_date = floor_date(weather_data_fl$date[1], 'month')
end_date = weather_data_fl$date[eind]
date_dt = data.table(date = seq(from = start_date, to = end_date, by = 'days'))
weather_data_fl = date_dt %>% left_join(weather_data_fl) %>% dplyr::mutate(year = year(date), month = month(date), day = day(date), jd = as.numeric(format(date, '%j')))
nod = nrow(weather_data_fl)
prcp = weather_data_fl$prcp
prcp.occ = weather_data_fl$prcpocc
tmin = weather_data_fl$tmin
tmax = weather_data_fl$tmax
rhmin = weather_data_fl$rhmin
rhmax = weather_data_fl$rhmax
windmin = weather_data_fl$windmin
windmax = weather_data_fl$windmax
sradmean = weather_data_fl$srad
jd_seq = as.numeric(as.matrix(dplyr::select(weather_data_fl, jd)))
years = unique(weather_data_fl$year)
noy = length(years)
nom = length(unique(paste0(weather_data_fl$year, '-', weather_data_fl$month)))
noy1 = noy + 1
leap = ifelse(years %% 4 == 0, 1, 0)
inds.yrend = c(which(diff(weather_data_fl$year) != 0), nod)
inds.yrstart = c(1, inds.yrend[-length(inds.yrend)] + 1)
yr.length = weather_data_fl$jd[inds.yrend]
#monthly stat covariates
weather_data_month = weather_data_fl %>% group_by(year, month) %>% dplyr::summarise(prcp.mon = sum(prcp, na.rm = T), prcpocc.mon = sum(prcpocc, na.rm = T), tmin.mon = mean(tmin, na.rm = T), tmax.mon = mean(tmax, na.rm = T), rhmin.mon = mean(rhmin, na.rm = T), rhmax.mon = mean(rhmax, na.rm = T))
weather_data_monvars = weather_data_fl %>% dplyr::select(date, year, month) %>% left_join(weather_data_month) %>% arrange(date)
prcpocc.mon = as.numeric(as.matrix(dplyr::select(weather_data_monvars, prcpocc.mon)))
prcpocc.mon1 = prcpocc.mon[-1]
prcp.mon = as.numeric(as.matrix(dplyr::select(weather_data_monvars, prcp.mon)))
prcp.mon1 = prcp.mon[-1]
tmin.mon = as.numeric(as.matrix(dplyr::select(weather_data_monvars, tmin.mon)))
tmin.mon1 = tmin.mon[-1]
tmax.mon = as.numeric(as.matrix(dplyr::select(weather_data_monvars, tmax.mon)))
tmax.mon1 = tmax.mon[-1]
rhmin.mon = as.numeric(as.matrix(dplyr::select(weather_data_monvars, rhmin.mon)))
rhmin.mon1 = rhmin.mon[-1]
rhmax.mon = as.numeric(as.matrix(dplyr::select(weather_data_monvars, rhmax.mon)))
rhmax.mon1 = rhmax.mon[-1]
daycos = NULL
daysin = NULL
daycos2x = NULL
daysin2x = NULL
daycos3x = NULL
daysin3x = NULL
daycos4x = NULL
daysin4x = NULL
daycos5x = NULL
daysin5x = NULL
for(i in 1:noy) {
int = 2 * pi / yr.length[i] * 1:yr.length[i]
daycos = c(daycos, cos(int)) #cosine wave for seasonal cycle
daysin = c(daysin, sin(int)) #sine wave for seasonal cycle
daycos2x = c(daycos2x, cos(int*2))
daysin2x = c(daysin2x, sin(int*2))
daycos3x = c(daycos3x, cos(int*3))
daysin3x = c(daysin3x, sin(int*3))
daycos4x = c(daycos4x, cos(int*4))
daysin4x = c(daysin4x, sin(int*4))
daycos5x = c(daycos5x, cos(int*5))
daysin5x = c(daysin5x, sin(int*5))
}
daycos1 = daycos[-1] #removes first value to consider lag-1
daysin1 = daysin[-1] #removes first value to consider lag-1
daycos2x1 = daycos2x[-1] #removes first value to consider lag-1
daysin2x1 = daysin2x[-1] #removes first value to consider lag-1
daycos3x1 = daycos3x[-1] #removes first value to consider lag-1
daysin3x1 = daysin3x[-1] #removes first value to consider lag-1
daycos4x1 = daycos4x[-1] #removes first value to consider lag-1
daysin4x1 = daysin4x[-1] #removes first value to consider lag-1
daycos5x1 = daycos5x[-1] #removes first value to consider lag-1
daysin5x1 = daysin5x[-1] #removes first value to consider lag-1
daycosresid = cos(2 * pi / 366 * 1:366)
daysinresid = sin(2 * pi / 366 * 1:366)
daycosresidx2 = cos(4 * pi / 366 * 1:366)
daysinresidx2 = sin(4 * pi / 366 * 1:366)
daycosresidx3 = cos(6 * pi / 366 * 1:366)
daysinresidx3 = sin(6 * pi / 366 * 1:366)
daycosresidx4 = cos(8 * pi / 366 * 1:366)
daysinresidx4 = sin(8 * pi / 366 * 1:366)
daycosresidx5 = cos(10 * pi / 366 * 1:366)
daysinresidx5 = sin(10 * pi / 366 * 1:366)
jd_seq1 = jd_seq[-1]
prcp.occ1 = prcp.occ[-1] #removes first value to consider lag-1
prcp.occn = prcp.occ[-nod] #removes first value to consider lag-1
prcp.int = prcp[prcp.occ == 1] #only considers wet days
tmin1 = tmin[-1] #removes first value to consider lag-1
tminn = tmin[-nod] #removes last value; lag-1 vector
tmax1 = tmax[-1] #removes first value to consider lag-1
tmaxn = tmax[-nod] #removes last value; lag-1 vector
rhmin = rhmin / 100 #convert to 0 to 1
rhmax = rhmax / 100 #convert to 0 to 1
rhmin[which(rhmin >1)] = 1
rhmax[which(rhmax >1)] = 1
rhmin1 = rhmin[-1] #removes first value to consider lag-1
rhminn = rhmin[-nod] #removes last value; lag-1 vector
rhmax1 = rhmax[-1] #removes first value to consider lag-1
rhmaxn = rhmax[-nod] #removes last value; lag-1 vector
windmin1 = windmin[-1] #removes first value to consider lag-1
windminn = windmin[-nod] #removes last value; lag-1 vector
windmax1 = windmax[-1] #removes first value to consider lag-1
windmaxn = windmax[-nod] #removes last value; lag-1 vector
sradmean1 = sradmean[-1] #removes first value to consider lag-1
sradmeann = sradmean[-nod] #removes last value; lag-1 vector
##fit models
#prcp occurrence
occ.dat = data.frame(prcp = as.factor(prcp.occ1), prcp.occn, daycos1, daysin1, daycos2x1, daysin3x1, daycos3x1, daysin2x1, daycosprcpocc = daycos1*prcp.occn, daysinprcpocc = daysin1*prcp.occn, prcpocc.mon1)
glm.occ = glm(prcp ~., data = occ.dat, family=binomial(), na.action=na.exclude)
#prcp intensity
int.dat = data.frame(prcp = prcp.int, daycos = daycos[prcp.occ == 1], daysin = daysin[prcp.occ == 1], daycos2x1 = daycos2x1[prcp.occ == 1], daysin2x1 = daysin2x1[prcp.occ == 1], daycos3x1 = daycos3x1[prcp.occ == 1], daysin3x1 = daysin3x1[prcp.occ == 1], prcp.mon[prcp.occ == 1])
glm.int = glm(prcp ~., data = int.dat, na.action=na.exclude)
#temperature
tmin.dat = data.frame(tmin1, tminn, tmaxn, daycos1, daysin1, daycos2x1, daysin2x1, daysin3x1, daycos3x1, daysin4x1, daycos4x1, daysin5x1, daycos5x1, prcp.occ1, tmin.mon1)
tmax.dat = data.frame(tmax1, tmaxn, tmin1, daycos1, daysin1, daycos2x1, daysin2x1, daysin3x1, daycos3x1, daysin4x1, daycos4x1, daysin5x1, daycos5x1, prcp.occ1, tmax.mon1)
lm.tmin = lm(tmin1 ~., data = tmin.dat, na.action=na.exclude)
lm.tmax = lm(tmax1 ~., data = tmax.dat, na.action=na.exclude)
tmin.resid.dat = data.table(jd = jd_seq1, resid = resid(lm.tmin))
tmin.resid.dat = group_by(tmin.resid.dat, jd)
tmin.resid.sd = summarise(tmin.resid.dat, sd = sd(resid, na.rm = T))
tmin.resid.dat2 = data.frame(sd = log(tmin.resid.sd$sd), daycosresid, daysinresid, daycosresidx2, daysinresidx2, daycosresidx3, daysinresidx3, daycosresidx4, daysinresidx4, daycosresidx5, daysinresidx5)
lm.tmin.resid = lm(sd ~., data = tmin.resid.dat2, na.action=na.exclude)
tmax.resid.dat = data.table(jd = jd_seq1, resid = resid(lm.tmax))
tmax.resid.dat = group_by(tmax.resid.dat, jd)
tmax.resid.sd = summarise(tmax.resid.dat, sd = sd(resid, na.rm = T))
tmax.resid.dat2 = data.frame(sd = log(tmax.resid.sd$sd), daycosresid, daysinresid, daycosresidx2, daysinresidx2, daycosresidx3, daysinresidx3, daycosresidx4, daysinresidx4, daycosresidx5, daysinresidx5)
lm.tmax.resid = lm(sd ~., data = tmax.resid.dat2, na.action=na.exclude)
#relative humidity
rhmin.dat = data.frame(rhmin1, rhminn, rhmaxn, daycos1, daysin1, daycos2x1, daysin2x1, daysin3x1, daycos3x1, prcp.occ1, tmin1, tmax1, tminn, tmaxn, rhmin.mon1)
rhmax.dat = data.frame(rhmax1, rhmaxn, rhmin1, daycos1, daysin1, daycos2x1, daysin2x1, daysin3x1, daycos3x1, prcp.occ1, tmin1, tmax1, tminn, tmaxn, rhmax.mon1)
glm.rhmin = suppressWarnings(glm(rhmin1 ~., data = rhmin.dat, family = binomial(), na.action=na.exclude))
glm.rhmax = suppressWarnings(glm(rhmax1 ~., data = rhmax.dat, family = binomial(), na.action=na.exclude))
rhmin.resid.dat = data.table(jd = jd_seq1, resid = resid(glm.rhmin))
rhmin.resid.dat = group_by(rhmin.resid.dat, jd)
rhmin.resid.sd = summarise(rhmin.resid.dat, sd = sd(resid, na.rm = T))
rhmin.resid.dat2 = data.frame(sd = log(rhmin.resid.sd$sd), daycosresid, daysinresid, daycosresidx2, daysinresidx2, daycosresidx3, daysinresidx3, daycosresidx4, daysinresidx4, daycosresidx5, daysinresidx5)
lm.rhmin.resid = lm(sd ~., data = rhmin.resid.dat2, na.action=na.exclude)
rhmax.resid.dat = data.table(jd = jd_seq1, resid = resid(glm.rhmax))
rhmax.resid.dat = group_by(rhmax.resid.dat, jd)
rhmax.resid.sd = summarise(rhmax.resid.dat, sd = sd(resid, na.rm = T))
rhmax.resid.dat2 = data.frame(sd = log(rhmax.resid.sd$sd), daycosresid, daysinresid, daycosresidx2, daysinresidx2, daycosresidx3, daysinresidx3, daycosresidx4, daysinresidx4, daycosresidx5, daysinresidx5)
lm.rhmax.resid = lm(sd ~., data = rhmax.resid.dat2, na.action=na.exclude)
#winds
windmin.dat = data.frame(windmin1, windminn, windmaxn, daycos1, daysin1, daycos2x1, daysin2x1, daysin3x1, daycos3x1, tmin1, tmax1, tminn, tmaxn)
windmax.dat = data.frame(windmax1, windminn, windmaxn, windmin1, daycos1, daysin1, daycos2x1, daysin2x1, daysin3x1, daycos3x1, tmin1, tmax1, tminn, tmaxn)
lm.windmin = lm(windmin1 ~., data = windmin.dat, na.action=na.exclude) #fit linear model
lm.windmax = lm(windmax1 ~., data = windmax.dat, na.action=na.exclude) #fit linear model
windmin.resid.dat = data.table(jd = jd_seq1, resid = resid(lm.windmin))
windmin.resid.dat = group_by(windmin.resid.dat, jd)
windmin.resid.sd = summarise(windmin.resid.dat, sd = sd(resid, na.rm = T))
windmin.resid.dat2 = data.frame(sd = log(windmin.resid.sd$sd), daycosresid, daysinresid, daycosresidx2, daysinresidx2, daycosresidx3, daysinresidx3)
lm.windmin.resid = lm(sd ~., data = windmin.resid.dat2, na.action=na.exclude)
windmax.resid.dat = data.table(jd = jd_seq1, resid = resid(lm.windmax))
windmax.resid.dat = group_by(windmax.resid.dat, jd)
windmax.resid.sd = summarise(windmax.resid.dat, sd = sd(resid, na.rm = T))
windmax.resid.dat2 = data.frame(sd = log(windmax.resid.sd$sd), daycosresid, daysinresid, daycosresidx2, daysinresidx2, daycosresidx3, daysinresidx3)
lm.windmax.resid = lm(sd ~., data = windmax.resid.dat2, na.action=na.exclude)
#solar radiation
sradmean.dat = data.frame(sradmean1, sradmeann, daycos1, daysin1, daycos2x1, daysin2x1, daysin3x1, daycos3x1, prcp.occ1, prcp.occn, tmin1, tmax1, tminn, tmaxn)
lm.sradmean = lm(sradmean1 ~., data = sradmean.dat, na.action = na.exclude)
sradmean.resid.dat = data.table(jd = jd_seq1, resid = resid(lm.sradmean, typ = 'working'))
sradmean.resid.dat = group_by(sradmean.resid.dat, jd)
sradmean.resid.sd = summarise(sradmean.resid.dat, sd = sd(resid, na.rm = T))
sradmean.resid.dat2 = data.frame(sd = log(sradmean.resid.sd$sd), daycosresid, daysinresid, daycosresidx2, daysinresidx2, daycosresidx3, daysinresidx3)
lm.sradmean.resid = lm(sd ~., data = sradmean.resid.dat2, na.action=na.exclude)
if(pred_flag == T){ #simulation loop
#model parameters
coefocc = glm.occ$coef #beta values of prcp occurrence model
sigmaocc = sd(resid(glm.occ, typ = 'working'), na.rm = T)
coefint = glm.int$coef #beta values if prcp intensity model
# intshape = gamma.shape(glm.int)$alpha #gamma shape parameter of prcp intensity model
sigmatmin = exp(lm.tmin.resid$fitted.values) #standard deviation of tmin model errors
sigmatmax = exp(lm.tmax.resid$fitted.values) #standard deviation of tmax model errors
coeftmin = lm.tmin$coef #beta values of tmin model
coeftmax = lm.tmax$coef #beta values of tmax model
sigmarhmin = exp(lm.rhmin.resid$fitted.values)
sigmarhmax = exp(lm.rhmax.resid$fitted.values)
coefrhmin = glm.rhmin$coef #beta values of rhmin model
coefrhmax = glm.rhmax$coef #beta values of rhmax model
sigmawindmin = exp(lm.windmin.resid$fitted.values) #standard deviation of windmin model errors
sigmawindmax = exp(lm.windmax.resid$fitted.values) #standard deviation of windmax model errors
coefwindmin = lm.windmin$coef #beta values of windmin model
coefwindmax = lm.windmax$coef #beta values of windmax model
sigmasradmean = exp(lm.sradmean.resid$fitted.values) #standard deviation of sradmean model errors
coefsradmean = lm.sradmean$coef #beta values of sradmean model
##MODEL OUTPUT ARRAYS
prcp.occ.sim.m = matrix(0,nrow = nod, ncol = nsim)
prcp.int.sim.m = matrix(0,nrow = nod, ncol = nsim)
tmin.sim.m = matrix(0,nrow = nod, ncol = nsim)
tmax.sim.m = matrix(0,nrow = nod, ncol = nsim)
rhmin.sim.m = matrix(0,nrow = nod, ncol = nsim)
rhmax.sim.m = matrix(0,nrow = nod, ncol = nsim)
windmin.sim.m = matrix(0,nrow = nod, ncol = nsim)
windmax.sim.m = matrix(0,nrow = nod, ncol = nsim)
sradmean.sim.m = matrix(0,nrow = nod, ncol = nsim)
cl = makeCluster(7)
registerDoParallel(cl)
wgen_sims = foreach(icount(nsim)) %dopar% { #simulation loop
library(dplyr)
library(tidyr)
library(data.table)
library(boot)
#temp simulation vars
prcp.occ.sim = rep(0,nod)
prcp.int.sim = rep(0,nod)
tmin.sim = rep(0,nod)
tmax.sim = rep(0,nod)
rhmin.sim = rep(0,nod)
rhmax.sim = rep(0,nod)
windmin.sim = rep(0,nod)
windmax.sim = rep(0,nod)
sradmean.sim = rep(0,nod)
#starting values
var.init = data.table(prcp.occ = prcp.occ[inds.yrstart], tmin = tmin[inds.yrstart], tmax = tmax[inds.yrstart], rhmin = rhmin[inds.yrstart], rhmax = rhmax[inds.yrstart], windmin = windmin[inds.yrstart], windmax = windmax[inds.yrstart], sradmean = sradmean[inds.yrstart]) %>% dplyr::filter(!is.na(prcp.occ))
ind.init = 1:nrow(var.init)
ind.j1 = sample(ind.init, 1) #randomly select index of a jan 1
prcp.occ.sim[1] = var.init$prcp.occ[ind.j1]
tmin.sim[1] = var.init$tmin[ind.j1]
tmax.sim[1] = var.init$tmax[ind.j1]
rhmin.sim[1] = var.init$rhmin[ind.j1]
rhmax.sim[1] = var.init$rhmax[ind.j1]
windmin.sim[1] = var.init$windmin[ind.j1]
windmax.sim[1] = var.init$windmax[ind.j1]
sradmean.sim[1] = var.init$sradmean[ind.j1]
#nod loop
for (k in 2:nod){
jd.simday = jd_seq[k] #julian day of simulation day for resid models
#prcp occurrence
covocc = as.numeric(c(1, prcp.occ.sim[k - 1], daycos[k], daysin[k], daycos2x[k], daysin2x[k], daycos3x[k], daysin3x[k], daycos[k]*prcp.occ.sim[k - 1], daysin[k]*prcp.occ.sim[k - 1], prcpocc.mon[k]))
occerr = rnorm(1, 0, sigmaocc) #resamples from prcp occurrence error distribution
pk = exp(coefocc %*% covocc + occerr) / (1 + exp(coefocc %*% covocc + occerr))
prcp.occ.sim[k] = ifelse(runif(1) < pk, 1, 0)
#temperature
covtmin = c(1, tmin.sim[k-1], tmax.sim[k-1], daycos[k], daysin[k], daycos2x[k], daysin2x[k], daycos3x[k], daysin3x[k], daycos4x[k], daysin4x[k], daycos5x[k], daysin5x[k], prcp.occ.sim[k], tmin.mon[k])
tmin.sim[k] = coeftmin %*% covtmin + rnorm(1, 0, sigmatmin)
covtmax = c(1, tmax.sim[k-1], tmin.sim[k], daycos[k], daysin[k],daycos2x[k],daysin2x[k], daycos3x[k], daysin3x[k], daycos4x[k], daysin4x[k], daycos5x[k], daysin5x[k], prcp.occ.sim[k], tmax.mon[k])
tmax.sim[k] = coeftmax %*% covtmax + rnorm(1, 0, sigmatmax[jd.simday])
ta.temp = c(tmax.sim[k], tmin.sim[k])
tmin.sim[k] = min(ta.temp)
tmax.sim[k] = max(ta.temp)
#relative humidity
covrhmin = as.numeric(c(1, rhmin.sim[k-1], rhmax.sim[k-1], daycos[k], daysin[k], daycos2x[k],daysin2x[k], daycos3x[k], daysin3x[k], prcp.occ.sim[k], tmin.sim[k], tmax.sim[k], tmin.sim[k-1], tmax.sim[k-1], rhmin.mon[k]))
rhmin.sim[k] = inv.logit(coefrhmin %*% covrhmin)
covrhmax = as.numeric(c(1, rhmax.sim[k-1], rhmin.sim[k], daycos[k], daysin[k], daycos2x[k],daysin2x[k], daycos3x[k], daysin3x[k], prcp.occ.sim[k], tmin.sim[k], tmax.sim[k], tmin.sim[k-1], tmax.sim[k-1], rhmax.mon[k]))
rhmax.sim[k] = inv.logit(coefrhmax %*% covrhmax)
rh.temp = c(rhmax.sim[k], rhmin.sim[k])
rhmin.sim[k] = ifelse(min(rh.temp) < 0, 0, min(rh.temp))
rhmax.sim[k] = ifelse(max(rh.temp) < 0, 0, max(rh.temp))
rhmax.sim[k] = ifelse(rhmax.sim[k] > 1, 1, rhmax.sim[k])
#winds
covwindmin = c(1, windmin.sim[k-1], windmax.sim[k-1], daycos[k], daysin[k], daycos2x[k], daysin2x[k], daycos3x[k], daysin3x[k], tmin.sim[k], tmax.sim[k], tmin.sim[k-1], tmax.sim[k-1])
windmin.sim[k] = coefwindmin %*% covwindmin + rnorm(1,0,sigmawindmin[jd.simday])
covwindmax = c(1, windmin.sim[k-1], windmax.sim[k-1],windmin.sim[k], daycos[k], daysin[k], daycos2x[k],daysin2x[k], daycos3x[k], daysin3x[k], tmin.sim[k], tmax.sim[k], tmin.sim[k-1], tmax.sim[k-1])
windmax.sim[k] = coefwindmax %*% covwindmax + rnorm(1,0,sigmawindmax[jd.simday])
wind.temp = c(windmin.sim[k], windmax.sim[k])
windmin.sim[k] = ifelse(min(wind.temp) >= 0, min(wind.temp), 0)
windmax.sim[k] = ifelse(max(wind.temp) >= 0, max(wind.temp), 0)
#solar radiation
covsradmean = c(1, sradmean.sim[k-1], daycos[k], daysin[k],daycos2x[k], daysin2x[k], daycos3x[k], daysin3x[k], prcp.occ.sim[k], prcp.occ.sim[k-1], tmin.sim[k], tmax.sim[k], tmin.sim[k-1], tmax.sim[k-1])
sradmean.sim[k] = coefsradmean %*% covsradmean + rnorm(1,0,sigmasradmean[jd.simday])
sradmean.sim[k] = ifelse(sradmean.sim[k] >= 0, sradmean.sim[k], 0)
} #end nod loop (k)
#prcp intensity
covint = cbind(1, daycos[which(prcp.occ.sim == 1)], daysin[which(prcp.occ.sim == 1)], daycos2x[which(prcp.occ.sim == 1)], daysin2x[which(prcp.occ.sim == 1)], daycos3x[which(prcp.occ.sim == 1)], daysin3x[which(prcp.occ.sim == 1)], prcp.mon[which(prcp.occ.sim == 1)])
int.temp = as.numeric(t(as.matrix(coefint)) %*% t(covint))
int.temp[int.temp < 0] = 0
# intmu = exp(abs(apply(coefint * covint, FUN = sum, MAR = 1, na.rm = T)))
# intscale = intmu / intshape
# # length(which(prcp.occ.sim == 1))
# int.temp = rgamma(nod, intshape, scale = intscale)
prcp.int.sim[which(prcp.occ.sim == 1)] = int.temp
#store simulation values
prcp.occ.sim.m[,i] = prcp.occ.sim
prcp.int.sim.m[,i] = prcp.int.sim
tmin.sim.m[,i] = tmin.sim
tmax.sim.m[,i] = tmax.sim
rhmin.sim.m[,i] = rhmin.sim * 100
rhmax.sim.m[,i] = rhmax.sim * 100
windmin.sim.m[,i] = windmin.sim
windmax.sim.m[,i] = windmax.sim
sradmean.sim.m[,i] = sradmean.sim
data.table(prcpocc = prcp.occ.sim, prcp = prcp.int.sim, tmin = tmin.sim, tmax = tmax.sim, rhmin = rhmin.sim, rhmax = rhmax.sim, windmin = windmin.sim, windmax = windmax.sim, srad = sradmean.sim)
} #end simulation loop (i)
stopCluster(cl)
wgen_sims_dt = do.call('bind_rows', wgen_sims) %>% dplyr::mutate(date = rep(seq(from = start_date, to = end_date, by = 'days'), nsim))
saveRDS(wgen_sims_dt, file = paste0(dir_out, sta_name_sel, '_', run_name, '_', covar, '.rda'))
} #end pred
anova_tbl = NULL
anova_tbl = bind_rows(anova_tbl, regr_tab(summary(lm.tmin), 'tmin'))
anova_tbl = bind_rows(anova_tbl, regr_tab(summary(lm.tmax), 'tmax'))
anova_tbl = bind_rows(anova_tbl, regr_tab(summary(glm.rhmin), 'rhmin'))
anova_tbl = bind_rows(anova_tbl, regr_tab(summary(glm.int), 'rhmax'))
anova_tbl = bind_rows(anova_tbl, regr_tab(summary(lm.windmin), 'windmin'))
anova_tbl = bind_rows(anova_tbl, regr_tab(summary(glm.rhmax), 'rhmax'))
anova_tbl = bind_rows(anova_tbl, regr_tab(summary(lm.windmax), 'windmax'))
anova_tbl = bind_rows(anova_tbl, regr_tab(summary(lm.sradmean), 'sradmean'))
anova_tbl = bind_rows(anova_tbl, regr_tab(summary(glm.occ), 'prcpocc'))
anova_tbl = bind_rows(anova_tbl, regr_tab(summary(glm.int), 'prcp'))
write.csv(anova_tbl, paste0(dir_out, sta_name_sel, '_', run_name, '_', covar, '_anova.csv'), row.names = F)