OD SAO PAULO

library(foreign)
library(questionr)
library(tidyverse)
-- Attaching packages --------------------------------------- tidyverse 1.3.2 --
v ggplot2 3.3.6      v purrr   0.3.5 
v tibble  3.1.8      v dplyr   1.0.10
v tidyr   1.2.1      v stringr 1.4.1 
v readr   2.1.3      v forcats 0.5.2 
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
library(easystats)
# Attaching packages: easystats 0.5.2 (red = needs update)
x insight     0.18.4   v datawizard  0.6.2 
v bayestestR  0.13.0   v performance 0.10.0
v parameters  0.19.0   v effectsize  0.8.0 
v modelbased  0.8.5    v correlation 0.8.3 
v see         0.7.3    v report      0.5.5 

Restart the R-Session and update packages in red with `easystats::easystats_update()`.
library(gtsummary)
#Uighur
library(gt)
library(lme4)
Le chargement a nécessité le package : Matrix

Attachement du package : 'Matrix'

Les objets suivants sont masqués depuis 'package:tidyr':

    expand, pack, unpack
library(brms)
Le chargement a nécessité le package : Rcpp
Loading 'brms' package (version 2.18.0). Useful instructions
can be found by typing help('brms'). A more detailed introduction
to the package is available through vignette('brms_overview').

Attachement du package : 'brms'

L'objet suivant est masqué depuis 'package:lme4':

    ngrps

L'objet suivant est masqué depuis 'package:stats':

    ar
library(RColorBrewer)
library(sf)
Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 7.2.1; sf_use_s2() is TRUE
library(flextable)

Attachement du package : 'flextable'

Les objets suivants sont masqués depuis 'package:gtsummary':

    as_flextable, continuous_summary

L'objet suivant est masqué depuis 'package:purrr':

    compose
setwd("C:/Users/bmotteba/Nextcloud/Donnees/Bresil/OD_SaoPaulo/OD_2017/Banco de dados")
load("ba.RData")

Cross table Home based vs Outdoor Worplace

#show_header_names(z1) #connaitre le nom des colonnes
ba %>% tbl_cross(TRAB1_REf, TRABEXT1f, percent = "cell"
             , label = c(1 ~ "Home based" , 2 ~ "Outdoors")
                  ) %>% bold_labels()  %>% as_gt()%>%
  tab_header(title = "Workers by type of workplace",
    subtitle = "and the 3 groups included in the analysis") %>%
  tab_source_note(source_note = "Source: Data: Sao Paulo OD survey 2007 & 2017") %>%
  tab_footnote(    footnote = "Home Based Work",
    locations = cells_body(columns = stat_2, 2)) %>%
  tab_footnote(    footnote = "Outdoor Workers without a known work address",
    locations = cells_body(columns = stat_1, 3)) %>%
  tab_footnote(    footnote = "Worker reference group",
    locations = cells_body(columns = stat_2, 3))
Workers by type of workplace
and the 3 groups included in the analysis
Outdoors Total
Yes No
Home based
    Yes 2,000 (2.6%) 6,436 (8.4%)1 8,436 (11%)
    No 9,618 (13%)2 55,093 (72%)3 64,711 (84%)
    Other 1,727 (2.3%) 1,868 (2.4%) 3,595 (4.7%)
Total 13,345 (17%) 63,397 (83%) 76,742 (100%)
Source: Data: Sao Paulo OD survey 2007 & 2017
1 Home Based Work
2 Outdoor Workers without a known work address
3 Worker reference group

Preparation & summary of variables

# On enl�ve les agriculteurs, les employ�s de maison log�s sur place
# On ne s�lectionne que les individus qui d�clarent TRAB1_RE == 1
ba$Workplace <- "Other"
ba$Workplace[ba$TRAB1_REf == "Yes" & ba$TRABEXT1f == "No"] <- "Home Based"
ba$Workplace[ba$TRAB1_REf == "No" & ba$TRABEXT1f == "Yes"] <- "Outdoor"
ba$Workplace[ba$TRAB1_REf == "No" & ba$TRABEXT1f == "No"] <- "Reference"

