-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.qmd
1606 lines (1280 loc) Β· 45.4 KB
/
index.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
```{r}
#| label: setup
#| include: false
source(here::here("R", "_setup.R"))
```
## Overview
This report explores potential associations between childhood undernutrition and the Standardized Precipitation Evapotranspiration Index ([SPEI](https://en.wikipedia.org/wiki/Standardised_Precipitation_Evapotranspiration_Index)) in Brazilian municipalities (2008β2019). It is part of the [Sustentarea](https://www.fsp.usp.br/sustentarea) Research and Extension Group's project titled *Global syndemic: The impact of anthropogenic climate change on the health and nutrition of children under five years old served by Brazil's public health system (SUS)*.
::: {.callout-warning}
Please note that this report is designed to support decision-making and may not include all the details of the analysis.
Any additional analyses or updates will be incorporated into the report in future revisions, if necessary.
:::
## Question
This analysis seeks to address the following question:
::: {style="text-align: center; font-size: 1.1em; font-weight: bold; color: #ED6B4D; padding-top: 1em; padding-bottom: 1em;"}
Does the Standardized Precipitation Evapotranspiration Index (SPEI) significantly improve the prediction of childhood undernutrition in Brazilian municipalities?
:::
## Methods
### Approach and Procedure Method
This study employed the hypothetical-deductive method, also known as the method of conjecture and refutation [@popper1979, p. 164], as its problem-solving approach. Procedurally, it applied an enhanced version of Null Hypothesis Significance Testing (NHST), grounded on the original ideas of Neyman-Pearson framework for data testing [@neyman1928; @neyman1928a; @perezgonzalez2015].
The analysis was conducted using Generalized Additive Models ([GAMs](https://en.wikipedia.org/wiki/Generalized_additive_model)) to model and control for potential effects, alongside visual inspections of the data. Results are also presented for each cluster of the Revised Multidimensional Index for Sustainable Food Systems (MISFS-R) [@carvalho2021a; @norde2023].
### Source of Data/Information
The data used in this analysis have as sources:
- The Brazilian Institute of Geography and Statistics ([IBGE](https://www.ibge.gov.br/)) Automatic Retrieval System ([SIDRA](https://sidra.ibge.gov.br/)), for data on GDP per capita and the Gini Index [@ibgen; @ibgel].
- Brazil's Food and Nutrition Surveillance System ([SISVAN](https://sisaps.saude.gov.br/sisvan/)), for data on malnutrition [@sisvana].
- [WorldClim](https://worldclim.org/data/index.html), for data on bioclimatic variables, which allowed us to calculate the Standardised Precipitation Evapotranspiration Index (SPEI) for municipalities in Brazil [@harris2020; @fick2017].
Some data are imported directly from the source, while others rely on external data files available in the [data](https://github.com/sustentarea/gs-data-analysis-report-3/tree/main/data) directory of the code repository.
### Data Wrangling
Data wrangling and analysis followed the data science framework outlined by @wickham2023e, as illustrated in [@fig-wickham-at-al-2024-figure-1]. All processes were made using the R programming language [@rcoreteama], RStudio IDE [@positteama], and several R packages.
The [tidyverse](https://www.tidyverse.org/) and [rOpenSci](https://ropensci.org/) peer-reviewed package ecosystem and other R packages adherents of the tidy tools manifesto [@wickham2023e] were prioritized. All processes were made in order to provide result reproducibility and to be in accordance with the FAIR principles [@wilkinson2016].
::: {#fig-wickham-at-al-2024-figure-1}
{width=75%}
[Source: Reproduced from @wickham2023e.]{.legend}
Model of the data science process created by Wickham, Γetinkaya-Runde, and Grolemund.
:::
The Tidyverse [code style guide](https://style.tidyverse.org/) and [design principles](https://design.tidyverse.org/) were followed to ensure consistency and enhance readability.
All the analyses are 100% reproducible and can be run again at any time. See the [README](https://github.com/sustentarea/gs-data-analysis-report-3/blob/main/README.md) file in the code repository to learn how to run them.
### Model Parameters
The models were built using the `mgcv` R package [@wood]. Since the dependent variables are relative frequencies (continuous), we used the beta distribution family (@fig-beta-distributions) with a logit link function (@eq-logit-link-function)(@fig-logit-funciton)(see @casella2002[p. 591). The [REML](https://en.wikipedia.org/wiki/Restricted_maximum_likelihood) (Restricted Maximum Likelihood) method was used to estimate the smoothing parameters.
$$
\text{logit}(P) = \ln\left(\frac{P}{1 - P}\right) = \beta_{0} + \beta_{1} X_{1} + \cdots + \beta_{k} X_{k}
$$ {#eq-logit-link-function}
::: {#fig-beta-distributions}
```{r}
list <-
dplyr::tibble(
alpha = c(0.5, 5, 1, 2, 2),
beta = c(0.5, 1, 3, 2, 5),
color = c(
"a = b == 0.5",
"a = 5, b = 1",
"a = 1, b = 3",
"a = 2, b = 2",
"a = 2, b = 5"
)
) %>%
split(., seq(nrow(.)))
plot <-
ggplot2::ggplot(NULL, ggplot2::aes(x = x, color = color)) +
ggplot2::labs(
x = "x",
y = "Probability Density Function (PDF)",
color = "Parameters"
) +
ggplot2::scale_y_continuous(limits = c(0, 2.5))+
scale_color_brand_d()
for (i in list) {
plot <-
plot +
ggplot2::stat_function(
data = dplyr::tibble(x = 0:1, color = factor(i$color)),
fun = stats::dbeta,
args = list(shape1 = i$alpha, shape2 = i$beta),
n = 1000,
linewidth = 1.5
)
}
plot |> print() |> rutils::shush()
```
[Source: Created by the authors.]{.legend}
Examples of beta distributions with different parameters.
:::
::: {#fig-logit-funciton}
```{r}
ggplot2::ggplot() +
ggplot2::stat_function(
data = dplyr::tibble(x = 0:1, color = factor(i$color)),
fun = stats::qlogis,
args = list(location = 0, scale = 1, log = FALSE),
n = 1000,
linewidth = 1.5,
color = get_brand_color("red")
) +
ggplot2::geom_hline(
yintercept = 0,
linewidth = 0.25,
linetype = "dashed",
color = get_brand_color("grey")
) +
ggplot2::geom_vline(
xintercept = 0.5,
linewidth = 0.25,
linetype = "dashed",
color = get_brand_color("grey")
) +
ggplot2::lims(
x = c(0, 1),
y = c(-6, 6)
) +
ggplot2::labs(
x = "Probability",
y = "Logit(0,1)"
)
```
[Source: Created by the authors.]{.legend}
Example of a logit function (i.e., the quantile function of the logistic distribution).
:::
### Hypothesis Testing
We tested whether SPEI significantly improves model fit when predicting MBEPR & BEIPR (stunting) and MAPER & MPEPR (wasting) with nested models. We compared a restricted model (excluding SPEI) with a full model (including SPEI). To ensure practical significance, we applied a Minimum Effect Size (MES) criterion, following the original Neyman-Pearson framework for hypothesis testing [@neyman1928; @neyman1928a; @perezgonzalez2015].
The MES was set at Cohen's threshold for small effects ($f^2 = 0.02$, equivalent to $\text{R}^2 = 0.01960784$). Thus, SPEI was considered significant only if its inclusion accounted for at least $1.960784\%$ of the variance in the dependent variable.
The test was structured as follows:
- **Null hypothesis** ($\text{H}_{0}$): Adding *SPEI* does not meaningfully improve the modelβs fit, indicated by a negligible change in the adjusted $\text{R}^{2}$ or a non-significant F-test (with a Type I error probability ($\alpha$) of $0.05$).
- **Alternative Hypothesis** ($\text{H}_{a}$): Adding *SPEI* meaningfully improves the modelβs fit, indicated by an increase in the adjusted $\text{R}^{2}$ exceeding the MES and a significant F-test (with $\alpha < 0.05$).
Formally:
$$
\begin{cases}
\text{H}_{0}: \Delta \ \text{Adjusted} \ \text{R}^{2} \leq \text{MES} \quad \text{or} \quad \text{F-test is not significant} \ (\alpha \geq 0.05) \\
\text{H}_{a}: \Delta \ \text{Adjusted} \ \text{R}^{2} > \text{MES} \quad \text{and} \quad \text{F-test is significant} \ (\alpha < 0.05)
\end{cases}
$$
Where:
$$
\Delta \ \text{Adjusted} \ \text{R}^{2} = \text{Adjusted} \ \text{R}^{2}_{\text{full}} - \text{Adjusted} \ \text{R}^{2}_{\text{restricted}}
$$
The restricted model is the same as the first model presented in this document, minus the SPEI variable: ~ `te(gini_index, gdp_per_capita)` + `s(year)` (Continuous `year`).
### Interpretation of Results
#### Standardized Precipitation Evapotranspiration Index (SPEI)
Since the original SPEI authors [@vicente-serrano2010] did not establish definitive thresholds for SPEI values and their corresponding drought conditions, we adopt the benchmark values provided by @mehr2020 (@tbl-mehr-et-al-2020-table-2).
::: {#tbl-mehr-et-al-2020-table-2}
| Classification | SPI Threshold | SPEI Threshold |
|-------------------------|--------------------|--------------------|
| Extremely wet | 2.0 β€ SPI | 1.83 β€ SPEI |
| Severely wet | 2.0 > SPI β₯ 1.5 | 1.82 > SPEI β₯ 1.43 |
| Moderately wet | 1.49 > SPI β₯ 1.0 | 1.42 > SPEI β₯ 1.0 |
| Near normal | -1.0 β€ SPI β€ 1.0 | -1.0 β€ SPEI β€ 1.0 |
| Moderate drought (MoD) | -1.49 β€ SPI < -1.0 | -1.42 β€ SPEI < -1.0|
| Severe drought (SD) | -2.0 β€ SPI < -1.5 | -1.82 β€ SPEI < -1.43|
| Extreme drought (ED) | SPI < -2.0 | SPEI < -1.83 |
[Source: Reproduced from @mehr2020.]{.legend}
Classifications of wet and dry conditions using SPI and SPEI indices.
:::
#### Revised Multidimensional Index for Sustainable Food Systems (MISFS-R)
Results are also presented for each cluster of the Revised Multidimensional Index for Sustainable Food Systems (MISFS-R) ([@fig-norde-2023-figure-6]).
The MISFS is a tool designed to assess the sustainability of food systems at a subnational level in Brazil, incorporating local behaviors and practices. The MISFS-R is a revised version that introduces new indicators and a refined methodology for calculating the index ([@fig-norde-2023-figure-6]). For more details, see @carvalho2021a and @norde2023.
::: {#fig-norde-2023-figure-6}
{width=90%}
[Source: Reproduced from @norde2023.]{.legend}
Dendrogram for cluster analysis between Brazilian states considering all the Revised Multidimensional Index for Sustainable Food Systems (MISFS-R) indicators and geographical location of each cluster.
:::
#### Pratical Significance
To ensure practical significance, the adjusted $\text{R}^2$ of the models are analysed for their effect sizes considering a confidence interval of $95\%$. We use @cohen1988a benchmark for interpretation.
A $\text{R}^2$ less than $\approx 0.0196$ is considered negligeble.
> SMALL EFFECT SIZE: $f^2 = .02$. Translated into $\text{R}^{2}$ or partial $\text{R}^{2}$ for Case 1, this gives $.02 / (1 + .02) = .0196$. We thus define a small effect as one that accounts for 2% of the $\text{Y}$ variance (in contrast with 1% for $r$), and translate to an $\text{R} = \sqrt{0196} = .14$ (compared to .10 for $r$). This is a modest enough amount, **just barely escaping triviality** and (alas!) all too frequently in practice represents the true order of magnitude of the effect being tested [@cohen1988a, p. 413].
> [...] in many circumstances, all that is intended by "proving" the null hypothesis is that the ES [Effect Size] is not necessarily zero but **small enough to be negligible** [...]. [@cohen1988a, p. 461].
## Setting the Enviroment
```{r}
#| eval: false
library(beepr)
library(broom)
library(cli)
library(clipr)
library(colorspace)
library(dplyr)
library(effectsize)
library(GGally)
library(geobr)
library(ggplot2)
library(ggspatial)
library(glmmTMB)
library(here)
library(janitor)
library(lme4)
library(lubridate)
library(lubritime) # github.com/danielvartan/lubritime
library(magrittr)
library(mgcv)
library(mgcViz)
library(MuMIn)
library(pal) # gitlab.com/rpkg.dev/pal
library(patchwork)
library(performance)
library(polyglotr)
library(prettycheck) # github.com/danielvartan/prettycheck
library(psychometric)
library(purrr)
library(r2glmm)
library(ragg)
library(RColorBrewer)
library(readr)
library(readxl)
library(rutils) # github.com/danielvartan/rutils
library(sidrar)
library(stats)
library(stringr)
library(summarytools)
library(tidyr)
```
```{r}
#| include: false
library(magrittr)
library(mgcv)
library(GGally)
library(ggplot2)
library(summarytools)
```
```{r}
source(here::here("R", "cohens_f_squared.R"))
source(here::here("R", "get_and_aggregate_sidra_by_year.R"))
source(here::here("R", "gam_misfs.R"))
source(here::here("R", "plot_brazil.R"))
source(here::here("R", "plot_dist.R"))
source(here::here("R", "plot_gam.R"))
source(here::here("R", "plot_ggally.R"))
source(here::here("R", "summarise_coefs.R"))
source(here::here("R", "summarise_r2.R"))
source(here::here("R", "tabset_panel_brazil_municipality.R"))
source(here::here("R", "tabset_panel_gam.R"))
source(here::here("R", "tabset_panel_gam_by_misfs.R"))
source(here::here("R", "tabset_panel_var_distribution.R"))
source(here::here("R", "tabset_panel_var_distribution_by_misfs.R"))
source(here::here("R", "utils.R"))
source(here::here("R", "utils-plots.R"))
```
## Importing and Tidying the Data
### Nutrition Data
Based on SISVAN [Nutritional Status](https://opendatasus.saude.gov.br/dataset/sisvan-estado-nutricional) dataset [@sisvana].
```{r}
#| eval: false
#| include: false
test_nutrition_data <-
here::here("data", "Banco_dados_malnutritio_clima - Adaptado.csv") |>
readr::read_csv(col_types = readr::cols(.default = "c")) |>
janitor::clean_names() |>
dplyr::rename(
year = ano,
municipality_code = code_muni,
) |>
dplyr::select(year, municipality_code, mbepr, beipr, maper, mpepr) |>
dplyr::mutate(
dplyr::across(
.cols = -dplyr::all_of(c("year", "municipality_code")),
.fns = as.numeric
),
dplyr::across(
.cols = dplyr::all_of(c("year", "municipality_code")),
.fns = as.integer
)
)
```
```{r}
#| output: false
nutrition_data <-
here::here("data", "Banco_dados_malnutritio_clima - Adaptado.csv") |>
readr::read_csv(col_types = readr::cols(.default = "c")) |>
janitor::clean_names() |>
dplyr::rename(
year = ano,
municipality_code = code_muni,
sisvan_cover = cobrs,
number_of_children = n_ao_de_criana_as_municipio_x,
n_mbepr = muito_baixa_e_i_n_x,
n_beipr = baixa_e_i_n_x,
n_maper = magreza_acentuada_p_e_n_x,
n_mpepr = magreza_p_e_n_x
) |>
dplyr::select(
year, municipality_code, misf, number_of_children, sisvan_cover,
n_mbepr, n_beipr, n_maper, n_mpepr
) |>
dplyr::mutate(
dplyr::across(
.cols = dplyr::all_of(
c("number_of_children", "n_mbepr", "n_beipr", "n_maper", "n_mpepr")
),
.fns = ~
dplyr::case_when(
!(as.numeric(.x) %% 1 == 0) ~ stringr::str_remove(.x, "\\."),
TRUE ~ .x
) |>
as.integer()
)
) |>
dplyr::mutate(
# year = as.integer(year),
year =
year |>
factor(
levels = year |> unique() |> sort(),
ordered = TRUE
),
municipality_code = as.integer(municipality_code),
misf = factor(misf, levels = c("A", "B", "C", "D"), ordered = FALSE),
sisvan_cover = as.numeric(sisvan_cover),
mbepr = n_mbepr,
beipr = n_beipr,
n_mbepr_beipr = n_mbepr + n_beipr,
mbepr_beipr = n_mbepr_beipr,
maper = n_maper,
mpepr = n_mpepr,
n_maper_mpepr = n_maper + n_mpepr,
maper_mpepr = n_maper_mpepr
) |>
dplyr::filter(
dplyr::between(sisvan_cover, 0, 1),
number_of_children >= 0,
number_of_children > n_mbepr,
number_of_children > n_beipr,
number_of_children > mbepr_beipr,
number_of_children > n_maper,
number_of_children > n_mpepr,
number_of_children > maper_mpepr
) |>
dplyr::mutate(
dplyr::across(
.cols = dplyr::all_of(
c("mbepr", "beipr", "mbepr_beipr", "maper", "mpepr", "maper_mpepr")
),
.fns = ~
.x %>%
`/`(number_of_children * sisvan_cover)
),
dplyr::across(
.cols = dplyr::all_of(
c("mbepr", "beipr", "mbepr_beipr", "maper", "mpepr", "maper_mpepr")
),
.fns = ~ pmax(0.00001, pmin(.x, 0.99999))
)
) |>
dplyr::select(
year, municipality_code, misf, number_of_children, sisvan_cover,
n_mbepr, mbepr, n_beipr, beipr, n_mbepr_beipr, mbepr_beipr,
n_maper, maper, n_mpepr, mpepr, n_maper_mpepr, maper_mpepr
)
```
```{r}
#| eval: false
#| include: false
var <- "maper"
diff <- (nutrition_data[[var]] - test_nutrition_data[[var]])
summary(diff)
diff |> hist(breaks = seq(-1, 1, 0.01))
```
### GDP Per Capita Data
Source: IBGE-SIDRA [Table 5938](https://sidra.ibge.gov.br/tabela/5938) β Gross domestic product at current prices, taxes net of subsidies on products at current prices, and gross value added at current prices, total and by economic activity, and their respective shares - **Reference year 2010** [@ibgen].
```{r}
#| include: false
file_path <- here::here("data", "ibge-table-5938.rds")
if (prettycheck:::test_file_exists(file_path)) {
ibge_table_5938 <- readRDS(file_path)
} else {
cli::cli_abort("Data file not found.")
}
```
```{r}
#| eval: false
#| output: false
tictoc::tic()
ibge_table_5938 <-
get_and_aggregate_sidra_by_year(
years = nutrition_data$year |> unique(),
api_start = "/t/5938/n6/all/v/37/p/",
api_end = "/d/v37%200"
) |>
regularize_col_names()
tictoc::toc()
beepr::beep(1)
Sys.sleep(3)
```
```{r}
#| eval: false
#| output: false
# Last download: 2025-01-22 (~6m1s)
ibge_table_5938 |>
saveRDS(here::here("data", "ibge-table-5938.rds"))
ibge_table_5938 |>
readr::write_csv(here::here("data", "ibge-table-5938.csv"))
```
Source: IBGE-SIDRA [Table 6579](https://sidra.ibge.gov.br/tabela/6579) β Estimated resident population [@ibgel].
```{r}
#| include: false
file_path <- here::here("data", "ibge-table-6579.rds")
if (prettycheck:::test_file_exists(file_path)) {
ibge_table_6579 <- readRDS(file_path)
} else {
cli::cli_abort("Data file not found.")
}
```
```{r}
#| eval: false
#| output: false
tictoc::tic()
ibge_table_6579 <-
get_and_aggregate_sidra_by_year(
years = nutrition_data$year |> unique(),
api_start = "/t/6579/n6/all/v/all/p/",
api_end = ""
) |>
regularize_col_names()
tictoc::toc()
beepr::beep(1)
Sys.sleep(3)
```
```{r}
#| eval: false
#| output: false
# Last download: 2025-01-22 (~4m42s)
ibge_table_6579 |>
saveRDS(here::here("data", "ibge-table-6579.rds"))
ibge_table_6579 |>
readr::write_csv(here::here("data", "ibge-table-6579.csv"))
```
```{r}
gdp_data <-
ibge_table_5938 |>
dplyr::left_join(
ibge_table_6579,
by = c("year", "municipality_code")
) |>
dplyr::select(year, municipality_code, municipality.x, value.x, value.y) |>
dplyr::rename(
gdp = value.x,
population = value.y,
municipality = municipality.x
) |>
dplyr::filter(gdp >= 0, population >= 0) |>
dplyr::mutate(
# year = as.integer(year),
year =
year |>
factor(
levels = year |> unique() |> sort(),
ordered = TRUE
),
gdp = gdp * 1000,
gdp_per_capita = gdp / population,
municipality_code = municipality_code |> as.integer()
)
```
### Gini Index Data
::: {.callout-note}
The Brazilian Institute of Geography and Statistics ([IBGE](https://www.ibge.gov.br/)) Automatic Retrieval System ([SIDRA](https://sidra.ibge.gov.br/)) provides GINI data by municipality only for the year 1991 ([Table 115](https://sidra.ibge.gov.br/tabela/155)) [@ibgeo]. However, we found data processed by the Institute for Applied Economic Research ([IPEA](https://www.ipea.gov.br)), which had access to census data for the years 2000 and 2010 [@ibgep].
:::
Source: IBGE/IPEA β Gini Index of per capita household income by municipality: Period: 1991, 2000, and 2010 [@ibgel].
```{r}
gini_data <-
"http://tabnet.datasus.gov.br/cgi/ibge/censo/bases/ginibr.csv" |>
readr::read_delim(
delim = ";",
col_names = FALSE,
col_types = readr::cols(.default = "c"),
# locale = readr::locale(decimal_mark = ","),
trim_ws = TRUE,
skip = 3
) |>
dplyr::rename_with(~ c("municipality", "x1991", "x2000", "x2010")) |>
dplyr::slice(1:5565) |>
dplyr::mutate(
dplyr::across(
.cols = dplyr::everything(),
.fns = ~ iconv(.x, from = "latin1", to = "UTF-8")
),
dplyr::across(
.cols = dplyr::starts_with("x"),
.fns = ~ dplyr::case_when(
.x == "..." ~ NA,
TRUE ~ .x |> stringr::str_replace_all(",", ".")
)
),
dplyr::across(
.cols = dplyr::starts_with("x"),
.fns = as.numeric
)
) |>
tidyr::pivot_longer(
cols = starts_with("x"),
names_to = "year",
values_to = "gini_index"
) |>
dplyr::mutate(
year = year |> stringr::str_remove("x") |> as.integer(),
municipality_code =
municipality |>
stringr::str_extract("\\d*") |>
as.integer(),
municipality =
municipality |>
stringr::str_remove("\\d*") |>
stringr::str_trim()
)|>
dplyr::relocate(year, municipality_code, .before = municipality)
```
```{r}
#| eval: false
#| include: false
# Last download: 2025-01-29 (~5s)
gini_data |>
saveRDS(here::here("data", "ibge_gini_index.rds"))
gini_data |>
readr::write_csv(here::here("data", "ibge_gini_index.csv"))
```
### SPEI Data
Based on WorldClim 2.1 [Historical Monthly Weather]((https://worldclim.org/data/monthlywth.html)) dataset [@harris2020; @fick2017].
```{r}
#| output: false
spei_data <-
here::here("data", "spei_Extreme_drought_event_municipality_year2.csv") |>
readr::read_csv(col_types = readr::cols(.default = "c")) |>
janitor::clean_names() |>
dplyr::rename(municipality_code = code_muni) |>
dplyr::select(
municipality_code,
dplyr::all_of(paste0("spei_12m_", nutrition_data$year |> unique()))
) |>
dplyr::mutate(
dplyr::across(
.cols = dplyr::starts_with("spei_12m"),
.fns = as.numeric
)
) |>
tidyr::pivot_longer(
cols = starts_with("spei_12m"),
names_to = "year",
values_to = "spei_12m"
) |>
dplyr::mutate(
year =
year |>
stringr::str_remove("spei_12m_") |>
as.integer(),
year =
year |>
factor(
levels = year |> unique() |> sort(),
ordered = TRUE
),
municipality_code = municipality_code |> as.integer()
)
```
### Analysis Data
<!-- https://geoftp.ibge.gov.br/organizacao_do_territorio/estrutura_territorial/evolucao_da_divisao_territorial_do_brasil/evolucao_da_divisao_territorial_do_brasil_1872_2010/notas_tecnicas.pdf -->
```{r}
#| output: false
data <-
nutrition_data |>
dplyr::mutate(year = year |> as.character()) |>
# In order to adapt the data to the municipalities in the nutrition and
# gini data, since they do not have the check digit.
dplyr::rename(temp_municipality_code = municipality_code) |>
dplyr::left_join(
y = gdp_data |>
dplyr::mutate(
year = year |> as.character(),
temp_municipality_code =
municipality_code |>
stringr::str_sub(end = -2) |>
as.integer()
),
by = c("year", "temp_municipality_code")
) |>
dplyr::left_join(
y =
gini_data |>
dplyr::filter(year == 2010) |>
dplyr::select(municipality_code, gini_index) |>
dplyr::rename(temp_municipality_code = municipality_code),
by = c("temp_municipality_code"),
relationship = "many-to-one"
) |>
dplyr::left_join(
y = spei_data |>
dplyr::mutate(
year = year |> as.character(),
temp_municipality_code =
municipality_code |>
stringr::str_sub(end = -2) |>
as.integer()
),
by = c("year", "temp_municipality_code")
) |>
dplyr::rename(
municipality_code = municipality_code.x
) |>
dplyr::select(
year, municipality_code, municipality, misf,
number_of_children, sisvan_cover,
mbepr, beipr, mbepr_beipr, maper, mpepr, maper_mpepr,
gini_index, gdp_per_capita, spei_12m
) |>
dplyr::mutate(
# year = as.integer(year),
year =
year |>
factor(
levels = year |> unique() |> sort(),
ordered = TRUE
)
) |>
dplyr::filter(sisvan_cover >= 0.05) |>
tidyr::drop_na(number_of_children, sisvan_cover)
data
```
```{r}
#| output: false
dplyr::tibble(
n = data |> nrow(),
n_sisvan_cover_less_than_0_05 =
data |>
dplyr::filter(sisvan_cover < 0.05) |>
nrow(),
n_sisvan_cover_more_than_1 =
data |>
dplyr::filter(sisvan_cover > 1) |>
nrow(),
n_brazil_municipalities =
geobr::read_municipality(year = 2022, showProgress = FALSE) |>
dplyr::pull(code_muni) |>
length() |>
rutils::shush(),
missing_municipalities =
geobr::read_municipality(year = 2022, showProgress = FALSE) |>
dplyr::pull(code_muni) |>
as.integer() |>
setdiff(data |> dplyr::pull(municipality_code)) |>
length() |>
rutils::shush()
) |>
tidyr::pivot_longer(cols = dplyr::everything())
```
#### Dictionary
- `year`: Year of data collection.
- `municipality_code`: Brazilian Institute of Geography and Statistics (IBGE) municipality code.
- `misf`: Cluster of the Revised Multidimensional Index for Sustainable Food Systems (MISFS-R) (A, B, C, or D).
- `number_of_children`: Number of children under five years old in the municipality.
- `sisvan_cover`: Proportion of children under five covered by the Brazilian Food and Nutrition Surveillance System (SISVAN) in the municipality.
- `mbepr`: Relative frequency of children under five years old with Very Short Stature for Age (*Muito Baixa Estatura Para a Idade*) in the municipality.
- `beipr`: Relative frequency of children under five years old with Short Stature for Age (*Baixa Estatura Para Idade*) in the municipality.
- `mbepr_beipr`: Relative frequency of children under five years old with Very Short/Short Stature for Age (*Muito Baixa/Baixa Estatura Para Idade*) in the municipality.
- `maper`: Relative frequency of children under five years old with Severe Thinness for Height (*Magreza Acentuada Para a Estatura*) in the municipality.
- `mpepr`: Relative frequency of children under five years old with Thinness for Height (*Magreza Por Estatura*) in the municipality.
- `maper_mpepr`: Relative frequency of children under five years old with Severe/Moderate Thinness for Height (*Magreza Acentuada/Moderada Para a Estatura*) in the municipality.
- `gini_index`: Gini index of per capita household income in the municipality at 2010. Due to limited data availability at the municipal level, all values are based on the 2010 Brazilian Census.
- `gdp_per_capita`: Gross Domestic Product (GDP) per capita in the municipality in Brazilian Reais (BRL) based at current prices on 2010.
- `spei_12m`: Standardised Precipitation Evapotranspiration Index (SPEI) in a 12-month times cale for the municipality.
## Checking SISVAN Cover
```{r}
brand_div_palette <- function(x) {
make_color_vector(
n_prop = x,
colors = c(
get_brand_color("dark-red"),
# get_brand_color("white"),
get_brand_color_mix(
position = 950,
color_1 = "dark-red",
color_2 = "dark-red-triadic-blue",
alpha = 0.5
),
get_brand_color("dark-red-triadic-blue")
)
)
}
```
```{r}
#| eval: false
#| include: false
plot <-
data |>
dplyr::mutate(sisvan_cover = sisvan_cover * 100) |>
dplyr::filter(year == 2019) |>
plot_brazil_municipality(
col_fill = "sisvan_cover",
comparable_areas = TRUE,
transform = "identity",
breaks = seq(0, 100, 25),
reverse = FALSE,
limits = c(0, 100),
print = FALSE,
palette = brand_div_palette
) +
ggplot2::labs(
title = "Year: 2019"
)
plot |> print() |> rutils::shush()
```
```{r}
#| eval: false
# Run this chunk to produce the plots and the animation.
data |>
dplyr::mutate(sisvan_cover = sisvan_cover * 100) |>
animate_plot_brazil_municipality(
col_fill = "sisvan_cover",
col_group = "year",
group_label = "Year",
comparable_areas = TRUE
suffix = NULL,
width = 1344,
height = 960,
dpi = 150,
transform = "identity",
breaks = seq(0, 100, 25),
reverse = FALSE,
limits = c(0, 100),
palette = brand_div_palette
)
```
```{r}
#| eval: false
#| include: false
# Run this chunk to produce the panel-tabset for the plots.
tabset_panel_brazil_municipality(
col = "sisvan_cover",
caption = long_string(
"
Percentage of children under five monitored by Brazil's Food and
Nutrition Surveillance System (SISVAN) across Brazil's
historically comparable municipalities
(minimum comparable areas [@ehrl2017]).
"
),
source = "Created by the authors based on data from @sisvana and @ibgel.",
years = 2008:2019,
heading = "###"
)
```
{{< include ./qmd/_panel-tabset-brazil-municipality-sisvan-cover.qmd >}}
## Checking SPEI Variations
### Year-to-Year comparison
```{r}
#| eval: false
#| include: false
plot <-
data |>
dplyr::filter(year == 2019) |>
plot_brazil_municipality(
col_fill = "spei_12m",
comparable_areas = TRUE,
transform = "identity",
# breaks = c(-2, -1.83, -1.43, -1, 1, 1.42, 1.82, 2),
breaks = seq(-2 , 2, 0.5),
reverse = FALSE,
limits = c(-2, 2),
print = FALSE,
palette = brand_div_palette
) +
ggplot2::labs(
title = "Year: 2019"
)
plot |> print() |> rutils::shush()
```
```{r}
#| eval: false
# Run this chunk to produce the plots and the animation.
data |>
animate_plot_brazil_municipality(
col_fill = "spei_12m",
col_group = "year",
group_label = "Year",
comparable_areas = TRUE
suffix = NULL,
width = 1344,
height = 960,
dpi = 150,
transform = "identity",
# breaks = c(-2, -1.83, -1.43, -1, 1, 1.42, 1.82, 2),
breaks = seq(-2 , 2, 0.5),
reverse = FALSE,
limits = c(-2, 2),
palette = brand_div_palette
)
```
```{r}
#| eval: false
#| include: false
# Run this chunk to produce the panel-tabset for the plots.
tabset_panel_brazil_municipality(
col = "spei_12m",
caption = long_string(
"
Standardized Precipitation Evapotranspiration Index (SPEI)
across Brazil's historically comparable municipalities
(minimum comparable areas [@ehrl2017]).
"
),
source = long_string(
"
Created by the authors using data from the WorldClim 2.1
[Historical Monthly Weather](https://worldclim.org/data/monthlywth.html)
dataset [@harris2020; @fick2017].
"
),
years = 2008:2019,
heading = "####"
)
```
{{< include ./qmd/_panel-tabset-brazil-municipality-spei-12m.qmd >}}
### Comparison with External Data
@fig-fao-2025-figure-annex-6-1 presents an independent analysis for comparison, while our results are shown in @fig-spei-comparison.
::: {#fig-fao-2025-figure-annex-6-1}
{width=90%}
[Source: Reproduced from @fao2025[Figure VI.1, p. 185].]{.legend}
Average Standardized Precipitation Evapotranspiration Index (SPEI) across Brazil's historically comparable municipalities (minimum comparable areas [@ehrl2017]).
:::
::: {#fig-spei-comparison}
```{r}
plot <-
data |>
dplyr::filter(year >= 2010, year <= 2019) |>
plot_brazil_municipality(
col_fill = "spei_12m",
comparable_areas = TRUE,
transform = "identity",
binned = FALSE,
breaks = seq(-2 , 2, 0.5),
reverse = FALSE,
limits = c(-2, 2),
print = FALSE,
quiet = TRUE,
palette = function(x) {
make_color_vector(
n_prop = x,
colors = c(
"#6F0322",
"#BC2D32",
"#E28668",
"#F9D0BC",
"#F1F1F3",
"#BBDAE9",
"#65A9D1",
"#266DB1",
"#0D2648"
)
)