-
Notifications
You must be signed in to change notification settings - Fork 0
/
Supplemental_materials.Rmd
3369 lines (2225 loc) · 155 KB
/
Supplemental_materials.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
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
---
title: 'Supplemental Materials for: Climatic Damage Cause Variations of Agricultural Insurance Loss for the Pacific Northwest Region of the United States'
author: "Erich Seamon, Paul E. Gessler, John T. Abatzoglou, Philip W. Mote, Stephen S. Lee"
date: "November 2023"
subtitle: 'The material contained herein is supplementary to the article named in the title and submitted to the journal, Agriculture.'
always_allow_html: yes
header-includes:
- \usepackage{pdflscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
- \usepackage{setspace}\doublespacing
- \renewcommand{\figurename}{Figure S}
- \renewcommand{\tablename}{Table S}
output:
pdf_document: default
html_document:
css: custom.css
code_folding: hide
highlight: textmate
theme: united
toc: true
toc_float: true
params: null
mainfont: serif
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.align = "center", out.width='400px', out.height='700px', dpi=400, dev = "cairo_pdf")
usePackage <- function(p) {
if (!is.element(p, installed.packages()[,1]))
install.packages(p, dep = TRUE, repos = "http://cran.us.r-project.org")
require(p, character.only = TRUE)
}
usePackage("extrafont")
usePackage("ggplot2")
usePackage("webshot")
#webshot::install_phantomjs()
loadfonts()
```
This supplemental appendix provides exploratory data and factor analyses of agricultural insurance loss for the Pacific Northwest (PNW) and the inland Pacific Northwest (iPNW) of the United States.
\newpage
## Supplemental Figures and Tables List
### 1. Agricultural Insurance Loss Example Table
Table S1. Example of insurance loss records that were acquired from the USDA Risk Management Agency (RMA). Each record represents an individual insurance claim. Full datasets are available via the following DOI: https://doi.org/10.5061/dryad.hhmgqnknh
<br></br>
### 2. Study Area Figure
Figure S1. Pacific Northwest study area, which includes agricultural regions for the inland Pacific Northwest, the southern Idaho valley, and the Willamette valley.
<br></br>
### 3. Pacific Northwest (PNW) Insurance Summaries Figures
Figure S2. PNW agricultural insurance loss by year, 1989 to 2022.
<br></br>
Figure S3. PNW agricultural insurance loss by year, 2001 to 2022.
<br></br>
Figure S4. PNW agricultural insurance loss by damage cause: 2001 to 2022.
<br></br>
Figure S5. PNW agricultural insurance loss by commodity: 2001 to 2022
<br></br>
### 4. Inland Pacific Northwest (iPNW) Insurance Summaries Figures
Figure S6. iPNW agricultural insurance loss by year, 2001 to 2022.
<br></br>
Figure S7. iPNW agricultural insurance loss by damage cause, 2001 to 2022.
<br></br>
Figure S8. iPNW agricultural insurance loss by commodity, 2001 to 2022.
### 5. Inland Pacific Northwest (iPNW) Wheat and Apples Insurance Summaries Figures
Figure S9. iPNW agricultural insurance loss for wheat by damage cause: 2001 to 2022.
<br></br>
Figure S10. iPNW stacked barplot of annual wheat agricultural insurance loss, by top damage causes, 2001 to 2022.
<br></br>
Figure S11. iPNW map of wheat losses, 2001 to 2022.
<br></br>
Figure S12. iPNW agricultural insurance loss for apples by damage cause: 2001 to 2022.
<br></br>
Figure S13. iPNW stacked barplot of annual apples agricultural insurance loss, by top damage causes, 2001 to 2022.
<br></br>
Figure S14. iPNW map of apple losses, 2001 to 2022.
<br></br>
### 6. Inland Pacific Northwest (iPNW) PCA Figures
Figure S17. Top panel: IPNW All commodities by county, with damage cause as factor loadings. Bottom panel: Scree plot. Data from 2001 is 2022 is used
<br></br>
Figure S18. Top panel: IPNW All commodities by month, with damage cause as factor loadings. Bottom panel: Scree plot. Data from 2001 is 2022 is used
<br></br>
Figure S19. Top panel: IPNW Wheat by year, with damage cause as factor loadings, using a Kmeans technique for grouping. Bottom panel: Scree plot. Data from 2001 is 2022 is used.
<br></br>
Figure S20. Top panel: IPNW Wheat by county, with damage cause as factor loadings, using a Kmeans technique for grouping. Bottom panel: Scree plot. Data from 2001 is 2022 is used.
<br></br>
Figure S21. Top panel: IPNW Apples by year, with damage cause as factor loadings, using a Kmeans technique for grouping. Bottom panel: Scree plot. Data from 2001 is 2022 is used.
<br></br>
Figure S22. Top panel: IPNW Apples by county, with damage cause as factor loadings, using a Kmeans technique for grouping. Bottom panel: Scree plot. Data from 2001 is 2022 is used.
<br></br>
```{r message=FALSE, warning=FALSE, echo=FALSE}
usePackage("knitr")
usePackage("tab")
usePackage("car")
usePackage("RCurl")
usePackage("lme4")
usePackage("ez")
usePackage("lattice")
usePackage("ggplot2")
usePackage("coefplot2")
usePackage("broom")
usePackage("htmlTable")
usePackage("gridExtra")
usePackage("kableExtra")
usePackage("repmis")
#options(scipen=999)
#------add crop insurance data
destfile <- "./data/RMA_originaldata/RMA_originaldata.zip"
#download.file(destfile, destfile)
outDir<-"/tmp/aginsurance/RMA_originaldata/"
unzip(destfile,exdir=outDir)
rma1989 <- read.csv("/tmp/aginsurance/RMA_originaldata/1989.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma1990 <- read.csv("/tmp/aginsurance/RMA_originaldata/1990.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma1991 <- read.csv("/tmp/aginsurance/RMA_originaldata/1991.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma1992 <- read.csv("/tmp/aginsurance/RMA_originaldata/1992.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma1993 <- read.csv("/tmp/aginsurance/RMA_originaldata/1993.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma1994 <- read.csv("/tmp/aginsurance/RMA_originaldata/1994.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma1995 <- read.csv("/tmp/aginsurance/RMA_originaldata/1995.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma1996 <- read.csv("/tmp/aginsurance/RMA_originaldata/1996.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma1997 <- read.csv("/tmp/aginsurance/RMA_originaldata/1997.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma1998 <- read.csv("/tmp/aginsurance/RMA_originaldata/1998.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma1999 <- read.csv("/tmp/aginsurance/RMA_originaldata/1999.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2000 <- read.csv("/tmp/aginsurance/RMA_originaldata/2000.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2001 <- read.csv("/tmp/aginsurance/RMA_originaldata/2001.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2002 <- read.csv("/tmp/aginsurance/RMA_originaldata/2002.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2003 <- read.csv("/tmp/aginsurance/RMA_originaldata/2003.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2004 <- read.csv("/tmp/aginsurance/RMA_originaldata/2004.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2005 <- read.csv("/tmp/aginsurance/RMA_originaldata/2005.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2006 <- read.csv("/tmp/aginsurance/RMA_originaldata/2006.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2007 <- read.csv("/tmp/aginsurance/RMA_originaldata/2007.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2008 <- read.csv("/tmp/aginsurance/RMA_originaldata/2008.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2009 <- read.csv("/tmp/aginsurance/RMA_originaldata/2009.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2010 <- read.csv("/tmp/aginsurance/RMA_originaldata/2010.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2011 <- read.csv("/tmp/aginsurance/RMA_originaldata/2011.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2012 <- read.csv("/tmp/aginsurance/RMA_originaldata/2012.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2013 <- read.csv("/tmp/aginsurance/RMA_originaldata/2013.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2014 <- read.csv("/tmp/aginsurance/RMA_originaldata/2014.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2015 <- read.csv("/tmp/aginsurance/RMA_originaldata/2015.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2016 <- read.csv("/tmp/aginsurance/RMA_originaldata/2016.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2017 <- read.csv("/tmp/aginsurance/RMA_originaldata/2017.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2018 <- read.csv("/tmp/aginsurance/RMA_originaldata/2018.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2019 <- read.csv("/tmp/aginsurance/RMA_originaldata/2019.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2020 <- read.csv("/tmp/aginsurance/RMA_originaldata/2020.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2021 <- read.csv("/tmp/aginsurance/RMA_originaldata/2021.txt", sep = "|", header = FALSE, strip.white = TRUE)
rma2022 <- read.csv("/tmp/aginsurance/RMA_originaldata/2022.txt", sep = "|", header = FALSE, strip.white = TRUE)
#load individual files from 1989 to 2000
RMA_1989 <- rbind(rma1989, rma1990, rma1991, rma1992, rma1993, rma1994, rma1995, rma1996, rma1997, rma1998, rma1999, rma2000)
#load individual files from 2001 to 2015
RMA_2015 <- rbind(rma2001, rma2002, rma2003, rma2004, rma2005, rma2006, rma2007, rma2008, rma2009, rma2010, rma2011, rma2012, rma2013, rma2014, rma2015)
#load individual files from 2015 to 2023
RMA_2022 <- rbind(rma2016, rma2017, rma2018, rma2019, rma2020, rma2021, rma2022)
#filter columns
RMA_1989 <- data.frame(RMA_1989[,1], RMA_1989[,3], RMA_1989[,5], RMA_1989[,7], RMA_1989[,12], RMA_1989[,13], RMA_1989[,14], RMA_1989[,15])
#filter columns
RMA_2015 <- data.frame(RMA_2015[,1], RMA_2015[,3], RMA_2015[,5], RMA_2015[,7], RMA_2015[,12], RMA_2015[,13], RMA_2015[,14], RMA_2015[,15], RMA_2015[,16])
#filter columns
RMA_2022 <- data.frame(RMA_2022[,1], RMA_2022[,3], RMA_2022[,5], RMA_2022[,7], RMA_2022[,13], RMA_2022[,14], RMA_2022[,15], RMA_2022[,20], RMA_2022[,21])
#set column names
colnames(RMA_1989) <- c("year", "state", "county", "commodity", "damagecause", "monthcode", "month", "loss")
colnames(RMA_2015) <- c("year", "state", "county", "commodity", "damagecause", "monthcode", "month", "acres", "loss")
colnames(RMA_2022) <- c("year", "state", "county", "commodity", "damagecause", "monthcode", "month", "acres", "loss")
#load individual files from 2001 to 2015
RMA <- rbind(RMA_2015, RMA_2022)
#RMA <- rbind(rma2001, rma2002, rma2003, rma2004, rma2005, rma2006, rma2007, rma2008, rma2009, rma2010, rma2011, rma2012, rma2013, rma2014, rma2015, rma2016, rma2017, rma2018, rma2019, rma2020, rma2021, rma2022, rma2023 )
#filter columns
#RMA <- data.frame(RMA[,1], RMA[,3], RMA[,5], RMA[,7], RMA[,12], RMA[,13], RMA[,14], RMA[,15], RMA[,16])
#set column names
#colnames(RMA) <- c("year", "state", "county", "commodity", "damagecause", "monthcode", "month", "acres", "loss")
#subset 2001 to 2015 for ID, WA, and OR
RMA_PNW <- subset(RMA, state == "WA" | state == "OR" | state == "ID" )
#subset 1989 to 2000 for ID, WA, and OR
RMA_PNW_1989 <- subset(RMA_1989, state == "WA" | state == "OR" | state == "ID" )
#calculate loss per acre
RMA_PNW$lossperacre <- RMA_PNW$loss / RMA_PNW$acres
#remove records with missing months
RMA_PNW <- subset(RMA_PNW, month != "")
#set a crop year column (i.e. water year)
RMA_PNW$cropyear <- RMA_PNW$year
#make oct/nov/dec part of the crop year
RMA_PNW$cropyear[RMA_PNW$monthcode >= 10] <- RMA_PNW$year + 1
#remove 2016
#RMA_PNW <- subset(RMA_PNW, year != 2016)
#subset just for Idaho
RMA_Idaho_PNW <- subset(RMA_PNW, state == "ID")
#subset just for Oregon
RMA_Oregon_PNW <- subset(RMA_PNW, state == "OR")
#subset just for Washington
RMA_Washington_PNW <- subset(RMA_PNW, state == "WA")
#subset just for iPNW counties for Idaho
RMA_Idaho_IPNW <- subset(RMA_Idaho_PNW, county == "Idaho" | county == "Lewis" | county == "Nez Perce" | county == "Latah" | county == "Benewah")
#subset just for iPNW counties for Oregon
RMA_Oregon_IPNW <- subset(RMA_Oregon_PNW, county == "Wasco" | county == "Sherman" | county == "Gilliam" | county == "Morrow" | county == "Umatilla" | county == "Union" | county == "Wallowa")
#subset just for iPNW counties for Washington
RMA_Washington_IPNW <- subset(RMA_Washington_PNW, county == "Douglas" | county == "Grant" | county == "Benton" | county == "Franklin" | county == "Walla Walla" | county == "Adams" | county == "Lincoln" | county == "Spokane" | county == "Whitman" | county == "Columbia" | county == "Garfield" | county == "Asotin")
#combine all iPNW counties
RMA_IPNW <- rbind(RMA_Idaho_IPNW,RMA_Oregon_IPNW,RMA_Washington_IPNW)
#aggregate by year/state/county/commodity, for total loss in $
RMA_commodity_loss <- aggregate(RMA$loss, by = list(RMA$year, RMA$state, RMA$county, RMA$commodity), FUN = sum)
colnames(RMA_commodity_loss) <- c("year", "state", "county", "commodity", "loss")
RMA_commodity_loss <- subset(RMA_commodity_loss, commodity != "ADJUSTED GROSS REVENUE")
#aggregate by year/state/county/commodity for total number of claims
RMA_commodity_count <- aggregate(RMA$loss, by = list(RMA$year, RMA$state, RMA$county, RMA$commodity), FUN = length)
colnames(RMA_commodity_count) <- c("year", "state", "county", "commodity", "count")
RMA_commodity_count <- subset(RMA_commodity_count, commodity != "ADJUSTED GROSS REVENUE")
#aggregate for year/state/county/commodity for acreage
RMA_commodity_acres <- aggregate(RMA$acres, by = list(RMA$year, RMA$state, RMA$county, RMA$commodity), FUN = sum)
colnames(RMA_commodity_acres) <- c("year", "state", "county", "commodity", "acres")
RMA_commodity_acres <- subset(RMA_commodity_acres, commodity != "ADJUSTED GROSS REVENUE")
#aggregate for year/state/county/damage cause for total loss in $
RMA_damage_loss <- aggregate(RMA$loss, by = list(RMA$year, RMA$state, RMA$county, RMA$commodity, RMA$damagecause), FUN = sum)
colnames(RMA_damage_loss) <- c("year", "state", "county", "commodity", "damagecause", "loss")
#aggregate for year/state/county/commodity/damage cause by total loss in $
RMA_damage_count <- aggregate(RMA$loss, by = list(RMA$year, RMA$state, RMA$county, RMA$commodity, RMA$damagecause), FUN = length)
colnames(RMA_damage_count) <- c("year", "state", "county", "commodity", "damagecause", "count")
#aggregate by year/state/county/commodity/damage cause by acres
RMA_damage_acres <- aggregate(RMA$acres, by = list(RMA$year, RMA$state, RMA$county, RMA$commodity, RMA$damagecause), FUN = sum)
colnames(RMA_damage_acres) <- c("year", "state", "county", "commodity", "damaecause", "acres")
#combine all the aggregated commodity values (loss, count, acreage)
RMA_commodity_combined <- cbind(RMA_commodity_loss, RMA_commodity_count$count, RMA_commodity_acres$acres)
colnames(RMA_commodity_combined) <- c("year", "state", "county", "commodity", "loss", "count", "acres")
#calculate lossperacre for aggregated commodity
RMA_commodity_combined$lossperacre <- RMA_commodity_combined$loss / RMA_commodity_combined$acres
#calculate loss per claim for aggregated commodity
RMA_commodity_combined$lossperclaim <- RMA_commodity_combined$loss / RMA_commodity_combined$count
#calculate acres per claim for aggregated commodity
RMA_commodity_combined$acresperclaim <- RMA_commodity_combined$acres / RMA_commodity_combined$count
#combine all aggregated damage cause values (loss, count, acreage)
RMA_damage_combined <- cbind(RMA_damage_loss, RMA_damage_count$count, RMA_damage_acres$acres)
colnames(RMA_damage_combined) <- c("year", "state", "county", "commodity", "damagecause", "loss", "count", "acres")
#calculate lossperacre for aggregated damage cause
RMA_damage_combined$lossperacre <- RMA_damage_combined$loss / RMA_damage_combined$acres
#calculate loss per claim for aggregated damage cause
RMA_damage_combined$lossperclaim <- RMA_damage_combined$loss / RMA_damage_combined$count
#calculate acres per claim for aggregated damage cause
RMA_damage_combined$acresperclaim <- RMA_damage_combined$acres / RMA_damage_combined$count
#set NAs for loss per acre for damage cause combo to zero.
RMA_damage_combined$lossperacre[!is.finite(RMA_damage_combined$lossperacre)] <- 0
#subset for PNW - 2001 - 2015
RMA_damage_PNW <- subset(RMA_damage_combined, state == "WA" | state == "OR" | state == "ID")
#remove all other counties values
RMA_damage_PNW <- subset(RMA_damage_PNW, county != "All Other Counties")
#subset just for Idaho
RMA_Idaho <- subset(RMA_damage_PNW, state == "ID")
#subset just for Oregon
RMA_Oregon <- subset(RMA_damage_PNW, state == "OR")
#subset just for Washington
RMA_Washington <- subset(RMA_damage_PNW, state == "WA")
#subset just for iPNW counties for Idaho
RMA_Idaho_IPNW <- subset(RMA_Idaho, county == "Idaho" | county == "Lewis" | county == "Nez Perce" | county == "Latah" | county == "Benewah")
#subset just for iPNW counties for Oregon
RMA_Oregon_IPNW <- subset(RMA_Oregon, county == "Wasco" | county == "Sherman" | county == "Gilliam" | county == "Morrow" | county == "Umatilla" | county == "Union" | county == "Wallowa")
#subset just for iPNW counties for Washington
RMA_Washington_IPNW <- subset(RMA_Washington, county == "Douglas" | county == "Grant" | county == "Benton" | county == "Franklin" | county == "Walla Walla" | county == "Adams" | county == "Lincoln" | county == "Spokane" | county == "Whitman" | county == "Columbia" | county == "Garfield" | county == "Asotin")
#combine all iPNW counties
RMA_damage_IPNW <- rbind(RMA_Idaho_IPNW,RMA_Oregon_IPNW,RMA_Washington_IPNW)
palousecounties <- c("Idaho", "Lewis", "Nez Perce", "Clearwater", "Latah", "Benewah", "Kootenai","Douglas", "Grant", "Benton", "Franklin", "Walla Walla", "Adams", "Lincoln", "Spokane", "Whitman", "Columbia", "Garfield", "Asotin","Wasco", "Sherman", "Gilliam", "Morrow", "Umatilla", "Union", "Wallowa")
sumloss_palouse <- RMA_damage_PNW[RMA_damage_PNW$county %in% palousecounties, ]
sumloss_nonpalouse <- RMA_damage_PNW[!RMA_damage_PNW$county %in% palousecounties, ]
#----
```
```{r functions, message=FALSE, warning=FALSE, error=TRUE, echo = FALSE, include=FALSE}
# A function for captioning and referencing images
fig <- local({
i <- 0
ref <- list()
list(
cap=function(refName, text) {
i <<- i + 1
ref[[refName]] <<- i
paste("Figure ", i, ": ", text, sep="")
},
ref=function(refName) {
ref[[refName]]
})
})
```
\newpage
\blandscape
## 1. Agricultural Insurance Loss Example Table
```{r message=FALSE, warning=FALSE, echo=FALSE}
#```{r message=FALSE, warning=FALSE, error=TRUE, echo = FALSE, fig.width = 8, fig.height = 8, fig.cap=paste("Example of the original data that was acquired from the USDA Risk Management Agency (RMA). Each record represents an individual insurance claim.")}
RMA_PNW_table <- RMA_PNW[-6]
knitr::kable(RMA_PNW_table[1:9,], row.names = FALSE, booktabs = T, format="latex", caption = "Example of insurance loss records that were acquired from the USDA Risk Management Agency (RMA). Each record represents an individual insurance claim. Full datasets are available via the following DOI: https://doi.org/10.5061/dryad.hhmgqnknh") %>%
kable_styling(latex_options = c("hold_position"),
full_width = F)
```
\newpage
\elandscape
## 2. Study Area Figure
```{r message=FALSE, warning=FALSE, echo = FALSE, fig.width = 8, fig.height = 7, fig.cap=paste("Pacific Northwest study area, which includes agricultural regions for the inland Pacific Northwest, the southern Idaho valley, and the Willamette valley.")}
usePackage("data.table")
usePackage("maptools")
usePackage("classInt")
usePackage("leaflet")
usePackage("dplyr")
usePackage("raster")
usePackage("htmltools")
destfile <- "./data/states/states_conus.zip"
#download.file(URL, destfile)
outDir<-"/tmp/aginsurance/states_conus/"
unzip(destfile,exdir=outDir)
states <- readShapePoly('/tmp/aginsurance/states_conus/states_conus.shp',
proj4string=CRS
("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
projection = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
destfile <- "./data/counties/threestate_boundary.zip"
#download.file(URL, destfile)
outDir<-"/tmp/aginsurance/threestate_boundary/"
unzip(destfile,exdir=outDir)
threestates <- readShapePoly('/tmp/aginsurance/threestate_boundary/threestate_boundary.shp',
proj4string=CRS
("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
projection = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
#setwd("/dmine/data/counties/")
destfile <- "./data/counties/threestate_willamette.zip"
#download.file(URL, destfile)
outDir<-"/tmp/aginsurance/threestate_willamette/"
unzip(destfile,exdir=outDir)
counties_willamette <- readShapePoly('/tmp/aginsurance/threestate_willamette/threestate_willamette.shp',
proj4string=CRS
("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
projection = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
destfile <- "./data/counties/threestate_southernID.zip"
#download.file(URL, destfile)
outDir<-"/tmp/aginsurance/threestate_southernID/"
unzip(destfile,exdir=outDir)
counties_ID <- readShapePoly('/tmp/aginsurance/threestate_southernID/threestate_southernID.shp',
proj4string=CRS
("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
projection = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
destfile <- "./data/counties/threestate_palouse.zip"
#download.file(URL, destfile)
outDir<-"/tmp/aginsurance/threestate_palouse/"
unzip(destfile,exdir=outDir)
counties_palouse <- readShapePoly('/tmp/aginsurance/threestate_palouse/threestate_palouse.shp',
proj4string=CRS
("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
projection = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
#pal <- colorNumeric(palette = c("white", "orange", "darkorange", "red", "darkred"),
# domain = counties$NAME)
exte <- as.vector(extent(threestates))
#label <- paste(sep = "<br/>", counties$NAME, round(counties$NAME, 0))
#markers <- data.frame(label)
#labs <- as.list(counties$NAME)
counties_palouse <- counties_palouse[counties_palouse$NAME != "Kootenai",]
counties_palouse <- counties_palouse[counties_palouse$NAME != "Clearwater",]
#--
par(mar=c(5, 8.1, 7, 0), family = 'serif', mgp=c(1, 1, 0), las=0)
usePackage("RgoogleMaps")
usePackage("sp")
usePackage("ggplot2")
usePackage("ggmap")
usePackage("maps")
usePackage("mapdata")
usePackage("mapproj")
maps::map("worldHires","Canada", xlim=c(-130,-110),ylim=c(40,50), col="gray90", fill=TRUE) #plot the region of Canada I want
maps::map("worldHires","usa", xlim=c(-130,-110),ylim=c(40,50), col="gray95", fill=TRUE, add=TRUE) #add the adjacent parts of the US; can't forget my homeland
plot(counties_palouse, add=TRUE, col=alpha("darkgreen", 0.6), border=FALSE)
lines(counties_palouse)
lines(states)
box(lty = 1, col = 'black')
plot(counties_willamette, add=TRUE, col=alpha("red", 0.6), border=FALSE)
lines(counties_willamette)
plot(counties_ID, add=TRUE, col=alpha("yellow", 0.6), border=FALSE)
lines(counties_ID)
map.scale(x=-120.5, y=41, ratio=FALSE, relwidth=0.17)
#north.arrow(xb = -121, yb=41, len = 0.22, lab="N")
# Inmap
par(usr=c(-129, 110, 22, 144))
rect(xleft =-126.2,ybottom = 23.8,xright = -65.5,ytop = 50.6,col = "white")
rect(xleft =60,ybottom = 75,xright = -35,ytop = 125,border = "red", col = "NA", lwd=2)
maps::map("usa", xlim=c(-126.2,-65.5), ylim=c(23.8,50.6),add=T)
maps::map("state", xlim=c(-126.2,-65.5), ylim=c(23.8,50.6),add=T, boundary = F, interior = T, lty=2)
#map("state", region="california", fill=T, add=T)
#points(-121.6945, 39.36708, bg = "white", pch = 21)
maps::map("state", region=c("washington", "Idaho", "Oregon"), fill=T, add=T)
#mapz <- leaflet(options = leafletOptions(zoomSnap = .4, zoomDelta = .4)) %>%
# addProviderTiles(providers$Hydda.Base) %>%
# addProviderTiles(providers$Stamen.TonerLines) %>%
# fitBounds(exte[1], exte[3], exte[2], exte[4]) %>% addPolygons(data = counties_palouse, color = "lightyellow", fillOpacity = .8, weight = 1) %>% addPolygons(data = counties_palouse, color = "gray", fillOpacity = 0, weight = 1) %>% addPolygons(data = counties_ID, color = "gray", fillOpacity = 0, weight = 1) %>% addPolygons(data = counties_ID, color = "red", weight = 1) %>% addPolygons(data = counties_willamette, color = "green", weight = 1) %>% addPolygons(data = counties_willamette, color = "gray", fillOpacity = 0, weight = 1) %>% addPolygons(data = states, stroke = TRUE, color = "black", weight = 3, fillOpacity = 0)
#addScaleBar(mapz, position = c("bottomright"), options = scaleBarOptions())
```
\newpage
## 3. Pacific Northwest (PNW) Insurance Summaries Figures
```{r message=FALSE, warning=FALSE, error=TRUE, echo = FALSE, fig.width = 8, fig.height = 8, fig.cap=paste("Pacific Northwest agricultural insurance loss by year, 1989 to 2022")}
usePackage("htmlTable")
usePackage("gridExtra")
#palouse_sumloss_aggregate <- subset(palouse_sumloss_aggregate, year >= 2001 & year <= 2015)
#palouse_sumloss_1989_2015 <- subset(palouse_sumloss_aggregate, commodity == "WHEAT")
RMA_PNW2 <- RMA_PNW[-8]
RMA_PNW2 <- RMA_PNW2[-9]
RMA_PNW2 <- RMA_PNW2[-9]
RMA_all <- rbind(RMA_PNW_1989, RMA_PNW2)
#barplot of 1989 to 2015
RMA_PNW_aggregates <- aggregate(RMA_PNW$loss, by=list(RMA_PNW$year), FUN = "sum")
RMA_PNW_1989_aggregates <- aggregate(RMA_PNW_1989$loss, by=list(RMA_PNW_1989$year), FUN = "sum")
RMA_allyear <- rbind(RMA_PNW_1989_aggregates, RMA_PNW_aggregates)
colnames(RMA_allyear) <- c("Year", "Loss")
#barplot of 1989 to 2015 for damage causes
RMA_PNW_aggregates_d <- aggregate(RMA_PNW$loss, by=list(RMA_PNW$damagecause), FUN = "sum")
RMA_PNW_1989_aggregates_d <- aggregate(RMA_PNW_1989$loss, by=list(RMA_PNW_1989$damagecause), FUN = "sum")
RMA_allyear_d <- rbind(RMA_PNW_1989_aggregates_d, RMA_PNW_aggregates_d)
colnames(RMA_allyear_d) <- c("Damagecause", "Loss")
RMA_allyear_d <- aggregate(RMA_allyear_d$Loss, list(RMA_allyear_d$Damagecause), FUN = "sum")
colnames(RMA_allyear_d) <- c("Damagecause", "Loss")
RMA_allyear_d <- RMA_allyear_d[order(-RMA_allyear_d$Loss),]
par(mar=c(5, 8.1, 7, 0), family = 'serif', mgp=c(1, 1, 0), las=0)
#levels(YYY1$commodity)[1] <- "ADJ GROSS REVENUE"
barplot(RMA_allyear$Loss, names.arg = RMA_allyear$Year, las = 3, main = "", yaxt="n", col = "lightblue", xlab = "", cex.main = 1, cex.lab = 1, cex.names = 1, font.lab = 1, font.axis = 1)
text(x=24, y=1500000000, "83% of \n Agriculture claims occur \n after 2000", cex = 1.5)
#graphics::title(main = "PNW total insurance loss by year: 1989 to 2022", line = 1, adj = 0, cex.main = 1.5)
pts <- pretty(RMA_allyear$Loss / 1000000)
pts2 <- pretty(RMA_allyear$Loss)
axis(2, las = 1, cex.axis = 1, at = pts2, labels = paste(pts, "M", sep = ""), cex.axis=1.5)
abline(v = 14.5, lty = 2, color = "red")
mtext("Year", cex = 1.5, side=1, line=4)
mtext("Insurance Claim Loss ($)", cex = 1.5, side=2, line=5.5)
```
\newpage
```{r message=FALSE, warning=FALSE, error=TRUE, echo = FALSE, fig.width = 8, fig.height = 8, fig.cap=paste("Pacific Northwest agricultural insurance loss by year: 2001 to 2022")}
palouse_sumloss_1989_2015 <- aggregate(RMA_PNW$loss, list(RMA_PNW$year), FUN = "sum")
X <- palouse_sumloss_1989_2015
colnames(X) <- c("year", "loss")
#X <- palouse_sumloss_2007_2015_aggregate[order(palouse_sumloss_2007_2015_aggregate$loss),]
#colnames(X) <- c("damagecause", "loss")
XX <- X
#XX <- subset(X, loss > 50000000)
YYY1 <- XX
tt3 <- ttheme_default(core=list(fg_params=list(hjust=0, x=0.1)),
rowhead=list(fg_params=list(hjust=0, x=0)))
#YYY1<- YYY1[seq(dim(YYY1)[1],1),]
YYY1$loss <- round(YYY1$loss, 0)
#grid.table(YYY1, theme = tt3, rows = NULL)
YYY2 <- YYY1
YYY2$loss <- format(YYY2$loss,big.mark=",",scientific=FALSE)
YYY2$loss <- paste("$", YYY2$loss, sep="")
YYY2$loss <- gsub(" ", "", YYY2$loss, fixed = TRUE)
dd2 <- cbind(YYY2[1:15, ])
colnames(dd2) <- c("year", "loss")
dd2[is.na(dd2)] <- ""
par(mar=c(8, 8.1, 7, 0), family = 'serif', mgp=c(2, 1, 0), las=0)
#levels(YYY1$commodity)[1] <- "ADJ GROSS REVENUE"
barplot(YYY1$loss, names.arg = YYY1$year, las = 3, yaxt="n", col = "lightblue", cex.lab = 1, cex.main = 1, cex.names = 1.5, font.lab = 1, font.axis = 1)
#graphics::title(main = "PNW total insurance loss by year: 2001 to 2022", line = 1, adj = 0, cex.main = 1.5)
pts <- pretty(YYY1$loss / 1000000)
pts2 <- pretty(YYY1$loss)
axis(2, las = 1, cex.axis = 1, at = pts2, labels = paste(pts, "M", sep = ""), cex.axis=1.5)
#abline(v = 14.5, lty = 2, color = "red")
mtext("Insurance Claim Loss ($)", cex = 1.5, side=2, line=5.5)
mtext("Year", cex = 1.5, side=1, line=5)
#htmlTable(dd2,
# cgroup = c("2001-2015"),
# n.cgroup = c(2),
# caption = "PNW total insurance loss by year, 2001-2015",
# align = "c",
# rnames = FALSE,
# css.cell = "padding-left: .5em; padding-right: .5em;")
```
\newpage
```{r message=FALSE, warning=FALSE, error=TRUE, echo = FALSE, fig.width = 8, fig.height = 8, fig.cap=paste("Pacific Northwest agricultural insurance loss by damage cause: 2001 to 2022.")}
#Figure 4
#--barplot of damage causes from 2001 to 2015
palouse_sumloss_1989_2015 <- aggregate(RMA_damage_PNW$loss, list(RMA_damage_PNW$damagecause), FUN = "sum")
palouse_sumloss_1989_2015 <- palouse_sumloss_1989_2015[c(8:nrow(palouse_sumloss_1989_2015)),]
X <- palouse_sumloss_1989_2015
colnames(X) <- c("year", "loss")
#X <- palouse_sumloss_2007_2015_aggregate[order(palouse_sumloss_2007_2015_aggregate$loss),]
#colnames(X) <- c("damagecause", "loss")
XX <- X
#XX <- subset(X, loss > 50000000)
YYY1 <- palouse_sumloss_1989_2015
colnames(YYY1) <- c("damagecause", "loss")
tt3 <- ttheme_default(core=list(fg_params=list(hjust=0, x=0.1)),
rowhead=list(fg_params=list(hjust=0, x=0)))
#YYY1<- YYY1[seq(dim(YYY1)[1],1),]
YYY1$loss <- round(YYY1$loss, 0)
#grid.table(YYY1, theme = tt3, rows = NULL)
YYY1$damagecause <- as.factor(YYY1$damagecause)
levels(YYY1$damagecause)[8] <- "Excessive Moisture"
par(mar=c(14, 8.1, 7, 0), family = 'serif', mgp=c(2, 1, 0), las=0)
#levels(YYY1$commodity)[1] <- "ADJ GROSS REVENUE"
YYY1 <- YYY1[order(-YYY1$loss), ]
YYY1 <- YYY1[1:11,]
YYY1[8,]$loss <- YYY1[8,]$loss + YYY1[11,]$loss
YYY1 <- YYY1[1:10,]
YYY1 <- YYY1[order(YYY1$loss, decreasing = TRUE),]
YYY1$damagecause <- factor(YYY1$damagecause)
levels(YYY1$damagecause)[5] <- "Excessive Moisture"
levels(YYY1$damagecause)[10] <- "Wind"
barplot(YYY1$loss, names.arg = YYY1$damagecause, las = 3, main = "", yaxt="n", col = "lightblue", xlab = "", cex.main = 1, cex.lab = 1, cex.names = 1.5, font.lab = 1, font.axis = 1)
#graphics::title(main = "PNW total insurance loss by damage cause: 2001 to 2022", line = 1, adj = 0, cex.main = 1.5)
pts <- pretty(YYY1$loss / 1000000)
pts2 <- pretty(YYY1$loss)
axis(2, las = 1, cex.axis = 1, at = pts2, labels = paste(pts, "M", sep = ""), cex.axis=1.5)
#abline(v = 14.5, lty = 2, color = "red")
mtext("Damage Cause", cex = 1.5, side=1, line=12.5)
mtext("Insurance Claim Loss ($)", cex = 1.5, side=2, line=5.5)
#htmlTable(dd2,
# cgroup = c("2001-2015"),
# n.cgroup = c(2),
# caption = "PNW total insurance loss by year, 2001-2015",
# align = "c",
# rnames = FALSE,
# css.cell = "padding-left: .5em; padding-right: .5em;")
```
\newpage
```{r message=FALSE, warning=FALSE, error=TRUE, echo = FALSE, fig.width = 8, fig.height = 8, fig.cap=paste("Pacific Northwest agricultural insurance loss per claim: 2001 to 2022.")}
#options(scipen = 999)
# PNW_lossperclaim <- ggplot(RMA_damage_PNW, aes(x=factor(year), y=lossperclaim))+
# geom_boxplot(outlier.shape = NA)+ theme_bw() + scale_y_continuous(limits = c(0, 500000)) + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
#
# IPNW_lossperclaim <- ggplot(RMA_damage_IPNW, aes(x=factor(year), y=lossperclaim))+
# geom_boxplot(outlier.shape = NA)+ theme_bw() + scale_y_continuous(limits = c(0, 500000))+ theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
# RMA_damage_IPNW_wheat <- subset(RMA_damage_IPNW, commodity == "Wheat" | commodity == "WHEAT")
# IPNW_wheat_lossperclaim <- ggplot(RMA_damage_IPNW_wheat, aes(x=factor(year), y=lossperclaim))+
# geom_boxplot(outlier.shape = NA)+ theme_bw() + scale_y_continuous(limits = c(0, 500000))+ theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
#
# RMA_damage_IPNW_apples <- subset(RMA_damage_IPNW, commodity == "Apples" | commodity == "APPLES")
# IPNW_apples_lossperclaim <- ggplot(RMA_damage_IPNW_apples, aes(x=factor(year), y=lossperclaim))+
# geom_boxplot(outlier.shape = NA)+ theme_bw() + scale_y_continuous(limits = c(0, 500000))+ theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
#
# grid.arrange(IPNW_wheat_lossperclaim,IPNW_apples_lossperclaim, nrow=1, ncol=2)
```
\newpage
```{r message=FALSE, warning=FALSE, error=TRUE, echo = FALSE, fig.width = 8, fig.height = 8, fig.cap=paste("PNW total insurance loss by commodity: 2001 to 2022")}
usePackage("plotly")
usePackage("gridExtra")
usePackage("knitr")
usePackage("kableExtra")
usePackage("htmlTable")
usePackage("stringi")
usePackage("extrafont")
#pie chart for 2007-2015
options(scipen=999)
addNoAnswer <- function(x){
if(is.factor(x)) return(factor(x, levels=c(levels(x), "No Answer")))
return(x)
}
palouse_sumloss_1989_2015_lossperacres <- aggregate(RMA_PNW$lossperacre, list( RMA_PNW$year, RMA_PNW$state, RMA_PNW$commodity), FUN = "mean")
colnames(palouse_sumloss_1989_2015_lossperacres) <- c("year", "state", "commodity", "lossperacres")
palouse_sumloss_1989_2015_lossperacres2 <- aggregate(RMA_PNW$lossperacre, list( RMA_PNW$year, RMA_PNW$damagecause), FUN = "mean")
colnames(palouse_sumloss_1989_2015_lossperacres2) <- c("year", "damagecause", "lossperacres")
palouse_sumloss_1989_2015_lossperacres3 <- aggregate(RMA_PNW$lossperacre, list( RMA_PNW$year, RMA_PNW$damagecause, RMA_PNW$state), FUN = "mean")
colnames(palouse_sumloss_1989_2015_lossperacres3) <- c("year", "damagecause", "state", "lossperacres")
#palouse_sumloss_1989_2015_lossperacres <- subset(palouse_sumloss_1989_2015_lossperacres, lossperacres > 200)
#palouse_sumloss_1989_2015_lossperacres2 <- subset(palouse_sumloss_1989_2015_lossperacres2, lossperacres > 200)
#palouse_sumloss_1989_2015_lossperacres <- subset(palouse_sumloss_1989_2015_lossperacres, commodity != "ADJUSTED GROSS REVENUE")
#palouse_sumloss_1989_2015_lossperacres <- subset(palouse_sumloss_1989_2015_lossperacres, commodity != "Other (Snow/Lightning-Etc.)")
palouse_sumloss_1989_2015_lossperacres2$damagecause <- factor(palouse_sumloss_1989_2015_lossperacres2$damagecause)
palouse_sumloss_1989_2015_lossperacres$commodity <- factor(palouse_sumloss_1989_2015_lossperacres$commodity)
levels(palouse_sumloss_1989_2015_lossperacres$commodity)[14] <- "FORAGE"
levels(palouse_sumloss_1989_2015_lossperacres$commodity)[15] <- "APRICOTS"
levels(palouse_sumloss_1989_2015_lossperacres$commodity)[16] <- "FREESTONE PEACHES"
levels(palouse_sumloss_1989_2015_lossperacres$commodity)[17] <- "NECTARINES"
levels(palouse_sumloss_1989_2015_lossperacres$commodity)[24] <- "PASTURE, RANGELAND"
levels(palouse_sumloss_1989_2015_lossperacres$commodity)[30] <- "BLACKBERRIES"
levels(palouse_sumloss_1989_2015_lossperacres2$damagecause)[8] <- "Excessive Moisture"
levels(palouse_sumloss_1989_2015_lossperacres2$damagecause)[25] <- "Snow/Lightning"
levels(palouse_sumloss_1989_2015_lossperacres2$damagecause)[21] <- "Hurricane"
levels(palouse_sumloss_1989_2015_lossperacres2$damagecause)[24] <- "Mycotoxin"
levels(palouse_sumloss_1989_2015_lossperacres2$damagecause)[22] <- "Lack Irrig Prep"
palouse_sumloss_1989_2015_loss <- aggregate(RMA_PNW$loss, by = list( RMA_PNW$year, RMA_PNW$commodity), FUN = "sum")
colnames(palouse_sumloss_1989_2015_loss) <- c("year", "commodity", "loss")
palouse_sumloss_1989_2015_loss2 <- aggregate(RMA_PNW$loss, list( RMA_PNW$year, RMA_PNW$damagecause), FUN = "sum")
colnames(palouse_sumloss_1989_2015_loss2) <- c("year", "damagecause", "loss")
palouse_sumloss_1989_2015_loss4 <- aggregate(RMA_PNW$loss, list( RMA_PNW$year, RMA_PNW$damagecause, RMA_PNW$state), FUN = "sum")
colnames(palouse_sumloss_1989_2015_loss4) <- c("year", "damagecause", "state", "loss")
psloss <- palouse_sumloss_1989_2015_loss
palouse_sumloss_1989_2015_loss <- subset(palouse_sumloss_1989_2015_loss, loss > 20000000)
palouse_sumloss_1989_2015_loss2 <- subset(palouse_sumloss_1989_2015_loss2, loss > 20000000)
palouse_sumloss_1989_2015_loss <- subset(palouse_sumloss_1989_2015_loss, commodity != "ADJUSTED GROSS REVENUE")
palouse_sumloss_1989_2015_loss2$damagecause <- factor(palouse_sumloss_1989_2015_loss2$damagecause)
palouse_sumloss_1989_2015_loss$commodity <- factor(palouse_sumloss_1989_2015_loss$commodity)
levels(palouse_sumloss_1989_2015_loss2$damagecause)[4] <- "Excessive Moisture"
palouse_sumloss_1989_2015_count <- aggregate(RMA_PNW$loss, list( RMA_PNW$year, RMA_PNW$state, RMA_PNW$commodity), FUN = "length")
colnames(palouse_sumloss_1989_2015_count) <- c("year", "state", "commodity", "loss")
palouse_sumloss_1989_2015_count2<- aggregate(RMA_PNW$loss, list( RMA_PNW$year, RMA_PNW$damagecause, RMA_PNW$state), FUN = "length")
colnames(palouse_sumloss_1989_2015_count2) <- c("year", "damagecause", "state", "loss")
palouse_sumloss_1989_2015_loss3<- aggregate(RMA_PNW$loss, list( RMA_PNW$year, RMA_PNW$commodity, RMA_PNW$state), FUN = "sum")
colnames(palouse_sumloss_1989_2015_loss3) <- c("year", "commodity", "state", "loss")
#-boxplots of count data for all commodities and all damage causes- 1989-2015
palouse_sumloss_1989_2015_count <- subset(palouse_sumloss_1989_2015_count, loss > 50)
palouse_sumloss_1989_2015_count2 <- subset(palouse_sumloss_1989_2015_count2, loss > 50)
palouse_sumloss_1989_2015_count <- subset(palouse_sumloss_1989_2015_count, commodity != "ADJUSTED GROSS REVENUE")
palouse_sumloss_1989_2015_count2$damagecause <- factor(palouse_sumloss_1989_2015_count2$damagecause)
palouse_sumloss_1989_2015_count$commodity <- factor(palouse_sumloss_1989_2015_count$commodity)
levels(palouse_sumloss_1989_2015_count2$damagecause)[5] <- "Excessive Moisture"
levels(palouse_sumloss_1989_2015_count2$damagecause)[11] <- "Other"
```
\newpage
```{r message=FALSE, warning=FALSE, error=TRUE, echo = FALSE, fig.width = 8, fig.height = 8, fig.cap=paste("Pacific Northwest agricultural insurance loss by commodity: 2001 to 2022.")}
#Figure 5
RMA_PNW_com <- RMA_PNW
RMA_PNW_com$commodity <- tolower(RMA_PNW_com$commodity)
RMA_PNW_com$commodity <- stri_trans_general(RMA_PNW_com$commodity, id = "Title")
palouse_sumloss_1989_2015 <- aggregate(RMA_PNW_com$loss, list(RMA_PNW_com$commodity), FUN = "sum")
X <- palouse_sumloss_1989_2015
colnames(X) <- c("commodity", "loss")
#X <- palouse_sumloss_2007_2015_aggregate[order(palouse_sumloss_2007_2015_aggregate$loss),]
#colnames(X) <- c("damagecause", "loss")
XX <- X
XX <- subset(X, loss > 50000000)
YYY1 <- XX
tt3 <- ttheme_default(core=list(fg_params=list(hjust=0, x=0.1)),
rowhead=list(fg_params=list(hjust=0, x=0)))
#YYY1<- YYY1[seq(dim(YYY1)[1],1),]
YYY1$loss <- round(YYY1$loss, 0)
#grid.table(YYY1, theme = tt3, rows = NULL)
YYY2 <- YYY1
YYY2$loss <- format(YYY2$loss,big.mark=",",scientific=FALSE)
YYY2$loss <- paste("$", YYY2$loss, sep="")
YYY2$loss <- gsub(" ", "", YYY2$loss, fixed = TRUE)
dd2 <- cbind(YYY2[1:14, ])
colnames(dd2) <- c("commodity", "loss")
dd2[is.na(dd2)] <- ""
YYY1$commodity <- tolower(YYY1$commodity)
YYY1$commodity <- stri_trans_general(YYY1$commodity, id = "Title")
YYY1$commodity <- factor(YYY1$commodity)
par(mar=c(10, 8.1, 6, 0), family = 'serif', mgp=c(2, 1, 0), las=0)
levels(YYY1$commodity)[1] <- "Gross Revenue"
levels(YYY1$commodity)[2] <- "Other Crops"
YYY1 <- YYY1[order(-YYY1$loss), ]
YYY1 <- YYY1[-c(2:3),]
YYY1$commodity <- factor(YYY1$commodity)
barplot(YYY1$loss, names.arg = YYY1$commodity, las = 3, yaxt="n", col = "lightblue", cex.main = 1.5, cex.lab = 1, cex.names = 1.5, font.lab = 1, font.axis = 1)
#graphics::title(main = "PNW total insurance loss by commodity: 2001 to 2022", line = 1, adj = 0, cex.main = 1.5)
pts <- pretty(YYY1$loss / 1000000)
pts2 <- pretty(YYY1$loss)
axis(2, las = 1, cex.axis = 1, at = pts2, labels = paste(pts, "M", sep = ""), cex.axis=1.5)
#abline(v = 14.5, lty = 2, color = "red")
mtext("Insurance Claim Loss ($)", cex = 1.5, side=2, line=5.5)
mtext("Commodity", cex = 1.5, side=1, line=8)
#htmlTable(dd2,
# cgroup = c("2001-2015"),
# n.cgroup = c(2),
# caption = "PNW total insurance loss by year, 2001-2015",
# align = "c",
# rnames = FALSE,
# css.cell = "padding-left: .5em; padding-right: .5em;")
```
\newpage
```{r message=FALSE, warning=FALSE, error=TRUE, echo = FALSE, fig.width = 8, fig.height = 8, fig.cap=paste("Top panel: Biplot of principal components for insurance loss for the entire PNW, by county, with commodity as the factor loadings. Bottom panel: Scree plot. Data from 2001 to 2022 is used.")}
usePackage("ggplot2")
usePackage("ggfortify")
usePackage("reshape2")
usePackage("extrafont")
#PNW by commodity
RMA_damage_PNW_com <- RMA_damage_PNW[RMA_damage_PNW$commodity == c("Wheat", "Apples", "Cherries", "Potatoes", "Dry Peas", "Grapes", "Barley", "Sugar Beets"),]
RMA_damage_PNW_transformed_commodity <- dcast(RMA_damage_PNW_com, county + state ~ commodity, value.var = c("loss"), sum)
RMA_damage_PNW_transformed_commodity$statecounty <- paste(RMA_damage_PNW_transformed_commodity$county, "_", RMA_damage_PNW_transformed_commodity$state, sep="")
rownames(RMA_damage_PNW_transformed_commodity) <- RMA_damage_PNW_transformed_commodity$statecounty
RMA_damage_PNW_exogenous_commodity <- RMA_damage_PNW_transformed_commodity[4:ncol(RMA_damage_PNW_transformed_commodity)-1]
#RMA_damage_PNW_exogenous_commodity <- RMA_damage_PNW_transformed_commodity[,3:41]
#RMA_damage_PNW_exogenous_commodity <- RMA_damage_PNW_exogenous_commodity[-1]
#RMA_damage_PNW_exogenous_commodity <- RMA_damage_PNW_exogenous_commodity[-20]
#RMA_damage_PNW_exogenous_commodity <- RMA_damage_PNW_exogenous_commodity[-37]
fit <- princomp(RMA_damage_PNW_exogenous_commodity, cor=TRUE)
#ggplot2::autoplot(prcomp(RMA_damage_PNW_exogenous_commodity, scale = TRUE, center = TRUE), data = RMA_damage_PNW_transformed_commodity, colour = 'county', loadings = TRUE, loadings.label = TRUE, loadings.label.size = 3, label = FALSE, label.size = 3, legend = FALSE) + geom_text(vjust=-1, label=rownames(RMA_damage_PNW_exogenous_commodity)) + theme(legend.position = "none")
plot1 <- ggplot2::autoplot(prcomp(RMA_damage_PNW_exogenous_commodity, center = TRUE, scale = TRUE), data = RMA_damage_PNW_transformed_commodity, loadings = TRUE, loadings.label = TRUE, colour = 'state', loadings.label.size = 3, label = FALSE, label.size = 2, legend = TRUE, loadings.label.repel=TRUE) + theme_bw() + theme(legend.position = "none") + ggtitle("Principal Components Analysis: PNW insurance loss by county with commodity loadings ") + theme(plot.title = element_text(size =12)) + theme(axis.title.y = element_text(size=12), axis.title.x = element_text(size = 12), axis.text.x = element_text(size=rel(1.5), hjust = 1), axis.text.y = element_text(size=rel(1.5), hjust = 1))
#colour = 'state',
#scree plot
std_dev <- fit$sdev
pr_var <- std_dev^2