bar <- subset(ba, SETOR1_rec != 1 & SIT_FAM_rec != 6 & Workplace != "Other")
bar$SIT_FAM_rec <- factor(bar$SIT_FAM_rec)
bar$SETOR1_rec <- factor(bar$SETOR1_rec)


z1 <- bar %>% tbl_cross(yearf, Workplace, percent = "col", margin = "column", label = list(yearf = "Year")) %>% add_p()
z2 <- bar %>% tbl_cross(VINC1_rec, Workplace, percent = "col", margin = "column", label = list(VINC1_rec = "Job type")) %>% add_p()
z3 <- bar %>% tbl_cross(SETOR1_rec, Workplace, percent = "col", margin = "column", label = list(SETOR1_rec = "Activity sector")) %>% add_p()
z4 <- bar %>% 
  tbl_cross(SEXOf, Workplace, percent = "col", margin = "column", label = list(SEXOf = "Gender")) %>% add_p() 
z5 <- bar %>% 
  tbl_cross(single, Workplace, percent = "col", margin = "column", label = list(single = "Single")) %>% add_p() 
z6 <- bar %>% 
  tbl_cross(GRAU_INS_rec, Workplace, percent = "col", margin = "column", label = list(GRAU_INS_rec = "Education level")) %>% add_p()
z7 <- bar %>% 
  tbl_cross(QT_AUTO_rec, Workplace, percent = "col", margin = "column", label = list(QT_AUTO_rec = "Number of cars")) %>% add_p()

tbl_stack(tbls = list(z1, z2, z3, z4, z5, z6, z7)) %>% bold_labels() %>% bold_labels() %>% as_gt()%>%
  tab_header(title = "Description of the variables used in the models") %>%
  tab_source_note(source_note = "Source: Data: Sao Paulo OD survey 2007 & 2017")
Description of the variables used in the models
Workplace Total p-value1
Home Based Outdoor Reference
Year <0.001
    2007 2,570 (48%) 7,373 (77%) 27,005 (49%) 36,948 (53%)
    2017 2,730 (52%) 2,199 (23%) 27,908 (51%) 32,837 (47%)
Job type <0.001
    1 503 (9.5%) 4,786 (50%) 34,449 (63%) 39,738 (57%)
    2 251 (4.7%) 906 (9.5%) 5,023 (9.1%) 6,180 (8.9%)
    3 5 (<0.1%) 614 (6.4%) 4,129 (7.5%) 4,748 (6.8%)
    4 2,841 (54%) 2,334 (24%) 4,739 (8.6%) 9,914 (14%)
    5 137 (2.6%) 326 (3.4%) 2,078 (3.8%) 2,541 (3.6%)
    6 721 (14%) 345 (3.6%) 2,002 (3.6%) 3,068 (4.4%)
    7 842 (16%) 261 (2.7%) 2,493 (4.5%) 3,596 (5.2%)
Activity sector <0.001
    11 113 (2.1%) 332 (3.5%) 4,895 (8.9%) 5,340 (7.7%)
    2 87 (1.6%) 392 (4.1%) 1,127 (2.1%) 1,606 (2.3%)
    3 385 (7.3%) 864 (9.0%) 6,694 (12%) 7,943 (11%)
    4 889 (17%) 1,789 (19%) 9,913 (18%) 12,591 (18%)
    5 27 (0.5%) 554 (5.8%) 629 (1.1%) 1,210 (1.7%)
    6 8 (0.2%) 707 (7.4%) 637 (1.2%) 1,352 (1.9%)
    7 54 (1.0%) 237 (2.5%) 2,322 (4.2%) 2,613 (3.7%)
    8 324 (6.1%) 104 (1.1%) 1,212 (2.2%) 1,640 (2.4%)
    9 286 (5.4%) 280 (2.9%) 2,454 (4.5%) 3,020 (4.3%)
    10 113 (2.1%) 501 (5.2%) 4,755 (8.7%) 5,369 (7.7%)
    12 2,997 (57%) 3,288 (34%) 18,060 (33%) 24,345 (35%)
    13 17 (0.3%) 524 (5.5%) 2,215 (4.0%) 2,756 (3.9%)
