-
Notifications
You must be signed in to change notification settings - Fork 0
/
slides.qmd
1355 lines (1026 loc) · 29.4 KB
/
slides.qmd
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: |
<h2 style='font-size: 75px'>
Data Analysis Workshop
</h2>
<div style='margin-bottom: 50px; font-size: 40px'>
Linking health survey data with climate datasets in R
</div>
author:
- |
<p style='font-size: 25px; margin-left: 50px'>
<img src='images/matt.png' width='100' style='vertical-align: middle'>
Matt Gunther - Senior Data Analyst
</p>
- |
<p style='font-size: 25px; margin-left: 50px'>
<img src='images/devon.png' width='100' style='vertical-align: middle'>
Devon Kristiansen - Project Manager
</p>
format:
revealjs:
theme: [default, custom.scss]
logo: images/logo-navy.png
chalkboard: true
smaller: true
scrollable: false
incremental: true
preview-links: true
---
## Data and Research Question {.center}
::: {.columns}
:::: {.column width="50%"}
::::: {.fragment .fade-in-then-semi-out}
IPUMS PMA: Fertility outcomes for women in Burkina Faso
:::::
::::: {.fragment .fade-in-then-semi-out}
Toy GPS data: coordinates for each PMA sample cluster
:::::
::::: {.fragment .fade-in-then-semi-out}
CHIRPS: Rainfall accumulation relative to local averages
:::::
::::: {.fragment .fade-in}
<p style='color: #98579B'>
Do rainfall conditions influence women's short-term family planning decisions?
</p>
:::::
::::
:::: {.column width="50%"}
![](images/goals.png){.absolute height=400}
::::
:::
## Setup {.nonincremental}
```{r}
knitr::opts_chunk$set(echo = TRUE)
```
Follow [these steps](https://github.com/matt-gunther/ined-pma-2022) to obtain today's code & publicly available data.
Open `analysis.rmd` and begin following along with these code chunks
::: {.columns}
:::: {.column width="48%"}
```{r}
# IPUMS & Spatial Data Tools
library(tidyverse)
library(ipumsr)
library(sf)
library(terra)
library(ggspatial)
```
::::
:::: {.column width="4%"}
::::
:::: {.column width="48%"}
```{r}
# Analysis Tools
library(srvyr)
library(survey)
library(lme4)
library(broom)
library(broom.mixed)
```
::::
:::
![](images/hex/tidyverse.png){.absolute left=0 height="200"}
![](images/hex/ipumsr.png){.absolute left=300 height="200"}
![](images/hex/sf.png){.absolute left=600 height="200"}
![](images/hex/terra.png){.absolute left=900 height="200"}
# 1 - IPUMS PMA Data
## Downloading IPUMS PMA data {.center .nonincremental}
::: {.columns}
:::: {.column width="50%"}
![](images/pma/home.png){}
::::
:::: {.column width="50%"}
[Visit the IPUMS PMA data website](https://pma.ipums.org/pma/)
* Sample
* Burkina Faso
* Longitudinal
* Female Respondents Only
* Variables
* [RESULTFQ](https://pma.ipums.org/pma-action/variables/RESULTFQ)
* [PANELBIRTH](https://pma.ipums.org/pma-action/variables/PANELBIRTH)
* [PANELWEIGHT](https://pma.ipums.org/pma-action/variables/PANELWEIGHT)
* [EAID](https://pma.ipums.org/pma-action/variables/EAID)
* [URBAN](https://pma.ipums.org/pma-action/variables/URBAN)
::::
:::
## Load IPUMS PMA data into R
You'll receive two files from IPUMS - put both in the `data` folder where `analysis.Rmd` is located.
::: {.fragment}
```{r, eval=FALSE}
pma <- read_ipums_micro(
ddi = "data/pma_00136.xml", # Use your own file name here
data = "data/pma_00136.dat.gz" # Use your own file name here
)
```
```{r, results='hide', echo = FALSE}
pma <- read_ipums_micro(
ddi = "local/data/pma_00136.xml", # Use your own file name here
data = "local/data/pma_00136.dat.gz" # Use your own file name here
)
```
:::
::: {.fragment}
```{r, echo = FALSE}
pma
```
:::
## Data format
We've selected `wide` format longitudinal data, so Phase 1 and Phase 2 data are stored in separate columns.
::: {.fragment}
```{r, eval=F}
pma %>% count(RESULTFQ_1, RESULTFQ_2)
```
:::
::: {.fragment}
```{r, echo=F}
pma %>% count(RESULTFQ_1, RESULTFQ_2)
```
:::
::: {.fragment}
Keep only women who completed the survey in *both* phases of the panel study.
```{r}
# Keep only panel members from both phases
pma <- pma %>% filter(RESULTFQ_1 == 1 & RESULTFQ_2 == 1)
```
:::
# Questions about IPUMS PMA data? {.center}
# 2- PMA GPS data
## About PMA GPS data {auto-animate="true"}
::: {.columns}
:::: {.column width="60%"}
::::: {.fragment .fade-in-then-semi-out}
PMA uses spatially referenced sample clusters - called "enumeration areas" (EAs) - sampled by probability proportional to population size.
:::::
::::: {.fragment .fade-in-then-semi-out}
At the beginning of the panel study, 35 households were randomly selected within each EA.
:::::
::::
:::: {.column width="40%"}
::::
:::
## About PMA GPS data {auto-animate="true" visibility="uncounted"}
::: {.columns}
:::: {.column width="60%"}
::::: {style="opacity:0.5"}
PMA uses spatially referenced sample clusters - called "enumeration areas" (EAs) - sampled by probability proportional to population size.
At the beginning of the panel study, 35 households were randomly selected within each EA.
:::::
::::: {.fragment .fade-in-then-semi-out}
IPUMS PMA does not disseminate the GPS coordinates for EAs, but you may [apply here](https://www.pmadata.org/data/request-access-datasets) for access directly from PMA.
:::::
::::: {.fragment .fade-in-then-semi-out}
Today, we'll be using **falsified** GPS coordinates as an example.
:::::
::::
:::: {.column width="40%"}
![](images/pma/gps1.png){.absolute top=100 right=50 height=300}
::::
:::
## About PMA GPS data {auto-animate="true" visibility="uncounted"}
::: {.columns}
:::: {.column width="60%"}
::::: {style="opacity:0.5"}
PMA uses spatially referenced sample clusters - called "enumeration areas" (EAs) - sampled by probability proportional to population size.
At the beginning of the panel study, 35 households were randomly selected within each EA.
IPUMS PMA does not disseminate the GPS coordinates for EAs, but you may [apply here](https://www.pmadata.org/data/request-access-datasets) for access directly from PMA.
:::::
Today, we'll be using **falsified** GPS coordinates as an example.
::::
:::: {.column width="40%"}
![](images/pma/gps1.png){.absolute top=300 right=0 height=300}
::::
:::
## About PMA GPS data {auto-animate="true" visibility="uncounted"}
::: {.columns}
:::: {.column width="60%"}
::::: {style="opacity:0.5"}
PMA uses spatially referenced sample clusters - called "enumeration areas" (EAs) - sampled by probability proportional to population size.
At the beginning of the panel study, 35 households were randomly selected within each EA.
IPUMS PMA does not disseminate the GPS coordinates for EAs, but you may [apply here](https://www.pmadata.org/data/request-access-datasets) for access directly from PMA.
Today, we'll be using **falsified** GPS coordinates as an example.
:::::
The coordinates represent the <span style="color:red">**centroid**</span> of an enumeration area, *not* the location of any sampled household.
::::
:::: {.column width="40%"}
![](images/pma/gps2.png){.absolute top=300 right=0 height=300}
::::
:::
## Load PMA GPS data into R
::: {.fragment}
A PMA GPS dataset is a simple CSV file with one row per EA, and columns containing latitude and longitude points.
```{r}
gps <- read_csv("data/pma_gps.csv")
```
:::
::: {.fragment}
```{r, echo = FALSE}
gps
```
:::
::: {.fragment}
The column `DATUM` describes the coordinate reference system: World Geodetic System 1984.
:::
## GPS data as a Simple Features Object
::: {.columns}
:::: {.column width="75%"}
::::: {.fragment}
The [sf](https://r-spatial.github.io/sf/index.html) ("simple features") package for R contains many of the same tools you would find in other GIS software
:::::
::::: {.fragment}
```{r, `code-line-numbers` = "1,2,5"}
gps <- gps %>%
st_as_sf(
coords = c("GPSLONG", "GPSLAT"),
crs = 4326
)
```
:::::
::::
:::: {.column width="15%"}
![](images/hex/sf.png){.absolute right=0 top=90 height="200"}
::::
:::
## GPS data as a Simple Features Object {auto-animate="true" visibility="uncounted"}
::: {.columns}
:::: {.column width="75%"}
The [sf](https://r-spatial.github.io/sf/index.html) ("simple features") package for R contains many of the same tools you would find in other GIS software
```{r, `code-line-numbers` = "3-4"}
gps <- gps %>%
st_as_sf(
coords = c("GPSLONG", "GPSLAT"),
crs = 4326
)
```
::::: {.fragment}
`4326` is the [EPSG](https://epsg.io/4326) code for World Geodetic System 1984.
:::::
::::: {.fragment}
```{r, echo = FALSE}
gps
```
:::::
::::
:::: {.column width="15%"}
![](images/hex/sf.png){.absolute right=0 top=90 height="200"}
::::
:::
## Adding a Shapefile
::: {.fragment}
We need a supplementary `shape` file to see where each point lies on a map of Burkina Faso.
:::
::: {.fragment}
You only need to specify the folder: R will locate the appropriate file.
```{r, results='hide'}
shape <- st_read("data/geobf") %>% select(ADMIN_NAME)
```
:::
::: {.fragment}
```{r, echo=FALSE}
shape
```
:::
## Mapping GPS coordinates {auto-animate=true}
::: {.fragment}
```{r, eval=FALSE}
ggplot() +
layer_spatial(gps) +
theme_minimal()
```
:::
::: {.fragment}
```{r, echo=FALSE, fig.height=4}
ggplot() +
layer_spatial(gps) +
theme_minimal()
```
:::
## Mapping GPS coordinates {auto-animate=true}
```{r, eval=FALSE, `code-line-numbers` = "3"}
ggplot() +
layer_spatial(gps) +
layer_spatial(shape, alpha = 0) +
theme_minimal()
```
```{r, echo=FALSE, fig.height=4, fig.align='center'}
ggplot() +
layer_spatial(gps) +
layer_spatial(shape, alpha = 0) +
theme_minimal()
```
## PMA displacement protocol
::: {.fragment}
In fact, these GPS coordinates are *not* the actual centroid of each EA.
:::
::: {.fragment}
:::: {style="text-align: center; margin-top: 1em"}
![](images/displacement.png){height="400"}
::::
In order to preserve confidentiality, PMA displaces the centroid location of each EA up to 2 km (urban areas) or 5 km (rural areas).
Displacement does not cross admin 1 boundaries.
:::
---
::: {style="display: flex; align-items: center; height: 90%"}
:::: {.columns }
::::: {.column width="50%"}
<h2>Meters on a Round Plant</h2>
:::::: {.fragment}
Remember: the `geometry` of our `gps` data is defined by arc-degrees, not meters.
::::::
:::::: {.fragment}
<span style='color: #98579B'>If we tried to account displacement in meters without "flattening" our map, the length of one meter vary based on distance from the equator.</span>
::::::
:::::
::::: {.column width=50%}
:::::
::::
:::
![](images/projection.gif){.absolute top=0 right=50 height=600}
## Projection to Meters
::: {.fragment}
We'll use [EPSG code 32630](https://epsg.io/32630) to focus our flat projection around Burkina Faso.
:::
::: {.fragment}
```{r}
gps <- gps %>% st_transform(crs = 32630)
shape <- shape %>% st_transform(crs = 32630)
```
:::
::: {.fragment}
```{r echo=FALSE}
gps
```
:::
::: {.fragment}
This changes the `geometry` column to meters `[m]`.
:::
## Creating Buffers
::: {.fragment}
To keep things simple in our example, we'll give every EA a 5 km buffer.
:::
::: {.fragment}
```{r, results='hide'}
gps_buffer <- gps %>% st_buffer(5000)
```
:::
::: {.fragment}
```{r echo=FALSE}
gps_buffer
```
:::
::: {.fragment}
Now, `geometry` contains several points on the circumference of a round polygon.
:::
## Intersecting Regional Boundaries
::: {style="display: flex; align-items: center; height: 90%"}
:::: {.columns}
::::: {.column width="40%"}
:::::: {.fragment}
In certain cases, a buffer may cross an admin 1 boundary in `shape`.
::::::
:::::: {.fragment}
For example, we'll look at enumeration area `854131009`
```{r}
test_ea <- 854131009
```
::::::
:::::
::::: {.column width="60%"}
:::::: {.fragment}
```{r, echo = FALSE, fig.height=4, fig.width=4, fig.align='center'}
ggplot() +
layer_spatial(gps_buffer %>% filter(EAID == test_ea), alpha = 0) +
layer_spatial(gps %>% filter(EAID == test_ea), color = "red") +
annotation_spatial(shape, alpha = 0) +
theme_minimal()
```
::::::
:::::
::::
:::
## Intersecting Regional Boundaries
::: {style="display: flex; align-items: center; height: 90%"}
:::: {.columns}
::::: {.column width="40%"}
In certain cases, a buffer may cross an admin 1 boundary in `shape`.
For example, we'll look at enumeration area `854131009`
```{r}
test_ea <- 854131009
```
Keep only the segment containing the original centroid.
```{r, eval = FALSE}
gps_buffer <- gps_buffer %>%
st_intersection(shape) %>%
st_filter(gps)
```
:::::
::::: {.column width="60%"}
```{r, echo = FALSE, fig.height=4, fig.width=4, fig.align='center'}
ggplot() +
layer_spatial(gps_buffer %>% filter(EAID == test_ea), alpha = 0) +
layer_spatial(gps %>% filter(EAID == test_ea), color = "red") +
annotation_spatial(shape, alpha = 0) +
theme_minimal()
```
:::::
::::
:::
## Intersecting Regional Boundaries
::: {style="display: flex; align-items: center; height: 90%"}
:::: {.columns}
::::: {.column width="40%"}
In certain cases, a buffer may cross an admin 1 boundary in `shape`.
For example, we'll look at enumeration area `854131009`
```{r}
test_ea <- 854131009
```
Keep only the segment containing the original centroid.
```{r, eval = TRUE}
gps_buffer <- gps_buffer %>%
st_intersection(shape) %>%
st_filter(gps)
```
:::::
::::: {.column width="60%"}
```{r, echo = FALSE, fig.height=4, fig.width=4, fig.align='center'}
ggplot() +
layer_spatial(gps_buffer %>% filter(EAID == test_ea), alpha = 0) +
layer_spatial(gps %>% filter(EAID == test_ea), color = "red") +
annotation_spatial(shape, alpha = 0) +
theme_minimal()
```
:::::
::::
:::
## Back to arc-degrees
:::: {.fragment}
Finally, we can return to our original CRS defined by arc-degrees.
::::
:::: {.fragment}
```{r}
gps_buffer <- gps_buffer %>% st_transform(crs = 4326)
shape <- shape %>% st_transform(crs = 4326)
```
::::
:::: {.fragment}
```{r, fig.height=4, fig.align='center'}
ggplot() +
layer_spatial(shape, alpha = 0) +
layer_spatial(gps_buffer, alpha = 0) +
theme_minimal()
```
::::
# Questions about PMA GPS Data?
# 3 - Mapping Birth Outcomes
## Birth outcomes for individuals
::: {.fragment .fade-in-then-semi-out}
Now, we combine `pma` together with `gps_buffer`.
:::
::: {.fragment .fade-in-then-semi-out}
The `pma` variable [PANELBIRTH_2](https://pma.ipums.org/pma-action/variables/PANELBIRTH) indicates whether each woman gave birth within the year that passed between Phase 1 and Phase 2 of the panel study.
:::
::: {.fragment .fade-in}
```{r}
pma %>% count(PANELBIRTH_2)
```
:::
::: {.fragment .fade-in-then-semi-out}
**Important:** Code `99` represents women who were "not in universe" because they had indicated elsewhere on the survey that they had never given birth.
:::
::: {.fragment .fade-in-then-semi-out}
<p style='color: #98579B'>We can treat these cases as "No".</p>
:::
::: {.fragment .fade-in}
```{r}
pma %>% count(PANELBIRTH_2 == 1)
```
:::
## Birth outcomes by EA {auto-animate=true}
::: {.fragment .fade-in}
Births above and below the median enumeration area:
:::
::: {.fragment .fade-in}
```{r}
ea_summary <- pma %>%
as_survey_design(weight = PANELWEIGHT) %>%
group_by(ea = EAID_1, urban = URBAN == 1) %>%
summarise(birth_prop = survey_mean(PANELBIRTH_2 == 1, vartype = NULL)) %>%
ungroup()
```
:::
::: {.fragment .fade-in}
```{r, echo=FALSE}
ea_summary
```
:::
## Birth outcomes by EA {auto-animate="true" visibility="uncounted"}
Births above and below the median enumeration area:
```{r, `code-line-numbers` = "6-8"}
ea_summary <- pma %>%
as_survey_design(weight = PANELWEIGHT) %>%
group_by(ea = EAID_1, urban = URBAN == 1) %>%
summarise(birth_prop = survey_mean(PANELBIRTH_2 == 1, vartype = NULL)) %>%
ungroup() %>%
mutate(
ntile = ntile(birth_prop, 2)
)
```
```{r, echo=FALSE}
ea_summary
```
## Birth outcomes by EA {auto-animate="true" visibility="uncounted"}
Births above and below the median enumeration area:
```{r, `code-line-numbers` = "8"}
ea_summary <- pma %>%
as_survey_design(weight = PANELWEIGHT) %>%
group_by(ea = EAID_1, urban = URBAN == 1) %>%
summarise(birth_prop = survey_mean(PANELBIRTH_2 == 1, vartype = NULL)) %>%
ungroup() %>%
mutate(
ntile = ntile(birth_prop, 2),
many_births = ntile == 2
)
```
```{r, echo=FALSE}
ea_summary
```
## Merging GPS data
::: {.fragment}
Use `full_join` to merge all rows of `ea_summary` to `gps_buffer`.
If you list `gps_buffer` *first*, the result will be another Simple Features object.
```{r}
ea_summary_gps <- full_join(
gps_buffer %>% select(ea = EAID),
ea_summary,
by = "ea"
)
```
:::
::: {.fragment}
```{r, echo = FALSE}
ea_summary_gps <- ea_summary_gps %>% relocate(geometry, .after = everything())
ea_summary_gps
```
:::
## Mapping Birth Outcomes by EA
```{r, fig.height=4, fig.align='center'}
ggplot() +
layer_spatial(ea_summary_gps, aes(fill = many_births)) +
layer_spatial(shape, alpha = 0) +
theme_minimal()
```
## Questions about mapping?
# 4 - CHIRPS: Annual rainfall summary
## Downloading CHIRPS data
::: {style="text-align: center; margin-top: 1em"}
![](images/chirps/home.png){height="500"}
[Download a CHRIPS data extract from ClimateSERV](https://climateserv.servirglobal.net/){
preview-link="true" style="text-align: center"
}
:::
## What is a Raster File?
::: {.columns}
:::: {.column width="75%"}
![](images/chirps/files.png){.border .border-thick}
::::
:::: {.column width="25%"}
::::: {.fragment .fade-in-then-semi-out}
When you open your download, you'll find *one file per day* in our selected space and time period.
:::::
::::: {.fragment .fade-in-then-semi-out}
The `.tif` file format is a high-resolution image.
:::::
::::: {.fragment .fade-in-then-semi-out}
For CHRIPS, each pixel represents mm rainfall in an area 0.05 degrees longitude by 0.05 degrees latitude.
:::::
::::
:::
## Load Raster Data into R
::: {.columns}
:::: {.column width="75%"}
The [terra](https://rspatial.github.io/terra/index.html) package reads Raster files.
You *could* simply read the data from a single day, and map the result.
```{r}
june5_2020 <- rast("data/chirps/20200605.tif")
```
```{r, echo=FALSE}
june5_2020
```
::::
:::: {.column width="15%"}
![](images/hex/terra.png){.absolute right=0 top=75 height=200}
::::
:::
::: {.fragment}
This output summaries the `.tif` file for June 5, 2020. Notice that there are:
* 115 rows of pixels
* 159 columns of pixels
* 1 *layer* named `20211028`
:::
## June 5, 2020 {auto-animate=true}
```{r, fig.align='center', eval=FALSE}
ggplot() +
layer_spatial(june5_2020) +
layer_spatial(shape, alpha = 0) +
theme_minimal()
```
## June 5, 2020 {auto-animate="true" visibility="uncounted"}
```{r, fig.align='center'}
ggplot() +
layer_spatial(june5_2020) +
layer_spatial(shape, alpha = 0) +
theme_minimal()
```
## June 5, 2020 {auto-animate="true" visibility="uncounted"}
```{r, `code-line-numbers`="2", fig.align='center'}
ggplot() +
layer_spatial(mask(june5_2020, vect(shape), touches = FALSE)) +
layer_spatial(shape, alpha = 0) +
theme_minimal()
```
## June 5, 2020 {auto-animate="true" visibility="uncounted"}
```{r, `code-line-numbers`="5-7", fig.align='center'}
ggplot() +
layer_spatial(mask(june5_2020, vect(shape), touches = FALSE)) +
layer_spatial(shape, alpha = 0) +
theme_minimal() +
scale_fill_gradient2(low = "transparent", high = "#4375B7", na.value = "transparent") +
labs(fill = "Rainfall (mm)")
```
## Aggregation
::: {.fragment}
Ultimately, you'll want to work with `.tif` images from *many* days at the same time.
:::
::: {.fragment}
For this workshop, I've removed all files outside of the range June 1 to Oct 1.
:::
::: {.fragment}
```{r}
years <- map(2001:2020, ~{
list.files("data/chirps/", pattern = paste0("^", .x), full.names = TRUE)
})
years <- set_names(years, 2001:2020)
```
:::
::: {.fragment}
```{r, echo = FALSE}
years
```
:::
## Raster Layers
::: {.fragment}
We now create one **multi-layered** raster for each year, and store all years in one large list.
```{r}
years <- years %>% map(~.x %>% rast)
```
:::
::: {.fragment}
For example, each of the 122 days from the `2020` rainy season are now layered together in a single raster.
:::
::: {.fragment}
```{r}
years$`2020`
```
:::
## Seasonal Rainfall Accumulation
::: {.fragment}
We could now use `sum` to add all of the daily rainfall totals from the `2020` rainy season. This reduces the number of layers from 122 to 1.
:::
::: {.fragment}
```{r}
years$`2020` %>% sum()
```
:::
::: {.fragment}
More efficiently, we'll apply the same `sum` function to *every* year in our list. Let's call this output `chirps_seasonal_sum`.
:::
::: {.fragment}
```{r}
chirps_seasonal_sum <- map(years, ~.x %>% sum)
```
```{r, echo=FALSE}
rm(years)
chirps_seasonal_sum
```
:::
## One Layer per Year
::: {.fragment}
Now that each year in our list contains only one layer each, we can generate a *new* multi-layered raster: one layer *per year*.
:::
::: {.fragment}
```{r}
chirps_seasonal_sum <- rast(chirps_seasonal_sum)
```
:::
:::{.fragment}
```{r, echo = FALSE}
chirps_seasonal_sum
```
:::
:::{.fragment}
For every 0.05 degrees lat by 0.05 degree lon, we now have the **total seasonal rainfall accumulation** for every year 2001-2020.
:::
## How rainy was 2020? {auto-animate=true}
:::{.fragment}
To answer this question, we'll compare total seasonal accumulation in 2020 to the 20-year average.
:::
:::{.fragment}
Here is the *average* seasonal rainfall accumulation for each pixel (across all years):
```{r}
chirps_avg <- mean(chirps_seasonal_sum)
```
:::
:::{.fragment}
And here is the *standard deviation* from that average:
```{r}
chirps_sd <- stdev(chirps_seasonal_sum)
```
:::
:::{.fragment}
Finally we can use both to compute a Z-score for each pixel in each year.
```{r}
chirps_z <- (chirps_seasonal_sum - chirps_avg) / chirps_sd