-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 29.9 KB
/
.Rhistory
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
View(alldata)
alldata <- alldata |> mutate(tim = as.Date(year=year, month=month,day=day) )
alldata <- alldata |> mutate(tim = lubridate::as_date(year=year, month=month,day=day) )
alldata <- alldata |> mutate(tim = as.Date(paste0(year,month,day, sep='-')) )
paste0(year,month,day, sep='-')
alldata <- alldata |> mutate(tim = as.Date(paste(year,month,day, sep='-')) )
View(alldata)
alldata <- alldata |> mutate(tim = as.Date(paste(year,month,day, sep='-'))) |> subset(!is.na(tim))
View(alldata)
alldata <- alldata |> mutate(tim = as.Date(paste(year,month,day, sep='-'))) |> subset(!is.na(tim)) |> arrange(id, tim)
cdata.all <- alldata |> left_join(cdata.tl, by=join_by(id==id,year==year,month==month, day==day))
View(cdata.all)
cdata.all <- alldata |> left_join(cdata.th, by=join_by(id==id,year==year,month==month, day==day)) |> left_join(cdata.tl, by=join_by(id==id,year==year,month==month, day==day)) |> left_join(cdata.p, by=join_by(id==id,year==year,month==month, day==day))
View(alldata)
View(xfilter)
stations <- xfilter1 |> subset(select = c(id, latitude, longitude, elevation, state, name)) |> unique()
stations <- xfilter |> subset(select = c(id, latitude, longitude, elevation, state, name)) |> unique()
View(stations)
alldata <- alldata |> left_join(stations, by=join_by(id=id))
View(alldata)
xfilter <- x |> subset(latitude > 42.9500-0.5 & latitude < 42.9500+0.5 & longitude > -85.6667 - 0.75 & longitude < -85.6667 + 0.75 & elevation > 0 & last_year - first_year > 5 & element %in% 'TMAX') |> mutate(time = last_year - first_year)
xfilter1 <- xfilter$id |> unique()
stations <- xfilter |> subset(select = c(id, latitude, longitude, elevation, state, name)) |> unique()
daynum <- c(1:31)
valnum <- paste0('VALUE', daynum)
cdata <- ghcnd(xfilter1)
cdata2 <- subset(cdata, element %in% c('TMAX', 'TMIN', 'PRCP'))
alldata <- unique(subset(cdata2, select=c('id', 'year', 'month')))
datanum.tab <- data.frame(day=daynum)
alldata <- merge(alldata, datanum.tab)
alldata <- alldata |> mutate(tim = as.Date(paste(year,month,day, sep='-'))) |> subset(!is.na(tim)) |> arrange(id, tim)
alldata <- alldata |> left_join(stations, by=join_by(id==id))
View(alldata)
colnames(alldata)
colnames(alldata) <- c("id","state","name","latitude","longitude","elevation","year","month","day","tim")
xfilter1 <- xfilter$id |> unique()
stations <- xfilter |> subset(select = c(id, latitude, longitude, elevation, state, name)) |> unique()
daynum <- c(1:31)
valnum <- paste0('VALUE', daynum)
cdata <- ghcnd(xfilter1)
cdata2 <- subset(cdata, element %in% c('TMAX', 'TMIN', 'PRCP'))
alldata <- unique(subset(cdata2, select=c('id', 'year', 'month')))
datanum.tab <- data.frame(day=daynum)
alldata <- merge(alldata, datanum.tab)
alldata <- alldata |> mutate(tim = as.Date(paste(year,month,day, sep='-'))) |> subset(!is.na(tim)) |> arrange(id, tim)
alldata <- alldata |> left_join(stations, by=join_by(id==id))
alldata <- alldata[,c("id","state","name","latitude","longitude","elevation","year","month","day","tim")]
View(alldata)
alldata <- alldata[,c("id","state","name","latitude","longitude","elevation","tim","year","month","day")]
for(i in 1:31){#i=1
cdata.tl0 <- cbind(subset(cdata2, element %in% c('TMIN'), select = c('id', 'year', 'month', valnum[i])) , day=i)
cdata.th0 <- cbind(subset(cdata2, element %in% c('TMAX'), select = c('id', 'year', 'month', valnum[i])) , day=i)
cdata.p0 <- cbind(subset(cdata2, element %in% c('PRCP'), select = c('id', 'year', 'month', valnum[i])) , day=i)
names(cdata.tl0) <- c('id', 'year', 'month','tl','day')
names(cdata.th0) <- c('id', 'year', 'month','th','day')
names(cdata.p0) <- c('id', 'year', 'month','p','day')
if(i==1){
cdata.tl=cdata.tl0
cdata.th=cdata.th0
cdata.p=cdata.p0
}else{
cdata.tl=rbind(cdata.tl,cdata.tl0)
cdata.th=rbind(cdata.th,cdata.th0)
cdata.p=rbind(cdata.p,cdata.p0)
}
}
cdata.all <- alldata |> left_join(cdata.th, by=join_by(id==id,year==year,month==month, day==day)) |> left_join(cdata.tl, by=join_by(id==id,year==year,month==month, day==day)) |> left_join(cdata.p, by=join_by(id==id,year==year,month==month, day==day))
View(cdata.all)
cdata.all <- cdata.all |> mutate(th = th/10, tl=tl/10)
View(cdata.all)
cdata <- ghcnd('USW00094860', refresh = TRUE)
View(cdata)
cdata <- ghcnd('USC00206013', refresh = TRUE)
View(cdata)
cdata <- ghcnd(xfilter1)
# cdata <- ghcnd('USC00206013', refresh = TRUE)
stations <- xfilter |> subset(select = c(id, latitude, longitude, elevation, state, name)) |> unique()
daynum <- c(1:31)
valnum <- paste0('VALUE', daynum)
cdata2 <- subset(cdata, element %in% c('TMAX', 'TMIN', 'PRCP'))
alldata <- unique(subset(cdata2, select=c('id', 'year', 'month')))
datanum.tab <- data.frame(day=daynum)
alldata <- merge(alldata, datanum.tab)
alldata <- alldata |> mutate(tim = as.Date(paste(year,month,day, sep='-'))) |> subset(!is.na(tim)) |> arrange(id, tim)
alldata <- alldata |> left_join(stations, by=join_by(id==id))
alldata <- alldata[,c("id","state","name","latitude","longitude","elevation","tim","year","month","day")]
for(i in 1:31){#i=1
cdata.tl0 <- cbind(subset(cdata2, element %in% c('TMIN'), select = c('id', 'year', 'month', valnum[i])) , day=i)
cdata.th0 <- cbind(subset(cdata2, element %in% c('TMAX'), select = c('id', 'year', 'month', valnum[i])) , day=i)
cdata.p0 <- cbind(subset(cdata2, element %in% c('PRCP'), select = c('id', 'year', 'month', valnum[i])) , day=i)
names(cdata.tl0) <- c('id', 'year', 'month','tl','day')
names(cdata.th0) <- c('id', 'year', 'month','th','day')
names(cdata.p0) <- c('id', 'year', 'month','p','day')
if(i==1){
cdata.tl=cdata.tl0
cdata.th=cdata.th0
cdata.p=cdata.p0
}else{
cdata.tl=rbind(cdata.tl,cdata.tl0)
cdata.th=rbind(cdata.th,cdata.th0)
cdata.p=rbind(cdata.p,cdata.p0)
}
}
cdata.all <- alldata |> left_join(cdata.th, by=join_by(id==id,year==year,month==month, day==day)) |> left_join(cdata.tl, by=join_by(id==id,year==year,month==month, day==day)) |> left_join(cdata.p, by=join_by(id==id,year==year,month==month, day==day))
cdata.all <- cdata.all |> mutate(th = th/10, tl=tl/10)
View(cdata.all)
cdata.all <- cdata.all |> mutate(t = (th+tl)/2)
View(cdata.all)
cdata.all <- cdata.all |> mutate(t = (th+tl)/2, decyear = lubridate::decimal_date(tim))
View(cdata.all)
cdata.all <- cdata.all |> mutate(t = (th+tl)/2, decyear = lubridate::decimal_date(tim), sindat = sin(decyear*2*3.141592))
cdata.all <- cdata.all |> mutate(t = (th+tl)/2, decyear = lubridate::decimal_date(tim), sindat = sin(decyear*2*3.141592),cosdat = cos(decyear*2*3.141592))
library(ranger)
train <- cdata.all |> subset(!is.na(t))
rf <- ranger(t ~ latitude+longitude+elevation+decyear+sindat+cosdat,
data=train)
View(stations)
View(cdata)
View(x)
View(xfilter)
fitpoints <- cdata.all |> subset(id %in% 'USW00094860')
View(fitpoints)
fitstat <- stations |> subset(id %in% 'USW00094860', select = c(id, id, latitude, longitude, elevation, state, name))
View(fitstat)
fitstat <- stations |> subset(id %in% 'USW00094860', select = c(id, latitude, longitude, elevation, state, name))
fittime <- cdata.all |> subset(select = c(year, month, day, decyear, sindat, cosdat)) |> unique() |> arrange(decyear)
View(fittime)
fitdat <- merge(fitstat, fittime)
View(fitdat)
fitdat <- merge(fitstat, fittime) |> left_join(train[,c("id", "decyear", "t")], by=join_by(id==id, decyear==decyear))
View(fitdat)
fitdat <- fitdat |> mutate(t.rf = predictions(rf, fitdat))
fitdat <- fitdat |> mutate(t.rf = predict(rf, fitdat))
fitdat <- fitdat |> mutate(t.rf = predictions(predict(rf, fitdat)))
View(fitdat)
rf <- ranger(t ~ latitude+longitude+elevation+decyear+sindat+cosdat,
data=train, min.node.size = 1, num.trees = 200)
fitstat <- stations |> subset(id %in% 'USW00094860', select = c(id, latitude, longitude, elevation, state, name))
fittime <- cdata.all |> subset(select = c(year, month, day, decyear, sindat, cosdat)) |> unique() |> arrange(decyear)
fitdat <- merge(fitstat, fittime) |> left_join(train[,c("id", "decyear", "t")], by=join_by(id==id, decyear==decyear))
fitdat <- fitdat |> mutate(t.rf = predictions(predict(rf, fitdat)))
View(fitdat)
View(train)
rf <- ranger(t ~ latitude+longitude+elevation+decyear+sindat+cosdat,
data=train, min.node.size = 1, num.trees = 200, sample.fraction = 1)
fitstat <- stations |> subset(id %in% 'USW00094860', select = c(id, latitude, longitude, elevation, state, name))
fittime <- cdata.all |> subset(select = c(year, month, day, decyear, sindat, cosdat)) |> unique() |> arrange(decyear)
fitdat <- merge(fitstat, fittime) |> left_join(train[,c("id", "decyear", "t")], by=join_by(id==id, decyear==decyear))
fitdat <- fitdat |> mutate(t.rf = predictions(predict(rf, fitdat)))
View(fitdat)
View(fittime)
View(fitdat)
rf <- ranger(t ~ latitude+longitude+elevation+decyear+sindat+cosdat,
data=train, min.node.size = 1, num.trees = 200, sample.fraction = 1, min.bucket = 1)
fitstat <- stations |> subset(id %in% 'USW00094860', select = c(id, latitude, longitude, elevation, state, name))
fittime <- cdata.all |> subset(select = c(year, month, day, decyear, sindat, cosdat)) |> unique() |> arrange(decyear)
fitdat <- merge(fitstat, fittime) |> left_join(train[,c("id", "decyear", "t")], by=join_by(id==id, decyear==decyear))
fitdat <- fitdat |> mutate(t.rf = predictions(predict(rf, fitdat)))
View(fitdat)
mod <- lm(t ~ latitude+longitude+elevation+decyear+sindat+cosdat,
data=train)
summary(mod)
mod <- lm(t ~ latitude+longitude+elevation+decyear+sindat+cosdat+id,
data=train)
summary(mod)
train <- train |> mutate(t.lm = predict(mod, train))
View(train)
train <- cdata.all |> subset(!is.na(t))
mod <- lm(t ~ latitude+longitude+elevation+decyear+sindat+cosdat,
data=train)
summary(mod)
train <- train |> mutate(t.lm = predict(mod, train))
train <- train |> mutate(t.lm = predict(mod, train))
View(train)
rf <- ranger(t ~ latitude+longitude+elevation+decyear+sindat+cosdat+t.lm,
data=train, min.node.size = 1, num.trees = 200, sample.fraction = 1, min.bucket = 1)
fitstat <- stations |> subset(id %in% 'USW00094860', select = c(id, latitude, longitude, elevation, state, name))
fittime <- cdata.all |> subset(select = c(year, month, day, decyear, sindat, cosdat)) |> unique() |> arrange(decyear)
fitdat <- merge(fitstat, fittime) |> left_join(train[,c("id", "decyear", "t")], by=join_by(id==id, decyear==decyear))
fitdat <- fitdat |> mutate(t.lm = predict(mod, fitdat), t.rf = predictions(predict(rf, fitdat)))
View(fitdat)
fitdat <- fitdat |> mutate(t.lm = predict(mod, fitdat))
fitdat <- fitdat |> mutate(t.rf = predictions(predict(rf, fitdat)))
rf <- ranger(t ~ latitude+longitude+elevation+decyear+sindat+cosdat+t.lm+id,
data=train, min.node.size = 1, num.trees = 200, sample.fraction = 1, min.bucket = 1)
fitstat <- stations |> subset(id %in% 'USW00094860', select = c(id, latitude, longitude, elevation, state, name))
fittime <- cdata.all |> subset(select = c(year, month, day, decyear, sindat, cosdat)) |> unique() |> arrange(decyear)
fitdat <- merge(fitstat, fittime) |> left_join(train[,c("id", "decyear", "t")], by=join_by(id==id, decyear==decyear))
fitdat <- fitdat |> mutate(t.lm = predict(mod, fitdat))
fitdat <- fitdat |> mutate(t.rf = predictions(predict(rf, fitdat)))
View(fitdat)
mod <- lm(t ~ latitude+longitude+elevation+decyear+sindat+cosdat+id,
data=train)
summary(mod)
train <- train |> mutate(t.lm = predict(mod, train))
rf <- ranger(t ~ latitude+longitude+elevation+decyear+sindat+cosdat+t.lm,
data=train, min.node.size = 1, num.trees = 200, sample.fraction = 1, min.bucket = 1)
fitstat <- stations |> subset(id %in% 'USW00094860', select = c(id, latitude, longitude, elevation, state, name))
fittime <- cdata.all |> subset(select = c(year, month, day, decyear, sindat, cosdat)) |> unique() |> arrange(decyear)
fitdat <- merge(fitstat, fittime) |> left_join(train[,c("id", "decyear", "t")], by=join_by(id==id, decyear==decyear))
fitdat <- fitdat |> mutate(t.lm = predict(mod, fitdat))
fitdat <- fitdat |> mutate(t.rf = predictions(predict(rf, fitdat)))
View(fitdat)
ggplot(data=fitdat, aes(x=decyear, y=t))+
geom_smooth()
ggplot(data=fitdat, aes(x=decyear, y=t))+
geom_point(alpha=0.1)
fitsum <- fitdat |> mutate(winter = ifelse(month %in% c(1,2,12), t.rf,NA), summer = ifelse(month %in% c(6,7,8), t.rf,NA),
year2 = floor(decyear-1/12))
View(fitsum)
fitsum <- fitdat |> mutate(winter = ifelse(month %in% c(1,2,12), t.rf,NA), summer = ifelse(month %in% c(6,7,8), t.rf,NA),
year2 = floor(decyear-0.161))
fitsum <- fitdat |> mutate(winter = ifelse(month %in% c(1,2,12), t.rf,NA), summer = ifelse(month %in% c(6,7,8), t.rf,NA),
year2 = floor(decyear-0.162))
View(fitsum)
fitsum <- fitdat |> mutate(winter = ifelse(month %in% c(1,2,12), t.rf,NA), summer = ifelse(month %in% c(6,7,8), t.rf,NA),
year2 = floor(decyear-0.1615)) |> group_by(year2) |> summarise(summer = mean(summer, na.rm=T), winter = mean(winter, na.rm=T), annual = mean(t, na.rm=T))
View(fitsum)
fitsum <- fitdat |> mutate(winter = ifelse(month %in% c(1,2,12), t.rf,NA), summer = ifelse(month %in% c(6,7,8), t.rf,NA),
year2 = floor(decyear-0.1615)) |> group_by(year2) |> summarise(summer = mean(summer, na.rm=T), winter = mean(winter, na.rm=T), annual = mean(t.rf, na.rm=T))
View(rf)
View(fitsum)
fitsum <- fitdat |> mutate(winter = ifelse(month %in% c(1,2,12), t.rf,NA), summer = ifelse(month %in% c(6,7,8), t.rf,NA),
year2 = floor(decyear-0.1615)) |> group_by(year2) |> summarise(summer = mean(summer, na.rm=T), winter = mean(winter, na.rm=T), annual = mean(t.rf, na.rm=T)) |> subset(year2 > 1891)
ggplot(data=fitsum, aes(x=year2))+
geom_line(aes(y=summer), color='red')+
geom_line(aes(y=winter), color='blue')+
geom_line(aes(y=annual), color='black')
library(data.table)
fitsum <- fitsum |> mutate(ann5yr = frollmean(annual, 5))
View(fitsum)
fitsum <- fitsum |> mutate(ann5yr = frollmean(annual, 5), summ5yr = frollmean(summer, 5), wint5yr = frollmean(winter, 5))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=summer), color='red')+
geom_point(aes(y=winter), color='blue')+
geom_point(aes(y=annual), color='black')+
geom_line(aes(y=summ5yr), color='red')+
geom_line(aes(y=wint5yr), color='blue')+
geom_line(aes(y=ann5yr), color='black')
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=summer), color='red')+
geom_point(aes(y=winter), color='blue')+
geom_point(aes(y=annual), color='black')+
geom_line(aes(y=summ5yr), color='red')+
geom_line(aes(y=wint5yr), color='blue')+
geom_line(aes(y=ann5yr), color='black')+
scale_y_continuous(name = 'temperature (C)')+
scale_x_continuous(name = 'year')
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=annual), color='black')+
geom_line(aes(y=ann5yr), color='black')+
scale_y_continuous(name = 'temperature (C)')+
scale_x_continuous(name = 'year')
c(0:12)*10
(c(0:12)*10+1900)
(c(-1:13)*10+1900)
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=annual), color='black')+
geom_line(aes(y=ann5yr), color='black')+
scale_y_continuous(name = 'temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
fitsum <- fitdat |> mutate(winter = ifelse(month %in% c(1,2,12), t.rf,NA), summer = ifelse(month %in% c(6,7,8), t.rf,NA),
year2 = floor(decyear-0.1615)) |> group_by(year2) |> summarise(summer = mean(summer, na.rm=T), winter = mean(winter, na.rm=T), annual = mean(t.rf, na.rm=T)) |> subset(year2 > 1892)
library(data.table)
fitsum <- fitsum |> mutate(ann5yr = frollmean(annual, 5), summ5yr = frollmean(summer, 5), wint5yr = frollmean(winter, 5))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=annual), color='black')+
geom_line(aes(y=ann5yr), color='black')+
scale_y_continuous(name = 'temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=annual), color='black')+
geom_line(aes(y=ann5yr), color='black')+
scale_y_continuous(name = 'annual temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=summer), color='red')+
geom_line(aes(y=summ5yr), color='red')+
scale_y_continuous(name = 'summer temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=winter), color='blue')+
geom_line(aes(y=wint5yr), color='blue')+
scale_y_continuous(name = 'winter temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
fitsum <- fitdat |> mutate(winter = ifelse(month %in% c(1,2,12), t.rf,NA), winter2 = ifelse(month %in% c(1,2,12), t,NA), summer = ifelse(month %in% c(6,7,8), t.rf,NA),
year2 = floor(decyear-0.1615)) |> group_by(year2) |> summarise(summer = mean(summer, na.rm=T), winter = mean(winter, na.rm=T), annual = mean(t.rf, na.rm=T)) |> subset(year2 > 1892)
fitsum <- fitdat |> mutate(winter = ifelse(month %in% c(1,2,12), t.rf,NA), winter2 = ifelse(month %in% c(1,2,12), t,NA), summer = ifelse(month %in% c(6,7,8), t.rf,NA), summer2 = ifelse(month %in% c(6,7,8), t,NA),
year2 = floor(decyear-0.1615)) |> group_by(year2) |> summarise(summer = mean(summer, na.rm=T), winter = mean(winter, na.rm=T), annual = mean(t.rf, na.rm=T), winter2 = mean(winter2, na.rm=T), summer2 = mean(summer2, na.rm=T)) |> subset(year2 > 1892)
library(data.table)
fitsum <- fitsum |> mutate(ann5yr = frollmean(annual, 5), summ5yr = frollmean(summer, 5), wint5yr = frollmean(winter, 5))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=summer), color='red')+
geom_point(aes(y=summer2), color='black')+
geom_line(aes(y=summ5yr), color='red')+
scale_y_continuous(name = 'summer temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=winter), color='blue')+
geom_point(aes(y=winter2), color='black')+
geom_line(aes(y=wint5yr), color='blue')+
scale_y_continuous(name = 'winter temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
train <- cdata.all |> subset(!is.na(t))
mod <- lm(t ~ latitude+longitude+elevation+decyear+sindat+cosdat+id,
data=train)
summary(mod)
# train <- train |> mutate(t.lm = predict(mod, train))
# rf <- ranger(t ~ latitude+longitude+elevation+decyear+sindat+cosdat+t.lm,
# data=train, min.node.size = 1, num.trees = 200, sample.fraction = 1, min.bucket = 1)
train <- train |> mutate(t.lm = predict(mod, fitdat))
train <- train |> mutate(t.lm = predict(mod, train))
View(x)
View(stations)
View(xfilter)
gr.resids <- train |> subset(grepl('Grand Rapids', name)) |> mutate(resid = t-t.lm)
gr.resids <- train |> subset(grepl('GRAND RAPIDS', name)) |> mutate(resid = t-t.lm)
View(gr.resids)
gr.resids <- gr.resids |> group_by(decyear) |> summarise(resid = mean(resid))
View(gr.resids)
fitstat <- stations |> subset(id %in% 'USW00094860', select = c(id, latitude, longitude, elevation, state, name))
fittime <- cdata.all |> subset(select = c(year, month, day, decyear, sindat, cosdat)) |> unique() |> arrange(decyear)
fitdat <- merge(fitstat, fittime) |> left_join(train[,c("id", "decyear", "t")], by=join_by(id==id, decyear==decyear))
fitdat <- fitdat |> mutate(t.lm = predict(mod, fitdat))
fitdat <- fitdat |> mutate(t.lm = predict(mod, fitdat)) |> group_by(gr.resids)
fitdat <- merge(fitstat, fittime) |> left_join(train[,c("id", "decyear", "t")], by=join_by(id==id, decyear==decyear))
fitdat <- fitdat |> mutate(t.lm = predict(mod, fitdat)) |> left_join(gr.resids)
View(fitdat)
gr.resids <- train |> mutate(resid = t-t.lm)
gr.resids <- gr.resids |> group_by(decyear) |> summarise(resid = mean(resid))
fitstat <- stations |> subset(id %in% 'USW00094860', select = c(id, latitude, longitude, elevation, state, name))
fittime <- cdata.all |> subset(select = c(year, month, day, decyear, sindat, cosdat)) |> unique() |> arrange(decyear)
fitdat <- merge(fitstat, fittime) |> left_join(train[,c("id", "decyear", "t")], by=join_by(id==id, decyear==decyear))
fitdat <- fitdat |> mutate(t.lm = predict(mod, fitdat)) |> left_join(gr.resids)
View(fitdat)
fitdat <- merge(fitstat, fittime) |> left_join(train[,c("id", "decyear", "t")], by=join_by(id==id, decyear==decyear))
fitdat <- fitdat |> mutate(t.lm = predict(mod, fitdat)) |> left_join(gr.resids) |> mutate(oldt = t, t=resid+t.lm)
View(fitdat)
fitsum <- fitdat |> mutate(winter = ifelse(month %in% c(1,2,12), t.rf,NA), winter2 = ifelse(month %in% c(1,2,12), t,NA), summer = ifelse(month %in% c(6,7,8), t.rf,NA), summer2 = ifelse(month %in% c(6,7,8), t,NA),
year2 = floor(decyear-0.1615)) |> group_by(year2) |> summarise(summer = mean(summer, na.rm=T), winter = mean(winter, na.rm=T), annual = mean(t.rf, na.rm=T), winter2 = mean(winter2, na.rm=T), summer2 = mean(summer2, na.rm=T)) |> subset(year2 > 1892)
fitdat <- merge(fitstat, fittime) |> left_join(train[,c("id", "decyear", "t")], by=join_by(id==id, decyear==decyear))
fitdat <- fitdat |> mutate(t.lm = predict(mod, fitdat)) |> left_join(gr.resids) |> mutate(oldt = t, t=resid+t.lm)
fitsum <- fitdat |> mutate(winter = ifelse(month %in% c(1,2,12), t,NA), winter2 = ifelse(month %in% c(1,2,12), oldt,NA), summer = ifelse(month %in% c(6,7,8), t,NA), summer2 = ifelse(month %in% c(6,7,8), oldt,NA),
year2 = floor(decyear-0.1615)) |> group_by(year2) |> summarise(summer = mean(summer, na.rm=T), winter = mean(winter, na.rm=T), annual = mean(t, na.rm=T), winter2 = mean(winter2, na.rm=T), summer2 = mean(summer2, na.rm=T)) |> subset(year2 > 1892)
library(data.table)
fitsum <- fitsum |> mutate(ann5yr = frollmean(annual, 5), summ5yr = frollmean(summer, 5), wint5yr = frollmean(winter, 5))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=annual), color='black')+
geom_line(aes(y=ann5yr), color='black')+
scale_y_continuous(name = 'annual temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=summer), color='red')+
geom_point(aes(y=summer2), color='black')+
geom_line(aes(y=summ5yr), color='red')+
scale_y_continuous(name = 'summer temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=winter), color='blue')+
geom_point(aes(y=winter2), color='black')+
geom_line(aes(y=wint5yr), color='blue')+
scale_y_continuous(name = 'winter temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=summer), color='red')+
# geom_point(aes(y=summer2), color='black')+
geom_line(aes(y=summ5yr), color='red')+
scale_y_continuous(name = 'summer temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=annual), color='black')+
geom_line(aes(y=ann5yr), color='black')+
scale_y_continuous(name = 'annual temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=winter), color='blue')+
# geom_point(aes(y=winter2), color='black')+
geom_line(aes(y=wint5yr), color='blue')+
scale_y_continuous(name = 'winter temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
fitsum <- fitsum |> mutate(ann5yr = frollmean(annual, 10), summ5yr = frollmean(summer, 10), wint5yr = frollmean(winter, 10))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=annual), color='black')+
geom_line(aes(y=ann5yr), color='black')+
scale_y_continuous(name = 'annual temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=summer), color='red')+
# geom_point(aes(y=summer2), color='black')+
geom_line(aes(y=summ5yr), color='red')+
scale_y_continuous(name = 'summer temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=winter), color='blue')+
# geom_point(aes(y=winter2), color='black')+
geom_line(aes(y=wint5yr), color='blue')+
scale_y_continuous(name = 'winter temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=annual), color='black')+
geom_line(aes(y=ann5yr), color='black')+
scale_y_continuous(name = 'annual temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
fitsum <- fitsum |> mutate(ann5yr = frollmean(annual, 5), summ5yr = frollmean(summer, 5), wint5yr = frollmean(winter, 5))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=annual), color='black')+
geom_line(aes(y=ann5yr), color='black')+
scale_y_continuous(name = 'annual temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=summer), color='red')+
# geom_point(aes(y=summer2), color='black')+
geom_line(aes(y=summ5yr), color='red')+
scale_y_continuous(name = 'summer temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=winter), color='blue')+
# geom_point(aes(y=winter2), color='black')+
geom_line(aes(y=wint5yr), color='blue')+
scale_y_continuous(name = 'winter temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=winter), color='blue')+
# geom_point(aes(y=winter2), color='black')+
geom_line(aes(y=wint5yr), color='blue')+
geom_smooth(aes(y=winter), color='black')+
scale_y_continuous(name = 'winter temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=annual), color='green')+
geom_line(aes(y=ann5yr), color='green')+
geom_smooth(aes(y=annual), color='black')+
scale_y_continuous(name = 'annual temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=annual), color='darkgreen')+
geom_line(aes(y=ann5yr), color='darkgreen')+
geom_smooth(aes(y=annual), color='black')+
scale_y_continuous(name = 'annual temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=summer), color='red')+
# geom_point(aes(y=summer2), color='black')+
geom_line(aes(y=summ5yr), color='red')+
geom_smooth(aes(y=summer), color='black')+
scale_y_continuous(name = 'summer temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
View(fitdat)
View(train)
gr.resids <- train |> mutate(resid = t-t.lm) |> subset(grepl('GRAND RAPIDS', name))
gr.resids <- gr.resids |> group_by(decyear) |> summarise(resid = mean(resid))
fitstat <- stations |> subset(id %in% 'USW00094860', select = c(id, latitude, longitude, elevation, state, name))
fittime <- cdata.all |> subset(select = c(year, month, day, decyear, sindat, cosdat)) |> unique() |> arrange(decyear)
fitdat <- merge(fitstat, fittime) |> left_join(train[,c("id", "decyear", "t")], by=join_by(id==id, decyear==decyear))
fitdat <- fitdat |> mutate(t.lm = predict(mod, fitdat)) |> left_join(gr.resids) |> mutate(oldt = t, t=resid+t.lm)
fitsum <- fitdat |> mutate(winter = ifelse(month %in% c(1,2,12), t,NA), winter2 = ifelse(month %in% c(1,2,12), oldt,NA), summer = ifelse(month %in% c(6,7,8), t,NA), summer2 = ifelse(month %in% c(6,7,8), oldt,NA),
year2 = floor(decyear-0.1615)) |> group_by(year2) |> summarise(summer = mean(summer, na.rm=T), winter = mean(winter, na.rm=T), annual = mean(t, na.rm=T), winter2 = mean(winter2, na.rm=T), summer2 = mean(summer2, na.rm=T)) |> subset(year2 > 1892)
library(data.table)
fitsum <- fitsum |> mutate(ann5yr = frollmean(annual, 5), summ5yr = frollmean(summer, 5), wint5yr = frollmean(winter, 5))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=annual), color='darkgreen')+
geom_line(aes(y=ann5yr), color='darkgreen')+
geom_smooth(aes(y=annual), color='black')+
scale_y_continuous(name = 'annual temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=summer), color='red')+
# geom_point(aes(y=summer2), color='black')+
geom_line(aes(y=summ5yr), color='red')+
geom_smooth(aes(y=summer), color='black')+
scale_y_continuous(name = 'summer temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=winter), color='blue')+
# geom_point(aes(y=winter2), color='black')+
geom_line(aes(y=wint5yr), color='blue')+
geom_smooth(aes(y=winter), color='black')+
scale_y_continuous(name = 'winter temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
gr.resids <- train |> mutate(resid = t-t.lm) #|> subset(grepl('GRAND RAPIDS', name))
gr.resids <- gr.resids |> group_by(decyear) |> summarise(resid = mean(resid))
fitstat <- stations |> subset(id %in% 'USW00094860', select = c(id, latitude, longitude, elevation, state, name))
fittime <- cdata.all |> subset(select = c(year, month, day, decyear, sindat, cosdat)) |> unique() |> arrange(decyear)
fitdat <- merge(fitstat, fittime) |> left_join(train[,c("id", "decyear", "t")], by=join_by(id==id, decyear==decyear))
fitdat <- fitdat |> mutate(t.lm = predict(mod, fitdat)) |> left_join(gr.resids) |> mutate(oldt = t, t=resid+t.lm)
fitsum <- fitdat |> mutate(winter = ifelse(month %in% c(1,2,12), t,NA), winter2 = ifelse(month %in% c(1,2,12), oldt,NA), summer = ifelse(month %in% c(6,7,8), t,NA), summer2 = ifelse(month %in% c(6,7,8), oldt,NA),
year2 = floor(decyear-0.1615)) |> group_by(year2) |> summarise(summer = mean(summer, na.rm=T), winter = mean(winter, na.rm=T), annual = mean(t, na.rm=T), winter2 = mean(winter2, na.rm=T), summer2 = mean(summer2, na.rm=T)) |> subset(year2 > 1892)
library(data.table)
fitsum <- fitsum |> mutate(ann5yr = frollmean(annual, 5), summ5yr = frollmean(summer, 5), wint5yr = frollmean(winter, 5))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=annual), color='darkgreen')+
geom_line(aes(y=ann5yr), color='darkgreen')+
geom_smooth(aes(y=annual), color='black')+
scale_y_continuous(name = 'annual temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=summer), color='red')+
# geom_point(aes(y=summer2), color='black')+
geom_line(aes(y=summ5yr), color='red')+
geom_smooth(aes(y=summer), color='black')+
scale_y_continuous(name = 'summer temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
ggplot(data=fitsum, aes(x=year2))+
geom_point(aes(y=winter), color='blue')+
# geom_point(aes(y=winter2), color='black')+
geom_line(aes(y=wint5yr), color='blue')+
geom_smooth(aes(y=winter), color='black')+
scale_y_continuous(name = 'winter temperature (C)')+
scale_x_continuous(name = 'year', breaks = (c(-1:13)*10+1900))
shiny::runApp('C:/workspace2/StationClimR2')
runApp('C:/workspace2/StationClimR2')
shiny::runApp()