Gender <0.001
    1 2,634 (50%) 6,699 (70%) 28,077 (51%) 37,410 (54%)
    2 2,666 (50%) 2,873 (30%) 26,836 (49%) 32,375 (46%)
Single <0.001
    0 4,679 (88%) 9,021 (94%) 51,528 (94%) 65,228 (93%)
    1 621 (12%) 551 (5.8%) 3,385 (6.2%) 4,557 (6.5%)
Education level <0.001
    3 1,718 (32%) 2,878 (30%) 12,125 (22%) 16,721 (24%)
    4 1,718 (32%) 3,863 (40%) 22,257 (41%) 27,838 (40%)
    5 1,864 (35%) 2,831 (30%) 20,531 (37%) 25,226 (36%)
Number of cars <0.001
    0 1,815 (34%) 3,184 (33%) 19,515 (36%) 24,514 (35%)
    1 2,435 (46%) 4,105 (43%) 23,252 (42%) 29,792 (43%)
    2 837 (16%) 1,719 (18%) 9,324 (17%) 11,880 (17%)
    3 213 (4.0%) 564 (5.9%) 2,822 (5.1%) 3,599 (5.2%)
Source: Data: Sao Paulo OD survey 2007 & 2017
1 Pearson's Chi-squared test

Model 1 & 2 : Outdoors & Home-based Work

# On enl�ve les agriculteurs, les employ�s de maison log�s sur place
# On ne s�lectionne que les individus qui d�clarent TRAB1_RE == 1
ba$SIT_FAM_rec <- factor(ba$SIT_FAM_rec)
ba$SETOR1_rec <- factor(ba$SETOR1_rec)
ba$TRAB1_REb <- ifelse(ba$TRAB1_RE == 1, 1, 0)
ba$TRABEXT1b <- ifelse(ba$TRABEXT1 == 1, 1, 0)

#Model 1 Unknown workplace outside of home vs Known Workplace (no HBW)
ba1 <- subset(ba, SETOR1_rec != 1 & SIT_FAM_rec != 6 & TRAB1_REf == "No" )
model1<-glmer(TRABEXT1b ~ 1 + (0 + yearf | VINC1_rec) + yearf
             + SIT_FAM_rec + SEXOf + QT_AUTO_rec + GRAU_INS_rec + SETOR1_rec                     + IDADE + single
            ,family = binomial, nAGQ = 0 #, REML=F
            ,control = glmerControl(calc.derivs = FALSE)
            ,data = ba1)

#Model 2 Home based work (HBW) vs Known Workplace
ba2 <- subset(ba, SETOR1_rec != 1 & SIT_FAM_rec != 6 & TRAB1_RE != 3 & TRABEXT1f == "No" )
model2<-glmer(TRAB1_REb ~ 1 + (0 + yearf | VINC1_rec) + yearf
             + SIT_FAM_rec + SEXOf + QT_AUTO_rec + GRAU_INS_rec + SETOR1_rec 
             + IDADE + single   #+ CRITERIOBR
            ,family = binomial, nAGQ = 0 #, REML=F
            ,control = glmerControl(calc.derivs = FALSE)
            ,data = ba2)

compare_parameters(model1, model2, style = "se_p", effects = "all",
  column_names = c("Outdoor workplace", "Home based work")) %>% 
  print_html(caption="Models parameters", digits = "signif2") 
