-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelperFunctions.R
1376 lines (1152 loc) · 60.1 KB
/
helperFunctions.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
# Helper functions
backTrans <- function(x){ #Back-transform units - data was sqrt transformed, so logSD is actually (log(sqrt(sd)))
pow <- function(x,p) x^p
expow <- function(x,p) log(exp(x)^p)
# x$pArea$predMean <- pow(x$pArea$predMean,2)
# x$pArea$predLogSD <- expow(x$pArea$predLogSD,2)
x$coverDist <- lapply(x$coverDist,function(y){
y$predMean <- pow(y$predMean,2)
y$predLogSD <- expow(y$predLogSD,2)
return(y)
})
# x$r$predMean <- pow(x$r$predMean,2)
# x$r$predLogSD <- expow(x$r$predLogSD,2)
return(x)
}
#Scales x between 0 and 1
range01 <- function(x) (x-min(x))/(max(x)-min(x))
seqGroup <- function(x,singles=TRUE){ #Turns sequential IDs into numbered groups.
#Singles = TRUE, sequential values must be exactly 1 greater (gaps matter)
#Singles = FALSE, sequential values must be greater (gaps don't matter)
if(singles){
xl <- (x-lag(x))!=1
} else {
xl <- (x-lag(x))<0
}
cumsum(ifelse(is.na(xl),FALSE,xl))+1
}
makePolys <- function(dat,width='w',dist='d',angle='a',backwards=FALSE){
# Make polygons from width, dist, and angle measurements, centered on location from dat
gType <- dat %>% st_geometry_type(FALSE)
if(gType!='POINT') warning(paste('Input data type is',gType,'not POINT',sep=' '))
rectFun <- function(x,y,w,d,a,b){
#Function to create corners of rotated rectangle from:
# starting location (x,y),
# width (w), distance (d), and angle (a)
# rotate rectangles 180 degrees? (b)
rotate <- ifelse(b,90,270)
a <- (rotate-a)*pi/180 #90-angle in radians.
v <- c(x,y) #Starting point
v1 <- c(d*cos(a),d*sin(a)) #Vectors to add together to get corners
v2 <- c(-(w/2)*sin(a),(w/2)*cos(a))
return(rbind(v+v2,v+v1+v2,v+v1-v2,v-v2,v+v2)) #Corners of rotated rectangle
}
datCRS <- st_crs(dat) #Coordinate system from dat
#Apply rectFun to all rows in dat. Probably could be done through map or some other purrr related thing.
xcoord <- st_coordinates(dat)[,1]
ycoord <- st_coordinates(dat)[,2]
polys <- lapply(1:nrow(dat),function(i){
r <- rectFun(x = xcoord[i], y = ycoord[i], w = dat[[width]][i], d = dat[[dist]][i],
a = dat[[angle]][i], b = backwards)
st_polygon(list(r))
})
#Combine dat with new polygon geometry, and add original CRS
dat2 <- st_sf(st_drop_geometry(dat),geometry=st_sfc(polys)) %>% st_set_crs(datCRS)
return(dat2)
}
mergePoly <- function(dat,fList=NULL){
#Function to merge together completely overlapping yield polygons (cover or perfect overlap)
# takes a list of functions (fList)
# columns not mentioned are dropped
vList <- as.list(names(fList))
# doesContain <- unique(st_contains(dat)) #Deals with identical overlap
doesContain <- st_covers(dat)
#Find polygons that are overlapped by others
remove <- unique(unlist(lapply(doesContain,function(x) if(length(x)>1) x[2:length(x)] else logical(0))))
doesContain <- doesContain[-remove] #Remove polygons from list
if(length(doesContain)==0) return(dat) #If polygons completely overlap, return original data
# cMat <- table(rep(1:length(doesContain),sapply(doesContain,length)),unlist(doesContain))
# diag(cMat) <- 0
# doesContain <- doesContain[colSums(cMat)[1:nrow(cMat)]==0] #Strips out inverse overlap cases
# any(sapply(doesContain,length)>1) #Do any polygons overlap completely?
newPolys <- lapply(doesContain,function(i){
if(length(i)>1){
p1 <- dat[i,] #Dataframe with overlapping polygons
newData <- map2(vList,fList, ~ st_drop_geometry(p1) %>% summarize(across(all_of(.x),.y))) %>%
reduce(data.frame)
rownames(newData) <- paste(i,collapse='.')
newGeom <- st_union(p1) %>% st_geometry()
return(list(data=newData,geom=newGeom))
} else if(i>0) {
newData <- dat[i,unlist(vList)] %>% st_drop_geometry()
newGeom <- dat[i,] %>% st_geometry()
return(list(data=newData,geom=newGeom))
}
})
dat2 <- do.call('rbind',lapply(newPolys,function(x) x$data)) %>%
st_set_geometry(st_sfc(sapply(newPolys,function(x) x$geom),crs=st_crs(dat)))
return(dat2)
}
#Yield conversions
# Requires starting and ending units
convertYield <- function(x,inUnits=NULL,outUnits=NULL){
if(identical(inUnits,outUnits)) return(x)
#Convert to g/m2
x <- switch(inUnits,
tpha = x/100, #Tonnes/ha
gpm2 = x #g per m2
)
#Convert to output units
xOut <- switch(outUnits,
tpha = x*100,
gpm2 = x)
return(xOut)
}
#Function to get smooth predictions from a model list
#smoothLabel = label of smooth to choose
#modList = model list to use
#predRange = range to predict over
#lengthOut = number of predictions over
#postSamp = sample posterior?
#noIntercept = set intercept to 0?
#returnDF = return DF? Otherwise returns vector of predictions
getSmooths <- function(smoothLabel,modList,xvals,postSamp=FALSE,noIntercept=TRUE,returnSE=FALSE){
# #Debugging
# smoothLabel <- 's(dist)'
# postSamp <- FALSE
# noIntercept <- TRUE
# xvals <- seq(0,100,length.out=101) #x-values to use for smoother predictions
require(mgcv)
smoothLabs <- sapply(modList$smooth,function(x) x$label) #Labels for smoothers
meanSmoothList <- modList$smooth[[which(smoothLabs==smoothLabel)]] #Get smoother info
#Get locations of coefficients
whichIntercept <- ifelse(grepl('s.1',smoothLabel),2,1) #Is intercept for mean or logSD?
meanVars <- c(which(grepl('Intercept',names(modList$coefs)))[whichIntercept],meanSmoothList$first.para:meanSmoothList$last.para)
if(postSamp){ #Sample from posterior
meanCoefs <- rnorm(rep(1,length(meanVars)),modList$coefs[meanVars],sqrt(diag(modList$vcv)[meanVars]))
} else { #Get ML estimate
meanCoefs <- unname(modList$coefs[meanVars])
}
#Marginalize across intercept (set intercept coef to 0)
if(noIntercept) meanCoefs[1] <- 0
if('matrix' %in% class(xvals)){ #If xvals is a matrix (2D smooth)
predDF <- data.frame(x=xvals[,1],y=xvals[,2]) #Dataframe for holding predictions
} else { #If not (1D smooth)
predDF <- data.frame(x=xvals) #Dataframe for holding predictions
}
names(predDF) <- meanSmoothList$term #Name of terms
if(meanSmoothList$by!='NA'){ #If using a by variable
predDF$by <- factor(meanSmoothList$by.level)
names(predDF)[2] <- meanSmoothList$by
}
#Model matrix
predMat <- PredictMat(meanSmoothList,data=predDF) #Basis function columns
predMat <- cbind(rep(1,nrow(predMat)),predMat) #Intercept column
predDF$pred <- predMat %*% meanCoefs #Get predictions (coef %*% model matrix)
if(returnSE){ #Gets SE of prediction at each xval - from plot.gam code
predDF$se <- sqrt(pmax(0,rowSums(predMat %*% modList$vcv[meanVars,meanVars] * predMat)))
}
return(predDF)
}
#Function to extract predictions from modList.Rdata at specified path
# l = number of replicates in each smooth prediction (length.out)
# margInt = marginalize across intercepts
# samp = sample from posterior distribution of coefficients rather than using the mean
getPreds <- function(path,l=c(30,100,500),margInt=c(FALSE,FALSE,FALSE),samp=FALSE,reps=1){
# #Debugging
# l <- c(30,100,500)
# # path <- paste0('./Figures/ModelCheck/',datSource$filename[2],' modList.Rdata') #Model type 1
# path <- paste0('./Figures/ModelCheck2/',datSource$filename[13],' modList.Rdata') #Model type 2
# margInt <- c(FALSE,TRUE,TRUE)
# samp <- FALSE
require(mgcv)
load(path) #Load data
# #Polygon area (basically speed) regression
# logPArea <- seq(log(modList$pAreaRange[1]),log(modList$pAreaRange[2]),length.out=l[1])
# pArea <- exp(logPArea)
retList <- lapply(1:reps,function(i){
#Indices for mean/sd intercepts
meanVars <- which(grepl('(^\\(Intercept\\)$|^log\\(pArea\\)$)',names(modList$coefs)))
sdVars <- which(grepl('(^\\(Intercept\\)\\.1$|^log\\(pArea\\)\\.1$)',names(modList$coefs)))
#Coefficients (intercept and slope) for mean and logSD relationship
if(samp){ #Sample from posterior
meanCoefs <- rnorm(rep(1,length(meanVars)),modList$coefs[meanVars],sqrt(diag(modList$vcv)[meanVars]))
sdCoefs <- rnorm(rep(1,length(meanVars)),modList$coefs[sdVars],sqrt(diag(modList$vcv)[sdVars]))
} else {
meanCoefs <- modList$coefs[meanVars]
sdCoefs <- modList$coefs[sdVars]
}
if(margInt[1]){ #Marginalize across intercept (set intercept coef to 0)
meanCoefs[1] <- 0
sdCoefs[1] <- 0
}
# pAreaDat <- data.frame(pArea,
# mean=cbind(rep(1,length(logPArea)),logPArea) %*% meanCoefs,
# logSD=cbind(rep(1,length(logPArea)),logPArea) %*% sdCoefs)
pAreaDat <- NA
# Field boundary distance smoothers
#Figure out if model has only 1 type of distance (model type 1) or multiple distances (model type 2)
#If first type, returns dataframe, otherwise a list of dataframes
type1 <- !any(grepl('(dist_|\\:)',sapply(modList$smooths,function(x) x$label)))
if(type1){
dRange <- with(modList$smooths[[which(sapply(modList$smooths,function(x) x$label)=='s(dist)')]],
range(Xu)+rep(shift,2)) #Get range from smooths
d <- seq(dRange[1],dRange[2],length.out=l[2]) #x values to sample from
distDat <- data.frame(dist=d,
mean=getSmooths(smoothLabel='s(dist)', modList=modList, xvals = d,postSamp=samp, noIntercept=margInt[2])$pred,
logSD=getSmooths(smoothLabel='s.1(dist)', modList=modList, xvals = d,postSamp=samp, noIntercept=margInt[2])$pred)
} else { #If using different types of distances
#Get names of different distances
distTypes <- unique(gsub('(\\)|s\\(|s.1\\()','',sapply(modList$smooths,function(x) x$label)))
distTypes <- distTypes[grepl('dist',distTypes)]
distDat <- lapply(distTypes,function(dType){
if(grepl('\\:',dType)){ #If smoother used "by" (rather than separate distance columns)
meanLab <- gsub('dist','s(dist)',dType) #Gets appropriate text labels for smoothers
sdLab <- gsub('dist','s.1(dist)',dType) #Gets appropriate text labels for smoothers
} else {
meanLab <- paste0('s(',dType,')') #Gets appropriate text labels for smoothers
sdLab <- paste0('s.1(',dType,')')
}
dRange <- with(modList$smooths[[which(sapply(modList$smooths,function(x) x$label)==meanLab)]],
range(Xu)+rep(shift,2)) #Get range from smooths
d <- seq(dRange[1],dRange[2],length.out=l[2]) #x values values to sample from
distDat <- data.frame(dist=d, #
mean=getSmooths(smoothLabel=meanLab, modList=modList, xvals = d,postSamp=samp, noIntercept=margInt[2])$pred,
logSD=getSmooths(smoothLabel=sdLab, modList=modList, xvals = d,postSamp=samp, noIntercept=margInt[2])$pred)
return(distDat)
})
names(distDat) <- distTypes
}
# #Point order (time of combining) smoothers
# rRange <- with(modList$smooths[[which(sapply(modList$smooths,function(x) x$label)=='s(r)')]],
# range(Xu)+rep(shift,2)) #Get range from smooths
# r <- seq(rRange[1],rRange[2],length.out=l[3]) #r values to sample from
#
# rDat <- data.frame(r=r,
# mean=getSmooths(smoothLabel='s(r)', modList=modList, xvals = r, postSamp=samp, noIntercept=margInt[3])$pred,
# logSD=getSmooths(smoothLabel='s.1(r)', modList=modList, xvals = r, postSamp=samp, noIntercept=margInt[3])$pred)
rDat <- NA
#Assemble into list
datList <- list(pAreaDat=pAreaDat,distDat=distDat,rDat=rDat)
return(datList)
})
if(reps==1){
return(retList[[1]])
} else {
return(retList)
}
}
# debugonce(getPreds)
# (temp <- getPreds(paste0('./Figures/ModelCheck/',datSource$filename[2],' modList.Rdata'),margInt=c(FALSE,TRUE,TRUE),samp=FALSE)) #Test with first type of model
# temp2 <- getSmooths(paste0('./Figures/ModelCheck/',datSource$filename[1],' modList.Rdata'),margInt=c(FALSE,TRUE,TRUE),samp=TRUE) #Test
#
# ggplot()+geom_line(data=temp$pAreaDat,aes(x=pArea,y=mean))+ #Works for this (I think)
# geom_line(data=temp2$pAreaDat,aes(x=pArea,y=mean),linetype='dashed',col='red')
#
# ggplot()+geom_line(data=temp$dist,aes(x=dist,y=mean))+ #Works for this
# geom_line(data=temp2$dist,aes(x=dist,y=mean),linetype='dashed',col='red')
#
# ggplot()+geom_line(data=temp$r,aes(x=r,y=mean))+ #Works for this
# geom_line(data=temp2$r,aes(x=r,y=mean),linetype='dashed',col='red')
# (temp <- getPreds(paste0('./Figures/ModelCheck2/',datSource$filename[2],' modList.Rdata'),margInt=c(FALSE,TRUE,TRUE),samp=FALSE)) #Test with second type of model
#Function to run the ith model, using datSource ds, and sampling below nSubSamp points if necessary
#kPar = basis dimensions for distance, E/N, and time smoothers + basis dimensions for logSD smoothers
runModI <- function(i,dS,nSubSamp=50000,kPar=c(12,60,60,12,60,60),modelCheckDir='./Figures/ModelCheck1',resultsDir='./Figures/YieldMaps',filterData=TRUE){
if(dS$modelComplete[i]) return('Already completed')
if(!dS$use[i]) return('Not used')
require(tidyverse)
theme_set(theme_bw())
require(mgcv)
require(sf)
require(ggpubr)
source('helperFunctions.R')
csvPath <- dS$dataPath[i] #Path to data
fieldName <- dS$filename[i] #Field/year ID
boundaryPath <- dS$boundaryPath2[i] #Path to boundary shapefile
cropType <- dS$crop[i] #Crop type
print('Reading in data')
dat <- read.csv(csvPath,stringsAsFactors=TRUE,fileEncoding='latin1')
#Takes 40 seconds with subsamp of 50000 using full 800000 samples from Alvin French's Al Jr Field
dat <- dat %>% rename_with(.fn = ~gsub('..L.ha.$','_lHa',.x)) %>%
rename_with(.fn = ~gsub('..tonne.ha.$','_tHa',.x)) %>%
rename_with(.fn = ~gsub('..m.s.$','_ms',.x)) %>%
rename_with(.fn = ~gsub('.km.h.$','_kmh',.x)) %>%
rename_with(.fn = ~gsub('..tonne.h.$','_th',.x)) %>%
rename_with(.fn = ~gsub('.ha.h.','_hah',.x)) %>%
rename_with(.fn = ~gsub('.m.','_m',.x)) %>%
rename_with(.fn = ~gsub('.deg.','Angle',.x)) %>%
rename_with(.fn = ~gsub('\\.','',.x)) %>%
rename('ID'='ObjId','DryYield'='YldMassDry_tHa','Lon'='Longitude','Lat'='Latitude','Pass'='PassNum','Speed'='Speed__m') %>%
st_as_sf(coords=c('Lon','Lat')) %>% #Add spatial feature info
st_set_crs(4326) %>% st_transform(3401) %>% #Lat-lon -> UTM
makePolys(width='SwthWdth_m',dist='Distance_m',angle='TrackAngle') %>%
mutate(pArea=as.numeric(st_area(.))) %>% #Area of polygon
st_centroid() %>% #Convert back to point
mutate(r=1:n()) %>% #row number
mutate(Pass=factor(seqGroup(ID,FALSE))) %>%
group_by(Pass) %>% mutate(rGroup=1:n()) %>% ungroup() %>%
mutate(E=st_coordinates(.)[,1],N=st_coordinates(.)[,2]) %>%
mutate(E=E-mean(E),N=N-mean(N)) #Center coordinates
print('Filtering data')
#Filter data
dat <- dat %>% mutate(vegaFilt = vegaFilter(.,DryYield,nDist = 30)) #Vega et al 2019 spatial "inlier" filter - takes about a minute
dat <- dat %>%
mutate(noBS = DryYield<ifelse(cropType=='Wheat',10.75,8)) %>% #JP recommends maximum filters of 160 bu (10.75 T/ha) for wheat, 120 bu (8 T/ha) for peas & canola
mutate(Qfilt = QuantileFilter(DryYield,q=0.98)) %>% #Trim dry yield outliers
mutate(bFilt = bearingFilter(TrackAngle,q=0.98)) %>% #Trim extreme bearing changes (turning)
mutate(speedFilt = QuantileFilter(Speed,q=0.98)) %>% #Trim absolute speed outliers
mutate(dSpeedFilt = dSpeedFilter(Speed,l=c(-2,-1,1,2),perc = 0.2)) %>% #Trim speed differences (>20% change 2 steps forward and backward, suggested by Lyle et al 2014)
mutate(posFilt = posFilter(.,q=0.98)) %>% #Trim points that are far away from eachother
#Combine filter criteria
mutate(allFilt = noBS & vegaFilt & Qfilt & bFilt & speedFilt & dSpeedFilt & posFilt) %>%
filter(allFilt)
if(nrow(dat)>nSubSamp){ #Limit to nSubSamp sequential samples if too large
dat <- dat %>% slice(round(seq(1,nrow(dat),length.out=nSubSamp)))
}
print('Calculating distance from boundary')
fieldEdge <- read_sf(boundaryPath) %>% st_cast('MULTILINESTRING') #Read in boundary file
# fieldEdge <- dat %>% st_union() %>% st_buffer(dist=10) %>% st_cast('MULTILINESTRING') #Old method
dat <- dat %>% #Distance from boundary
mutate(dist=as.numeric(st_distance(.,fieldEdge))[1:nrow(.)])
print('Fitting model')
#Fit non-isotropic GAM:
f <- sqrt(DryYield) ~ s(dist,k=kPar[1]) + s(E,N,k=kPar[2]) + s(r,k=kPar[3]) + log(pArea) #Mean model
f2 <- ~ s(dist,k=kPar[4]) + s(E,N,k=kPar[5]) + s(r,k=kPar[6]) + log(pArea) #Variance model
flist <- list(f,f2) #List of model formulae
#Fit Gaussian location-scale model
#NOTE: this model can't be run using bam because of location-scale modeling
a <- Sys.time() #Takes about 8 mins for a 50000 point model
mod <- gam(flist,data=dat,family=gaulss())
fitTime <- paste(as.character(round(Sys.time()-a,2)),units(Sys.time()-a)) #Time taken to fit model
#Model with dist(10) E,N(60) r(30); s.1dist(6) s.1E,N(60) s.1r(60) - Has a very large amount of temporal/spatial autocorrelation, but only takes 6 mins or so
#Trying with dist(12) E,N(80) r(80); s.1dist(12) s.1E,N(80) s.1r(80) - Similar temporal/spatial autocorrelation, but takes 4 times as long, and gives roughly the same answer
print('Saving model results')
#Plot results - FAILS HERE WHEN ALL.TERMS=TRUE. SOME KIND OF GAM NAMESPACE PROBLEM THAT ONLY OCCURS WITHIN FUNCTIONS
#https://stackoverflow.com/questions/45918662/plot-gam-from-mgcv-with-all-terms-true-within-a-function
png(paste0(modelCheckDir,'/',fieldName,' Summary.png'),width=12,height=8,units='in',res=200)
# plot(mod,scheme=2,too.far=0.01,pages=1,all.terms=TRUE)
plot(mod,scheme=2,too.far=0.01,pages=1,all.terms=FALSE)
dev.off()
capture.output({ #Model summary
print("Time taken: "); print(fitTime)
print(" ")
print("SUMMARY---------------------------------")
summary(mod)
},file=paste0(modelCheckDir,'/',fieldName,' results.txt'))
capture.output({ #GAM check results
print(" ")
print("GAM.CHECK-------------------------------")
png(paste0(modelCheckDir,'/',fieldName,' gamCheck.png'),width=8,height=8,units='in',res=200)
par(mfrow=c(2,2)); gam.check(mod); abline(0,1,col='red'); par(mfrow=c(1,1))
dev.off()
},file=paste0(modelCheckDir,'/',fieldName,' results.txt'),append=TRUE)
#Actual, Predicted, and SE maps
print('Making yield map')
yieldMap <- dat %>% filter(DryYield<quantile(DryYield,0.95)) %>% #Chop out upper 5% of data (mainly crazy high numbers)
ggplot() +
geom_sf(data=dat,col='grey',size=1)+
geom_sf(aes(col=DryYield),alpha=0.3) +
geom_sf(data=fieldEdge,col='magenta')+ #Field boundary
labs(title=paste(fieldName,'Data')) + #guides(alpha='none') +
scale_colour_distiller(type='div',palette = "Spectral") +
theme(legend.position='bottom')
# dat <- dat %>% mutate(yieldPred=mod$fit[,1],yieldVar=mod$fit[,2],resid=resid(mod))
#Marginalize across r (point order) and pArea (speed)
pred <- dat %>%
mutate(pArea=median(pArea)) %>% #Set pArea to its median value
predict(mod,newdata = .,type='response',exclude='s(r)') #Exclude s(r)
# mutate(r=1,pArea=median(pArea)) %>% #Sets r to 1, pArea to median
# predict(mod,newdata = .,type='response')
# pred2 <- dat %>%
# # mutate(r=1,pArea=mean(pArea)) %>%
# predict(mod,newdata = .,se.fit=TRUE)
#
# predVals <- cbind(sapply(pred,function(x) x[,1]),sapply(pred2,function(x) x[,1])) %>% as.data.frame()
# colnames(predVals) <- c('mean1','se1','mean2','se2')
#
# predVals %>% pivot_longer(everything()) %>% mutate(par=ifelse(grepl('mean',name),'mean','se')) %>%
# mutate(name=ifelse(grepl('1',name),'type1','type2')) %>%
# ggplot()+geom_histogram(aes(x=value))+facet_grid(name~par,scales='free_x')
dat <- dat %>% mutate(yieldMean=pred[,1],yieldSD=1/pred[,2],resid=resid(mod))
#Predicted yield, marginalizing across pArea and r
p1 <- ggplot(dat)+geom_sf(aes(col=yieldMean^2),alpha=0.3)+
labs(title='Predicted yield | Combine Speed, Point Order',fill='Yield (T per Ha)',col='Yield (T per Ha)')+
scale_colour_distiller(type='div',palette = "Spectral",direction=-1)+theme(legend.position='bottom')
#Predicted SD
p2 <- ggplot(dat)+geom_sf(aes(col=yieldSD^2),alpha=0.3)+
labs(title='Yield SD | Combine Speed, Point Order',fill='SD Yield ',col='SD Yield')+
scale_colour_distiller(type='div',palette = "Spectral",direction=-1)+theme(legend.position='bottom')
p <- ggarrange(yieldMap,p1,p2,ncol=3)
ggsave(paste0(resultsDir,'/',fieldName,'.png'),p,height=6,width=15,dpi=100)
#Residual plots/covariance plots
p3 <- ggplot(dat)+geom_sf(aes(col=resid),alpha=0.5)+labs(title='Residuals (space)')+
scale_colour_distiller(type='div',palette = "Spectral")+theme(legend.position='right')
p4 <- ggplot(dat)+geom_point(aes(x=r,y=resid),alpha=0.5)+labs(title='Residuals (sequence)',x='Sequence',y='Residuals')+
geom_hline(yintercept=0,col='red',linetype='dashed')
#STFDF does ST empirical covariance plots well, but STIDF doesn't work for some reason
# Also, there isn't any spatial replication across time, so not sure this matters
library(gstat)
library(spacetime)
dat_sp <- as_Spatial(select(dat,ID,resid))
v <- variogram(resid~1, data=dat_sp,width=5,cutoff=300) #Variogram
# vmod <- fit.variogram(v,vgm('Mat'),fit.kappa=TRUE) #Fit Matern covariance model
# plot(v,vmod)
p5 <- v %>%
ggplot()+geom_point(aes(x=dist,y=gamma))+
labs(title='Residual variogram',x='Distance',y='Semivariance')
# dat_variogram2 <- gstat::variogram(resid~1, #Used to look at non-stationary semivariance
# data=dat_sp,width=5,cutoff=150,alpha=c(0,45,90,135)) #North, NE, E, SW
res_acf <- acf(dat$resid,lag.max=300,type='correlation',plot=FALSE)
p6 <- with(res_acf,data.frame(acf=acf,lag=lag)) %>%
ggplot(aes(x=lag,y=acf))+geom_col()+
labs(x='Time Lag',y='Autocorrelation',title='Residual autocorrelation')
p <- ggarrange(p3,p4,p5,p6,ncol=2,nrow=2)
p <- annotate_figure(p,top = text_grob(paste0(fieldName,' Residuals')))
ggsave(paste0(modelCheckDir,'/',fieldName,' residuals.png'),p,height=8,width=10,dpi=100)
#Models are huge, so saving only key components (about 25% of the size)
modList <- list(cropType=cropType, #Crop type
coefs=coef(mod), #Coefficients
vcv=vcov(mod), #Covariance matrix
smooths=mod$smooth, #Smooth specifications
distRange=range(dat$dist), #Range of distances
pAreaRange=range(dat$pArea), #Range of polygon areas
rRange=range(dat$r)) #Range of r
save(modList,file=paste0(modelCheckDir,'/',fieldName,' modList.Rdata'))
print('Done')
gc()
# #Example of reconstructing basis function matrix from new data
# data.frame(dist=seq(0,100,5),
# pred=PredictMat(mod$smooth[[1]],data=data.frame(dist=seq(0,100,5))) %*% coef(mod)[c(mod$smooth[[1]]$first.para:mod$smooth[[1]]$last.para)]) %>%
# ggplot()+geom_point(aes(x=dist,y=pred))
#
# x <- seq(0,100,5)
# betas <- coef(mod)[c(mod$smooth[[1]]$first.para:mod$smooth[[1]]$last.para)] #Coefficients
# p <- PredictMat(mod$smooth[[1]],data=data.frame(dist=seq(0,100,5))) #Matrix of basis functions
#
# par(mfrow=c(2,1))
# plot(0,0,type='n',xlim=range(x),ylim=range(p),xlab='Dist',ylab='y',main='Unscaled basis functions') #Unscaled basis functions
# for(i in 1:ncol(p)) lines(x,p[,i],col=i)
# #Scaled basis functions
# plot(0,0,type='n',xlim=range(x),ylim=range(p*outer(rep(1,nrow(p)),betas)),xlab='Dist',ylab='y',main='Scaled basis functions')
# for(i in 1:ncol(p)) lines(x,p[,i]*betas[i],col=i)
# lines(x,p%*%betas,lwd=3)
# par(mfrow=c(1,1))
}
#Function to run the ith model, but with boundary type
#useClosest = use only closest boundary? Otherwise, uses distance from all boundaries
runModII <- function(i,dS,nSubSamp=50000,kPar=c(5,80,5,80),useClosest=TRUE,modelCheckDir='./Figures/ModelCheck2',resultsDir='./Figures/YieldMaps2',filterData=TRUE){
# i <- 295 #Debugging - "Trent_Clark W 34 2019"
# i <- 5 #Alvin French Zoltan - 1.2 million points
# dS <- datSource
# nSubSamp <- 50000
# useClosest <- TRUE
# kPar <- c(12,60,60,12,60,60)
# filterData <- TRUE
if(dS$modelComplete2[i]) return('Already completed')
if(!dS$use[i]) return('Not used')
require(tidyverse)
theme_set(theme_bw())
require(mgcv)
require(sf)
require(ggpubr)
source('helperFunctions.R')
csvPath <- dS$dataPath[i] #Path to data
fieldName <- dS$filename[i] #Field/year ID
boundaryPath <- dS$boundaryPath2[i] #Path to boundary shapefile
cropType <- dS$crop[i] #Crop type
print('Reading in data')
dat <- read.csv(csvPath,stringsAsFactors=TRUE,fileEncoding='latin1')
uprLim <- 150000 #Upper limit of 150,000 points (takes about 6 mins to do spatial inlier filtering)
if(nrow(dat)>uprLim){
dat <- dat %>% slice(round(seq(1,nrow(dat),length.out=uprLim)))
}
#Takes 40 seconds with subsamp of 50000 using full 800000 samples from Alvin French's Al Jr Field
dat <- dat %>% rename_with(.fn = ~gsub('..L.ha.$','_lHa',.x)) %>%
rename_with(.fn = ~gsub('..tonne.ha.$','_tHa',.x)) %>%
rename_with(.fn = ~gsub('..m.s.$','_ms',.x)) %>%
rename_with(.fn = ~gsub('.km.h.$','_kmh',.x)) %>%
rename_with(.fn = ~gsub('..tonne.h.$','_th',.x)) %>%
rename_with(.fn = ~gsub('.ha.h.','_hah',.x)) %>%
rename_with(.fn = ~gsub('.m.','_m',.x)) %>%
rename_with(.fn = ~gsub('.deg.','Angle',.x)) %>%
rename_with(.fn = ~gsub('\\.','',.x)) %>%
rename('ID'='ObjId','DryYield'='YldMassDry_tHa','Lon'='Longitude','Lat'='Latitude','Pass'='PassNum','Speed'='Speed__m') %>%
st_as_sf(coords=c('Lon','Lat')) %>% #Add spatial feature info
st_set_crs(4326) %>% st_transform(3401) %>% #Lat-lon -> UTM
# makePolys(width='SwthWdth_m',dist='Distance_m',angle='TrackAngle') %>%
# mutate(pArea=as.numeric(st_area(.))) %>% #Area of polygon
# st_centroid() %>% #Convert back to point
mutate(pArea=SwthWdth_m*Distance_m) %>% #Area of polygon
mutate(r=1:n()) %>% #row number
mutate(Pass=factor(seqGroup(ID,FALSE))) %>%
group_by(Pass) %>% mutate(rGroup=1:n()) %>% ungroup() %>%
mutate(E=st_coordinates(.)[,1],N=st_coordinates(.)[,2]) %>%
mutate(E=E-mean(E),N=N-mean(N)) #Center coordinates
print('Filtering data')
NrawDat <- nrow(dat)
#Filter data
dat <- dat %>%
#Vega et al 2019 spatial "inlier" filter - takes about a minute
mutate(vegaFilt = vegaFilter(.,DryYield,nDist = 30))
dat <- dat %>%
#BS filter - JP recommends maximum filters of 160 bu (10.75 T/ha) for wheat, 120 bu (8 T/ha) for peas & canola
mutate(noBS = DryYield<ifelse(cropType=='Wheat',10.75,8)) %>%
mutate(Qfilt = QuantileFilter(DryYield,q=0.98)) %>% #Trim dry yield outliers
mutate(bFilt = bearingFilter(TrackAngle,q=0.98)) %>% #Trim extreme bearing changes (turning)
mutate(speedFilt = QuantileFilter(Speed,q=0.98)) %>% #Trim absolute speed outliers
#Trim speed differences (>20% change 2 steps forward and backward, suggested by Lyle et al 2014)
mutate(dSpeedFilt = dSpeedFilter(Speed,l=c(-2,-1,1,2),perc = 0.2)) %>%
mutate(posFilt = posFilter(.,q=0.98)) %>% #Trim points that are far away from eachother
#Combine filter criteria
mutate(allFilt = noBS & vegaFilt & Qfilt & bFilt & speedFilt & dSpeedFilt & posFilt) %>%
filter(allFilt)
NfiltDat <- nrow(dat)
if(NfiltDat>nSubSamp){ #Limit to nSubSamp sequential samples if too large
dat <- dat %>% slice(round(seq(1,nrow(dat),length.out=nSubSamp)))
print(paste0('Limiting analysis to ',nSubSamp,' data points'))
}
print('Calculating distance from boundary')
fieldEdge <- read_sf(boundaryPath) %>% st_cast('MULTILINESTRING') #Read in boundary file
boundaryTypes <- unique(fieldEdge$type) #Get boundary types
if(useClosest){
#Get distance and type of closest boundary
dat <- dat %>%
bind_cols(.,data.frame(t(apply(st_distance(dat,fieldEdge),1,function(x) c(dist=min(x,na.rm=TRUE),boundaryType=fieldEdge$type[which.min(x)]))))) %>%
mutate(dist=as.numeric(dist),boundaryType=factor(boundaryType))
} else {
#Get distance columns for each boundary type
dist <- t(apply(st_distance(dat,fieldEdge),1,function(x) tapply(x,fieldEdge$type,min))) %>%
data.frame() %>% rename_with(~paste0('dist_',.x))
dat <- dat %>% bind_cols(.,dist) #Bind distance columns to data
}
print('Fitting model')
#Make model formula for non-isotropic gam
if(useClosest){
f2 <- paste0('~ s(dist,k=',kPar[1],',bs="ts",by=boundaryType) + s(E,N,k=',kPar[2],')')
f <- paste0('sqrt(DryYield)~ s(dist,k=',kPar[3],',bs="ts",by=boundaryType) + s(E,N,k=',kPar[4],')')
} else {
f2 <- paste0('~ ',paste0('s(dist_',boundaryTypes,',k=',kPar[1],',bs="ts")',collapse=' + '),' + s(E,N,k=',kPar[2],')')
f <- paste0('sqrt(DryYield) ~ ',paste0('s(dist_',boundaryTypes,',k=',kPar[3],',bs="ts")',collapse=' + '),' + s(E,N,k=',kPar[4],')')
}
# if(useClosest){ #These ones include a time smoother
# f2 <- paste0('~ s(dist,k=',kPar[1],',bs="ts",by=boundaryType) + s(E,N,k=',kPar[2],') + s(r,k=',kPar[3],')')
# f <- paste0('sqrt(DryYield)~ s(dist,k=',kPar[4],',bs="ts",by=boundaryType) + s(E,N,k=',kPar[5],') + s(r,k=',kPar[6],')')
# } else {
# f2 <- paste0('~ ',paste0('s(dist_',boundaryTypes,',k=',kPar[1],',bs="ts")',collapse=' + '),' + s(E,N,k=',kPar[2],') + s(r,k=',kPar[3],')')
# f <- paste0('sqrt(DryYield) ~ ',paste0('s(dist_',boundaryTypes,',k=',kPar[4],',bs="ts")',collapse=' + '),' + s(E,N,k=',kPar[5],') + s(r,k=',kPar[6],')')
# }
flist <- list(as.formula(f),as.formula(f2)) #List of model formulae
#Fit Gaussian location-scale model
a <- Sys.time() #Takes about 20 mins for a 50000 point model
mod <- gam(flist,data=dat,family=gaulss())
fitTime <- paste(as.character(round(Sys.time()-a,2)),units(Sys.time()-a)) #Time taken to fit model
#Other models:
# mod2 <- gam(as.formula(f),data=dat) #Standard model (constant variance)
# f2 <- paste0('~ s(dist,k=',kPar[1],',bs="ts",by=boundaryType) + s(E,N,k=',kPar[2],')')
# f <- paste0('sqrt(DryYield)~ s(dist,k=',kPar[4],',bs="ts",by=boundaryType) + s(E,N,k=',kPar[5],')')
# flist <- list(as.formula(f),as.formula(f2)) #List of model formulae
# mod3 <- gam(flist,data=dat,family=gaulss()) #Model without time smoothers
# mod4 <- gam(as.formula(f),data=dat) #Standard model without time smoother
print('Saving model results')
png(paste0(modelCheckDir,'/',fieldName,' Summary.png'),width=12,height=8,units='in',res=200)
# plot(mod,scheme=2,too.far=0.01,pages=1,all.terms=TRUE)
plot(mod,scheme=2,too.far=0.01,pages=1,all.terms=FALSE)
dev.off()
capture.output({ #Model summary
print("Time taken: "); print(fitTime)
print(" ")
print(paste0('Filtered out ',NrawDat-NfiltDat,' of ',NrawDat,' data points'))
if(NfiltDat>nSubSamp) print(paste0('Limiting analysis to ',nSubSamp,' data points'))
print(" ")
print("SUMMARY---------------------------------")
summary(mod)
},file=paste0(modelCheckDir,'/',fieldName,' results.txt'))
capture.output({ #GAM check results
print(" ")
print("GAM.CHECK-------------------------------")
png(paste0(modelCheckDir,'/',fieldName,' gamCheck.png'),width=8,height=8,units='in',res=200)
par(mfrow=c(2,2)); gam.check(mod); abline(0,1,col='red'); par(mfrow=c(1,1))
dev.off()
},file=paste0(modelCheckDir,'/',fieldName,' results.txt'),append=TRUE)
#Actual, Predicted, and SE maps
print('Making yield map')
yieldMap <- dat %>% filter(DryYield<quantile(DryYield,0.95)) %>% #Chop out upper 5% of data (mainly crazy high numbers)
ggplot() +
geom_sf(data=dat,col='grey',size=1)+
geom_sf(aes(col=DryYield),alpha=0.3) +
geom_sf(data=fieldEdge,col='black')+ #Field boundary
labs(title=paste(fieldName,'Raw Data'),colour='Yield (T/ha)') + #guides(alpha='none') +
scale_colour_distiller(type='div',palette = "Spectral") +
theme(legend.position='bottom')
#Marginalize across r (point order)
pred <- dat %>%
predict(mod,newdata = .,type='response',exclude='s(r)') #Exclude s(r)
dat <- dat %>% mutate(yieldMean=pred[,1],yieldSD=1/pred[,2],resid=resid(mod))
#Predicted yield, marginalizing across pArea and r
p1 <- ggplot(dat)+geom_sf(aes(col=yieldMean^2),alpha=0.3)+
labs(title='Predicted Mean | Point Order',fill='Yield (T/ha)',col='Yield (T/ha)')+
scale_colour_distiller(type='div',palette = "Spectral",direction=-1)+
geom_sf(data=fieldEdge,col='black')+ #Field boundary
theme(legend.position='bottom')
#Predicted SD
p2 <- ggplot(dat)+geom_sf(aes(col=yieldSD^2),alpha=0.3)+
labs(title='Predicted SD | Point Order',fill='SD Yield ',col='SD Yield')+
scale_colour_distiller(type='div',palette = "Spectral",direction=-1)+
geom_sf(data=fieldEdge,col='black')+ #Field boundary
theme(legend.position='bottom')
p <- ggarrange(yieldMap,p1,p2,ncol=3)
ggsave(paste0(resultsDir,'/',fieldName,'.png'),p,height=6,width=15,dpi=100)
#Residual plots/covariance plots
p3 <- ggplot(dat)+geom_sf(aes(col=resid),alpha=0.5)+labs(title='Residuals (space)')+
scale_colour_distiller(type='div',palette = "Spectral")+theme(legend.position='right')
p4 <- ggplot(dat)+geom_point(aes(x=r,y=resid),alpha=0.5)+labs(title='Residuals (sequence)',x='Sequence',y='Residuals')+
geom_hline(yintercept=0,col='red',linetype='dashed')
#STFDF does ST empirical covariance plots well, but STIDF doesn't work for some reason
# Also, there isn't any spatial replication across time, so not sure this matters
library(gstat)
library(spacetime)
dat_sp <- as_Spatial(select(dat,ID,resid))
v <- variogram(resid~1, data=dat_sp,width=5,cutoff=100) #Variogram
vmod <- fit.variogram(v,vgm('Exp'),fit.kappa=TRUE) #Fit Matern covariance model
# plot(v,vmod)
p5 <- v %>%
ggplot()+geom_point(aes(x=dist,y=gamma))+
geom_line(dat=variogramLine(vmod,maxdist = 100,min = 1,n=100),aes(x=dist,y=gamma))+
labs(title='Residual variogram',x='Distance',y='Semivariance')
# dat_variogram2 <- gstat::variogram(resid~1, #Used to look at non-stationary semivariance
# data=dat_sp,width=5,cutoff=150,alpha=c(0,45,90,135)) #North, NE, E, SW
res_acf <- acf(dat$resid,lag.max=100,type='correlation',plot=FALSE)
p6 <- with(res_acf,data.frame(acf=acf,lag=lag)) %>%
ggplot(aes(x=lag,y=acf))+geom_col()+
labs(x='Time Lag',y='Autocorrelation',title='Residual autocorrelation')
p <- ggarrange(p3,p4,p5,p6,ncol=2,nrow=2)
ggsave(paste0(modelCheckDir,'/',fieldName,' residuals.png'),p,height=8,width=10,dpi=100)
#Models are huge, so saving only key components (about 25% of the size)
if(useClosest){ #Range of distances
distRange <- with(dat,tapply(Distance_m,boundaryType,range))
} else {
distRange <- st_drop_geometry(dat) %>% select(contains('dist_')) %>% summarize(across(everything(),range)) %>% data.frame()
}
modList <- list(cropType=cropType, #Crop type
boundaryTypes=boundaryTypes, #Boundary types
coefs=coef(mod), #Coefficients
vcv=vcov(mod), #Covariance matrix
smooths=mod$smooth, #Smooth specifications
distRange=distRange,
pAreaRange=range(dat$pArea), #Range of polygon areas
rRange=range(dat$r)) #Range of r
save(modList,file=paste0(modelCheckDir,'/',fieldName,' modList.Rdata'))
print('Done')
gc()
}
#Function to run the ith model, but with no boundary included ("null" model)
runMod0 <- function(i,dS,nSubSamp=50000,kPar=c(60,60,60,60),useClosest=TRUE,modelCheckDir='./Figures/ModelCheck0',resultsDir='./Figures/YieldMaps0',startCoefs=NULL,filterData=TRUE){
i <- 1 #Debugging
dS <- datSource
nSubSamp <- 50000
useClosest <- TRUE
kPar <- c(60,60,60,60)
startCoefs <- NULL
startCoefs <- modList$coefs
if(dS$modelComplete0[i]) return('Already completed')
if(!dS$use[i]) return('Not used')
require(tidyverse)
theme_set(theme_bw())
require(mgcv)
require(sf)
require(ggpubr)
source('helperFunctions.R')
csvPath <- dS$dataPath[i] #Path to data
fieldName <- dS$filename[i] #Field/year ID
boundaryPath <- dS$boundaryPath2[i] #Path to boundary shapefile
cropType <- dS$crop[i] #Crop type
print('Reading in data')
dat <- read.csv(csvPath,stringsAsFactors=TRUE,fileEncoding='latin1')
#Takes 40 seconds with subsamp of 50000 using full 800000 samples from Alvin French's Al Jr Field
dat <- dat %>% rename_with(.fn = ~gsub('..L.ha.$','_lHa',.x)) %>%
rename_with(.fn = ~gsub('..tonne.ha.$','_tHa',.x)) %>%
rename_with(.fn = ~gsub('..m.s.$','_ms',.x)) %>%
rename_with(.fn = ~gsub('.km.h.$','_kmh',.x)) %>%
rename_with(.fn = ~gsub('..tonne.h.$','_th',.x)) %>%
rename_with(.fn = ~gsub('.ha.h.','_hah',.x)) %>%
rename_with(.fn = ~gsub('.m.','_m',.x)) %>%
rename_with(.fn = ~gsub('.deg.','Angle',.x)) %>%
rename_with(.fn = ~gsub('\\.','',.x)) %>%
rename('ID'='ObjId','DryYield'='YldMassDry_tHa','Lon'='Longitude','Lat'='Latitude','Pass'='PassNum','Speed'='Speed__m') %>%
st_as_sf(coords=c('Lon','Lat')) %>% #Add spatial feature info
st_set_crs(4326) %>% st_transform(3401) %>% #Lat-lon -> UTM
makePolys(width='SwthWdth_m',dist='Distance_m',angle='TrackAngle') %>%
mutate(pArea=as.numeric(st_area(.))) %>% #Area of polygon
st_centroid() %>% #Convert back to point
mutate(r=1:n()) %>% #row number
mutate(Pass=factor(seqGroup(ID,FALSE))) %>%
group_by(Pass) %>% mutate(rGroup=1:n()) %>% ungroup() %>%
mutate(E=st_coordinates(.)[,1],N=st_coordinates(.)[,2]) %>%
mutate(E=E-mean(E),N=N-mean(N)) #Center coordinates
print('Filtering data')
#Filter data
dat <- dat %>% mutate(vegaFilt = vegaFilter(.,DryYield,nDist = 30)) #Vega et al 2019 spatial "inlier" filter - takes about a minute
dat <- dat %>%
mutate(noBS = DryYield<ifelse(cropType=='Wheat',10.75,8)) %>% #JP recommends maximum filters of 160 bu (10.75 T/ha) for wheat, 120 bu (8 T/ha) for peas & canola
mutate(Qfilt = QuantileFilter(DryYield,q=0.98)) %>% #Trim dry yield outliers
mutate(bFilt = bearingFilter(TrackAngle,q=0.98)) %>% #Trim extreme bearing changes (turning)
mutate(speedFilt = QuantileFilter(Speed,q=0.98)) %>% #Trim absolute speed outliers
mutate(dSpeedFilt = dSpeedFilter(Speed,l=c(-2,-1,1,2),perc = 0.2)) %>% #Trim speed differences (>20% change 2 steps forward and backward, suggested by Lyle et al 2014)
mutate(posFilt = posFilter(.,q=0.98)) %>% #Trim points that are far away from eachother
#Combine filter criteria
mutate(allFilt = noBS & vegaFilt & Qfilt & bFilt & speedFilt & dSpeedFilt & posFilt) %>%
filter(allFilt)
if(nrow(dat)>nSubSamp){ #Limit to nSubSamp sequential samples if too large
dat <- dat %>% slice(round(seq(1,nrow(dat),length.out=nSubSamp)))
}
print('Fitting model')
#Make model formula for non-isotropic gam
f2 <- paste0('~ s(E,N,k=',kPar[1],') + s(r,k=',kPar[2],') + log(pArea)')
f <- paste0('sqrt(DryYield)~ s(E,N,k=',kPar[3],') + s(r,k=',kPar[4],') + log(pArea)')
flist <- list(as.formula(f),as.formula(f2)) #List of model formulae
#Fit Gaussian location-scale model
a <- Sys.time()
mod <- gam(flist,data=dat,family=gaulss(),start=startCoefs)
fitTime <- paste(as.character(round(Sys.time()-a,2)),units(Sys.time()-a)) #Time taken to fit model
print('Saving model results')
png(paste0(modelCheckDir,'/',fieldName,' Summary.png'),width=12,height=8,units='in',res=200)
# plot(mod,scheme=2,too.far=0.01,pages=1,all.terms=TRUE)
plot(mod,scheme=2,too.far=0.01,pages=1,all.terms=FALSE)
dev.off()
capture.output({ #Model summary
print("Time taken: "); print(fitTime)
print(" ")
print("SUMMARY---------------------------------")
summary(mod)
},file=paste0(modelCheckDir,'/',fieldName,' results.txt'))
capture.output({ #GAM check results
print(" ")
print("GAM.CHECK-------------------------------")
png(paste0(modelCheckDir,'/',fieldName,' gamCheck.png'),width=8,height=8,units='in',res=200)
par(mfrow=c(2,2)); gam.check(mod); abline(0,1,col='red'); par(mfrow=c(1,1))
dev.off()
},file=paste0(modelCheckDir,'/',fieldName,' results.txt'),append=TRUE)
#Actual, Predicted, and SE maps
print('Making yield map')
yieldMap <- dat %>% filter(DryYield<quantile(DryYield,0.95)) %>% #Chop out upper 5% of data (mainly crazy high numbers)
ggplot() +
geom_sf(data=dat,col='grey',size=1)+
geom_sf(aes(col=DryYield),alpha=0.3) +
geom_sf(data=fieldEdge,col='magenta')+ #Field boundary
labs(title=paste(fieldName,'Data')) + #guides(alpha='none') +
scale_colour_distiller(type='div',palette = "Spectral") +
theme(legend.position='bottom')
#Marginalize across r (point order) and pArea (speed)
pred <- dat %>%
mutate(pArea=median(pArea)) %>% #Set pArea to its median value
predict(mod,newdata = .,type='response',exclude='s(r)') #Exclude s(r)
dat <- dat %>% mutate(yieldMean=pred[,1],yieldSD=1/pred[,2],resid=resid(mod))
#Predicted yield, marginalizing across pArea and r
p1 <- ggplot(dat)+geom_sf(aes(col=yieldMean^2),alpha=0.3)+
labs(title='Predicted yield | Combine Speed, Point Order',fill='Yield (T per Ha)',col='Yield (T per Ha)')+
scale_colour_distiller(type='div',palette = "Spectral",direction=-1)+theme(legend.position='bottom')
#Predicted SD
p2 <- ggplot(dat)+geom_sf(aes(col=yieldSD^2),alpha=0.3)+
labs(title='Yield SD | Combine Speed, Point Order',fill='SD Yield ',col='SD Yield')+
scale_colour_distiller(type='div',palette = "Spectral",direction=-1)+theme(legend.position='bottom')
p <- ggarrange(yieldMap,p1,p2,ncol=3)
ggsave(paste0(resultsDir,'/',fieldName,'.png'),p,height=6,width=15,dpi=100)
#Residual plots/covariance plots
p3 <- ggplot(dat)+geom_sf(aes(col=resid),alpha=0.5)+labs(title='Residuals (space)')+
scale_colour_distiller(type='div',palette = "Spectral")+theme(legend.position='right')
p4 <- ggplot(dat)+geom_point(aes(x=r,y=resid),alpha=0.5)+labs(title='Residuals (sequence)',x='Sequence',y='Residuals')+
geom_hline(yintercept=0,col='red',linetype='dashed')
#STFDF does ST empirical covariance plots well, but STIDF doesn't work for some reason
# Also, there isn't any spatial replication across time, so not sure this matters
library(gstat)
library(spacetime)
dat_sp <- as_Spatial(select(dat,ID,resid))
v <- variogram(resid~1, data=dat_sp,width=5,cutoff=300) #Variogram
# vmod <- fit.variogram(v,vgm('Mat'),fit.kappa=TRUE) #Fit Matern covariance model
# plot(v,vmod)
p5 <- v %>%
ggplot()+geom_point(aes(x=dist,y=gamma))+
labs(title='Residual variogram',x='Distance',y='Semivariance')
# dat_variogram2 <- gstat::variogram(resid~1, #Used to look at non-stationary semivariance
# data=dat_sp,width=5,cutoff=150,alpha=c(0,45,90,135)) #North, NE, E, SW
res_acf <- acf(dat$resid,lag.max=300,type='correlation',plot=FALSE)
p6 <- with(res_acf,data.frame(acf=acf,lag=lag)) %>%
ggplot(aes(x=lag,y=acf))+geom_col()+
labs(x='Time Lag',y='Autocorrelation',title='Residual autocorrelation')
p <- ggarrange(p3,p4,p5,p6,ncol=2,nrow=2)
p <- annotate_figure(p,top = text_grob(paste0(fieldName,' Residuals')))
ggsave(paste0(modelCheckDir,'/',fieldName,' residuals.png'),p,height=8,width=10,dpi=100)
#Models are huge, so saving only key components (about 25% of the size)
modList <- list(cropType=cropType, #Crop type
coefs=coef(mod), #Coefficients
vcv=vcov(mod), #Covariance matrix
ll=logLik(mod), #Log-likelihood
aic=AIC(mod), #AIC
rmse=sqrt(mean(resid(mod)^2)), #Root mean square error
mae=mean(abs(resid(mod))), #Mean absolute error
smooths=mod$smooth, #Smooth specifications
pAreaRange=range(dat$pArea), #Range of polygon areas (pArea)
rRange=range(dat$r)) #Range of r
save(modList,file=paste0(modelCheckDir,'/',fieldName,' modList.Rdata'))
print('Done')
gc()
}
#Get model info from saved text file at path
getModelInfo <- function(path){
if(!file.exists(path)){
warning('File does not exist')
retList <- list(timeTaken=NA,
percDevianceExplained=NA,
REML=NA,AIC=NA,
hessian=NA,
params=NA,
smooths=NA,
gamCheck=NA)
return(retList)
}
txt <- readLines(path)
require(tidyverse)
#Get time taken to run model
timeLine <- which(grepl('Time taken',txt))+1
if(length(timeLine)==0){
timeTaken <- NA
} else {
timeTaken <- strsplit(txt[timeLine],'\"')[[1]][2] #Time taken
timeTaken <- strsplit(timeTaken,' ')[[1]]
timeTaken <- as.difftime(as.numeric(timeTaken[1]),units=timeTaken[2])
}
#Get deviance explained
devLine <- which(grepl('Deviance explained =',txt))
if(length(devLine)==0){
pExpDev <- NA
} else {
pExpDev <- txt[devLine]
pExpDev <- strsplit(pExpDev,c(' '))[[1]]
pExpDev <- as.numeric(gsub('%','',pExpDev[length(pExpDev)]))
}
#Get REML
remlLine <- which(grepl('-REML =',txt))
if(length(remlLine)==0){
reml <- NA
} else {
reml <- strsplit(txt[remlLine],' ')[[1]]
reml <- reml[reml!='']
reml <- as.numeric(reml[3])
}
#Check Hessian matrix
hessLine <- which(grepl('Hessian positive definite, eigenvalue range',txt))
hessCheck <- txt[hessLine]