-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.R
5770 lines (5150 loc) · 259 KB
/
test.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##### the scripture contains functions for testing latticeKrig results
# test for the makeQ function. Same inputs as makeQ
testMakeQ = function(kappa=1, rho=1, xRange=c(0,1), yRange=c(0,1), nx=40, ny=40, nLayer=1, nu=1.5) {
if(nLayer != 1) {
alphas = getAlphas(nLayer, nu)
}
else
alphas = NULL
# make precision matrix
Q = makeQ(kappa, rho, xRange, yRange, nx, ny, nLayer, alphas=alphas)
# simulate field
L = t(chol(inla.qsolve(Q, diag(nrow(Q)))))
Zs = matrix(rnorm(nrow(Q)), ncol=1)
Cs = matrix(L %*% Zs, nrow=ny, ncol=nx, byrow = TRUE)
Cs
}
# image.plot(testMakeQ(kappa=.1, rho=5))
testMakeQBuffer = function(kappa=1, rho=1, latticeInfo=makeLatGrids(nLayer=3), nu=1.5, savePlot=FALSE) {
nLayer=3
if(nLayer != 1) {
alphas = getAlphas(nLayer, nu)
}
else
alphas = NULL
# make precision matrix
Q = makeQ(kappa, rho, latticeInfo, alphas=alphas)
# simulate field
L = t(chol(inla.qsolve(Q, diag(nrow(Q)))))
Zs = matrix(rnorm(nrow(Q)), ncol=1)
Cs = L %*% Zs
## plot the coefficients of each layer
if(savePlot)
pdf(file="Figures/exampleSim.pdf", width=5, height=5)
par(mfrow=c(2,2))
startI = 1
for(l in 1:nLayer) {
# generate knot lattice locations and filter out locations
# too far outside of the data domain
nx = latticeInfo[[l]]$nx
ny = latticeInfo[[l]]$ny
xRange = latticeInfo[[l]]$xRangeKnots
yRange = latticeInfo[[l]]$yRangeKnots
nBuffer = latticeInfo[[l]]$nBuffer
out = rawGridToLK(xRange, nx, yRange, ny, nBuffer)
gridPts = latticeInfo[[l]]$latCoords
# filter out simulated coefficients for the layer
layerL = nrow(gridPts)
endI = startI + layerL - 1
layerCs = Cs[startI:endI]
quilt.plot(gridPts, layerCs, main=paste0("Layer ", l, " coefficients"),
xlim=xRange, ylim=yRange, xlab="x", ylab="y")
startI = endI+1
}
if(savePlot)
dev.off()
## plot the coefficient location in each layer together
cols = rainbow(nLayer)
for(l in 1:nLayer) {
gridPts = latticeInfo[[l]]$latCoords
xRange = latticeInfo[[l]]$xRangeKnots
yRange = latticeInfo[[l]]$yRangeKnots
if(l == 1) {
plot(gridPts[,1], gridPts[,2], main="Lattice points", col=cols[l], pch=19, cex=1.5-l*.45,
xlim=xRange, ylim=yRange, xlab="x", ylab="y")
}
else {
points(gridPts[,1], gridPts[,2], col=cols[l], pch=19, cex=1.5-l*.45,
xlim=xRange, ylim=yRange)
}
}
}
# test normalization of Q for 1 layer
testMakeNormalizedQ = function(buffer=1, kappa=1, rho=1, nu=1.5, seed=123, nx=10, ny=nx, mx=20, my=mx, sigma2 = .1^2) {
nLayer=1
if(nLayer != 1) {
alphas = getAlphas(nLayer, nu)
}
else
alphas = NULL
# get lattice points, prediction points
xRangeBasis = c(0-buffer, 1+buffer)
yRangeBasis = c(0-buffer, 1+buffer)
xRangeDat = c(0,1)
yRangeDat = c(0,1)
xs = seq(xRangeBasis[1], xRangeBasis[2], l=nx)
ys = seq(yRangeBasis[1], yRangeBasis[2], l=ny)
latPts = make.surface.grid(list(x=xs, y=ys))
xmask = (latPts[,1] >= xRangeDat[1]) & (latPts[,1] <= xRangeDat[2])
ymask = (latPts[,2] >= yRangeDat[1]) & (latPts[,2] <= yRangeDat[2])
mask = xmask & ymask
latPtsInDom = latPts[mask,]
# generate lattice and simulate observations
coords = make.surface.grid(list(x=seq(xRangeDat[1], xRangeDat[2], l=mx), y=seq(yRangeDat[1], yRangeDat[2], l=my)))
AObs = makeA(coords, xRangeBasis, nx, yRangeBasis, ny, nLayer=nLayer)
Q = makeQ(kappa=kappa, rho=1, xRange=xRangeBasis, yRange=yRangeBasis, nx=nx, ny=ny, nLayer=nLayer, alphas=alphas)
Qinv = inla.qsolve(Q, diag(nrow(Q)))
# make mid lattice point
xi = ceiling(nx/2)
yi = ceiling(ny/2)
midPt = matrix(c(seq(xRangeBasis[1], xRangeBasis[2], l=nx)[xi], seq(yRangeBasis[1], yRangeBasis[2], l=ny)[yi]), nrow=1)
# now construct relevant row of A for value at midpoint, and Q matrix
Ai = makeA(midPt, xRange, nx, yRange, ny, nLayer=nLayer)
# renormalize basis coefficients to have constant variance, and the process to have unit variance
sds = sqrt(diag(Qinv))
Qnorm = sweep(sweep(Q, 1, sds, "*"), 2, sds, "*")
procVar = as.numeric(Ai %*% inla.qsolve(Qnorm, t(Ai)))
Qfinal = Qnorm * (procVar / rho)
## plot simulations
# simulate lattice coefficients
L = t(chol(Qinv))
Zs = matrix(rnorm(nrow(Q)), ncol=1)
Cs = L %*% Zs
CsMat = matrix(Cs, nrow=ny, ncol=nx, byrow = TRUE)
image.plot(CsMat, main="Simulated lattice coefficients")
# now simulate the continuous process:
image.plot(matrix(AObs %*% Cs, nrow=my, ncol=mx, byrow=TRUE), main="Simulated Process")
## plot normalized and unnormalized coefficient variances
# unnormalized coefficients
vars = diag(Qinv)
quilt.plot(latPtsInDom, vars[mask], main="Marginal variance of unnormalized coefficients", nx=20, ny=20)
# normalized coefficeints
vars = diag(inla.qinv(Qnorm))
quilt.plot(latPtsInDom, vars[mask] + jitter(rep(0, sum(mask)), amount=.000001), main="Marginal variance of normalized coefficients", nx=20, ny=20)
# final coefficients
vars = diag(inla.qinv(Qfinal))
quilt.plot(latPtsInDom, vars[mask] + jitter(rep(0, sum(mask)), amount=.000001), main="Marginal variance of final coefficients", nx=20, ny=20)
## plot the normalized and unnormalized process variances
# unnormalized
vars = diag(AObs %*% inla.qsolve(Q, t(AObs)))
quilt.plot(coords, vars, main="Marginal variance of unnormalized process")
# normalized
vars = diag(AObs %*% inla.qsolve(Qnorm, t(AObs)))
quilt.plot(coords, vars, main="Marginal variance of normalized process")
# final
vars = diag(AObs %*% inla.qsolve(Qfinal, t(AObs)))
quilt.plot(coords, vars, main="Marginal variance of final process")
}
# test normalization of Q for multiple layers
testMakeNormalizedQMulti = function(buffer=1, kappa=1, rho=1, nu=1.5, seed=123, nx=10, ny=nx, mx=20, my=mx,
sigma2 = .1^2, nLayer=3, nBuffer=5, plotMinMax=FALSE) {
if(nLayer != 1) {
alphas = getAlphas(nLayer, nu)
}
else
alphas = NULL
# get lattice points, prediction points
xRangeBasis = c(0-buffer, 1+buffer)
yRangeBasis = c(0-buffer, 1+buffer)
xRangeDat = c(0,1)
yRangeDat = c(0,1)
xs = seq(xRangeBasis[1], xRangeBasis[2], l=nx)
ys = seq(yRangeBasis[1], yRangeBasis[2], l=ny)
latPts = make.surface.grid(list(x=xs, y=ys))
xmask = (latPts[,1] >= xRangeDat[1]) & (latPts[,1] <= xRangeDat[2])
ymask = (latPts[,2] >= yRangeDat[1]) & (latPts[,2] <= yRangeDat[2])
mask = xmask & ymask
latPtsInDom = latPts[mask,]
# generate lattice and simulate observations
coords = make.surface.grid(list(x=seq(xRangeDat[1], xRangeDat[2], l=mx), y=seq(yRangeDat[1], yRangeDat[2], l=my)))
AObs = makeA(coords, xRangeBasis, nx, yRangeBasis, ny, nLayer=nLayer, xRangeDat=xRangeDat, yRangeDat=yRangeDat, nBuffer=nBuffer)
Q = makeQ(kappa=kappa, rho=1, xRange=xRangeBasis, yRange=yRangeBasis, nx=nx, ny=ny, nLayer=nLayer,
alphas=alphas, nu=nu, xRangeDat=xRangeDat, yRangeDat=yRangeDat, nBuffer=nBuffer, normalized=FALSE)
Qfinal = makeQ(kappa=kappa, rho=1, xRange=xRangeBasis, yRange=yRangeBasis, nx=nx, ny=ny, nLayer=nLayer,
alphas=alphas, nu=nu, xRangeDat=xRangeDat, yRangeDat=yRangeDat, nBuffer=nBuffer, normalized=TRUE)
## plot the normalized and unnormalized process variances
# unnormalized
vars = diag(AObs %*% inla.qsolve(Q, t(AObs)))
pdf(file="Figures/varRatioUnnormalized.pdf", width=5, height=5)
quilt.plot(coords, vars, main="Marginal variance of unnormalized process")
maxI = which.max(vars)
minI = which.min(vars)
if(plotMinMax) {
points(coords[maxI,1], coords[maxI, 2], cex=2)
points(coords[minI,1], coords[minI, 2], cex=2)
}
unnormRatio = max(vars)/min(vars)
dev.off()
print(paste0("Ratio of largest to smallest variance for unnormalized process: ", unnormRatio))
# normalized
# vars = diag(AObs %*% inla.qsolve(Qnorm, t(AObs)))
# quilt.plot(coords, vars, main="Marginal variance of normalized (not final) process")
# normRatio = max(vars)/min(vars)
# print(paste0("Ratio of largest to smallest variance for normalized process: ", normRatio))
#
# final
vars = diag(AObs %*% inla.qsolve(Qfinal, t(AObs)))
pdf(file="Figures/varRatioFinal.pdf", width=5, height=5)
quilt.plot(coords, vars, main="Marginal variance of normalized process")
maxI = which.max(vars)
minI = which.min(vars)
if(plotMinMax) {
points(coords[maxI,1], coords[maxI, 2], cex=2)
points(coords[minI,1], coords[minI, 2], cex=2)
}
dev.off()
finalRatio = max(vars)/min(vars)
print(paste0("Ratio of largest to smallest variance for final process: ", finalRatio))
c(unnormRatio=unnormRatio, finalRatio=finalRatio)
}
# testMakeNormalizedQMulti(kappa=.5, buffer=2.5, nx=20, ny=20)
makeNormalizedQMultiTable = function() {
# testMakeNormalizedQMulti()
# testMakeNormalizedQMulti(kappa=.01)
# testMakeNormalizedQMulti(kappa=100)
# testMakeNormalizedQMulti(nu=.5)
# testMakeNormalizedQMulti(nu=.5, kappa=.01)
# testMakeNormalizedQMulti(nu=.5, kappa=100)
# testMakeNormalizedQMulti(nu=2.5)
# testMakeNormalizedQMulti(nu=2.5, kappa=.01)
# testMakeNormalizedQMulti(nu=2.5, kappa=100)
require(xtable)
tab5 = sapply(c(.01, .1, .5, 1, 5, 10, 100), testMakeNormalizedQMulti, buffer=2.5, nx=20, ny=20, nu=.5)
tab15 = sapply(c(.01, .1, .5, 1, 5, 10, 100), testMakeNormalizedQMulti, buffer=2.5, nx=20, ny=20)
tab25 = sapply(c(.01, .1, .5, 1, 5, 10, 100), testMakeNormalizedQMulti, buffer=2.5, nx=20, ny=20, nu=2.5)
fullTab = rbind(tab5, tab15, tab25)
colnames(fullTab) = c("kappa=0.01", "kappa=0.1", "kappa=0.5", "kappa=1", "kappa=5", "kappa=10", "kappa=100")
xtable(fullTab, digits=2)
}
# test for makeA and makeQ:
testMakeAQ = function(xRange=c(0,1), yRange=c(0,1), nx=10, ny=10,
kappa=.1, rho=1, resFac=3, nLayer=1, nu=1.5,
xRangeDat=xRange, yRangeDat=yRange, nBuffer=5,
normalize=FALSE, seed=1) {
set.seed(seed)
if(nLayer != 1) {
alphas = getAlphas(nLayer, nu)
}
else
alphas = NULL
# first step: make Q matrix
Qmat = makeQ(kappa, rho, xRange, yRange, nx, ny, nLayer, alphas=alphas, xRangeDat=xRangeDat,
yRangeDat=yRangeDat, nBuffer=nBuffer, normalize=normalize)
# second step: make A matrix
par(mfrow=c(1,1))
image(Qmat)
predPts = make.surface.grid(list(x=seq(xRange[1], xRange[2], l=nx*resFac),
y=seq(yRange[1], yRange[2], l=ny*resFac)))
Amat = makeA(predPts, xRange, nx, yRange, ny, nLayer=nLayer, xRangeDat=xRangeDat,
yRangeDat=yRangeDat, nBuffer=nBuffer)
# third step: simulate field on knot lattice and prediction points:
L = t(chol(solve(Qmat)))
Zs = matrix(rnorm(nrow(Qmat)), ncol=1)
Cs = matrix(as.numeric(L %*% Zs), ncol=1)
predSim = matrix(Amat %*% Cs, nrow=ny*resFac, ncol=nx*resFac)
# make color scale for plots
allVals = c(Cs, predSim)
valRange = range(allVals)
# make basis coefficient/resulting spatial field plots
if(nLayer == 1) {
par(mfrow=c(1,2))
CsMat = matrix(Cs, nrow=ny, ncol=nx, byrow = FALSE)
image.plot(CsMat, main="Basis Coefficients", breaks=seq(valRange[1], valRange[2], l=65))
image.plot(predSim, main="Spatial Field", breaks=seq(valRange[1], valRange[2], l=65))
}
else if(nLayer < 4) {
par(mfrow=c(2,2))
layerInds = 1:(nx*ny)
thisNX = nx
thisNY = ny
for(lay in 1:nLayer) {
# plot this basis layer
CsMat = matrix(Cs[layerInds], nrow=thisNY, ncol=thisNX, byrow = FALSE)
image.plot(CsMat, main=paste0("Basis Coefficients (Layer ", lay, ")"),
breaks=seq(valRange[1], valRange[2], l=65))
# update layer indices
maxInd = max(layerInds)
thisNX = thisNX*2
thisNY = thisNY*2
layerInds = (maxInd + 1):(maxInd + thisNX*thisNY)
}
image.plot(predSim, main="Spatial Field", breaks=seq(valRange[1], valRange[2], l=65))
}
else {
image.plot(predSim, main="Spatial Field", breaks=seq(valRange[1], valRange[2], l=65))
}
}
testMakeGraph1 = function(nx=3, ny=4) {
image(makeGraph(nx, ny))
}
testMakeGraph2 = function(nx=3, ny=4) {
Q = makeQ(nx=nx, ny=ny)
image(Q != 0)
}
# testMakeGraph1()
# testMakeGraph2()
plotExampleDatasets = function(savePlots=FALSE) {
# plot example datasets:
# ozone2 dataset:
# 8 hr average surface O3 levels (from 9am-4pm) in ppb
# 153 sites in midwest
# June 3 1987 - August 31 1987 (89 days)
# 153 x 89 = 13,617 observations
data("ozone2")
names(ozone2)
dim(ozone2$y)
lonLat = ozone2$lon.lat
y1 = ozone2$y[1,]
if(savePlots)
pdf("ozoneDataSet.pdf", width=5, height=5)
quilt.plot(lonLat, y1, main="ozone2 (t=1 of 89)", xlab="Longitude", ylab="Latitude")
US(add=TRUE)
if(savePlots)
dev.off()
# also uses ozone2 in some examples
# monthly min/max temperatures (deg C) and precip (total mm) from 1895 to 1997 (103 years).
# 376 stations, MAM= March, April, May
# 376 x 103 = 38,728 observations
data(COmonthlyMet)
?COmonthlyMet
if(savePlots)
pdf("coloradoTempDataSet.pdf", width=5, height=5)
quilt.plot( CO.loc,CO.tmax.MAM[103,], main="Recorded MAM max temperatures (t=103 of 103)",
xlab="Longitude", ylab="Latitude")
US( add=TRUE)
if(savePlots)
dev.off()
# LatticeKrig paper also uses NorthAmericanRainfall dataset
# 1720 stations precip in JJA=June,July,August based on data from 1950-2010 (61 years).
# Also includes elevation.
# dataset in LK package only includes linear trends and intercepts for each station
# 1720 x 1 = 1720 observations (or, with full dataset, 1720 x 61 = 104,920 observations)
data(NorthAmericanRainfall)
x<- cbind(NorthAmericanRainfall$longitude, NorthAmericanRainfall$latitude)
y<- NorthAmericanRainfall$precip
if(savePlots)
pdf("precipitationDataSet.pdf", width=5, height=5)
quilt.plot( x,y/10, main="Mean JJA Precipitation, 1950-2010 (mm)", xlab="Longitude", ylab="Latitude")
world( add=TRUE)
if(savePlots)
dev.off
if(savePlots)
pdf("precipitationCoordsDataSet.pdf", width=5, height=5)
plot(x[, 1], x[, 2], main="Mean JJA Precipitation, 1950-2010 (mm)", xlab="Longitude", ylab="Latitude",
pch=19, cex=.1)
world( add=TRUE)
if(savePlots)
dev.off()
}
#####
# test out the correlation induced by kappa and rho
testCorrelation = function(nsim=1000, kappa=1, rho=1, N=30, NPred=60) {
# get lattice points, prediction points, basis matrix APred
gridPts = make.surface.grid(list(xs=seq(0, 1, l=10),
ys=seq(0, 1, l=10)))
predPts = make.surface.grid(list(xs=seq(0,1,l=20),
ys=seq(0,1,l=20)))
APred = makeA(predPts)
# generate simulations
Q = makeQ(kappa=kappa, rho=rho)
L = t(chol(solve(Q)))
zsims = matrix(rnorm(nsim*100), ncol=nsim)
fieldSims = L %*% zsims
pointSims = APred %*% fieldSims
# estimate correlogram
fullCorGram = NULL
fullCorGramPred = NULL
for(i in 1:nsim) {
corG = myvgram(gridPts, fieldSims[,i], type="correlogram", colMeans=0)
corGPred = myvgram(predPts, pointSims[,i], type="correlogram", colMeans=0)
if(is.null(fullCorGram)) {
fullCorGram = corG
fullCorGramPred = corGPred
}
else {
fullCorGram$d = c(fullCorGram$d, corG$d)
fullCorGram$vgram = c(fullCorGram$vgram, corG$vgram)
fullCorGramPred$d = c(fullCorGramPred$d, corGPred$d)
fullCorGramPred$vgram = c(fullCorGramPred$vgram, corGPred$vgram)
}
}
## plot empirical versus theoretical correlograms for basis coefficients
corMean = getVGMean(fullCorGram, N=N)
notnans = !is.nan(corMean$ys)
corRange = range(c(corMean$ys[notnans], 0, 1))
plot(corMean$centers[notnans], corMean$ys[notnans], ylim=corRange, xlim=c(0, sqrt(2)), type="o", pch=19,
xlab="Distance", ylab="Correlation", main="Lattice Coefficient Correlation")
xs = seq(0, sqrt(2), l=100)
# > Matern(sqrt(8))
# [1] 0.1002588
# effectiveRange=sqrt(8*1)/kappa # the distance at which Matern correlation is roughly .1
squareWidth = .1
effectiveRange=2.687/kappa*squareWidth
lines(xs, Matern(xs, smoothness=1, range=effectiveRange/sqrt(8)))
## plot empirical versus theoretical correlograms for basis coefficients
corMean = getVGMean(fullCorGramPred, N=NPred)
notnans = !is.nan(corMean$ys)
corRange = range(c(corMean$ys[notnans], 0, 1))
plot(corMean$centers[notnans], corMean$ys[notnans], ylim=corRange, xlim=c(0, sqrt(2)), type="o", pch=19,
xlab="Distance", ylab="Correlation", main="Latent Field Correlation")
xs = seq(0, sqrt(2), l=100)
# > Matern(sqrt(8))
# [1] 0.1002588
# effectiveRange=sqrt(8*1)/kappa # the distance at which Matern correlation is roughly .1
squareWidth = .1
effectiveRange=2.687/kappa*squareWidth
lines(xs, Matern(xs, smoothness=1, range=effectiveRange/sqrt(8)))
}
# control.family(list(hyper=list(prec=list(initial=starting_value_for_log_prec, fixed=TRUE))))
# reduce observation noise by a lot (sigma=.1, sigma2=.01)
# look at marginal variance of the field
# compare results of lattice krig, inla.spde
# simulate a realization from the LK model with params near peak of priors
# fit a model
# test against LK, inla.spde
#####
# test out the correlation induced by kappa and rho
testCorrelation2 = function(kappa=1, rho=1, nx=30, mx=20, maxPlot=300, minEdgeDist=1, buffer=1) {
# get lattice points, prediction points, basis matrix APred
gridPts = make.surface.grid(list(xs=seq(0-buffer, 1+buffer, l=nx),
ys=seq(0-buffer, 1+buffer, l=nx)))
predPts = make.surface.grid(list(xs=seq(0,1,l=mx),
ys=seq(0,1,l=mx)))
latWidth = (1+buffer - (0 - buffer))/(nx-1)
theta = 2.5*latWidth
APred = makeA(predPts, theta=theta, xNKnot=nx, yNKnot=nx, xRangeKnot=c(0-buffer,1+buffer), yRangeKnot=c(0-buffer,1+buffer))
# compute which points are far from the edge, and convert into the appropriate vectorized matrix using outer
gridPtsCntr = (gridPts[,1] > 0-buffer + minEdgeDist) & (gridPts[,1] < 1+buffer - minEdgeDist) &
(gridPts[,2] > 0-buffer + minEdgeDist) & (gridPts[,2] < 1+buffer - minEdgeDist)
predPtsCntr = (predPts[,1] > 0-buffer + minEdgeDist) & (predPts[,1] < 1+buffer - minEdgeDist) &
(predPts[,2] > 0-buffer + minEdgeDist) & (predPts[,2] < 1+buffer - minEdgeDist)
gridPtsCntr = c(outer(gridPtsCntr, gridPtsCntr))
predPtsCntr = c(outer(predPtsCntr, predPtsCntr))
# compute variance and correlation matrices for lattice coefficients (c) and pred points (Ac)
Q = makeQ(kappa=kappa, rho=rho, nx=nx, ny=nx, xRange=c(0-buffer,1+buffer), yRange=c(0-buffer,1+buffer))
varC = solve(Q)
varAC = APred %*% varC %*% t(APred)
sigmasC = sqrt(diag(varC))
sigmasAC = sqrt(diag(varAC))
corC = c(sweep(sweep(as.matrix(varC), 1, 1/sigmasC, "*"), 2, 1/sigmasC, "*"))
corAC = c(sweep(sweep(as.matrix(varAC), 1, 1/sigmasAC, "*"), 2, 1/sigmasAC, "*"))
D = c(rdist(gridPts))
DPred = c(rdist(predPts))
## plot empirical versus theoretical correlograms for basis coefficients
# get unique distances and correlations and points far enough from edge
uniqueDI = !duplicated(corC)
uniqueDPredI = !duplicated(corAC)
uniqueDI = uniqueDI & gridPtsCntr
uniqueDPredI = uniqueDPredI & predPtsCntr
uniqueD = D[uniqueDI]
uniqueDPred = DPred[uniqueDPredI]
uniqueCorC = corC[uniqueDI]
uniqueCorAC = corAC[uniqueDPredI]
if(length(uniqueD) > maxPlot) {
inds = sample(1:length(uniqueD), maxPlot)
uniqueD = uniqueD[inds]
uniqueCorC = uniqueCorC[inds]
}
if(length(uniqueDPred) > maxPlot) {
inds = sample(1:length(uniqueDPred), maxPlot)
uniqueDPred = uniqueDPred[inds]
uniqueCorAC = uniqueCorAC[inds]
}
# make sure all correlations at zero distance are 1
zeroDistCors = corC[D == 0]
if(any(rdist(zeroDistCors, 1) > 1e-6))
warning(paste0("Bad lattice correlation at dist=0: ", zeroDistCors[match(1, rdist(zeroDistCors, 1) > 1e-6)]))
zeroDistCors = corAC[DPred == 0]
if(any(zeroDistCors != 1))
warning(paste0("Bad latent field correlation at dist=0: ", zeroDistCors[match(1, rdist(zeroDistCors, 1) > 1e-6)]))
# add a point at (0,1)
uniqueD = c(uniqueD, 0)
uniqueCorC = c(uniqueCorC, 1)
uniqueDPred = c(uniqueDPred, 0)
uniqueCorAC = c(uniqueCorAC, 1)
plot(uniqueD, uniqueCorC, ylim=c(0,1), xlim=c(0, (latWidth*nx - 2*buffer)*sqrt(2)), pch=19, cex=.5,
xlab="Distance", ylab="Correlation", main="Lattice Coefficient Correlation")
xs = seq(0, sqrt(2), l=100)
# > Matern(sqrt(8))
# [1] 0.1002588
# effectiveRange=sqrt(8*1)/kappa # the distance at which Matern correlation is roughly .1
squareWidth = latWidth
effectiveRange1=sqrt(8)/kappa*squareWidth
effectiveRange2=sqrt(8)/kappa * latWidth
lines(xs, Matern(xs, smoothness=1, range=effectiveRange1/sqrt(8)), col="blue")
lines(xs, Matern(xs, smoothness=1, range=effectiveRange2/sqrt(8)), col="purple")
legend("topright", c(TeX("$w/\\kappa$"), bquote(sqrt(8)*w/kappa)),
col=c("blue", "purple"), lty=1)
## plot empirical versus theoretical correlograms for basis coefficients
plot(uniqueDPred, uniqueCorAC, ylim=c(0,1), xlim=c(0, (latWidth*nx - 2*buffer)*sqrt(2)), pch=19, cex=.3,
xlab="Distance", ylab="Correlation", main="Latent Field Correlation")
xs = seq(0, sqrt(2), l=100)
# > Matern(sqrt(8))
# [1] 0.1002588
lines(xs, Matern(xs, smoothness=1, range=effectiveRange1/sqrt(8)), col="blue")
lines(xs, Matern(xs, smoothness=1, range=effectiveRange2/sqrt(8)), col="purple")
legend("topright", c(TeX("$w/\\kappa$"), bquote(sqrt(8)*w/kappa)),
col=c("blue", "purple"), lty=1)
}
# test out the correlation induced by kappa and rho for multi-layer
# model. Plots saved as:
# margVarTest.pdf
# paste0("Figures/corNx", nx, "Nu", round(nu, digits=2), "L", nLayer, ".pdf")
testCorrelation3 = function(kappa=1, rho=1, nx=30, mx=20, maxPlot=300, minEdgeDist=1, buffer=1, nu=1, nLayer=2) {
# get lattice points, prediction points, basis matrix APred
gridPts = make.surface.grid(list(xs=seq(0-buffer, 1+buffer, l=nx),
ys=seq(0-buffer, 1+buffer, l=nx)))
predPts = make.surface.grid(list(xs=seq(0,1,l=mx),
ys=seq(0,1,l=mx)))
latWidth = (1+buffer - (0 - buffer))/(nx-1)
theta = 2.5*latWidth
APred = makeA(predPts, xNKnot=nx, yNKnot=nx, xRangeKnot=c(0-buffer,1+buffer), yRangeKnot=c(0-buffer,1+buffer), nLayer=nLayer,
xRangeDat=c(0,1), yRangeDat=c(0,1))
# compute variance and correlation matrices for lattice coefficients (c) and pred points (Ac)
Q = makeQ(kappa=kappa, rho=rho, nx=nx, ny=nx, xRange=c(0-buffer,1+buffer), yRange=c(0-buffer,1+buffer),
nLayer=nLayer, nu=nu, xRangeDat=c(0,1), yRangeDat=c(0,1))
ny=nx
# make mid lattice point
xi = ceiling(nx/2)
yi = ceiling(ny/2)
midPt = matrix(c(seq(0-buffer, 1+buffer, l=nx)[xi], seq(0-buffer, 1+buffer, l=ny)[yi]), nrow=1)
# now construct relevant row of A for value at midpoint, and Q matrix
Ai = makeA(midPt, c(0-buffer,1+buffer), nx, c(0-buffer, 1+buffer), ny, nLayer=nLayer,
xRangeDat=c(0,1), yRangeDat=c(0,1))
# renormalize basis coefficients to have constant variance, and the process to have unit variance
Qinv = inla.qinv(Q)
sds = sqrt(diag(Qinv))
# Qnorm = sweep(sweep(Q, 1, sds, "*"), 2, sds, "*")
Qnorm = Diagonal(x=sds) %*% Q %*% Diagonal(x=sds)
procVar = as.numeric(Ai %*% inla.qsolve(Qnorm, t(Ai)))
Q = Qnorm * (procVar / rho)
# varC = solve(Q)
varAC = APred %*% inla.qsolve(Q, t(APred))
sigmasC = sqrt(diag(inla.qinv(Q)))
sigmasCMatInv = Diagonal(x=1/sigmasC)
sigmasAC = sqrt(diag(varAC))
sigmasACMatInv = Diagonal(x=1/sigmasAC)
# fullCorC = sweep(sweep(as.matrix(varC), 1, 1/sigmasC, "*"), 2, 1/sigmasC, "*")
fullCorC = sigmasCMatInv %*% inla.qsolve(Q, sigmasCMatInv)
# corAC = c(sweep(sweep(as.matrix(varAC), 1, 1/sigmasAC, "*"), 2, 1/sigmasAC, "*"))
corAC = as.numeric(sigmasACMatInv %*% varAC %*% sigmasACMatInv)
# compute distance matrix for prediction locations
DPred = c(rdist(predPts))
# plot correlation approximation and alpha weight for each layer
if((nLayer >= 2) && (nLayer <= 3)) {
pdf(file=paste0("Figures/corNx", nx, "Nu", round(nu, digits=2), "L", nLayer, ".pdf"), width=10, height=8)
par(mfrow=c(2,2))
}
else if(nLayer > 3) {
if(nLayer > 5)
stop("nLayer > 5")
pdf(file=paste0("Figures/corNx", nx, "Nu", round(nu, digits=2), "L", nLayer, ".pdf"), width=10, height=12)
par(mfrow=c(3,2))
}
else {
pdf(file=paste0("Figures/corNx", nx, "Nu", round(nu, digits=2), "L", nLayer, ".pdf"), width=10, height=5)
par(mfrow=c(1,2))
}
startI=1
alphas=getAlphas(nLayer, nu)
for(l in 1:nLayer) {
# get resolution for this lattice layer
res = 2^(l-1)
nLatticePts = (res*nx)^2
layerInds = startI:(startI+nLatticePts-1)
startI = startI + nLatticePts
# get C submatrix for this layer in vector form
corC = as.numeric(fullCorC[layerInds, layerInds])
# get lattice points for this layer
gridPts = make.surface.grid(list(xs=seq(0-buffer, 1+buffer, l=nx*res),
ys=seq(0-buffer, 1+buffer, l=nx*res)))
# compute which points are far from the edge, and convert into the appropriate vectorized matrix using outer
gridPtsCntr = (gridPts[,1] > 0-buffer + minEdgeDist) & (gridPts[,1] < 1+buffer - minEdgeDist) &
(gridPts[,2] > 0-buffer + minEdgeDist) & (gridPts[,2] < 1+buffer - minEdgeDist)
predPtsCntr = (predPts[,1] > 0-buffer + minEdgeDist) & (predPts[,1] < 1+buffer - minEdgeDist) &
(predPts[,2] > 0-buffer + minEdgeDist) & (predPts[,2] < 1+buffer - minEdgeDist)
gridPtsCntr = c(outer(gridPtsCntr, gridPtsCntr))
predPtsCntr = c(outer(predPtsCntr, predPtsCntr))
# get distance matrix for lattice points
D = c(rdist(gridPts))
## plot approximate versus theoretical correlograms for basis coefficients
# get unique distances and correlations and points far enough from edge
uniqueDI = !duplicated(corC)
uniqueDI = uniqueDI & gridPtsCntr
uniqueD = D[uniqueDI]
uniqueCorC = corC[uniqueDI]
# subsample correlogram points to plot so there aren't too many
if(length(uniqueD) > maxPlot) {
inds = sample(1:length(uniqueD), maxPlot)
uniqueD = uniqueD[inds]
uniqueCorC = uniqueCorC[inds]
}
# make sure all correlations at zero distance are 1
zeroDistCors = corC[D == 0]
if(any(rdist(zeroDistCors, 1) > 1e-6))
warning(paste0("Bad lattice correlation at dist=0: ", zeroDistCors[match(1, rdist(zeroDistCors, 1) > 1e-6)]))
zeroDistCors = corAC[DPred == 0]
if(any(zeroDistCors != 1))
warning(paste0("Bad latent field correlation at dist=0: ", zeroDistCors[match(1, rdist(zeroDistCors, 1) > 1e-6)]))
# add a point at (0,1)
uniqueD = c(uniqueD, 0)
uniqueCorC = c(uniqueCorC, 1)
plot(uniqueD, uniqueCorC, ylim=c(0,1), xlim=c(0, (latWidth*nx - 2*buffer)*sqrt(2)), pch=19, cex=.5,
xlab="Distance", ylab="Correlation", main=paste0("Lattice Coefficient Correlation, Layer ", l, " alpha = ", round(alphas[l], digits=3)))
xs = seq(0, sqrt(2), l=100)
# > Matern(sqrt(8))
# [1] 0.1002588
# effectiveRange=sqrt(8*1)/kappa # the distance at which Matern correlation is roughly .1
squareWidth = latWidth/res
effectiveRange=sqrt(8)/kappa*squareWidth
lines(xs, Matern(xs, smoothness=1, range=effectiveRange/sqrt(8)), col="blue")
# plot variance of layer basis coefficients
# thisVarC = as.matrix(varC[layerInds, layerInds])
# coefVars = diag(inla.qinv(Q)[layerInds, layerInds])
# quilt.plot(gridPts, coefVars, main=paste0("Basis coefficient variance (layer ", l, ")"))
# plot variance of layer predictions/process
# tmp = as.matrix(APred)[,layerInds]
# predVars = tmp %*% thisVarC %*% t(tmp)
# quilt.plot(predPts, diag(predVars), main=paste0("Layer ", l, " process variance"))
}
# get correlations at prediction points
uniqueDPredI = !duplicated(corAC)
uniqueDPredI = uniqueDPredI & predPtsCntr
uniqueCorAC = corAC[uniqueDPredI]
uniqueDPred = DPred[uniqueDPredI]
# subsample correlogram points to plot so there aren't too many
if(length(uniqueDPred) > maxPlot) {
inds = sample(1:length(uniqueDPred), maxPlot)
uniqueDPred = uniqueDPred[inds]
uniqueCorAC = uniqueCorAC[inds]
}
# add a point at (0,1)
uniqueDPred = c(uniqueDPred, 0)
uniqueCorAC = c(uniqueCorAC, 1)
## plot empirical versus theoretical correlograms for basis coefficients
plot(uniqueDPred, uniqueCorAC, ylim=c(0,1), xlim=c(0, (latWidth*nx - 2*buffer)*sqrt(2)), pch=19, cex=.3,
xlab="Distance", ylab="Correlation", main=paste0("Latent Field Correlation, ", nLayer, " Layers"))
xs = seq(0, sqrt(2), l=100)
effectiveRange = sqrt(8)/kappa*latWidth
lines(xs, Matern(xs, smoothness=nu, range=effectiveRange/sqrt(8)), col="blue")
# add line representing weighted sum of materns based on alphas
cors = matrix(nrow=length(xs), ncol=nLayer)
for(l in 1:nLayer) {
res = 2^(l-1)
squareWidth = latWidth/res
effectiveRange=sqrt(8)/kappa*squareWidth
cors[,l] = alphas[l] * Matern(xs, smoothness=1, range=effectiveRange/sqrt(8))
}
ys = rowSums(cors)
lines(xs, ys, col="purple")
legend("topright", c("single Matern", "weighted Matern sum"),
col=c("blue", "purple"), lty=1)
dev.off()
##### now for testing marginal variance
sigma2 = rho/(4*pi*(4+kappa^2-4))
print(paste0("Theoretical marginal variance is: ", sigma2))
pdf("Figures/margVarTest_procVar.pdf", width=5, height=5)
quilt.plot(predPts, sigmasAC^2, main="Process variance")
dev.off()
tmp = as.matrix(APred)
quilt.plot(predPts, tmp %*% rep(1, ncol(APred)), main="Sum of basis functions")
quilt.plot(predPts, tmp %*% rep(sigma2, ncol(APred)), main="Sum of basis functions times theoretical variance")
# test the amount of inflation versus kappa
myFun = function(x, thisRho=rho) { c(getMargVar(x, rho=thisRho)$inflation, getMargVar(x, rho=thisRho)$theorVar, getMargVar(x, rho=thisRho)$actualVar) }
kappas = 10^(seq(-4, 1, l=100))
vars = sapply(kappas, myFun)
inflates = vars[1,]
actualVars = vars[3,]
theorVars = vars[2,]
options(scipen=5)
pdf("Figures/margVarTest.pdf", width=5, height=5)
plot(log10(kappas), theorVars, main="Normalized variance estimates", ylim=c(min(actualVars), max(actualVars)),
xlab=TeX("$\\kappa$"), ylab=TeX("$\\sigma^2 / \\rho $"), type="l", col="red", xaxt="n",
xlim=c(-2, 1))
axis(1,at=c(-2, -1, 0, 1),labels=c("0.001", "0.1", "1", "10"))
lines(log10(kappas), actualVars, col="blue")
# lines(kappas, inflates, col="purple")
# legend("topright", c("True Variance", "Asymptotic Variance", "Variance Ratio"),
# lty=1, col=c("blue", "red", "purple"))
legend("topright", c("True Variance", "Asymptotic Variance"),
lty=1, col=c("blue", "red"))
dev.off()
}
# nu=1; L=7; sqrt(exp(-2*nu*(1:L))/sum(exp(-2*nu*(1:L))))
testAlphaPrior = function() {
require(hexbin)
par(mfrow=c(2,3))
samples = rdirichlet(1000000, alpha=c(1, 1, 1))
h <- hexbin(samples[,1:2])
plot(h)
h <- hexbin(samples[,c(1,3)])
plot(h)
h <- hexbin(samples[,2:3])
plot(h)
hist(samples[,1], freq=FALSE)
hist(samples[,2], freq=FALSE)
hist(samples[,3], freq=FALSE)
}
# tests the fitLKINLAStandard function using data simulated from the LK model
# buffer: buffer distance between domain edge of basis lattice and domain edge of data.
# n: number of observations
# xRange: range of x coordinates
# yRange: range of y coordinates
# nx: number of basis function lattice points in x directions
# NOTE: ny is determined automatically to match scale of x lattice points
# Xmat: design matrix
# ys: observations
# first.time: is first time evaluating function. User should always set to FALSE
testLKINLAModelStandard = function(buffer=2.5, kappa=1, rho=1, nu=1.5, seed=1, nLayer=3, nx=20, ny=nx, n=900, sigma2 = .2^2,
nBuffer=5, normalize=TRUE, fastNormalize=TRUE, NC=5, testCovs=TRUE, alphas=NULL,
printVerboseTimings=FALSE) {
set.seed(seed)
# compute alphas, the variance weights for each layer, depending on nu if necessary:
if(is.null(alphas))
alphas = getAlphas(nLayer, nu)
# generate lattice and simulate observations
coords = matrix(runif(2*n), ncol=2)
xRangeDat = range(coords[,1])
yRangeDat = range(coords[,2])
latInfo = makeLatGrids(xRangeDat, yRangeDat, NC, nBuffer, nLayer)
AObs = makeA(coords, latInfo)
Q = makeQ(kappa=kappa, rho=rho, latInfo, alphas=alphas, normalized=normalize, fastNormalize=fastNormalize)
L = as.matrix(t(chol(solve(Q))))
zsims = matrix(rnorm(nrow(Q)), ncol=1)
fieldSims = L %*% zsims
ys = as.numeric(AObs %*% fieldSims) + 1 # add a constant unit mean term to be estimated by INLA
# ys = 1 + as.numeric(AObs %*% fieldSims) + coords[,1] # x-valued mean term to be estimated by INLA
errs = rnorm(n, sd=sqrt(sigma2))
ys = ys + errs
# plot the observations
pdf(file="Figures/standardObservations.pdf", width=5, height=5)
par(mfrow=c(1,1))
quilt.plot(coords, ys)
dev.off()
# make prediction coordinates on a grid
xRange=c(0,1)
yRange=c(0,1)
mx = 100
my = 100
predPts = make.surface.grid(list(x=seq(xRange[1], xRange[2], l=mx), y=seq(yRange[1], yRange[2], l=my)))
# generate hyperparameters based on median and quantiles of inverse exponential and inverse gamma
priorPar = getPrior(.1, .1, 10)
X = matrix(rep(1, n), ncol=1)
# X = matrix(coords[,1], ncol=1)
XPred = matrix(rep(1, mx*my), ncol=1)
# add linear terms in lat/lon to covariate matrices if requested
if(testCovs) {
X = cbind(X, coords)
XPred = cbind(XPred, predPts)
}
# show priors on effective correlation, marginal variance, and error variance:
xs1 = seq(.01, 1, l=500)
pdf(file="Figures/standardPriorEffRange.pdf", width=5, height=5)
plot(xs1, dinvexp(xs1, rate=priorPar$corScalePar), type="l", col="blue",
xlab="Effective Correlation Range", main="Effective Correlation Prior",
ylab="Prior Density")
abline(v=qinvexp(.5, rate=priorPar$corScalePar), col="red")
dev.off()
xs2 = seq(.01, 10.5, l=500)
pdf(file="Figures/standardPriorMargVar.pdf", width=5, height=5)
plot(xs2, invgamma::dinvgamma(xs2, shape=priorPar$varPar1, rate=priorPar$varPar2), type="l", col="blue",
xlab="Marginal Variance", main="Marginal Variance Prior",
ylab="Prior Density")
abline(v=qinvgamma(.1, shape=priorPar$varPar1, rate=priorPar$varPar2), col="red")
abline(v=qinvgamma(.9, shape=priorPar$varPar1, rate=priorPar$varPar2), col="red")
dev.off()
xs2 = seq(.001, invgamma::qinvgamma(.905, shape=0.1, rate=0.1), l=500)
pdf(file="Figures/standardPriorErrorVar.pdf", width=5, height=5)
plot(xs2, invgamma::dinvgamma(xs2, shape=0.1, rate=0.1), type="l", col="blue",
xlab="Error Variance", main="Error Variance Prior",
ylab="Prior Density")
abline(v=invgamma::qinvgamma(.1, shape=0.1, rate=0.1), col="red")
abline(v=invgamma::qinvgamma(.9, shape=0.1, rate=0.1), col="red")
dev.off()
for(l in 1:nLayer) {
pdf(file=paste0("Figures/standardPriorAlpha", l, ".pdf"), width=5, height=5)
xs = seq(0, 1, l=500)
tempYs = dbeta(xs, priorPar$alphaPar[l], sum(priorPar$alphaPar[-l]))
plot(xs, tempYs, type="l", xlab=TeX(paste0("$\\alpha_", l, "$")), ylab="Density",
main=TeX(paste0("Marginal for $\\alpha_", l, "$")), xlim=c(0,1), ylim=c(0, max(tempYs[is.finite(tempYs)])))
abline(v=alphas[l], col="green")
abline(v=qbeta(c(0.025, 0.975), priorPar$alphaPar[l], sum(priorPar$alphaPar[-l])), col="purple", lty=2)
dev.off()
}
# fit the model
time = system.time(out <- fitLKINLAStandard2(coords, ys, predCoords=predPts, nu=nu, seed=seed, nLayer=nLayer, NC=NC,
nBuffer=nBuffer, priorPar=priorPar, xObs=X, xPred=XPred, normalize=normalize,
intStrategy="auto", strategy="laplace", fastNormalize=fastNormalize,
printVerboseTimings=printVerboseTimings))
mod = out$mod
preds=out$preds
predSDs=out$SDs
latInfo=out$latInfo
latWidth=out$latWidth
obsPreds=out$obsPreds
obsSDs=out$obsSDs
coefPreds = out$coefPreds
coefSDs = out$coefSDs
# print out the total time
print(paste0("Total time: ", time[3]))
# show a model summary
print(summary(mod))
# function for determining if points are in correct range
inRange = function(pts, rangeShrink=0) {
inX = (rangeShrink < pts[,1]) & (pts[,1] < 1-rangeShrink)
inY = (rangeShrink < pts[,2]) & (pts[,2] < 1-rangeShrink)
inX & inY
}
# show predictive surface, SD, and data
pdf(file="Figures/standardPreds.pdf", width=15, height=6)
if(nLayer==1) {
par(mfrow=c(2,3))
# obsInds = 1:n
# predInds = (n+1):(n+mx*my)
# coefInds = (n+mx*my+1):(n+mx*my+nx*ny)
colRangeDat = range(c(ys-errs, obsPreds, preds, coefPreds))
colRangeSD = range(c(range(predSDs[inRange(predPts)]), coefSDs[[1]][inRange(gridPtsL1)],
coefSDs[[2]][inRange(gridPtsL2)], coefSDs[[3]][inRange(gridPtsL3)]))
gridPtsL1 = latInfo[[1]]$latCoords
quilt.plot(coords, ys-errs, main="True Process", zlim=colRangeDat, xlim=xRangeDat, ylim=yRangeDat)
quilt.plot(gridPtsL1[,1], gridPtsL1[,2], coefPreds[[1]], main="Basis Coefficient Mean (Layer 1)", xlim=xRangeDat, ylim=yRangeDat)
quilt.plot(gridPtsL1[,1], gridPtsL1[,2], coefSDs[[1]], main="Basis Coefficient SD (Layer 1)", zlim=colRangeSD, xlim=xRangeDat, ylim=yRangeDat)
# quilt.plot(coords, obsPreds, main="Prediction mean", zlim=colRangeDat, xlim=xRangeDat, ylim=yRangeDat,
# zlim=range(predSDs[inRange(predPts)]))
plot.new()
quilt.plot(predPts[,1], predPts[,2], preds, main="Prediction Mean", zlim=colRangeDat,
xlim=xRangeDat, ylim=yRangeDat)
quilt.plot(predPts[,1], predPts[,2], predSDs, main="Prediction SD",
xlim=xRangeDat, ylim=yRangeDat)
}
else if(nLayer==2) {
par(mfrow=c(2,4))
# obsInds = 1:n
# predInds = (n+1):(n+mx*my)
# coefInds = (n+mx*my+1):(n+mx*my+nx*ny)
colRangeDat = range(c(ys-errs, obsPreds, preds, coefPreds))
colRangeCoef = range(c(coefPreds))
colRangeSD = range(c(predSDs, obsSDs, coefSDs))
gridPtsL1 = latInfo[[1]]$latCoords
gridPtsL2 = latInfo[[2]]$latCoords
quilt.plot(coords, ys-errs, main="True Process", zlim=colRangeDat, xlim=xRangeDat, ylim=yRangeDat)
quilt.plot(predPts[,1], predPts[,2], preds, main="Prediction Mean", zlim=colRangeDat,
xlim=xRangeDat, ylim=yRangeDat)
quilt.plot(gridPtsL1[,1], gridPtsL1[,2], coefPreds[[1]], main="Basis Coefficient Mean (Layer 1)", xlim=xRangeDat, ylim=yRangeDat)
quilt.plot(gridPtsL2[,1], gridPtsL2[,2], coefPreds[[2]], main="Basis Coefficient Mean (Layer 2)", xlim=xRangeDat, ylim=yRangeDat)
quilt.plot(coords, obsPreds, main="Observation Mean", zlim=colRangeDat, xlim=xRangeDat, ylim=yRangeDat)
quilt.plot(predPts[,1], predPts[,2], predSDs, main="Prediction SD",
xlim=xRangeDat, ylim=yRangeDat)
quilt.plot(gridPtsL1[,1], gridPtsL1[,2], coefSDs[[1]], main="Basis Coefficient SD (Layer 1)", zlim=colRangeSD, xlim=xRangeDat, ylim=yRangeDat)
quilt.plot(gridPtsL2[,1], gridPtsL2[,2], coefSDs[[2]], main="Basis Coefficient SD (Layer 2)", zlim=colRangeSD, xlim=xRangeDat, ylim=yRangeDat)
}
else if(nLayer==3) {
par(mfrow=c(2,5), mar=c(5.1, 4.1, 4.1, 6))
# obsInds = 1:n
# predInds = (n+1):(n+mx*my)
# coefInds = (n+mx*my+1):(n+mx*my+nx*ny)
# colRangeDat = range(c(ys-errs, obsPreds, preds, coefPreds))
colRangeDat = range(c(ys, obsPreds, preds, coefPreds))
colRangeCoef = range(c(coefPreds))
colRangeSD = range(c(predSDs, obsSDs, coefSDs))
gridPtsL1 = latInfo[[1]]$latCoords
gridPtsL2 = latInfo[[2]]$latCoords
gridPtsL3 = latInfo[[3]]$latCoords
quilt.plot(coords, ys-errs, main="True Process", zlim=colRangeDat, xlim=xRangeDat, ylim=yRangeDat)
quilt.plot(predPts[,1], predPts[,2], preds, main="Prediction Mean", zlim=colRangeDat,
xlim=xRangeDat, ylim=yRangeDat)
quilt.plot(gridPtsL1[,1], gridPtsL1[,2], coefPreds[[1]], main="Basis Coefficient Mean (Layer 1)",
xlim=xRangeDat, ylim=yRangeDat, zlim=colRangeCoef)
quilt.plot(gridPtsL2[,1], gridPtsL2[,2], coefPreds[[2]], main="Basis Coefficient Mean (Layer 2)",
xlim=xRangeDat, ylim=yRangeDat, zlim=colRangeCoef)
quilt.plot(gridPtsL3[,1], gridPtsL3[,2], coefPreds[[3]], main="Basis Coefficient Mean (Layer 3)",
xlim=xRangeDat, ylim=yRangeDat, zlim=colRangeCoef)
# quilt.plot(coords, obsPreds, main="Observation Mean", zlim=colRangeDat, xlim=xRangeDat, ylim=yRangeDat)
# plot.new()
quilt.plot(coords, ys, main="Observations", zlim=colRangeDat, xlim=xRangeDat, ylim=yRangeDat)
quilt.plot(predPts[,1], predPts[,2], predSDs, main="Prediction SD",
xlim=xRangeDat, ylim=yRangeDat, zlim=range(predSDs[inRange(predPts, rangeShrink=.03)]))
quilt.plot(gridPtsL1[,1], gridPtsL1[,2], coefSDs[[1]], main="Basis Coefficient SD (Layer 1)", zlim=colRangeSD, xlim=xRangeDat, ylim=yRangeDat)
quilt.plot(gridPtsL2[,1], gridPtsL2[,2], coefSDs[[2]], main="Basis Coefficient SD (Layer 2)", zlim=colRangeSD, xlim=xRangeDat, ylim=yRangeDat)
quilt.plot(gridPtsL3[,1], gridPtsL3[,2], coefSDs[[3]], main="Basis Coefficient SD (Layer 3)", zlim=colRangeSD, xlim=xRangeDat, ylim=yRangeDat)
}