Models parameters
Parameter Outdoor workplace Home based work
Fixed Effects
(Intercept) -1.6*** (0.17) -3.3*** (0.64)
yearf (2017) -1.7*** (0.22) -0.57 (0.34)
SIT FAM rec (2) -0.038 (0.036) -0.085 (0.046)
SIT FAM rec (3) -0.11** (0.036) -0.33*** (0.058)
SIT FAM rec (4) -0.19*** (0.052) -0.0025 (0.072)
SEXOf (2) -0.6*** (0.028) 0.27*** (0.038)
QT AUTO rec (1) 0.15*** (0.029) 0.026 (0.04)
QT AUTO rec (2) 0.16*** (0.039) -0.24*** (0.055)
QT AUTO rec (3) 0.14* (0.058) -0.44*** (0.088)
GRAU INS rec (4) -0.057 (0.031) -0.32*** (0.044)
GRAU INS rec (5) -0.22*** (0.037) -0.55*** (0.051)
SETOR1 rec (2) 1.1*** (0.089) 0.22 (0.16)
SETOR1 rec (3) 0.34*** (0.073) 0.64*** (0.12)
SETOR1 rec (4) 0.69*** (0.069) 0.42*** (0.11)
SETOR1 rec (5) 2.2*** (0.09) 0.37 (0.24)
SETOR1 rec (6) 2.5*** (0.087) -0.56 (0.39)
SETOR1 rec (7) 0.27** (0.093) 0.19 (0.18)
SETOR1 rec (8) -0.15 (0.12) 0.52*** (0.13)
SETOR1 rec (9) 0.46*** (0.092) 0.56*** (0.13)
SETOR1 rec (10) 0.28*** (0.077) -0.85*** (0.14)
SETOR1 rec (12) 0.63*** (0.065) 1.1*** (0.11)
SETOR1 rec (13) 1.1*** (0.081) 0.093 (0.28)
IDADE 0.0017 (0.0011) 0.0086*** (0.0014)
single (1) 0.058 (0.053) 0.46*** (0.061)
Random Effects
SD (yearf2007: VINC1_rec) 0.38 (NA) 1.6 (NA)
SD (yearf2017: VINC1_rec) 0.67 (NA) 2.4 (NA)
Cor (yearf2007~yearf2017: VINC1_rec: VINC1_rec) 0.56 (NA) 0.98 (NA)

Best Linear Unbiased Predictions

