-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalregression.Rmd
697 lines (486 loc) · 19.8 KB
/
localregression.Rmd
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
---
title: "Local Regression analysis"
author: "Rosie"
date: "7/22/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(lubridate)
library(tidyverse)
library(vegan)
library(sf)
if (!require("rspatial")) devtools::install_github('rspatial/rspatial')
library(rspatial)
library( spgwr )
library(nlme)
library(boot)
library(itsadug)
library(mgcv)
library(raster)
#load the data
temps = readRDS("Temp_filtered (1).rds")
#first calculate the daily means
tempmean = temps %>%
filter(Date > as.Date("2014-1-1"), Station != "RYF") %>%
mutate(julian = yday(Date)) %>%
group_by(Station, julian) %>%
summarize(Tempave = mean(Temp, na.rm = T), TempMax = max(Temp, na.rm = T),
TempMin = min(Temp, na.rm = T))
tempmeanx = temps %>%
filter(Date > as.Date("2014-1-1"), Station != "RYF") %>%
mutate(julian = yday(Date), Year = year(Date)) %>%
group_by(Station, julian, Year, Date) %>%
summarize(Tempave = mean(Temp, na.rm = T), TempMax = max(Temp, na.rm = T),
TempMin = min(Temp, na.rm = T), Temprange = TempMax-TempMin)
save(tempmeanx, file = "tempmeanx.RData")
```
## Local regression
One of the cool ways you can address differences across space is run a regression that calculates different coefficients over space. I ran the model over the continuous stations, then extrapolated the results to the rest of the Detla.
First we have to do some data manipulation to get everything into a spatial format.
```{r}
#read in shapefile of the delta
delta = read_sf("DeltaShapefile/hydro_delta_marsh.shp")
#add lat/longs for the stations
stas = read.csv("StationLatLongs.csv")
#attached lat/longs to mean temperature
tempmean2 = merge(tempmean, stas)
#Specify a coordinate reference system and turn it into a spatial object
alb <- CRS("+proj=aea +lat_1=34 +lat_2=40.5 +lat_0=0 +lon_0=-120 +x_0=0 +y_0=-4000000 +ellps=GRS80 +datum=NAD83 +units=m +no_defs")
#Filter the Delta shapefile so it doesn't include areas where we dont have a lot of data
delta2 = sf::as_Spatial(dplyr::filter(delta, HNAME != "SAN FRANCISCO BAY", HNAME != "SAN PABLO BAY"))
#I'm not positive we have to do this transformation, but it was in teh example so I'm doing it.
ctst <- spTransform(delta2, alb)
```
Now I'll filter out just the data from January-June and find the optimal badwidth for the interpolation. I think this basically tells you how much "smoothing" to put between your sampling points.
```{r}
tempmean2.1 = dplyr::filter(tempmean2, julian <190)
tempmeansub = sample_n(tempmean2.1, 1000)
sp = tempmeansub
coordinates(sp) = ~ Longitude + Latitude
crs(sp) <- "+proj=longlat +datum=NAD83"
spt <- spTransform(sp, alb)
#find optimal bandwith
#THIS TAKES FOREVER!
bw <- gwr.sel( Temp~ julian, data= spt)
```
To make the pretty map, I need to create a blank raster from the Delta shapefile. Once I calculate the predicted values for the regression, I'll fill it in.
```{r}
r <- raster(ctst, res=100)
rdelta2 <- rasterize(ctst, r)
newpts <- rasterToPoints(rdelta2)
#Turn Newpoints into a spatial grid
newpts2 = as.data.frame(newpts)
coordinates(newpts2) = ~ x + y
crs(newpts2) = alb
save(newpts2, rdelta2, r, delta2, file = "spatialdata.RData")
```
Now I run the GWR function to calculate local regressions
```{r warning=F}
#
g <- gwr(Temp ~ julian, data= spt,
bandwidth=bw, fit.points=newpts[, 1:2])
g
resultsg = as.data.frame(g$SDF)
#sigTest = abs(g$SDF$julian) -2 * g$SDF$julian_se
#Note: i'd like to run it with "se.fit = T" so I can calculate the
#standard error, but it doens't seem to be working. I had it running
#all weekend, and noting.
```
Put the fitted points back on my raster and plot it!
```{r fig.width=15}
slope <- rdelta2
intercept <- rdelta2
slope[!is.na(slope)] <- g$SDF$julian
intercept[!is.na(intercept)] <- g$SDF$'(Intercept)'
s <- stack(intercept, slope)
names(s) <- c('intercept', 'slope')
plot(s, xlim = c(-200000, -100000), ylim = c(-50000,50000))
```
##Now the cool part - how things have changed over the historical record
```{r}
#first calculate the daily means
temps$Year = year(temps$Date)
tempmax = temps %>%
mutate(julian = yday(Date)) %>%
group_by(Station, julian, Year, Date) %>%
summarize(Tempave = mean(Temp, na.rm = T),
maxTemp = max(Temp, na.rm = T),
minTemp = min(Temp, na.rm = T))
tempmax2 = merge(tempmax, stas)
#tempmax2.1 = dplyr::filter(tempmax2, julian <250)
#spx = sample_n(tempmax2.1, 10000)
spx = tempmax2
#turn it into a spatial object
coordinates(spx) = ~ Longitude + Latitude
crs(spx) <- "+proj=longlat +datum=NAD83"
sptx <- spTransform(spx, alb)
#find badwidth
bwx <- gwr.sel( maxTemp~ julian + Year, data= sptx)
gx <- gwr(maxTemp~ julian + Year, data= sptx,
bandwidth=bwx, fit.points=newpts[, 1:2])
gx
```
Put the fitted points back on my raster and plot it!
```{r fig.width=15}
slopeDay <- rdelta2
slopeYear <- rdelta2
intercept <- rdelta2
slopeDay[!is.na(slopeDay)] <- gx$SDF$julian
slopeYear[!is.na(slopeYear)] <- gx$SDF$Year
intercept[!is.na(intercept)] <- gx$SDF$'(Intercept)'
s <- stack(intercept, slopeDay, slopeYear)
names(s) <- c('intercept', 'Seasonal Change', "Inter-annual Change")
plot(s, xlim = c(-200000, -100000), ylim = c(-50000,50000))
```
## Temporal autocorrelation
I'm not sure I need to worry about temporal autocorrelation if time is part of my model, but I'll do some testing.
```{r}
testdat = filter(tempmax2.1, Station == first(Station)) %>%
arrange(Date)
acf(testdat$maxTemp)
pacf(testdat$maxTemp)
lmtest = lm(maxTemp~ julian + Year, data = testdat)
summary(lmtest)
acf(lmtest$residuals)
pacf(lmtest$residuals)
#find the start value of rho
acf(resid(lmtest), plot=FALSE)$acf[2]
# I'm confused and don't know what i'm diong
lmtest2 = gls(maxTemp~ julian + Year, data = testdat, correlation = corAR1(form = ~ Date))
acf(lmtest2$residuals)
acf_resid(lmtest2)
#try a gam
m1 = bam(maxTemp ~ s(julian) + s(Year), data=testdat)
acf(m1$residuals)
summary(m1)
#calculate start value of Rho
r1 = acf(resid(m1), plot=FALSE)$acf[2]
#identify the start of the time series
testdat = start_event(testdat, column="Date", event=c("Station", "Year"), label.event="Event")
m1AR1 = bam(maxTemp ~ s(julian) + s(Year), data=testdat, rho=r1, AR.start=testdat$start.event)
summary(m1AR1)
acf_resid(m1AR1)
```
So, I may want to try and use a GAM or something. Not sure.
## Package GWmodel
Before I investigate spatial GAMs, I want to try a different package for the geographically weighted regression.
First, compute the distances between the points on the grid where the model will be estimated, and the station locations. This takes a while, but if it is pre-computed, successive applications of GWR functions are speeded up.
```{r}
library(GWmodel)
DM <- gw.dist(dp.locat=coordinates(spt),rp.locat=coordinates(newpts2))
#now the model
gwr.res <- gwr.basic(Temp~ julian, data= spt, regression.points=newpts2, bw=bw, dMat=DM,kernel='gaussian')
gwr.res
```
Put the fitted points back on my raster and plot it!
```{r fig.width=15}
slopeDay <- rdelta2
intercept <- rdelta2
slopeDay[!is.na(slopeDay)] <- gwr.res$SDF$julian
#slopeYear[!is.na(slopeYear)] <- gx$SDF$Year
intercept[!is.na(intercept)] <- gwr.res$SDF$Intercept
s <- stack(intercept, slopeDay)
names(s) <- c('intercept', 'Seasonal Change')
plot(s, xlim = c(-200000, -100000), ylim = c(-50000,50000))
```
Estimate standard errors using bootstrapping
```{r}
bsm.res1 <- gwr.bootstrap(Temp~julian, kernel = "gaussian", sptx, R=99)
bsm.res1
set.seed(4676)
gwrcoef <- function(hpdf,i) gwr.basic, data=spt[i,], regression.points=newpts2, bw=bw, dMat=DM[i,],kernel='gaussian')$SDF$PROF
bootres <- boot(spt,gwrcoef,100)
gwr.res$SDF$bsePROF <- sqrt(apply(bootres$t,2,var))
image(gwr.res$SDF,'bsePROF')
plot(londonborough,add=TRUE)
###UGH!! NONE OF THIS IS WORKING!!!
```
Maybe I should just use seasonal means so I don' tneed to worry about the temporal autocorrelation
```{r}
#filter out just the summer months and calculate mean, min max for the season
summer = dplyr::filter(tempmax2, julian <244, julian >153) %>%
group_by(Year, Station, Latitude, Longitude) %>%
summarize(maxTemp = mean(maxTemp, na.rm = T), minTemp = mean(minTemp, na.rm = T), Temp = mean(Temp, na.rm = T))
#spx = sample_n(tempmax2.1, 10000)
sppt = summer
#turn it into a spatial object
coordinates(sppt) = ~ Longitude + Latitude
crs(sppt) <- "+proj=longlat +datum=NAD83"
sppt <- spTransform(sppt, alb)
#find badwidth
bwx <- gwr.sel( maxTemp~ Year, data= sppt)
DM <- gw.dist(dp.locat=coordinates(sppt),rp.locat=coordinates(newpts2))
#now the model
gwr.res <- gwr.basic(Temp~ Year, data= sppt, regression.points=newpts2, bw=bwx, dMat=DM,kernel='gaussian')
gwr.res
#see what the other package gives us
gx <- gwr(maxTemp~ Year, data= sppt,
bandwidth=bwx, fit.points=newpts[, 1:2])
gx
slopeYear <- rdelta2
intercept <- rdelta2
slopeYear[!is.na(slopeYear)] <- gwr.res$SDF$Year
intercept[!is.na(intercept)] <- gwr.res$SDF$Intercept
s <- stack(intercept, slopeYear)
names(s) <- c('intercept', 'long-term change')
plot(s, xlim = c(-200000, -100000), ylim = c(-50000,50000))
bsm.res1 <- gwr.bootstrap(Temp~Year, kernel = "gaussian", sppt, R=99)
bsm.res1
#UGH still not working
```
## GAMs
```{r}
tempmax2 = arrange(tempmax2, Station, Date) %>%
filter(Year >1995)
g1 = bam(Temp ~ s(julian, bs="cc" ) + s(Year) + s(Latitude, Longitude),
data = tempmax2, method = "REML")
summary(g1)
plot(g1)
vis.gam(g1, view = c("Longitude", "Latitude"), plot.type = "contour",
too.far = 0.1)
vis.gam(g1, view = c("Longitude", "Latitude"), plot.type = "persp",
too.far = 0.1, se = 2)
#use a tensor product smooth for an interaction between
#space and time
g2 = bam(Temp ~ s(julian, bs="cc" ) + s(Year) +
te(Latitude, Longitude, Year),
data = tempmax2, method = "REML")
summary(g2)
#now the interaction version
g3 = bam(Tempave ~ s(julian, bs="cc" ) + s(Year) +
ti(Latitude, Longitude, Year) +
ti(Latitude, Longitude, julian),
data = tempmax2, method = "REML")
summary(g3)
plot(g3)
```
try adding the temporal correlation term
```{r}
#calculate start value of Rho
r1 = acf(resid(g3), plot=FALSE)$acf[2]
#identify the start of the time series
testdat = start_event(tempmax2, column="Date", event=c("Station", "Year"), label.event="Event")
g4 = bam(Tempave ~ s(julian, bs = "cc") + s(Year) +
ti(Latitude, Longitude, Year)+
ti(Latitude, Longitude, julian),
data =testdat, method = "REML", rho=r1, AR.start=testdat$start.event)
summary(g4)
plot(g4)
acf(resid(g4))
acf_resid(g4, split_pred = c("Year"))
```
now do it with maximum temperatures
```{r}
g3x = bam(maxTemp ~ s(julian) + s(Year) +
ti(Latitude, Longitude, Year) +
ti(Latitude, Longitude, julian),
data = testdat, method = "REML")
#calculate start value of Rho
r1x = acf(resid(g3x), plot=FALSE)$acf[2]
g4x = bam(maxTemp ~ s(julian) + s(Year) +
ti(Latitude, Longitude, Year)+
ti(Latitude, Longitude, julian),
data =testdat, method = "REML", rho=r1x, AR.start=testdat$start.event)
summary(g4x)
plot(g4x)
acf_resid(g4x)
vis.gam(g4x, view = c("Longitude", "Latitude"), plot.type = "contour",
too.far = 0.1)
#gamtabs(g4x, type = 'html')
g4x2 = bam(maxTemp ~
te(Latitude, Longitude, Year)+
te(Latitude, Longitude, julian),
data =testdat, method = "REML", rho=r1x, AR.start=testdat$start.event)
summary(g4x2)
plot(g4x2)
acf_resid(g4x2)
vis.gam(g4x, view = c("Longitude", "Latitude"), plot.type = "contour",
too.far = 0.1)
#gamtabs(g4x, type = 'html')
```
Just the stations with long-term data
```{r}
load("Tempmax.RData")
tempmax_sum = group_by(tempmax, Station) %>%
summarize(minyear = min(Year), maxyear = max(Year))
tempmax_long = left_join(testdat, tempmax_sum) %>%
filter(minyear < 2001)
g3x2 = bam(maxTemp ~ s(julian, bs = "cc") + s(Year),
data = tempmax_long, method = "REML")
#calculate start value of Rho
r1x2 = acf(resid(g3x2), plot=FALSE)$acf[2]
g4x2 = bam(maxTemp ~
s(Year)+
s(julian, bs = "cc"),
data =tempmax_long, method = "REML", rho=r1x2, AR.start=tempmax_long$start.event)
summary(g4x2)
plot(g4x2)
#now try the Minimum temperatures, like peggy suggested
g3x2m = bam(minTemp ~ s(julian, bs = "cc") + s(Year),
data = tempmax_long, method = "REML")
#calculate start value of Rho
r1x2m = acf(resid(g3x2m), plot=FALSE)$acf[2]
g4x2m = bam(minTemp ~
s(Year)+
s(julian, bs = "cc"),
data =tempmax_long, method = "REML", rho=r1x2m, AR.start=tempmax_long$start.event)
summary(g4x2m)
plot(g4x2m)
#hm. Minimum temperatures were not very interesting either
```
Just winter
```{r}
#let's try just the winter, since peggy and sam both thought that
#might be useful
winter = mutate(tempmax_long, Month = month(Date)) %>%
filter(Month %in% c(11))
gwin = bam(minTemp ~
s(Year)+
s(julian),
data =winter, method = "REML", rho=r1x2m, AR.start=winter$start.event)
summary(gwin)
plot(gwin)
gwin2 = bam(Tempave ~
s(Year)+
s(julian),
data =winter, method = "REML", rho=r1x2m, AR.start=winter$start.event)
summary(gwin2)
plot(gwin2)
```
If I don't have a lot of stations with the long time series, ditch spatial structure and move to random effects.
```{r}
save(tempmax, file = "Tempmax.RData")
save(tempmax2.1, file = "tempmax2.1.RData")
save(tempmean, file = "tempmean.RData")
save(tempmean2.1, file = "tempmean.RData")
save(testdat, file = "testdat.RData")
load("testdat.RData")
g4x2 = gamm(maxTemp ~
s(Year)+
s(julian, bs = "cc"),
data =tempmax_long, method = "REML", correlation = corAR1(form = ~1|Station))
#Error: cannot allocate vector of size 10.0 Gb
#meh
```
We wouldn't expect to see a ton of change-over-time anyway. Maybe concentrate on spatial differences
```{r}
g5 = bam(maxTemp ~ s(julian, bs = "cc") +
te(Latitude, Longitude),
data =testdat, method = "REML")
r5 = acf(resid(g5), plot=FALSE)$acf[2]
g5 = bam(maxTemp ~ s(julian, bs = "cc") +
te(Latitude, Longitude),
data =testdat, method = "REML", rho=r5, AR.start=testdat$start.event)
plot(g5)
```
I gotta figure out how to put these results on a map
```{r}
load("spatialdata.RData")
regions = read_sf("RosieRegions/shpExport.shp")
regions = st_transform(regions, crs = 4326)
stas = read.csv("StationLatLongs.csv")
coordinates(stas) = ~ Longitude + Latitude
crs(stas) <- "+proj=longlat +datum=NAD83"
delta2x = spTransform(delta2, CRS("+init=epsg:4326"))
r <- raster(delta2x, res=100)
rdelta3 <- rasterize(delta2, r)
newpts3 <- rasterToPoints(rdelta3)
predict(newpts, g5)
delta = st_transform(delta,crs=4326)
stas = st_as_sf(stas) %>%
st_transform(stas, crs=4326)
WQ_pred<-function(Full_data=Data,
Delta_subregions = regions,
Delta_water=delta,
Stations = stas,
n=100,
Years=round(seq(min(Full_data$Year)+2, max(Full_data$Year)-2, length.out=9)),
Julian_days=yday(ymd(paste("2001", c(1,4,7,10), "15", sep="-"))) #Jan, Apr, Jul, and Oct 15 for a non-leap year
){
# Create point locations on a grid for predictions
Points<-st_make_grid(Delta_subregions, n=n)%>%
st_as_sf(crs=st_crs(Delta_subregions))%>%
st_join(Delta_water)%>%
# Joining a map of delta waterways (from my spacetools package) to ensure all these points are over water.
st_coordinates()%>%
as_tibble()%>%
mutate(Location=1:nrow(.))%>%
dplyr::select(Longitude=X, Latitude=Y, Location)
# Create full dataset for predictions
newdata<-expand.grid(Year= Years,
Location=1:nrow(Points),
Julian_day=Julian_days)%>% # Create all combinations of predictor variables
left_join(Points, by="Location")%>% #Add Lat/Longs to each location
mutate(Latitude_s=(Latitude-mean(Full_data$Latitude, na.rm=T))/sd(Full_data$Latitude, na.rm=T), # Standardize each variable based on full dataset for model
Longitude_s=(Longitude-mean(Full_data$Longitude, na.rm=T))/sd(Full_data$Longitude, na.rm=T),
Year_s=(Year-mean(Full_data$Year, na.rm=T))/sd(Full_data$Year, na.rm=T),
Julian_day_s = (Julian_day-mean(Full_data$julian, na.rm=T))/sd(Full_data$julian, na.rm=T),
Year_fac=factor(Year)) %>%
st_as_sf(coords=c("Longitude", "Latitude"), crs=4326, remove=FALSE)%>% # Turn into sf object
st_transform(crs=st_crs(Delta_subregions))%>% # transform to crs of Delta shapefile
st_join(Delta_subregions, join = st_intersects)
return(newdata)
}
newdata_year <- WQ_pred(Full_data=tempmax_long,
Julian_days = yday(ymd(paste("2001", 1:12, "15", sep="-"))),
Years=round(min(tempmax_long$Year):max(tempmax_long$Year)))
newdata_year = rename(newdata_year, julian = Julian_day)
modellc4_predictions<-predict(g5, newdata=newdata_year, type="response", se.fit=TRUE, discrete=T) # Create predictions
# Predictions stored as "modellc4_predictions.Rds"
newdata<-newdata_year%>%
mutate(Prediction=modellc4_predictions$fit)%>%
mutate(SE=modellc4_predictions$se.fit,
L95=Prediction-SE*1.96,
U95=Prediction+SE*1.96)%>%
mutate(Date=as.Date(julian, origin=as.Date(paste(Year, "01", "01", sep="-")))) # Create Date variable from Julian Day and Year
library(stars)
# Function to rasterize all dates. Creates a 3D raster Latitude x Longitude x Date
Rasterize_all <- function(data, var, out_crs=4326, n=100){
var<-rlang::enquo(var)
rlang::as_name(var)
preds<-map(unique(data$Date), function(x) st_rasterize(data%>%
filter(Date==x)%>%
dplyr::select(!!var),
template=st_as_stars(st_bbox(delta2), dx=diff(st_bbox(delta2)[c(1, 3)])/n, dy=diff(st_bbox(delta2)[c(2, 4)])/n, values = NA_real_))%>%
st_warp(crs=out_crs))
# Then bind all dates together into 1 raster
out <- exec(c, !!!preds, along=list(Date=unique(data$Date)))
return(out)
}
# Create full rasterization of all predictions for interactive visualizations
rastered_preds<-Rasterize_all(newdata, Prediction)
# Same for SE
rastered_SE<-Rasterize_all(newdata, SE)
# Bind SE and predictions together
rastered_predsSE<-c(rastered_preds, rastered_SE)
newdata_rast_season <- newdata%>%
mutate(Month=month(Date))%>%
filter(Year%in%round(seq(min(newdata$Year)+2, max(newdata$Year)-2, length.out=9)) & Month%in%c(1,4,7,10))
raster_plot<-function(data, Years=unique(newdata$Year), labels="All"){
ggplot()+
geom_stars(data=data)+
# facet_wrap(julian~.)+
scale_fill_viridis_c(name="Temperature", na.value="white", breaks=seq(6,26,by=1), labels= function(x) ifelse((x/2)==as.integer(x/2), as.character(x), ""),
guide = guide_colorbar(direction="horizontal", title.position = "top", barwidth = 4, ticks.linewidth = 2,
barheight=0.4, title.hjust=0.5, label.position="bottom", label.theme=element_text(size=8),
title.theme=element_text(size=10)))+
coord_sf()+
ylab("Latitude")+
xlab("Longitude")+
theme_bw()
}
raster_plot(rastered_preds)
#########################################
rp2 = as.data.frame(rastered_preds)
ggplot()+
geom_stars(data=rastered_preds)+
scale_fill_viridis_c(name="Temperature", na.value="white", breaks=seq(6,26,by=1), labels= function(x) ifelse((x/2)==as.integer(x/2), as.character(x), ""),
guide = guide_colorbar(direction="horizontal", title.position = "top", barwidth = 4, ticks.linewidth = 2,
barheight=0.4, title.hjust=0.5, label.position="bottom", label.theme=element_text(size=8),
title.theme=element_text(size=10)))+
coord_sf()+
ylab("Latitude")+
xlab("Longitude")+
theme_bw()
```