/
main.R
248 lines (173 loc) · 7.75 KB
/
main.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
####### R code for Katzfuss & Schaefer #########
# This file contains or sources the code to reproduce all plots and results.
# Set your working directory to the folder containing this file.
# Place all additional R files in a subfolder called "code/".
# Place prec_days.RData and prec_all.RData in a subfolder called "data/".
# Ensure that there are (empty) subfolders called "output/" and "plots/".
# Note: Some portions of the code can take a very long time to run.
### required functions
source('code/nonlinearSpatial_functions.R') # method implementation
source('code/logScore_comparison.R') # helper functions for comparisons
###### illustration of maximin ordering ######
## compute ordering, NN, and length scales
n=60^2
Ns=100; N.test=1
nonlin=0 # degree of nonlinearity
reg=TRUE # regular grid or randomly sampled locations
sepa=0 # separation for bimodal residual errors
source('code/createMaternSine.R')
NNarray=NNarray.max[,1:4]
scales=computeScales(locs.ord,NNarray)
## plot orderings
is=c(13,51,290)
for(i in is){
pdf(file=paste0('plots/maxmin',i,'.pdf'),width=4.0,height=4.0)
par(mgp = c(1.6,.5,0), mar=c(.1,.1,.1,.1)) # bltr
plot(locs.ord[,1],locs.ord[,2],pch='.',xaxt='n',yaxt='n',
xlab='',ylab='',col='grey',cex=1.5)
points(locs.ord[1:(i-1),1],locs.ord[1:(i-1),2],col=1,cex=1.5)
points(locs.ord[NNarray[i,],1],locs.ord[NNarray[i,],2],
col=3,pch='x',cex=2)
points(locs.ord[i,1],locs.ord[i,2]-1/80,col=4,pch='+',cex=3)
segments(locs.ord[i,1],locs.ord[i,2],locs.ord[NNarray[i,1],1],
locs.ord[NNarray[i,1],2],col=2,lwd=2)
if(i==13) text(mean(locs.ord[c(i,NNarray[i,1]),1])+.03,
mean(locs.ord[c(i,NNarray[i,1]),2])-.03,
expression(italic(l[i])),cex=2,col=2)
dev.off()
}
## plot length scales (min distances)
pdf(file='plots/scales.pdf',width=3.0,height=3.0)
par(mgp = c(1.6,.5,0), mar=c(2.6,2.6,.3,.1)) # bltr
plot(1:n,scales,xlab='i',ylab='min dist',cex=1,col=2)
lines((1:n)^(-.5),col=4)
legend('topright',c(expression(min~dist~~italic(l[i])),expression(1/sqrt(i))),
pch=c(1,NA),lty=c(NA,1),col=c(2,4),pt.cex=c(1,NA))
dev.off()
### decay of Matern coefficients
# conditional sd
theta=coef(lm(log(cond.sds) ~ log(scales)))
pdf(file='plots/matern_nugget.pdf',width=4.0,height=4.0)
par(mgp = c(1.6,.5,0), mar=c(2.6,2.6,.3,.1)) # bltr
plot(cond.sds,xlab='i',ylab='residual SD')
lines(exp(theta[1]+theta[2]*log(scales)),col=2)
dev.off()
# plot of kriging weights
pdf(file='plots/matern_sq_weights.pdf',width=4.0,height=4.0)
par(mgp = c(1.6,.5,0), mar=c(2.6,2.6,.3,.1)) # bltr
squared.dev=apply(weights^2,2,mean,na.rm=TRUE)
plot(squared.dev[1:15],xlab='k',ylab='mean squared weights',col=1,cex=1.5)
abline(h=0)
lines(squared.dev[1:15],col=1,lwd=2)
dev.off()
### scatterplot vs NN
i=290
k=5
NN=NNarray.max[i,]
ra=range(data.all[NN[c(1,k)],])
pdf(file='plots/matern3d.pdf',width=4.0,height=4.0)
par(mgp = c(.1,.1,0), mar=c(.1,.1,.1,.1)) # bltr
s3d=scatterplot3d::scatterplot3d(t(data.all[c(NN[1],NN[k],i),]),angle=30,
color=rainbow(100)[cut(c(data.all[i,]),100)],
xlim=ra,ylim=ra,pch=20,xlab='1st NN',
zlab=expression(y[i]),ylab='', #paste0(k,'th NN'),
cex.symbols = 1.2)
# type='h',box=FALSE,highlight.3d=TRUE)
s3d$box3d(col='grey')
s3d$plane3d(c(0, #sum(weights[i,-c(1,k)]*rowMeans(data.all[NN[-c(1,k)],])),
weights[i,c(1,k)]),col=1)
dims <- par("usr")
x <- dims[1]+ 0.85*diff(dims[1:2])
y <- dims[3]+ 0.08*diff(dims[3:4])
text(x,y,paste0(k,'th NN'),srt=30)
dev.off()
###### matern+sine illustrations ######
source('code/maternSine_illustration.R')
###### matern+sine KL comparisons ######
names=c('linear','S-linear','nonlin','S-nonlin','DPM','MatCov','local',
'tapSamp','autoFRK')
### compute KL values for different settings, save in output folder
source('code/maternSine_comparison_all.R')
### plot comparisons results for increasing N
files=c('sin_lin','sin','sin_random','sin_DPM')
for(fi in 1:length(files)){
load(file=paste0('output/compRes_all_',files[fi],'.RData'))
pdf(file=paste0('plots/klcomp_all_',files[fi],'.pdf'),width=3.5,height=3.5)
par(mgp = c(1.6,.5,0), mar=c(2.6,2.6,.3,.1)) # bltr
if(fi==3) m.inds=c(methods,6,8:9) else m.inds=c(methods,6:9)
matplot(log10(Ns),kls,type='l',lwd=3.5,xlab='n',ylab='KL',xaxt='n',
lty=m.inds,col=m.inds,ylim=range(kls[,c(1:3,5)],na.rm=TRUE))
axis(1,at=log10(Ns),labels=Ns,lwd=0,lwd.ticks=1)
if(fi %in% c(1,4)) lloc='topright' else lloc='bottomleft'
legend(lloc,c(names[m.inds]),lwd=2.5,bg='white',lty=m.inds,col=m.inds)
dev.off()
}
fil='prod'
load(file=paste0('output/compRes_all_',fil,'.RData'))
pdf(file=paste0('plots/klcomp_all_',fil,'.pdf'),width=3.5,height=3.5)
par(mgp = c(1.6,.5,0), mar=c(2.6,2.6,.3,.1)) # bltr
m.inds=c(methods,6:9)
matplot(log10(Ns),ls,type='l',lwd=3.5,xlab='n',ylab='log score',xaxt='n',
lty=m.inds,col=m.inds,ylim=range(ls[,c(1:3,5)],na.rm=TRUE))
axis(1,at=log10(Ns),labels=Ns,lwd=0,lwd.ticks=1)
legend(lloc,c(names[m.inds]),lwd=2.5,bg='white',lty=m.inds,col=m.inds)
dev.off()
###### prepare precip anomalies for each day in june ######
load(file='data/prec_days.RData') # precs=[locs,98yrs,30d]
locs=cbind(lon,lat)
## log transform
logprecs=log(precs+1e-10)
## anomalies: standardize according to june 1
mus=apply(logprecs[,,1],1,mean)
sds=apply(logprecs[,,1],1,sd)
xs=array(dim=dim(logprecs))
for(d in 1:30) xs[,,d]=t(scale(t(logprecs[,,d]),mus,sds))
save(xs,locs,file='output/prec_anomalies.RData')
###### climate data illustrations ######
source('code/climate_illustrations.R')
###### climate data: log-score comparison incl holdout ######
source('code/climateComparison_holdout.R')
load(file='output/compRes_climate_holdout.RData')
## full map
als=-apply(ls[,,,1],2:3,mean,na.rm=TRUE)
pdf(file='plots/ls_precip.pdf',width=4.0,height=4.0)
par(mgp = c(1.6,.5,0), mar=c(2.6,2.6,.3,.1)) # bltr
matplot(Ns,als,type='l',lwd=2,xlab='n',ylab='LS',
ylim=range(als[,c(1,3,5,6)],na.rm=TRUE),lty=c(1:6,4),col=c(1:6,8))
legend('bottomleft',c(name[-4],'MatCov','local'),lty=c(1:3,5,6,4),
col=c(1:3,5,6,8),bg='white',lwd=2)
dev.off()
round(apply(als,2,min))
## holdout set
als=-apply(ls[,,,2],2:3,mean,na.rm=TRUE)
pdf(file='plots/ls_precip_ho.pdf',width=4.0,height=4.0)
par(mgp = c(1.6,.5,0), mar=c(2.6,2.6,.3,.1)) # bltr
matplot(Ns,als,type='l',lwd=2,xlab='n',ylab='LS',
ylim=range(als[,c(1,3,5,6)],na.rm=TRUE),lty=c(1:6,4),col=c(1:6,8))
legend('bottomleft',c(name[-4],'MatCov','local'),lty=c(1:3,5,6,4),
col=c(1:3,5,6,8),bg='white',lwd=2)
dev.off()
round(apply(als,2,min))
###### comparison for global climate data ######
source('code/climate_comparison_global.R')
load(file='output/compRes_climate_global.RData')
als=-apply(ls,2:3,mean,na.rm=TRUE)
pdf(file='plots/ls_precip_global.pdf',width=4.0,height=4.0)
par(mgp = c(1.6,.5,0), mar=c(2.6,2.6,.3,.1)) # bltr
matplot(Ns,als,type='l',lwd=2,xlab='n',ylab='LS',
lty=methods,col=methods)
legend('topright',name[methods],lty=methods,col=methods,lwd=2.5)
dev.off()
###### plot global data and conditional samples ######
source('code/draw_global_maps.R')
###### examine fit for largest setting ######
source('code/climate_saveTheta.R')
load(file='output/fit_global.RData')
n
m.threshold(fg$theta,m.max=30)
## posterior median of d_i
d.med=sqrt(qinvgamma(.5,fg$alpha.posts,fg$beta.posts))
sum(d.med>.05*max(d.med))
# plot(d.med,xlab='maximin index',
# ylab=expression(posterior~median~of~d[i]))
# plot(sort(d.med,TRUE))