estimate_grouplevel(model1, type = c("total")) %>%
pivot_wider(names_from = Parameter,values_from = Coefficient)  %>% print_html(caption="Probability of an Outdoor Workplace 
          (Best Linear Unbiased Predictions)", digits = "signif2") #%>% flextable
Probability of an Outdoor Workplace (Best Linear Unbiased Predictions)
Level yearf2007 yearf2017
1 -0.34 -1.6
2 -0.13 -1.5
3 -0.22 -1.6
4 0.72 -0.33
5 -0.049 -2.4
6 0.4 -2.1
7 -0.36 -2.3
estimate_grouplevel(model2, type = c("total")) %>%
pivot_wider(names_from = Parameter,values_from = Coefficient)  %>% print_html(caption="Probability of Home Based Work 
          (Best Linear Unbiased Predictions)", digits = "signif2")
Probability of Home Based Work (Best Linear Unbiased Predictions)
Level yearf2007 yearf2017
1 -1.3 -2.5
2 -0.53 -1.1
3 -3 -5
4 1.7 2
5 0.18 -0.59
6 1.2 1.9
7 1.7 1.2
compare_performance(model1, model2) %>% print_html(digits = "signif3", caption="Indices of model performance", 
             layout="vertical") %>%
  tab_header(
    title = md("**Indices of model performance**"),
    )%>%
  tab_source_note(
    source_note = "Source: Data: Sao Paulo OD survey 2007 & 2017"
  ) %>%
  cols_label(
    model1 = "Outdoor workplace", model2 = "Home based work")
Warning: When comparing models, please note that probably not all models were fit
  from same data.
Indices of model performance
Metric Outdoor workplace Home based work
Model glmerMod glmerMod
AIC (weights) 46608.7 (0) 23650.6 (1)
AICc (weights) 46608.7 (0) 23650.6 (1)
BIC (weights) 46853.7 (0) 23893.8 (1)
R2 (cond.) 0.285 0.523
R2 (marg.) 0.239 0.0885
ICC 0.0602 0.477
RMSE 0.331 0.245
Sigma 1 1
Log_loss 0.36 0.195
Score_log -Inf -Inf
Score_spherical 2.18e-05 0.000662
Source: Data: Sao Paulo OD survey 2007 & 2017

Model 3: Commuting distances

Model estimates

ba3 <- subset(ba, SETOR1_rec != 1 & SIT_FAM_rec != 6 & TRAB1_REf == "No" & TRABEXT1f == "No")
model3<-lmer(distkm ~ 1 + (1 | DistritoR/VINC1_rec) + yearf
             #+ SIT_FAM_rec 
             + SEXOf + QT_AUTO_rec + GRAU_INS_rec + SETOR1_rec 
             #+ single + IDADE
            ,control = lmerControl(calc.derivs = FALSE),  REML = F
            ,data = ba3)
model_parameters(model3, effects = "all", summary = TRUE)  %>% print_html()
Model Summary
Parameter Coefficient SE 95% CI t(54891) p
Fixed Effects
(Intercept) 4.53 0.28 (3.97, 5.09) 15.91 < .001
yearf (2017) -0.27 0.06 (-0.40, -0.15) -4.43 < .001
SEXOf (2) -0.77 0.06 (-0.89, -0.65) -12.54 < .001
QT AUTO rec (1) 0.38 0.07 (0.24, 0.52) 5.36 < .001
QT AUTO rec (2) 0.80 0.10 (0.61, 0.99) 8.28 < .001
QT AUTO rec (3) 0.80 0.15 (0.50, 1.09) 5.30 < .001
GRAU INS rec (4) 0.88 0.08 (0.72, 1.04) 10.87 < .001
GRAU INS rec (5) 2.11 0.10 (1.92, 2.30) 21.33 < .001
SETOR1 rec (2) 4.28 0.24 (3.81, 4.74) 18.04 < .001
SETOR1 rec (3) 1.15 0.14 (0.87, 1.44) 8.05 < .001
SETOR1 rec (4) 1.00 0.13 (0.74, 1.26) 7.49 < .001
SETOR1 rec (5) 2.33 0.30 (1.74, 2.92) 7.74 < .001
SETOR1 rec (6) 2.63 0.30 (2.05, 3.21) 8.85 < .001
SETOR1 rec (7) 2.91 0.18 (2.56, 3.27) 16.15 < .001
SETOR1 rec (8) 0.71 0.23 (0.25, 1.16) 3.04 0.002
SETOR1 rec (9) 0.60 0.18 (0.24, 0.96) 3.27 0.001
SETOR1 rec (10) 2.48 0.15 (2.19, 2.76) 16.97 < .001
SETOR1 rec (12) 2.60 0.12 (2.36, 2.84) 21.34 < .001
SETOR1 rec (13) 2.13 0.19 (1.76, 2.50) 11.29 < .001
Random Effects
SD (Intercept: VINC1_rec:DistritoR) 2.74
SD (Intercept: DistritoR) 2.61
SD (Residual) 6.90
Model: distkm ~ 1 + yearf + SEXOf + QT_AUTO_rec + GRAU_INS_rec + SETOR1_rec (54913 Observations)
Residual standard deviation: 6.895 (df = 54891)
Conditional R2: 0.257; Marginal R2: 0.034

Maps preparation

#Best Linear Unbiased Predictions
z2 <- estimate_grouplevel(model3, type = "random")%>% 
  filter(Group == "DistritoR") %>% select(Level, Coefficient) %>%
  rename(Distrito = "Level") %>% mutate(Distrito = as.numeric(Distrito))

z3 <- estimate_grouplevel(model3, type = "random") %>% 
reshape_grouplevel(indices = "Coefficient", group = "DistritoR") %>% summary() %>% select(-Intercept.x) %>% rename(Distrito = "DistritoR") %>%
pivot_wider(names_from = VINC1_rec,values_from = Intercept.y) %>% mutate(Distrito = as.numeric(Distrito))

####maps
setwd("C:/Users/bmotteba/Nextcloud/Donnees/Bresil/OD_SaoPaulo/OD_2007/Banco de Dados")
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
d <- st_read("Distritos_2007_region.shp")
Reading layer `Distritos_2007_region' from data source 
  `C:\Users\bmotteba\Nextcloud\Donnees\Bresil\OD_SaoPaulo\OD_2007\Banco de Dados\Distritos_2007_region.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 134 features and 3 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 275295.9 ymin: 7337234 xmax: 429153.6 ymax: 7435608
Projected CRS: Corrego Alegre 1970-72 / UTM zone 23S
d <- d %>% mutate(Distrito = as.numeric(Distrito))
z1 <- left_join(z2,z3)
Joining, by = "Distrito"
dz1 <- merge(d,z1, by="Distrito")

Map 3.0: random effect by workplace district

plot(dz1["Coefficient"], breaks = c(-6,-3,-2,-1,0,1,2,3,5,10), pal = brewer.pal(9,"Oranges"), main="Random effects, All workers, Residence district")

Map 3.1: random effect by workplace district

plot(dz1["1"], breaks = c(-6,-3,-2,-1,0,1,2,3,5,10), pal = brewer.pal(9,"Oranges"), main="Random effects, Formal employees, Residence district")

Map 3.2: random effect by workplace district

plot(dz1["2"], breaks = c(-6,-3,-2,-1,0,1,2,3,5,10), pal = brewer.pal(9,"Oranges"), main="Random effects, Informal employees, Residence district")

Map 3.4: random effect by workplace district

plot(dz1["4"], breaks = c(-6,-3,-2,-1,0,1,2,3,5,10), pal = brewer.pal(9,"Oranges"), main="Random effects, Self-employed, Residence district")

Map 3.7: random effect by workplace district

plot(dz1["7"], breaks = c(-6,-3,-2,-1,0,1,2,3,5,10), pal = brewer.pal(9,"Oranges"), main="Random effects, Family buisness, Residence district")

Model 5: Commuting distances by workplace distrito

Model estimates

ba3 <- subset(ba, SETOR1_rec != 1 & SIT_FAM_rec != 6 & TRAB1_REf == "No" & TRABEXT1f == "No")
model5<-lmer(distkm ~ 1  + (1 | DistritoT/VINC1_rec) + yearf
             #+ SIT_FAM_rec 
             + SEXOf + QT_AUTO_rec + GRAU_INS_rec + SETOR1_rec 
              #+ single #+ IDADE
            ,control = lmerControl(calc.derivs = FALSE),  REML = F
            ,data = ba3)
model_parameters(model5, effects = "all", summary = TRUE)  %>% print_html()
Model Summary
Parameter Coefficient SE 95% CI t(54891) p
Fixed Effects
(Intercept) 5.02 0.22 (4.60, 5.45) 23.36 < .001
yearf (2017) 0.35 0.07 (0.22, 0.48) 5.28 < .001
SEXOf (2) -0.84 0.07 (-0.97, -0.71) -12.53 < .001
QT AUTO rec (1) 0.43 0.08 (0.28, 0.58) 5.70 < .001
QT AUTO rec (2) 0.41 0.10 (0.21, 0.61) 3.99 < .001
QT AUTO rec (3) 0.21 0.16 (-0.10, 0.52) 1.30 0.192
GRAU INS rec (4) 0.28 0.09 (0.11, 0.45) 3.23 0.001
GRAU INS rec (5) -0.69 0.10 (-0.89, -0.48) -6.69 < .001
SETOR1 rec (2) 4.05 0.26 (3.55, 4.56) 15.78 < .001
SETOR1 rec (3) 1.84 0.16 (1.53, 2.14) 11.71 < .001
SETOR1 rec (4) 0.40 0.14 (0.12, 0.69) 2.79 0.005
SETOR1 rec (5) 2.23 0.33 (1.59, 2.87) 6.80 < .001
SETOR1 rec (6) 2.75 0.32 (2.11, 3.38) 8.48 < .001
SETOR1 rec (7) 1.51 0.20 (1.13, 1.90) 7.65 < .001
SETOR1 rec (8) -0.23 0.25 (-0.72, 0.26) -0.93 0.351
SETOR1 rec (9) -0.53 0.20 (-0.92, -0.15) -2.69 0.007
SETOR1 rec (10) 2.02 0.16 (1.71, 2.34) 12.67 < .001
SETOR1 rec (12) 1.75 0.13 (1.49, 2.01) 13.12 < .001
SETOR1 rec (13) 1.74 0.21 (1.32, 2.16) 8.18 < .001
Random Effects
SD (Intercept: VINC1_rec:DistritoT) 2.02
SD (Intercept: DistritoT) 1.49
SD (Residual) 7.47
Model: distkm ~ 1 + yearf + SEXOf + QT_AUTO_rec + GRAU_INS_rec + SETOR1_rec (54913 Observations)
Residual standard deviation: 7.465 (df = 54891)
Conditional R2: 0.120; Marginal R2: 0.020

Maps preparation

#Best Linear Unbiased Predictions
z2 <- estimate_grouplevel(model5, type = "random")%>% 
  filter(Group == "DistritoT") %>% select(Level, Coefficient) %>%
  rename(Distrito = "Level") %>% mutate(Distrito = as.numeric(Distrito))

z3 <- estimate_grouplevel(model5, type = "random") %>% 
reshape_grouplevel(indices = "Coefficient", group = "DistritoT") %>% summary() %>% select(-Intercept.x) %>% rename(Distrito = "DistritoT") %>%
pivot_wider(names_from = VINC1_rec,values_from = Intercept.y) %>% mutate(Distrito = as.numeric(Distrito))

####maps
setwd("C:/Users/bmotteba/Nextcloud/Donnees/Bresil/OD_SaoPaulo/OD_2007/Banco de Dados")
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
d <- st_read("Distritos_2007_region.shp")
Reading layer `Distritos_2007_region' from data source 
  `C:\Users\bmotteba\Nextcloud\Donnees\Bresil\OD_SaoPaulo\OD_2007\Banco de Dados\Distritos_2007_region.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 134 features and 3 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 275295.9 ymin: 7337234 xmax: 429153.6 ymax: 7435608
Projected CRS: Corrego Alegre 1970-72 / UTM zone 23S
d <- d %>% mutate(Distrito = as.numeric(Distrito))
z1 <- left_join(z2,z3)
Joining, by = "Distrito"
dz2 <- merge(d,z1, by="Distrito")

Map 5.0: random effect by workplace district

plot(dz2["Coefficient"], breaks = c(-6,-2,-1,-.5,0,.5,1,2,3,10), pal = brewer.pal(9,"Oranges"), main="Random effects, All workers, Workplace district")

Map 5.1: random effect by workplace district

plot(dz2["1"], breaks = c(-6,-2,-1,-.5,0,.5,1,2,3,10), pal = brewer.pal(9,"Oranges"), main="Random effects, Formal employees, Workplace district")

Map 5.2: random effect by workplace district

plot(dz2["2"], breaks = c(-6,-2,-1,-.5,0,.5,1,2,3,10), pal = brewer.pal(9,"Oranges"), main="Random effects, Informal employees, Workplace district")

Map 5.4: random effect by workplace district

plot(dz2["4"], breaks = c(-6,-2,-1,-.5,0,.5,1,2,3,10), pal = brewer.pal(9,"Oranges"), main="Random effects, Self-employed, Workplace district")

Map 5.7: random effect by workplace district

plot(dz2["7"], breaks = c(-6,-2,-1,-.5,0,.5,1,2,3,10), pal = brewer.pal(9,"Oranges"), main="Random effects, Family buisness, Workplace district")