13  Exercises IIIb

\[ \newcommand{\tr}{\mathrm{tr}} \newcommand{\rank}{\mathrm{rank}} \newcommand{\plim}{\operatornamewithlimits{plim}} \newcommand{\diag}{\mathrm{diag}} \newcommand{\bm}[1]{\boldsymbol{\mathbf{#1}}} \newcommand{\Var}{\mathrm{Var}} \newcommand{\Exp}{\mathrm{E}} \newcommand{\Cov}{\mathrm{Cov}} \newcommand\given[1][]{\:#1\vert\:} \newcommand{\irow}[1]{% \begin{pmatrix}#1\end{pmatrix} } \]

Required packages

pkgs <- c("sf", "mapview", "spdep", "spatialreg", "ggplot2", "tmap", "viridis", "viridisLite", 
          "plm", "lfe", "splm", "SDPDmod")
lapply(pkgs, require, character.only = TRUE)

Session info

R version 4.5.1 (2025-06-13 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22631)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=English_United Kingdom.utf8 
[2] LC_CTYPE=English_United Kingdom.utf8   
[3] LC_MONETARY=English_United Kingdom.utf8
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.utf8    

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
 [1] SDPDmod_0.0.6     splm_1.6-5        lfe_3.1.1        
 [4] plm_2.6-6         viridis_0.6.5     viridisLite_0.4.2
 [7] tmap_4.1          ggplot2_3.5.2     spatialreg_1.3-6 
[10] Matrix_1.7-3      spdep_1.3-13      spData_2.3.4     
[13] mapview_2.11.2    sf_1.0-21        

loaded via a namespace (and not attached):
 [1] Rdpack_2.6.4            DBI_1.2.3              
 [3] deldir_2.0-4            gridExtra_2.3          
 [5] tmaptools_3.2           s2_1.1.9               
 [7] logger_0.4.0            sandwich_3.1-1         
 [9] rlang_1.1.6             magrittr_2.0.3         
[11] multcomp_1.4-28         e1071_1.7-16           
[13] compiler_4.5.1          png_0.1-8              
[15] vctrs_0.6.5             stringr_1.5.1          
[17] pkgconfig_2.0.3         wk_0.9.4               
[19] fastmap_1.2.0           lwgeom_0.2-14          
[21] leafem_0.2.4            rmarkdown_2.29         
[23] spacesXYZ_1.6-0         miscTools_0.6-28       
[25] xfun_0.52               satellite_1.0.5        
[27] jsonlite_2.0.0          collapse_2.1.2         
[29] terra_1.8-54            parallel_4.5.1         
[31] LearnBayes_2.15.1       R6_2.6.1               
[33] stringi_1.8.7           RColorBrewer_1.1-3     
[35] boot_1.3-31             lmtest_0.9-40          
[37] stars_0.6-8             Rcpp_1.0.14            
[39] knitr_1.50              zoo_1.8-14             
[41] base64enc_0.1-3         leaflet.providers_2.0.0
[43] splines_4.5.1           tidyselect_1.2.1       
[45] rstudioapi_0.17.1       dichromat_2.0-0.1      
[47] abind_1.4-8             maptiles_0.10.0        
[49] maxLik_1.5-2.1          codetools_0.2-20       
[51] lattice_0.22-7          tibble_3.3.0           
[53] leafsync_0.1.0          withr_3.0.2            
[55] coda_0.19-4.1           evaluate_1.0.4         
[57] survival_3.8-3          units_0.8-7            
[59] proxy_0.4-27            pillar_1.10.2          
[61] KernSmooth_2.23-26      stats4_4.5.1           
[63] generics_0.1.4          sp_2.2-0               
[65] scales_1.4.0            xtable_1.8-4           
[67] class_7.3-23            glue_1.8.0             
[69] tools_4.5.1             leaflegend_1.2.1       
[71] data.table_1.17.6       RSpectra_0.16-2        
[73] dotCall64_1.2           mvtnorm_1.3-3          
[75] XML_3.99-0.18           grid_4.5.1             
[77] rbibutils_2.3           crosstalk_1.2.1        
[79] bdsmatrix_1.3-7         colorspace_2.1-1       
[81] nlme_3.1-168            cols4all_0.8           
[83] raster_3.6-32           Formula_1.2-5          
[85] cli_3.6.5               spam_2.11-1            
[87] dplyr_1.1.4             gtable_0.3.6           
[89] digest_0.6.37           classInt_0.4-11        
[91] TH.data_1.1-3           htmlwidgets_1.6.4      
[93] farver_2.1.2            htmltools_0.5.8.1      
[95] lifecycle_1.0.4         leaflet_2.2.2          
[97] microbenchmark_1.5.0    MASS_7.3-65            

13.1 Inkar data: the effect of regional characteristics on life expectancy

Below, we read and transform some characteristics of the INKAR data on the level of German counties.

load("_data/inkar2.Rdata")

Variables are

Variable Description
“Kennziffer” ID
“Raumeinheit” Name
“Aggregat” Level
“year” Year
“poluation_density” Population Density
“median_income” Median Household income (only for 2020)
“gdp_in1000EUR” Gross Domestic Product in 1000 euros
“unemployment_rate” Unemployment rate
“share_longterm_unemployed” Share of longterm unemployed (among unemployed)
“share_working_indutry” Share of employees in undistrial sector
“share_foreigners” Share of foreign nationals
“share_college” Share of school-finishers with college degree
“recreational_space” Recreational space per inhabitant
“car_density” Density of cars
“life_expectancy” Life expectancy

13.2 County shapes

1) Please map the life expectancy across Germany

  1. Merge data with the shape file (as with conventional data)

  2. Create a map of life-expectancy

2) Chose some variables that could predict life expectancy. See for instance the following paper.

3) Generate a neighbours object (e.g. the 10 nearest neighbours).

4) Estimate a cross-sectional spatial model for the year 2020 and calculate the impacts.

5) Calculate the spatial lagged variables for your covariates (e.g. use create_WX(), which needs a non-spatial df as input) .

6) Can you run a spatial machine learning model? (for instance, using randomForest)?

Esimate an FE model with SLX specification

  1. Loop over years to generate WX

  2. Estimate a twoways FE SLX panel model

  3. Estimate a twoways FE SAR panel model (use spml())

  4. Estimate the summary impacts.