## Note the start time
<-proc.time()[3]
start.time# Download mapfiles and data files from https://github.com/sujit-sahu/bookbmstdr
# Set the following paths
<- "figures/chap9figures/" # Folder to save or read the figures from
figurepath <- "txttables/" # Folder to save or read the tables from
filepath <- "mapfiles" # Contains the map files
mappath <- "datafiles/" # Contains the data files
dpath <- FALSE # Should we run the MCMC models to check the results.
longrun # If FALSE we use the results, e.g. tables, figures and model fits from previous runs
## Warning in .recacheSubclasses(def@className, def, env): undefined subclass
## "numericVector" of class "Mnumeric"; definition not updated
1 Code to reproduce Figure 9.1
<- read.table(paste0(dpath, "C9newminimummses.txt"), head=T)
u $hour <- u$hour +1
uplot(u$hour, sqrt(u$cmaqmse), pch="+", las=1, cex=1.5, ylim=range(30, 2, sqrt(c(u$cmaqmse, u$modmse))), ylab="RMSE", xlab="Hour of Day (EDT)", col=6)
points(u$hour, sqrt(u$modmse), pch="*", cex=1.5, col=2)
points(u$hour, sqrt(u$linfitmse), pch="a", cex=1, col=4)
<- nrow(u)
k <- data.frame(phisw=rep(u$phis, 3), phitw= rep(u$phitw, 3),
unew rmse=c(rep("CMAQ", k), rep("Linear", k), rep("Bayes", k)),
vcrit=sqrt(c(u$cmaqmse, u$linfitmse, u$modmse)),
hour=rep(u$hour, 3), day=rep(u$day, 3))
$rmse <- factor(unew$rmse, levels=c("CMAQ", "Linear", "Bayes"))
unew$day <- as.factor(unew$day)
unew$hour <- as.factor(unew$hour)
unew# aphi$Criteria <- factor(aphi$Criteria, levels=rev(levels(aphi$Criteria)))
<- ggplot() +
p geom_point(data=unew, aes(x=hour, y=vcrit, group=rmse, color=rmse, shape=rmse)) +
theme(legend.position=c(0.87, 0.87)) +
scale_x_discrete(name="Hour of day", breaks=seq(from=6, to =16, by=2),
labels=c("6AM", "8AM", "10AM", "Noon", "2PM", "4PM")) +
scale_y_continuous(name="Root mean square error")
p
ggsave(filename = paste0(figurepath, "easrernusforecastrmse.png"))
## Saving 7 x 5 in image
2 Figures in Section 9.4 for nysptiime data set
if (longrun){
<- y8hrmax~xmaxtemp+xwdsp+xrh
f2 <- getvalidrows(sn=28, tn=62, valids=1:28, validt=61:62)
vs length(vs)
<- Bsptime(package="stan",formula=f2, data=nysptime, validrows=vs,
M4 coordtype="utm", coords=4:5, scale.transform = "SQRT",
N=1500, burn.in=500, mchoice=T,
verbose = F)
summary(M4)
<- nysptime[-vs, ]
dfit <- nysptime[vs, ]
dfore
<- Bsptime(package="spTimer", formula=f2, data=dfit, coordtype="utm",
M3new coords=4:5, scale.transform = "SQRT", N=5000, n.report = 1)
summary(M3new)
<- predict(M3new$fit,newdata=dfore, newcoords=~Longitude+Latitude,
nfore type="temporal", foreStep=2, tol=0.05)
<- calculate_validation_statistics(yval=dfore$y8hrmax,
res1 yits=nfore$fore.samples, summarystat = "median")
# unlist(res1$stats)
# unlist(M4$stats)
<- data.frame(sptimer=unlist(res1$stats), stan=unlist(M4$stats))
a
a
<- dfore$y8hrmax
yobs
<- M4$valpreds
stanpreds dim(stanpreds)
<- nfore$fore.samples
sptpreds dim(sptpreds)
<- get_validation_summaries(t(sptpreds))
psumspt <- get_validation_summaries(t(stanpreds))
psumstan # cbind(yobs, psumspt, psumstan)
<- hitandfalsealarm(thresh=65, yobs=yobs, ypred=psumspt$medianpred)
spthitfa65 <- hitandfalsealarm(thresh=75, yobs=yobs, ypred=psumspt$medianpred)
spthitfa75 # spthitfa65
# spthitfa75
<- hitandfalsealarm(thresh=65, yobs=yobs, ypred=psumstan$medianpred)
stanhitfa65 <- hitandfalsealarm(thresh=75, yobs=yobs, ypred=psumstan$medianpred)
stanhitfa75 # stanhitfa65
# stanhitfa75
#
<- get_parameter_estimates(t(sptpreds), level=50)
ints50 <- get_parameter_estimates(t(sptpreds), level=90)
ints90 <- ints50$up - ints50$low
ints50lenspt <- ints90$up - ints90$low
ints90lenspt
<- get_parameter_estimates(t(stanpreds), level=50)
ints50 <- get_parameter_estimates(t(stanpreds), level=90)
ints90 <- ints50$up - ints50$low
ints50lenstan <- ints90$up - ints90$low
ints90lenstan
<- length(ints50lenspt)
k <- rep(c("spTimer50", "spTimer90", "stan50", "stan90"), each=k)
ids <- rep(c("spTimer", "spTimer", "stan", "stan"), each=k)
package <- data.frame(lenths = c(ints50lenspt, ints90lenspt, ints50lenstan, ints90lenstan),
lendat ids = factor(ids), package=factor(package))
head(lendat)
# lendat
<- function(x) {
findprop <- x[1]
value <- x[-1]
samples <- length(samples[samples<value])/length(samples)
prop
prop
}
<- cbind(yobs, sptpreds)
x dim(x)
<- na.omit(x)
y dim(y)
findprop((y[1, ]))
<- apply(y, 1, findprop)
sptpitvals
<- cbind(yobs, stanpreds)
x dim(x)
<- na.omit(x)
y dim(y)
findprop((y[1, ]))
<- apply(y, 1, findprop)
stanpitvals <- length(stanpitvals)
k
<- data.frame(pit=c(sptpitvals, stanpitvals), package=rep(c("spTimer", "Stan"), each=k))
pitvals
<- function(yobs, ysample) {
fbarhat # yobs m by 1
# ysample N by m
<- cbind(yobs, t(ysample))
x dim(x)
<- na.omit(x)
y dim(y)
<- y[, -1]
x dim(x)
length(yk)
<- ncol(x)
N
N# x is the matrix of MCMC samples
<- nrow(x)
m <- matrix(NA, nrow=m, ncol=K)
amat for (i in 1:m) {
<- matrix(x[i, ], nrow=N, ncol=K, byrow=TRUE)
ynewmat dim(yomat)
<- rbind(yk, ynewmat)
u dim(u)
<- apply(u, 2, findprop)
v <- as.vector(v)
amat[i, ]
}<- apply(amat, 2, mean)
Fbarhat # dans <- Fbarhat - Ghat
Fbarhat
}
<- 100
K <- na.omit(yobs)
ygood summary(ygood)
<- seq(from=min(ygood), to = max(ygood), length=K)
yk <- matrix(ygood, nrow=K, ncol=length(ygood), byrow=TRUE)
yomat dim(yomat)
<- cbind(yk, yomat)
u dim(u)
<- apply(u, 1, findprop)
Ghat length(Ghat)
summary(Ghat)
<- fbarhat(yobs, t(sptpreds)) - Ghat
dansspt summary(dansspt)
<- fbarhat(yobs, t(stanpreds)) - Ghat
dansstan summary(dansstan)
<- factor(rep(c("spTimer", "Stan"), each=K))
package <- data.frame(dans=c(dansspt, dansstan), yk = rep(yk, 2), package=package)
ddat head(ddat)
summary(ddat)
save(lendat, pitvals, ddat, file="forecast/nysptimeresults.RData")
else{
} load(file="forecast/nysptimeresults.RData")
}
<- ggplot(data=lendat, aes(x=ids, y=lenths, col=package)) +
sharpdiag geom_boxplot() +
labs(x = "", y = "", title="Sharpness diagram")
sharpdiag
# Figure 9.2
ggsave(filename = paste0(figurepath, "ny_spt_stan_sharpdiagram.png"))
<- ggplot(data=pitvals, aes(pit, colour=package, fill=package)) +
pitdiag geom_histogram() +
facet_wrap(~ package, ncol = 2) +
labs(x = "", y = "") +
scale_colour_manual(values = c("spTimer" = "red",
"Stan" = "blue"))
pitdiag
# Figure 9.3
ggsave(filename = paste0(figurepath, "ny_sptimer_Stan_pitdiagram.png"))
<- ggplot(data=ddat, aes(x=yk, y=dans, colour=package, linetype=package)) +
mcpdiag geom_line() +
labs(x = "", y = "") +
geom_abline(slope=0, intercept = 0, size=1, col="black")+
scale_colour_manual(values = c("spTimer" = "red",
"Stan" = "blue"))
mcpdiag
ggsave(filename =paste0(figurepath, "ny_sptstanmcp_diagram.png"))
3 Figures and tables in Section 9.5
load(file=paste0(dpath, "C94states.RData"))
<- unique(cmaqdata4[, 1:4])
cmaqsites
<- 0.5
delta <- c(min(data4$long)-1.5*delta, max(data4$long)+delta)
a <- c(min(data4$lat)-delta, max(data4$lat)+1.5*delta)
b
a## [1] -92.0858 -80.0725
b## [1] 36.1081 43.2169
<- spT.grid.coords(Lon=a, Lat=b, by=c(15, 15))
knots.coords dim(knots.coords)
## [1] 225 2
<- data.frame(s.index=1:nrow(knots.coords), long=knots.coords[,1], lat=knots.coords[,2])
knots head(knots)
## s.index long lat
## 1 1 -92.0858 36.10810
## 2 2 -92.0858 36.61587
## 3 3 -92.0858 37.12364
## 4 4 -92.0858 37.63141
## 5 5 -92.0858 38.13919
## 6 6 -92.0858 38.64696
<- maps::map.where(database = "state", x =knots$long, y = knots$lat)
a <- data.frame(knots, states=a)
b head(b)
## s.index long lat states
## 1 1 -92.0858 36.10810 arkansas
## 2 2 -92.0858 36.61587 missouri
## 3 3 -92.0858 37.12364 missouri
## 4 4 -92.0858 37.63141 missouri
## 5 5 -92.0858 38.13919 missouri
## 6 6 -92.0858 38.64696 missouri
<- data.frame(states=c("ohio", "indiana", "illinois", "kentucky"))
d <- merge(b, d)
knots4 dim(knots4)
## [1] 108 4
head(knots4)
## states s.index long lat
## 1 illinois 70 -88.65343 40.67804
## 2 illinois 38 -90.36961 39.66250
## 3 illinois 67 -88.65343 39.15473
## 4 illinois 68 -88.65343 39.66250
## 5 illinois 69 -88.65343 40.17027
## 6 illinois 71 -88.65343 41.18581
<- knots4[, c(2, 1, 3, 4)]
knots4 head(data4)
## s.index states long lat year month day obsO8hrmax cmaqO8hrmax
## 9 56 illinois -91.3358 39.9175 2010 7 1 54.28571 54.65538
## 13 56 illinois -91.3358 39.9175 2010 7 2 78.71429 78.54122
## 7 56 illinois -91.3358 39.9175 2010 7 3 77.00000 70.05393
## 12 56 illinois -91.3358 39.9175 2010 7 4 62.85714 47.91287
## 14 56 illinois -91.3358 39.9175 2010 7 5 47.25000 56.27459
## 8 56 illinois -91.3358 39.9175 2010 7 6 35.71429 45.65875
<- unique(data4[, 1:4])
fourstates dim(fourstates)
## [1] 156 4
<- map_data(database="state",regions=name4)
map4states $type <- "Data"
fourstates$type <- "Knots"
knots4$type <- "Prediction"
cmaqsiteshead(fourstates)
## s.index states long lat type
## 9 56 illinois -91.3358 39.9175 Data
## 18 78 illinois -90.5172 41.5144 Data
## 41 84 illinois -90.3286 39.1097 Data
## 48 92 illinois -90.1603 38.6122 Data
## 61 93 illinois -90.1481 38.8903 Data
## 82 95 illinois -90.1058 38.8606 Data
head(cmaqsites)
## s.index states long lat type
## 12 7225 kentucky -89.25056 36.52489 Prediction
## 17 7365 kentucky -88.70091 36.59464 Prediction
## 34 7505 kentucky -88.28477 36.67218 Prediction
## 54 7645 kentucky -87.59857 36.72611 Prediction
## 69 7771 kentucky -88.93400 36.93809 Prediction
## 84 7785 kentucky -87.04594 36.78782 Prediction
head(knots4)
## s.index states long lat type
## 1 70 illinois -88.65343 40.67804 Knots
## 2 38 illinois -90.36961 39.66250 Knots
## 3 67 illinois -88.65343 39.15473 Knots
## 4 68 illinois -88.65343 39.66250 Knots
## 5 69 illinois -88.65343 40.17027 Knots
## 6 71 illinois -88.65343 41.18581 Knots
<- rbind(fourstates, knots4, cmaqsites)
allsites <- rbind(fourstates, knots4)
allsites head(allsites)
## s.index states long lat type
## 9 56 illinois -91.3358 39.9175 Data
## 18 78 illinois -90.5172 41.5144 Data
## 41 84 illinois -90.3286 39.1097 Data
## 48 92 illinois -90.1603 38.6122 Data
## 61 93 illinois -90.1481 38.8903 Data
## 82 95 illinois -90.1058 38.8606 Data
<- ggplot(data=map4states, aes(x=long, y=lat, group = group)) +
p4 geom_polygon(colour='black',size=0.8, fill=NA) +
# geom_point(data=fourstates, aes(x=long, y = lat), inherit.aes = FALSE, col="Red", size=1.2) +
# geom_point(data=cmaqsites, aes(x=long, y = lat), inherit.aes = FALSE, col="blue", size=0.5) +
# geom_point(data=knots4, aes(x=long, y = lat), inherit.aes = FALSE, col="green", size=1) +
geom_point(data=allsites, aes(x=long, y = lat, col=type, shape=type), inherit.aes = FALSE, size=1.5) +
labs( title= "156 data and 108 knot locations", x="", y = "") +
theme(legend.position =c(0.92, 0.2)) +
theme(axis.text.x = element_blank(), axis.text.y = element_blank(), axis.ticks = element_blank()) +
::north(data=map4states, location="bottomleft", symbol=12) +
ggsn::scalebar(data =map4states, dist =100, location ="topright", transform=T, dist_unit = "km",
ggsnst.dist = .05, st.size = 5, height = .06, st.bottom=T, model="WGS84")
p4
ggsave(filename=paste0(figurepath, "4statesmap.png"))
## Saving 7 x 5 in image
if (longrun) {
head(data4)
summary(data4)
<- obsO8hrmax~ sqrt(cmaqO8hrmax)
f2 <- Bsptime(package="spTimer", model="GP", formula=f2, data=data4,
M0 coordtype="lonlat", coords=3:4, scale.transform = "SQRT", n.report=2)
summary(M0)
# plot(M0)
# head(data4)
<- data4[data4$day<8, ]
d1 <- data4[data4$day==8, ]
vd1 dim(vd1)
<- data4[(data4$day>1) & (data4$day<9), ]
d2 table(d2$day)
<- data4[data4$day==9, ]
vd2 dim(vd2)
<- data4[(data4$day>2) & (data4$day<10), ]
d3 table(d3$day)
<- data4[data4$day==10, ]
vd3 dim(vd3)
<- data4[(data4$day>3) & (data4$day<11), ]
d4 table(d4$day)
<- data4[data4$day==11, ]
vd4 dim(vd4)
<- data4[(data4$day>4) & (data4$day<12), ]
d5 table(d5$day)
<- data4[data4$day==12, ]
vd5 dim(vd5)
<- data4[(data4$day>5) & (data4$day<13), ]
d6 table(d6$day)
<- data4[data4$day==13, ]
vd6 dim(vd6)
<- data4[(data4$day>6) & (data4$day<14), ]
d7 table(d7$day)
<- data4[data4$day==14, ]
vd7 dim(vd7)
# save(fourstates, data4, name4, cmaqdata4, cmaqsites, allsites, knots4,
# eaststates, d1, d2, d3, d4, d5, d6, d7, vd1, vd2, vd3, vd4, vd4, vd5, vd6, vd7,
# file=paste0(dpath, "4states.RData"))
<- obsO8hrmax~ sqrt(cmaqO8hrmax)
f2
<- Bsptime(package="spTimer", model="GP", formula=f2, data=d1,
M1 coordtype="lonlat", coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GP", formula=f2, data=d2,
M2 coordtype="lonlat", coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GP", formula=f2, data=d3,
M3 coordtype="lonlat", coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GP", formula=f2, data=d4,
M4 coordtype="lonlat", coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GP", formula=f2, data=d5,
M5 coordtype="lonlat", coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GP", formula=f2, data=d6,
M6 coordtype="lonlat", coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GP", formula=f2, data=d7,
M7 coordtype="lonlat", coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- predict(M1$fit,newdata=vd1, newcoords=~long+lat,
gpf1 type="temporal", foreStep=1, tol=0.005)
<- predict(M2$fit,newdata=vd2, newcoords=~long+lat,
gpf2 type="temporal", foreStep=1, tol=0.005)
<- predict(M3$fit,newdata=vd3, newcoords=~long+lat,
gpf3 type="temporal", foreStep=1, tol=0.005)
<- predict(M4$fit,newdata=vd4, newcoords=~long+lat,
gpf4 type="temporal", foreStep=1, tol=0.005)
<- predict(M5$fit,newdata=vd5, newcoords=~long+lat,
gpf5 type="temporal", foreStep=1, tol=0.005)
<- predict(M6$fit,newdata=vd6, newcoords=~long+lat,
gpf6 type="temporal", foreStep=1, tol=0.005)
<- predict(M7$fit,newdata=vd7, newcoords=~long+lat,
gpf7 type="temporal", foreStep=1, tol=0.005)
<- calculate_validation_statistics(vd1$obsO8hrmax, yits=gpf1$fore.samples, summarystat = "median")
ugp1 <- calculate_validation_statistics(vd2$obsO8hrmax, yits=gpf2$fore.samples, summarystat = "median")
ugp2 <- calculate_validation_statistics(vd3$obsO8hrmax, yits=gpf3$fore.samples, summarystat = "median")
ugp3 <- calculate_validation_statistics(vd4$obsO8hrmax, yits=gpf4$fore.samples, summarystat = "median")
ugp4 <- calculate_validation_statistics(vd5$obsO8hrmax, yits=gpf5$fore.samples, summarystat = "median")
ugp5 <- calculate_validation_statistics(vd6$obsO8hrmax, yits=gpf6$fore.samples, summarystat = "median")
ugp6 <- calculate_validation_statistics(vd7$obsO8hrmax, yits=gpf7$fore.samples, summarystat = "median")
ugp7
<- rbind(unlist(ugp1$stats), unlist(ugp2$stats), unlist(ugp3$stats),
allugp unlist(ugp4$stats), unlist(ugp5$stats), unlist(ugp6$stats), unlist(ugp7$stats))
# allugp
<- data.frame(allugp, model="GP")
allugp
allugp<- Bsptime(package="spTimer", model="GPP", formula=f2, data=d1,
M1gpp coordtype="lonlat", knots.coords = knots4[, 3:4], coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GPP", formula=f2, data=d2,
M2gpp coordtype="lonlat",knots.coords = knots4[, 3:4], coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GPP", formula=f2, data=d3,
M3gpp coordtype="lonlat",knots.coords = knots4[, 3:4], coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GPP", formula=f2, data=d4,
M4gpp coordtype="lonlat", knots.coords = knots4[, 3:4], coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GPP", formula=f2, data=d5,
M5gpp coordtype="lonlat",knots.coords = knots4[, 3:4], coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GPP", formula=f2, data=d6,
M6gpp coordtype="lonlat", knots.coords = knots4[, 3:4], coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GPP", formula=f2, data=d7,
M7gpp coordtype="lonlat",knots.coords = knots4[, 3:4], coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- predict(M1gpp$fit,newdata=vd1, newcoords=~long+lat,
fore1 type="temporal", foreStep=1, tol=0.005)
<- predict(M2gpp$fit,newdata=vd2, newcoords=~long+lat,
fore2 type="temporal", foreStep=1, tol=0.005)
<- predict(M3gpp$fit,newdata=vd3, newcoords=~long+lat,
fore3 type="temporal", foreStep=1, tol=0.005)
<- predict(M4gpp$fit,newdata=vd4, newcoords=~long+lat,
fore4 type="temporal", foreStep=1, tol=0.005)
<- predict(M5gpp$fit,newdata=vd5, newcoords=~long+lat,
fore5 type="temporal", foreStep=1, tol=0.005)
<- predict(M6gpp$fit,newdata=vd6, newcoords=~long+lat,
fore6 type="temporal", foreStep=1, tol=0.005)
<- predict(M7gpp$fit,newdata=vd7, newcoords=~long+lat,
fore7 type="temporal", foreStep=1, tol=0.005)
<- calculate_validation_statistics(vd1$obsO8hrmax, yits=fore1$fore.samples, summarystat = "median")
u1 <- calculate_validation_statistics(vd2$obsO8hrmax, yits=fore2$fore.samples, summarystat = "median")
u2 <- calculate_validation_statistics(vd3$obsO8hrmax, yits=fore3$fore.samples, summarystat = "median")
u3 <- calculate_validation_statistics(vd4$obsO8hrmax, yits=fore4$fore.samples, summarystat = "median")
u4 <- calculate_validation_statistics(vd5$obsO8hrmax, yits=fore5$fore.samples, summarystat = "median")
u5 <- calculate_validation_statistics(vd6$obsO8hrmax, yits=fore6$fore.samples, summarystat = "median")
u6 <- calculate_validation_statistics(vd7$obsO8hrmax, yits=fore7$fore.samples, summarystat = "median")
u7
<- rbind(unlist(u1$stats), unlist(u2$stats), unlist(u3$stats), unlist(u4$stats),
agpp unlist(u5$stats), unlist(u6$stats), unlist(u7$stats))
<- data.frame(agpp, model="GPP")
agpp # agpp
# Fit AR models
<- Bsptime(package="spTimer", model="AR", formula=f2, data=d1,
M1ar coordtype="lonlat", coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="AR", formula=f2, data=d2,
M2ar coordtype="lonlat", coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="AR", formula=f2, data=d3,
M3ar coordtype="lonlat", coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="AR", formula=f2, data=d4,
M4ar coordtype="lonlat", coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="AR", formula=f2, data=d5,
M5ar coordtype="lonlat", coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="AR", formula=f2, data=d6,
M6ar coordtype="lonlat", coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="AR", formula=f2, data=d7,
M7ar coordtype="lonlat", coords=3:4, scale.transform = "SQRT", n.report=1, N=5000)
# Forecast using AR models
<- predict(M1ar$fit,newdata=vd1, newcoords=~long+lat,
arf1 type="temporal", foreStep=1, tol=0.005)
<- predict(M2ar$fit,newdata=vd2, newcoords=~long+lat,
arf2 type="temporal", foreStep=1, tol=0.005)
<- predict(M3ar$fit,newdata=vd3, newcoords=~long+lat,
arf3 type="temporal", foreStep=1, tol=0.005)
<- predict(M4ar$fit,newdata=vd4, newcoords=~long+lat,
arf4 type="temporal", foreStep=1, tol=0.005)
<- predict(M5ar$fit,newdata=vd5, newcoords=~long+lat,
arf5 type="temporal", foreStep=1, tol=0.005)
<- predict(M6ar$fit,newdata=vd6, newcoords=~long+lat,
arf6 type="temporal", foreStep=1, tol=0.005)
<- predict(M7ar$fit,newdata=vd7, newcoords=~long+lat,
arf7 type="temporal", foreStep=1, tol=0.005)
# Calculate validation statistics
<- calculate_validation_statistics(vd1$obsO8hrmax, yits=arf1$fore.samples, summarystat = "median")
uar1 <- calculate_validation_statistics(vd2$obsO8hrmax, yits=arf2$fore.samples, summarystat = "median")
uar2 <- calculate_validation_statistics(vd3$obsO8hrmax, yits=arf3$fore.samples, summarystat = "median")
uar3 <- calculate_validation_statistics(vd4$obsO8hrmax, yits=arf4$fore.samples, summarystat = "median")
uar4 <- calculate_validation_statistics(vd5$obsO8hrmax, yits=arf5$fore.samples, summarystat = "median")
uar5 <- calculate_validation_statistics(vd6$obsO8hrmax, yits=arf6$fore.samples, summarystat = "median")
uar6 <- calculate_validation_statistics(vd7$obsO8hrmax, yits=arf7$fore.samples, summarystat = "median")
uar7
<- rbind(unlist(uar1$stats), unlist(uar2$stats), unlist(uar3$stats),
alluar unlist(uar4$stats), unlist(uar5$stats), unlist(uar6$stats), unlist(uar7$stats))
dim(alluar)
<- data.frame(alluar, model="AR")
alluar
dim(alluar)
colnames(alluar)
<- rbind(allugp, alluar, agpp)
allstats $days <- rep(8:14, 3)
allstats$criteria <- c("rmse")
allstats$lt2 <- c("mae")
allstats$lt3 <- c("crps")
allstats# allstats
<- ggplot() +
pcomp geom_line(data=allstats, aes(x=days, y=rmse, color=model, linetype=criteria)) +
geom_line(data=allstats, aes(x=days, y=mae, color=model, linetype=lt2)) +
geom_line(data=allstats, aes(x=days, y=crps, color=model, linetype=lt3))+
geom_point(data=allstats, aes(x=days, y=rmse, color=model, shape=model)) +
geom_point(data=allstats, aes(x=days, y=mae, color=model, shape=model)) +
geom_point(data=allstats, aes(x=days, y=crps, color=model, shape=model))+
labs(x="Forecast day", y="Criteria value") # +
#facet_wrap(~ , ncol = 1) +
pcomp# Not used
dim(allstats)
head(allstats)
<- c(allstats$rmse, allstats$mae, allstats$crps, allstats$cvg)
values length(values)
<- rep(allstats$model, 4)
model <- rep(allstats$days, 4)
days <- c(allstats$criteria, allstats$lt2, allstats$lt3, rep("Coverage", 21))
criteria length(criteria)
length(days)
length(model)
<- data.frame(values=values, model=model, days=days, criteria=criteria)
allbig dim(allbig)
head(allbig)
<- ggplot() +
pnewcomp geom_line(data=allbig, aes(x=days, y=values, color=model, linetype=criteria)) +
geom_point(data=allbig, aes(x=days, y=values, color=model, shape=model)) +
facet_wrap(~ criteria, ncol= 2, scales = "free")
pnewcomp
ggsave(filename=paste0(figurepath, "fourstates_rmse_mae.png"))
# Calculate Hit and False Alarm Rates
names(arf1)
<- hitandfalsealarm(65, yobs = vd1$obsO8hrmax, ypred=arf1$Median)
ans
ans<- hitandfalsealarm(75, yobs = vd1$obsO8hrmax, ypred=arf1$Median)
ans
ans
<- c(vd1$obsO8hrmax, vd2$obsO8hrmax, vd3$obsO8hrmax, vd4$obsO8hrmax,
yobs $obsO8hrmax, vd6$obsO8hrmax, vd7$obsO8hrmax)
vd5<- c(arf1$Median, arf2$Median,arf3$Median, arf4$Median,arf5$Median,
yarpred $Median, arf7$Median)
arf6
<- hitandfalsealarm(65, yobs =yobs, ypred=yarpred)
ansar65 <- hitandfalsealarm(75, yobs =yobs, ypred=yarpred)
ansar75
ansar65
ansar75
<- c(gpf1$Median,gpf2$Median, gpf3$Median,gpf4$Median,gpf5$Median,gpf6$Median,gpf7$Median)
ygp_pred
<- hitandfalsealarm(65, yobs =yobs, ypred=ygp_pred)
ansgp65 <- hitandfalsealarm(75, yobs =yobs, ypred=ygp_pred)
ansgp75
ansgp65
ansgp75
<- c(fore1$Median, fore2$Median, fore3$Median, fore4$Median,
ygpp_pred $Median, fore6$Median,fore7$Median)
fore5
<- hitandfalsealarm(65, yobs =yobs, ypred=ygpp_pred)
ansgpp65 <- hitandfalsealarm(75, yobs =yobs, ypred=ygpp_pred)
ansgpp75
ansgpp65
ansgpp75
<- c(ansgp65$hitrate, ansar65$hitrate, ansgpp65$hitrate,
a1 $hitrate, ansar75$hitrate, ansgpp75$hitrate)
ansgp75
<- c(ansgp65$falsealarm, ansar65$falsealarm, ansgpp65$falsealarm,
a2 $falsealarm, ansar75$falsealarm, ansgpp75$falsealarm)
ansgp75<- rbind(a1, a2)
u .1 <- u
table9# library(xtable)
# xtable(table9.1, digits=2)
dput(table9.1, file=paste0(filepath, "table9.1.txt"))
<- get_parameter_estimates(t(gpf1$fore.samples), level=50)
ints50gp1 <- get_parameter_estimates(t(gpf2$fore.samples), level=50)
ints50gp2 <- get_parameter_estimates(t(gpf3$fore.samples), level=50)
ints50gp3 <- get_parameter_estimates(t(gpf4$fore.samples), level=50)
ints50gp4 <- get_parameter_estimates(t(gpf5$fore.samples), level=50)
ints50gp5 <- get_parameter_estimates(t(gpf6$fore.samples), level=50)
ints50gp6 <- get_parameter_estimates(t(gpf7$fore.samples), level=50)
ints50gp7
<- get_parameter_estimates(t(gpf1$fore.samples), level=90)
ints90gp1 <- get_parameter_estimates(t(gpf2$fore.samples), level=90)
ints90gp2 <- get_parameter_estimates(t(gpf3$fore.samples), level=90)
ints90gp3 <- get_parameter_estimates(t(gpf4$fore.samples), level=90)
ints90gp4 <- get_parameter_estimates(t(gpf5$fore.samples), level=90)
ints90gp5 <- get_parameter_estimates(t(gpf6$fore.samples), level=90)
ints90gp6 <- get_parameter_estimates(t(gpf7$fore.samples), level=90)
ints90gp7
<- rbind(ints50gp1,ints50gp2, ints50gp3, ints50gp4, ints50gp5, ints50gp6, ints50gp7)
gp50ints <- rbind(ints90gp1,ints90gp2, ints90gp3, ints90gp4, ints90gp5, ints90gp6, ints90gp7)
gp90ints
<- get_parameter_estimates(t(arf1$fore.samples), level=50)
ints50ar1 <- get_parameter_estimates(t(arf2$fore.samples), level=50)
ints50ar2 <- get_parameter_estimates(t(arf3$fore.samples), level=50)
ints50ar3 <- get_parameter_estimates(t(arf4$fore.samples), level=50)
ints50ar4 <- get_parameter_estimates(t(arf5$fore.samples), level=50)
ints50ar5 <- get_parameter_estimates(t(arf6$fore.samples), level=50)
ints50ar6 <- get_parameter_estimates(t(arf7$fore.samples), level=50)
ints50ar7
<- get_parameter_estimates(t(arf1$fore.samples), level=90)
ints90ar1 <- get_parameter_estimates(t(arf2$fore.samples), level=90)
ints90ar2 <- get_parameter_estimates(t(arf3$fore.samples), level=90)
ints90ar3 <- get_parameter_estimates(t(arf4$fore.samples), level=90)
ints90ar4 <- get_parameter_estimates(t(arf5$fore.samples), level=90)
ints90ar5 <- get_parameter_estimates(t(arf6$fore.samples), level=90)
ints90ar6 <- get_parameter_estimates(t(arf7$fore.samples), level=90)
ints90ar7
<- rbind(ints50ar1,ints50ar2, ints50ar3, ints50ar4, ints50ar5, ints50ar6, ints50ar7)
ar50ints <- rbind(ints90ar1,ints90ar2, ints90ar3, ints90ar4, ints90ar5, ints90ar6, ints90ar7)
ar90ints
<- get_parameter_estimates(t(fore1$fore.samples), level=50)
ints50gpp1 <- get_parameter_estimates(t(fore2$fore.samples), level=50)
ints50gpp2 <- get_parameter_estimates(t(fore3$fore.samples), level=50)
ints50gpp3 <- get_parameter_estimates(t(fore4$fore.samples), level=50)
ints50gpp4 <- get_parameter_estimates(t(fore5$fore.samples), level=50)
ints50gpp5 <- get_parameter_estimates(t(fore6$fore.samples), level=50)
ints50gpp6 <- get_parameter_estimates(t(fore7$fore.samples), level=50)
ints50gpp7
<- get_parameter_estimates(t(fore1$fore.samples), level=90)
ints90gpp1 <- get_parameter_estimates(t(fore2$fore.samples), level=90)
ints90gpp2 <- get_parameter_estimates(t(fore3$fore.samples), level=90)
ints90gpp3 <- get_parameter_estimates(t(fore4$fore.samples), level=90)
ints90gpp4 <- get_parameter_estimates(t(fore5$fore.samples), level=90)
ints90gpp5 <- get_parameter_estimates(t(fore6$fore.samples), level=90)
ints90gpp6 <- get_parameter_estimates(t(fore7$fore.samples), level=90)
ints90gpp7
<- rbind(ints50gpp1,ints50gpp2, ints50gpp3, ints50gpp4, ints50gpp5, ints50gpp6, ints50gpp7)
gpp50ints <- rbind(ints90gpp1,ints90gpp2, ints90gpp3, ints90gpp4, ints90gpp5, ints90gpp6, ints90gpp7)
gpp90ints
head(gp50ints)
<- gp50ints$up- gp50ints$low
gp50len <- gp90ints$up- gp90ints$low
gp90len summary(gp50len)
summary(gp90len)
<- gpp50ints$up- gpp50ints$low
gpp50len <- gpp90ints$up- gpp90ints$low
gpp90len
<- ar50ints$up- ar50ints$low
ar50len <- ar90ints$up- ar90ints$low
ar90len
<- length(ar50len)
k <- rep(c("GP50", "AR50", "GPP50", "GP90", "AR90", "GPP90"), each=k)
ids <- rep(c("GP", "AR", "GPP", "GP", "AR", "GPP"), each=k)
model <- data.frame(lenths = c(gp50len, ar50len, gpp50len, gp90len, ar90len, gpp90len),
lendat ids = factor(ids), model=factor(model))
head(lendat)
head(lendat)
dim(lendat)
length(yobs)
<- cbind(t(gpf1$fore.samples), t(gpf2$fore.samples), t(gpf3$fore.samples),
ugp t(gpf4$fore.samples), t(gpf5$fore.samples),
t(gpf6$fore.samples), t(gpf7$fore.samples))
dim(ugp)
<- cbind(t(arf1$fore.samples), t(arf2$fore.samples), t(arf3$fore.samples),
uar t(arf4$fore.samples), t(arf5$fore.samples),
t(arf6$fore.samples), t(arf7$fore.samples))
dim(uar)
<- cbind(t(fore1$fore.samples), t(fore2$fore.samples), t(fore3$fore.samples),
ugpp t(fore4$fore.samples), t(fore5$fore.samples),
t(fore6$fore.samples), t(fore7$fore.samples))
dim(ugp)
<- function(x) {
findprop <- x[1]
value <- x[-1]
samples <- length(samples[samples<value])/length(samples)
prop
prop
}<- cbind(yobs, t(ugp))
x dim(x)
<- na.omit(x)
y dim(y)
findprop((y[1, ]))
<- apply(y, 1, findprop)
pitgp
<- cbind(yobs, t(uar))
x dim(x)
<- na.omit(x)
y dim(y)
<- apply(y, 1, findprop)
pitar
<- cbind(yobs, t(ugpp))
x dim(x)
<- na.omit(x)
y dim(y)
<- apply(y, 1, findprop)
pitgpp <- length(pitgpp)
m <- factor(rep(c("GP", "AR", "GPP"), each=m))
model <- data.frame(pits=c(pitgp, pitar, pitgpp), model=model)
adat head(adat)
summary(adat)
<- adat[adat$model !="GP", ]
twodat
<- 100
K <- na.omit(yobs)
ygood summary(ygood)
<- seq(from=min(ygood), to = max(ygood), length=K)
yk <- matrix(ygood, nrow=K, ncol=length(ygood), byrow=TRUE)
yomat dim(yomat)
<- cbind(yk, yomat)
u dim(u)
<- apply(u, 1, findprop)
Ghat length(Ghat)
<- function(yobs, ysample) {
fbarhat # yobs m by 1
# ysample N by m
<- cbind(yobs, t(ysample))
x dim(x)
<- na.omit(x)
y dim(y)
<- y[, -1]
x dim(x)
length(yk)
<- ncol(x)
N
N# x is the matrix of MCMC samples
<- nrow(x)
m <- matrix(NA, nrow=m, ncol=K)
amat for (i in 1:m) {
<- matrix(x[i, ], nrow=N, ncol=K, byrow=TRUE)
ynewmat dim(yomat)
<- rbind(yk, ynewmat)
u dim(u)
<- apply(u, 2, findprop)
v <- as.vector(v)
amat[i, ]
}<- apply(amat, 2, mean)
Fbarhat # dans <- Fbarhat - Ghat
Fbarhat
}
<- fbarhat(yobs, ugp) - Ghat
dansgp summary(dansgp)
<- fbarhat(yobs, uar) - Ghat
dansar summary(dansar)
<- fbarhat(yobs, ugpp) - Ghat
dansgpp summary(dansgpp)
<- factor(rep(c("GP", "AR", "GPP"), each=K))
model <- data.frame(dans=c(dansgp, dansar, dansgpp), yk = rep(yk, 3), model=model)
ddat head(ddat)
summary(ddat)
ggsave(filename=paste0(figurepath, "fourstates_mcp_diagram.png"))
rm(list=ls(pattern("M")))
rm(list=ls(pattern("arf")))
rm(list=ls(pattern("fore")))
save(table9.1, allbig, lendat, adat, twodat, ddat, file="forecast/fourstatesresults.RData" )
else {
} load(file="forecast/fourstatesresults.RData")
.1<- dget(file=paste0(filepath, "table9.1.txt"))
table9
}
.1
table9## [,1] [,2] [,3] [,4] [,5] [,6]
## a1 83.00469 90.610329 90.51643192 95.774648 96.338028 96.52582
## a2 14.08451 5.258216 0.09389671 1.784038 1.596244 0.00000
<- ggplot(data=lendat, aes(x=ids, y=lenths, col=model)) +
sharpdiag geom_boxplot() +
labs(x = "", y = "") +
scale_colour_manual(values = c("GP" = "green",
"AR" = "red",
"GPP" = "blue"))
sharpdiag
ggsave(filename=paste0(figurepath, "fourstates_sharpness.png"))
## Saving 7 x 5 in image
<- ggplot(data=adat, aes(pits, colour=model, fill=model)) +
pitdiag geom_histogram() +
facet_wrap(~ model, ncol = 3) +
labs(x = "", y = "") +
scale_colour_manual(values = c("GP" = "green",
"AR" = "red",
"GPP" = "blue"))
pitdiag## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggsave(paste0(figurepath, "fourstates_pitdiagram.png"))
## Saving 7 x 5 in image
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
<- ggplot(data=twodat, aes(pits, colour=model, fill=model)) +
pitdiag geom_histogram() +
facet_wrap(~ model, ncol = 2) +
labs(x = "", y = "") +
scale_colour_manual(values = c("GP" = "green",
"AR" = "red",
"GPP" = "blue"))
pitdiag## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggsave(paste0(figurepath, "fourstates_pitdiagram_2.png"))
## Saving 7 x 5 in image
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
<- ggplot(data=ddat, aes(x=yk, y=dans, colour=model, linetype=model)) +
mcpdiag geom_line() +
labs(x = "", y = "") +
geom_abline(slope=0, intercept = 0, size=1, col="black")+
scale_colour_manual(values = c("GP" = "green",
"AR" = "red",
"GPP" = "blue"))
mcpdiag
4 Figures and tables in Section 9.6
if (longrun) {
load(file=paste0(dpath, "C9dailydata.Rdata"))
# save(cmaqdailypred1451, f2, eaststates, dailyobs14day, delete.fnc.coords, knots.coords.151,
# knots.coords.158, knots.coords.105, fnc.delete.map.XYZ, file=paste0(dpath, "dailydata.Rdata"))
<- obsO8hrmax~ sqrt(cmaqO8hrmax)
f2 head(dailyobs14day)
head(cmaqdailypred1451)
dim(dailyobs14day)
# 9716 by 8
9716/14
# 694 sites times 14 days
<- Bsptime(formula=f2, data=dailyobs14day, package="spTimer", model="GPP",
M2 coordtype = "lonlat", coords=2:3,
prior.phi="Gamm", scale.transform = "SQRT",
knots.coords = knots.coords.158, n.report=2, N=5000)
plot(M2$fit)
summary(M2)
.2 <- M2$params
table9dput(table9.2, file=paste0(filepath, "table9.2.txt"))
round(table9.2, 4)
# library(xtable)
# xtable(table9.2, digits=4)
# Forecast validation for moving window of 7 days
# Prepare the data sets
head(dailyobs14day)
<- dailyobs14day[dailyobs14day$day<8, ]
d1 table(d1$day)
<- dailyobs14day[dailyobs14day$day==8, ]
vd1 dim(vd1)
head(vd1)
<- dailyobs14day[(dailyobs14day$day>1) & (dailyobs14day$day<9), ]
d2 table(d2$day)
<- dailyobs14day[dailyobs14day$day==9, ]
vd2 dim(vd2)
<- dailyobs14day[(dailyobs14day$day>2) & (dailyobs14day$day<10), ]
d3 table(d3$day)
<- dailyobs14day[dailyobs14day$day==10, ]
vd3 dim(vd3)
<- dailyobs14day[(dailyobs14day$day>3) & (dailyobs14day$day<11), ]
d4 table(d4$day)
<- dailyobs14day[dailyobs14day$day==11, ]
vd4 dim(vd4)
<- dailyobs14day[(dailyobs14day$day>4) & (dailyobs14day$day<12), ]
d5 table(d5$day)
<- dailyobs14day[dailyobs14day$day==12, ]
vd5 dim(vd5)
<- dailyobs14day[(dailyobs14day$day>5) & (dailyobs14day$day<13), ]
d6 table(d6$day)
<- dailyobs14day[dailyobs14day$day==13, ]
vd6 dim(vd6)
<- dailyobs14day[(dailyobs14day$day>6) & (dailyobs14day$day<14), ]
d7 table(d7$day)
<- dailyobs14day[dailyobs14day$day==14, ]
vd7 dim(vd7)
# Fit the models
<- Bsptime(package="spTimer", model="GPP", formula=f2, data=d1,
M1gpp coordtype="lonlat", coords=2:3, knots.coords= knots.coords.158,
scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GPP", formula=f2, data=d2,
M2gpp coordtype="lonlat", coords=2:3, knots.coords= knots.coords.158,
scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GPP", formula=f2, data=d3,
M3gpp coordtype="lonlat", coords=2:3, knots.coords= knots.coords.158,
scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GPP", formula=f2, data=d4,
M4gpp coordtype="lonlat", coords=2:3, knots.coords= knots.coords.158,
scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GPP", formula=f2, data=d5,
M5gpp coordtype="lonlat", coords=2:3, knots.coords= knots.coords.158,
scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GPP", formula=f2, data=d6,
M6gpp coordtype="lonlat", coords=2:3, knots.coords= knots.coords.158,
scale.transform = "SQRT", n.report=1, N=5000)
<- Bsptime(package="spTimer", model="GPP", formula=f2, data=d7,
M7gpp coordtype="lonlat", coords=2:3, knots.coords= knots.coords.158,
scale.transform = "SQRT", n.report=1, N=5000)
<- predict(M1gpp$fit,newdata=vd1, newcoords=~long+lat,
fore1 type="temporal", foreStep=1, tol=0.005)
<- predict(M2gpp$fit,newdata=vd2, newcoords=~long+lat,
fore2 type="temporal", foreStep=1, tol=0.005)
<- predict(M3gpp$fit,newdata=vd3, newcoords=~long+lat,
fore3 type="temporal", foreStep=1, tol=0.005)
<- predict(M4gpp$fit,newdata=vd4, newcoords=~long+lat,
fore4 type="temporal", foreStep=1, tol=0.005)
<- predict(M5gpp$fit,newdata=vd5, newcoords=~long+lat,
fore5 type="temporal", foreStep=1, tol=0.005)
<- predict(M6gpp$fit,newdata=vd6, newcoords=~long+lat,
fore6 type="temporal", foreStep=1, tol=0.005)
<- predict(M7gpp$fit,newdata=vd7, newcoords=~long+lat,
fore7 type="temporal", foreStep=1, tol=0.005)
<- calculate_validation_statistics(vd1$obsO8hrmax, yits=fore1$fore.samples, summarystat = "median")
u1 <- calculate_validation_statistics(vd2$obsO8hrmax, yits=fore2$fore.samples, summarystat = "median")
u2 <- calculate_validation_statistics(vd3$obsO8hrmax, yits=fore3$fore.samples, summarystat = "median")
u3 <- calculate_validation_statistics(vd4$obsO8hrmax, yits=fore4$fore.samples, summarystat = "median")
u4 <- calculate_validation_statistics(vd5$obsO8hrmax, yits=fore5$fore.samples, summarystat = "median")
u5 <- calculate_validation_statistics(vd6$obsO8hrmax, yits=fore6$fore.samples, summarystat = "median")
u6 <- calculate_validation_statistics(vd7$obsO8hrmax, yits=fore7$fore.samples, summarystat = "median")
u7
<- rbind(unlist(u1), unlist(u2), unlist(u3), unlist(u4), unlist(u5), unlist(u6), unlist(u7))
a
a<- spT.validation(vd1$obsO8hrmax, vd1$cmaqO8hrmax)
v1 <- spT.validation(vd2$obsO8hrmax, vd2$cmaqO8hrmax)
v2 <- spT.validation(vd3$obsO8hrmax, vd3$cmaqO8hrmax)
v3 <- spT.validation(vd4$obsO8hrmax, vd4$cmaqO8hrmax)
v4 <- spT.validation(vd5$obsO8hrmax, vd5$cmaqO8hrmax)
v5 <- spT.validation(vd6$obsO8hrmax, vd6$cmaqO8hrmax)
v6 <- spT.validation(vd7$obsO8hrmax, vd7$cmaqO8hrmax)
v7
<- c(v1[2], v2[2], v3[2], v4[2], v5[2], v6[2], v7[2])
b
b.3 <- data.frame(Startday=1:7, Forecastday=8:14, cmaq=b, a)
table9dput(table9.3, file=paste0(filepath, "table9.3.txt"))
.3
table9library(xtable)
xtable(table9.3[, -1], digits=2)
v1
v2
# Prediction plot not included
<- get_validation_summaries(t(fore7$fore.samples))
fsums head(fsums)
dim(fsums)
<- obs_v_pred_plot(yobs=vd7$obsO8hrmax, predsums = fsums, segments =F)
pplots
###
## Prediction Map
summary(d7)
## Fit model for days 7 to 13
<- Bsptime(package="spTimer", model="GPP", formula=f2, data=d7,
M7 coordtype="lonlat", coords=2:3, knots.coords= knots.coords.158,
scale.transform = "SQRT", n.report=1, N=5000)
summary(M7)
names(M7$fit)
$fit$PMCC
M7# Forecast at the 694 data sites
<- dailyobs14day[dailyobs14day$day==14, ]
vd7 head(vd7)
<- predict(M7$fit, newdata=vd7, newcoords=~long+lat,
fore7 type="temporal", foreStep=1, tol=0.005)
head(vd7)
<- data.frame(vd7[, -7], forecasts=fore7$Mean[1, ],
a7res foremed=fore7$Median[1, ], foresd=fore7$SD[1, ])
head(a7res)
## Forecast at un-observed grid locations on day 14
dim(cmaqdailypred1451)
head(cmaqdailypred1451)
<- cmaqdailypred1451[cmaqdailypred1451$day==14, ]
forecastdata dim(forecastdata)
head(forecastdata)
<- predict(M7$fit, newdata=forecastdata, newcoords=~long+lat,
forepred type="temporal", foreStep=1, tol=0.005)
names(forepred)
length(forepred$Mean[1, ])
summary(forepred$Mean[1, ])
summary(forepred$Median[1, ])
summary(forepred$SD[1, ])
<- data.frame(forecastdata, forecasts=forepred$Mean[1, ],
foresum foremed=forepred$Median[1, ], foresd=forepred$SD[1, ])
head(foresum)
<- rbind(a7res, foresum)
allforecasts head(allforecasts)
dim(allforecasts) # 2145 by 10 (694+1451)
save(table9.3, allforecasts, file="forecast/useasternresults.RData")
else {
} load(file="forecast/useasternresults.RData")
.2 <- dget(file=paste0(filepath, "table9.2.txt"))
table9
}
load(file=paste0(dpath, "eaststates.rda"))
round(table9.3, 2)
## Startday Forecastday cmaq stats.rmse stats.mae stats.crps stats.cvg
## 1 1 8 17.81 12.91 10.24 7.39 97.68
## 2 2 9 16.52 10.78 8.29 7.59 98.12
## 3 3 10 15.82 9.24 7.05 7.37 98.09
## 4 4 11 13.75 9.13 7.33 7.33 98.97
## 5 5 12 13.91 9.59 7.28 7.64 98.24
## 6 6 13 16.97 10.61 8.00 7.02 95.47
## 7 7 14 18.38 11.70 8.56 6.91 96.76
<- allforecasts[, c("long","lat")]
coord library(akima)
library(tidyr)
<- seq(from=min(coord$long)-0.5, to = max(coord$long)+0.8, length=200)
xo <- seq(from=min(coord$lat)-0.25, to = max(coord$lat)+0.8, length=200)
yo # use the median
<- interp(coord$long, coord$lat, allforecasts$foremed, xo=xo, yo=yo)
surf
names(surf)
## [1] "x" "y" "z"
<- list(x=surf$x, y=surf$y, z=surf$z)
surf <- fnc.delete.map.XYZ(xyz=surf)
v
<- data.frame(long = v$x, v$z )
interp1 names(interp1)[1:length(v$y)+1] <- v$y
<- gather(interp1,key = lat,value =Forecast,-long,convert = TRUE)
interp1
<- map_data(database="state",regions=eaststates)
useastmap head(useastmap)
## long lat group order region subregion
## 1 -87.46201 30.38968 1 1 alabama <NA>
## 2 -87.48493 30.37249 1 2 alabama <NA>
## 3 -87.52503 30.37249 1 3 alabama <NA>
## 4 -87.53076 30.33239 1 4 alabama <NA>
## 5 -87.57087 30.32665 1 5 alabama <NA>
## 6 -87.58806 30.32665 1 6 alabama <NA>
<- rev(c("firebrick4","firebrick2","white","dodgerblue2","dodgerblue4"))#colour palette
com <- range(interp1$Forecast, na.rm=T)
zr <- ggplot() +
P geom_raster(data=interp1, aes(x = long, y = lat,fill = Forecast)) +
geom_polygon(data=useastmap, aes(x=long, y=lat, group=group), color="black", size = 0.6, fill=NA) +
scale_fill_gradientn(colours=com, na.value="gray95", limits=zr) +
theme(axis.text = element_blank(), axis.ticks = element_blank()) +
::scalebar(data =interp1, dist = 500, location = "bottomleft", transform=T, dist_unit = "km",
ggsnst.dist = .05, st.size = 5, height = .06, st.bottom=T, model="WGS84") +
::north(data=interp1, location="bottomright", symbol=12) +
ggsnlabs(title= "Forecast map ozone levels for July 14, 2010", x="", y = "", size=2.5)
P
ggsave(filename=paste0(figurepath, "forecastmapJuly14.png"))
## Saving 7 x 5 in image
## Repeat for SD
head(allforecasts)
## s.index long lat year month day cmaqO8hrmax forecasts foremed
## 14 1 -94.6933 43.1233 2010 7 14 52.49989 44.85407 44.12131
## 28 2 -94.5886 38.7589 2010 7 14 63.97456 48.48949 47.45123
## 42 3 -94.5680 39.3322 2010 7 14 75.76489 51.88261 50.93674
## 56 5 -94.4175 37.2297 2010 7 14 51.08922 43.62887 42.54945
## 70 6 -94.3764 39.3030 2010 7 14 72.91255 50.81945 49.92338
## 84 7 -94.2833 39.4167 2010 7 14 64.30513 48.87018 47.75485
## foresd
## 14 11.40912
## 28 13.42955
## 42 14.03164
## 56 12.83883
## 70 11.79357
## 84 13.68402
<- interp(coord$long, coord$lat, allforecasts$foresd, xo=xo, yo=yo)
surf names(surf)
## [1] "x" "y" "z"
<- list(x=surf$x, y=surf$y, z=surf$z)
surf <- fnc.delete.map.XYZ(xyz=surf)
v
<- data.frame(long = v$x, v$z )
interp1 names(interp1)[1:length(v$y)+1] <- v$y
<- gather(interp1,key = lat,value =sd,-long,convert = TRUE)
interp1
<- map_data(database="state",regions=eaststates)
useastmap head(useastmap)
## long lat group order region subregion
## 1 -87.46201 30.38968 1 1 alabama <NA>
## 2 -87.48493 30.37249 1 2 alabama <NA>
## 3 -87.52503 30.37249 1 3 alabama <NA>
## 4 -87.53076 30.33239 1 4 alabama <NA>
## 5 -87.57087 30.32665 1 5 alabama <NA>
## 6 -87.58806 30.32665 1 6 alabama <NA>
<- rev(c("firebrick4","firebrick2","white","dodgerblue2","dodgerblue4"))#colour palette
com <- range(interp1$sd, na.rm=T)
zr <- ggplot() +
Psd geom_raster(data=interp1, aes(x = long, y = lat,fill = sd)) +
geom_polygon(data=useastmap, aes(x=long, y=lat, group=group), color="black", size = 0.6, fill=NA) +
scale_fill_gradientn(colours=com, na.value="gray95", limits=zr) +
theme(axis.text = element_blank(), axis.ticks = element_blank()) +
::scalebar(data =interp1, dist = 500, location = "bottomleft", transform=T, dist_unit = "km",
ggsnst.dist = .05, st.size = 5, height = .06, st.bottom=T, model="WGS84") +
::north(data=interp1, location="bottomright", symbol=12) +
ggsnlabs(title= "Sd of forecasted ozone levels for July 14, 2010", x="", y = "", size=2.5)
Psd
ggsave(filename=paste0(figurepath, "forecastmapJuly14_sd.png"))
## Saving 7 x 5 in image
# All done
<- proc.time()[3]
end.time <- (end.time-start.time)/60
comp.time# comp.time<-fancy.time(comp.time)
print(comp.time)
## elapsed
## 0.2175833