12  Exercise II

Required packages

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

Session info

sessionInfo()
R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default


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/London
tzcode source: internal

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

other attached packages:
 [1] SDPDmod_0.0.3     splm_1.6-2        plm_2.6-3        
 [4] viridisLite_0.4.2 tmap_3.3-3        spatialreg_1.2-9 
 [7] Matrix_1.5-4.1    spdep_1.2-8       spData_2.2.2     
[10] mapview_2.11.0    sf_1.0-13        

loaded via a namespace (and not attached):
 [1] tidyselect_1.2.0   dplyr_1.1.2        fastmap_1.1.1     
 [4] leaflet_2.1.2      TH.data_1.1-2      dotCall64_1.0-2   
 [7] XML_3.99-0.14      digest_0.6.31      lifecycle_1.0.3   
[10] LearnBayes_2.15.1  survival_3.5-5     terra_1.7-29      
[13] magrittr_2.0.3     compiler_4.3.1     rlang_1.1.1       
[16] tools_4.3.1        utf8_1.2.3         collapse_1.9.6    
[19] knitr_1.43         htmlwidgets_1.6.2  sp_1.6-1          
[22] classInt_0.4-9     RColorBrewer_1.1-3 multcomp_1.4-24   
[25] abind_1.4-5        KernSmooth_2.23-21 expm_0.999-7      
[28] leafsync_0.1.0     grid_4.3.1         stats4_4.3.1      
[31] fansi_1.0.4        xtable_1.8-4       lfe_2.9-0         
[34] e1071_1.7-13       leafem_0.2.0       colorspace_2.1-0  
[37] scales_1.2.1       MASS_7.3-60        dichromat_2.0-0.1 
[40] cli_3.6.1          mvtnorm_1.2-2      rmarkdown_2.22    
[43] miscTools_0.6-28   generics_0.1.3     RSpectra_0.16-1   
[46] rstudioapi_0.14    tmaptools_3.1-1    bdsmatrix_1.3-6   
[49] DBI_1.1.3          proxy_0.4-27       ibdreg_0.3.8      
[52] splines_4.3.1      stars_0.6-1        parallel_4.3.1    
[55] s2_1.1.4           base64enc_0.1-3    vctrs_0.6.3       
[58] boot_1.3-28.1      webshot_0.5.4      sandwich_3.0-2    
[61] jsonlite_1.8.5     Formula_1.2-5      crosstalk_1.2.0   
[64] units_0.8-2        spam_2.9-1         glue_1.6.2        
[67] lwgeom_0.2-13      codetools_0.2-19   deldir_1.0-9      
[70] raster_3.6-20      lmtest_0.9-40      munsell_0.5.0     
[73] tibble_3.2.1       pillar_1.9.0       htmltools_0.5.5   
[76] satellite_1.0.4    R6_2.5.1           maxLik_1.5-2      
[79] wk_0.7.3           Rdpack_2.4         evaluate_0.21     
[82] lattice_0.21-8     rbibutils_2.2.13   png_0.1-8         
[85] class_7.3-22       Rcpp_1.0.10        coda_0.19-4       
[88] nlme_3.1-162       xfun_0.39          zoo_1.8-12        
[91] pkgconfig_2.0.3   

12.1 Inkar data

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

di <- c("_data/")

# Define the downloaded filed
j <- c("inkar.csv")
c <- 1

for(k in j){
  header <- as.vector(t(read.table(paste0(di, k), nrows = 1, sep = ";")[1,]))
  # Clean header
  header <- stringi::stri_replace_all_fixed(
    header, 
    c("ä", "ö", "ü", "Ä", "Ö", "Ü"), 
    c("ae", "oe", "ue", "Ae", "Oe", "Ue"), 
    vectorize_all = FALSE
  )
  header <- gsub(" ", "", header)
  header <- gsub("\\.", "", header)
  header <- iconv(header, "latin1", "ASCII", sub = "")
  
  # Combine with second row header (year)
  header2 <- as.vector(t(read.table(paste0(di, k), skip = 1, nrows = 1, sep = ";")[1,]))
  header3 <- paste(header, header2, sep = "_")
  header3 <- gsub("_NA", "", header3)
  
  nc <- length(header3)
  # Input and rename data
  data <- read.csv(paste0(di, k), skip = 2, header = FALSE, sep = ";", 
                   quote = "\"", dec = ",", stringsAsFactors = F,
                   colClasses = "character")
  names(data) <- header3
  data1 <- data
  
  # Correct character vars (containing thousands separator)
  vars <- which(sapply(data1, is.character))
  vars <- vars[-which(vars %in% c(1:3))]
  for(l in vars){
    data1[,l] <- gsub("\\.", "", data1[,l])
    data1[,l] <- gsub("\\,", ".", data1[,l])
    data1[,l] <- as.numeric(data1[,l])
  }
  
  
  # #Save
  # l <- paste("bearb", k, sep = "_")
  # write.table(data1, file = l, row.names = FALSE, sep = ";", dec = ".", na = ".")
  
  # #Reshape
  # helpvar1 <- unique(header[4:length(header)])
  # helpvar2 <-  sort(unique(header2[!is.na(header2)]))
  # n_vars <- length(helpvar1)
  # n_times <- length(helpvar2)
  # helpvar1 <- sort(rep(helpvar1, times = n_times))
  # helpvar2 <- rep(helpvar2, times = n_vars)
  # helpvar3 <- paste(helpvar1, helpvar2, sep = "_")
  # count <- ncol(data1)+1
  # for(v in helpvar3) {
  #   if(v %in% names(data1)) {}
  #   else{
  #     data1[,count] <- NA
  #     colnames(data1)[count] <- v
  #     count <- count+1
  #   }
  # }
  # data1 <- data1[c(colnames(data1)[1:3], sort(helpvar3))]
  # 
  # data1 <- reshape(data1, direction = "long", varying = 4:ncol(data1), 
  #                  sep = "_")
  data.long <- tidyr::pivot_longer(data1, 
                                  cols = 4:ncol(data1),
                                  names_to = c(".value", "year"),
                                  names_pattern = "(.*)_(.*)")
  
  
  colnames(data.long) <- substr(colnames(data.long), 1, 30)
  
  if(c == 1){
    inkar.df <- data.long
  }else{
    inkar.df <- merge(inkar.df, data.long, all.x = TRUE, all.y = TRUE)
  }
  
  c <- c+1
  
}



inkar.df$year <- as.numeric(inkar.df$year)

names(inkar.df)[which(names(inkar.df) == "Pkw-Dichte")] <- "pkw_dichte"

save(inkar.df, file = "_data/inkar.Rdata")

Variables are

Variable Description
“Kennziffer” ID
“Raumeinheit” Name
“Aggregat” Level
“year” Year
“Bruttoinlandsproduktin1000Euro” Gross Domestic Product in 1000 euros
“HaushaltemitKindern” Households with children
“Lebenserwartung” Life expectancy
“KommunaleSchulden” Municipal debts
“Pkw-Dichte” Car density
“Straenverkehrsunfaelle” Road traffic accidents
“Einpendler” In-commuters
“Auspendler” Out-commuters
“Beschaeftigtenquote” Employment rate
“Bevoelkerunggesamt” Population
“Krankenhausbetten” Hospital beds per 1,000 inhabitants
“Gesamtwanderungssaldo” Net migration balance

12.2 County shapes

kreise.spdf <- st_read(dsn = "_data/Kreisgrenzen_2020_mit_Einwohnerzahl",
                       layer = "KRS_ew_20")
Reading layer `KRS_ew_20' from data source 
  `C:\work\Lehre\Geodata_Spatial_Regression\_data\Kreisgrenzen_2020_mit_Einwohnerzahl' 
  using driver `ESRI Shapefile'
Simple feature collection with 401 features and 19 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 5.86625 ymin: 47.27012 xmax: 15.04182 ymax: 55.05878
Geodetic CRS:  WGS 84

12.3 Exercises

12.3.1 Please map the life expectancy across Germany

12.3.2 Test the effect of regional characteristics on life expectancy

  1. Chose some variables that could predict life expectancy

Merge data (as with conventional data)

# Merge
inkar_2015.spdf <- merge(kreise.spdf, inkar.df[inkar.df$year == 2015, ], 
                         by.x = "ags", by.y = "Kennziffer")

Plot it

cols <- viridis(n = 100, direction = -1, option = "G")

mp1 <-  tm_shape(inkar_2015.spdf) + 
  tm_fill(col = "Lebenserwartung", 
          style = "cont", # algorithm to def cut points
          palette = cols, # colours
          stretch.palette = TRUE,
          title = "in years"
          ) +
  tm_borders(col = "white", lwd = 0.5, alpha = 0.5) +
  tm_layout(frame = FALSE,
            legend.frame = TRUE, legend.bg.color = TRUE,
            legend.position = c("right", "bottom"),
            legend.outside = FALSE,
            main.title = "Life expectancy", 
            main.title.position = "center",
            main.title.size = 1.6,
            legend.title.size = 0.8,
            legend.text.size = 0.8)

mp1

  1. Chose between:

Generate impacts

# nb <- poly2nb(kreise.spdf, row.names = "ags", queen = TRUE)
knn <- knearneigh(st_centroid(kreise.spdf), k = 10)
Warning: st_centroid assumes attributes are constant over
geometries
nb <- knn2nb(knn, row.names = kreise.spdf$ags)
listw <- nb2listw(nb, style = "W")
  1. Estimate a cross-sectional spatial model for a selected year

Use a spatial Durbin Error model

# Spec formula
fm <- Lebenserwartung ~   Beschaeftigtenquote + Krankenhausbetten + pkw_dichte + HaushaltemitKindern + Straenverkehrsunfaelle

# Estimate error model with Durbin = TRUE 
mod_1.durb <- errorsarlm(fm,  
                      data = inkar_2015.spdf, 
                      listw = listw,
                      Durbin = TRUE)

summary(mod_1.durb)

Call:
errorsarlm(formula = fm, data = inkar_2015.spdf, listw = listw, 
    Durbin = TRUE)

Residuals:
       Min         1Q     Median         3Q        Max 
-2.3061430 -0.3833525 -0.0034956  0.4119499  1.8500085 

Type: error 
Coefficients: (asymptotic standard errors) 
                              Estimate  Std. Error z value
(Intercept)                 7.5913e+01  2.3609e+00 32.1539
Beschaeftigtenquote        -3.3911e-03  1.2187e-02 -0.2782
Krankenhausbetten          -6.9723e-03  1.2376e-02 -0.5634
pkw_dichte                  6.6503e-05  6.7611e-04  0.0984
HaushaltemitKindern         5.8928e-02  1.2635e-02  4.6639
Straenverkehrsunfaelle     -2.7490e-04  4.5970e-04 -0.5980
lag.Beschaeftigtenquote     7.3011e-02  3.4370e-02  2.1243
lag.Krankenhausbetten      -9.1317e-02  5.5370e-02 -1.6492
lag.pkw_dichte             -7.5386e-03  2.5851e-03 -2.9162
lag.HaushaltemitKindern     7.1558e-02  3.6657e-02  1.9521
lag.Straenverkehrsunfaelle  3.9637e-03  1.5674e-03  2.5288
                            Pr(>|z|)
(Intercept)                < 2.2e-16
Beschaeftigtenquote         0.780824
Krankenhausbetten           0.573192
pkw_dichte                  0.921647
HaushaltemitKindern        3.103e-06
Straenverkehrsunfaelle      0.549846
lag.Beschaeftigtenquote     0.033644
lag.Krankenhausbetten       0.099104
lag.pkw_dichte              0.003543
lag.HaushaltemitKindern     0.050928
lag.Straenverkehrsunfaelle  0.011445

Lambda: 0.74639, LR test value: 93.205, p-value: < 2.22e-16
Asymptotic standard error: 0.049366
    z-value: 15.12, p-value: < 2.22e-16
Wald statistic: 228.6, p-value: < 2.22e-16

Log likelihood: -387.8592 for error model
ML residual variance (sigma squared): 0.3768, (sigma: 0.61384)
Number of observations: 401 
Number of parameters estimated: 13 
AIC: NA (not available for weighted model), (AIC for lm: 892.92)
# Calculate impacts (which is unnecessary in this case)
mod_1.durb.imp <- impacts(mod_1.durb, listw = listw, R = 300)
summary(mod_1.durb.imp, zstats = TRUE, short = TRUE)
Impact measures (SDEM, glht, n):
                              Direct     Indirect        Total
Beschaeftigtenquote    -0.0033910753  0.073011426  0.069620351
Krankenhausbetten      -0.0069723369 -0.091317318 -0.098289655
pkw_dichte              0.0000665025 -0.007538584 -0.007472081
HaushaltemitKindern     0.0589281106  0.071558303  0.130486413
Straenverkehrsunfaelle -0.0002748972  0.003963678  0.003688781
========================================================
Standard errors:
                             Direct    Indirect       Total
Beschaeftigtenquote    0.0121873300 0.034369504 0.034414827
Krankenhausbetten      0.0123764058 0.055370186 0.062065936
pkw_dichte             0.0006761139 0.002585061 0.002876977
HaushaltemitKindern    0.0126350740 0.036657394 0.038305933
Straenverkehrsunfaelle 0.0004597008 0.001567400 0.001699889
========================================================
Z-values:
                            Direct  Indirect     Total
Beschaeftigtenquote    -0.27824596  2.124308  2.022975
Krankenhausbetten      -0.56335716 -1.649215 -1.583633
pkw_dichte              0.09835991 -2.916211 -2.597198
HaushaltemitKindern     4.66385163  1.952084  3.406428
Straenverkehrsunfaelle -0.59799155  2.528824  2.170013

p-values:
                       Direct     Indirect  Total     
Beschaeftigtenquote    0.78082    0.0336444 0.04307568
Krankenhausbetten      0.57319    0.0991037 0.11327729
pkw_dichte             0.92165    0.0035431 0.00939876
HaushaltemitKindern    3.1035e-06 0.0509283 0.00065819
Straenverkehrsunfaelle 0.54985    0.0114446 0.03000584
  1. Estimate a spatial panel model (use the specification you prefer)

I am estimating a spatial FE SAR model here:

# Drop NA
inkar_sub.df <- inkar.df[which(complete.cases(inkar.df[, all.vars(fm)])),]


### Esimate FE SEM model
sarfe.mod <- spml(formula = fm, 
                  data = inkar_sub.df, 
                  index = c("Kennziffer", "year"),  # ID column
                  listw = listw,          # listw
                  model = "within",       # one of c("within", "random", "pooling").
                  effect = "individual",  # type of fixed effects
                  lag = TRUE,             # spatila lg of Y
                  spatial.error = "none", # "b" (Baltagi), "kkp" (Kapoor, Kelejian and Prucha)
                  method = "eigen",       # estimation method, for large data e.g. ("spam", "Matrix" or "LU")
                  na.action = na.fail,    # handling of missings
                  zero.policy = NULL)     # handling of missings

summary(sarfe.mod)
Spatial panel fixed effects lag model
 

Call:
spml(formula = fm, data = inkar_sub.df, index = c("Kennziffer", 
    "year"), listw = listw, na.action = na.fail, model = "within", 
    effect = "individual", lag = TRUE, spatial.error = "none", 
    method = "eigen", zero.policy = NULL)

Residuals:
      Min.    1st Qu.     Median    3rd Qu.       Max. 
-0.8708649 -0.1206026 -0.0033267  0.1202969  0.7825994 

Spatial autoregressive coefficient:
       Estimate Std. Error t-value  Pr(>|t|)    
lambda 0.420096   0.028664  14.656 < 2.2e-16 ***

Coefficients:
                         Estimate Std. Error t-value  Pr(>|t|)    
Beschaeftigtenquote    4.3692e-02 2.9643e-03 14.7398 < 2.2e-16 ***
Krankenhausbetten      1.9071e-02 8.6677e-03  2.2002 0.0277934 *  
pkw_dichte             7.2763e-04 1.8913e-04  3.8472 0.0001195 ***
HaushaltemitKindern    1.6637e-02 5.8347e-03  2.8514 0.0043533 ** 
Straenverkehrsunfaelle 6.8419e-06 1.0239e-04  0.0668 0.9467237    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
### Impacts
# Number of years
T <- length(unique(inkar_sub.df$year))

# impacts
sarfe.mod.imp <- impacts(sarfe.mod,
                         listw = listw,
                         time = T)
summary(sarfe.mod.imp)     
Impact measures (lag, trace):
                             Direct     Indirect        Total
Beschaeftigtenquote    4.456706e-02 3.077723e-02 7.534429e-02
Krankenhausbetten      1.945235e-02 1.343345e-02 3.288580e-02
pkw_dichte             7.421961e-04 5.125476e-04 1.254744e-03
HaushaltemitKindern    1.696972e-02 1.171899e-02 2.868872e-02
Straenverkehrsunfaelle 6.978862e-06 4.819480e-06 1.179834e-05
========================================================
Simulation results ( variance matrix):
Direct:

Iterations = 1:200
Thinning interval = 1 
Number of chains = 1 
Sample size per chain = 200 

1. Empirical mean and standard deviation for each variable,
   plus standard error of the mean:

                            Mean        SD  Naive SE Time-series SE
Beschaeftigtenquote    4.459e-02 2.915e-03 2.061e-04      2.061e-04
Krankenhausbetten      1.970e-02 9.607e-03 6.793e-04      6.793e-04
pkw_dichte             7.398e-04 1.913e-04 1.353e-05      1.353e-05
HaushaltemitKindern    1.729e-02 5.564e-03 3.934e-04      3.934e-04
Straenverkehrsunfaelle 2.642e-06 9.953e-05 7.038e-06      5.581e-06

2. Quantiles for each variable:

                             2.5%        25%        50%       75%
Beschaeftigtenquote     0.0395345  4.271e-02  4.439e-02 4.654e-02
Krankenhausbetten       0.0023178  1.273e-02  2.052e-02 2.610e-02
pkw_dichte              0.0003934  5.904e-04  7.354e-04 8.639e-04
HaushaltemitKindern     0.0066456  1.350e-02  1.721e-02 2.102e-02
Straenverkehrsunfaelle -0.0001944 -6.492e-05 -1.803e-06 7.394e-05
                           97.5%
Beschaeftigtenquote    0.0503857
Krankenhausbetten      0.0378315
pkw_dichte             0.0011034
HaushaltemitKindern    0.0287736
Straenverkehrsunfaelle 0.0001771

========================================================
Indirect:

Iterations = 1:200
Thinning interval = 1 
Number of chains = 1 
Sample size per chain = 200 

1. Empirical mean and standard deviation for each variable,
   plus standard error of the mean:

                            Mean        SD  Naive SE Time-series SE
Beschaeftigtenquote    3.081e-02 0.0042273 2.989e-04      4.939e-04
Krankenhausbetten      1.349e-02 0.0067081 4.743e-04      4.743e-04
pkw_dichte             5.132e-04 0.0001535 1.085e-05      1.085e-05
HaushaltemitKindern    1.197e-02 0.0041969 2.968e-04      2.968e-04
Straenverkehrsunfaelle 2.023e-06 0.0000700 4.950e-06      3.991e-06

2. Quantiles for each variable:

                             2.5%        25%        50%       75%
Beschaeftigtenquote     0.0230406  2.825e-02  3.074e-02 3.311e-02
Krankenhausbetten       0.0016335  8.619e-03  1.344e-02 1.784e-02
pkw_dichte              0.0002607  3.996e-04  4.945e-04 6.111e-04
HaushaltemitKindern     0.0042322  9.199e-03  1.173e-02 1.463e-02
Straenverkehrsunfaelle -0.0001400 -4.243e-05 -1.254e-06 5.035e-05
                           97.5%
Beschaeftigtenquote    0.0397528
Krankenhausbetten      0.0273577
pkw_dichte             0.0008314
HaushaltemitKindern    0.0205899
Straenverkehrsunfaelle 0.0001292

========================================================
Total:

Iterations = 1:200
Thinning interval = 1 
Number of chains = 1 
Sample size per chain = 200 

1. Empirical mean and standard deviation for each variable,
   plus standard error of the mean:

                            Mean        SD  Naive SE Time-series SE
Beschaeftigtenquote    7.540e-02 0.0062141 4.394e-04      6.929e-04
Krankenhausbetten      3.320e-02 0.0161873 1.145e-03      1.145e-03
pkw_dichte             1.253e-03 0.0003369 2.383e-05      2.383e-05
HaushaltemitKindern    2.926e-02 0.0095948 6.785e-04      6.785e-04
Straenverkehrsunfaelle 4.665e-06 0.0001692 1.196e-05      9.546e-06

2. Quantiles for each variable:

                             2.5%        25%        50%       75%
Beschaeftigtenquote     0.0635934  0.0707996  7.555e-02 0.0788972
Krankenhausbetten       0.0038687  0.0212281  3.396e-02 0.0440139
pkw_dichte              0.0006616  0.0010025  1.260e-03 0.0014714
HaushaltemitKindern     0.0108200  0.0231673  2.858e-02 0.0353547
Straenverkehrsunfaelle -0.0003412 -0.0001057 -3.057e-06 0.0001213
                           97.5%
Beschaeftigtenquote    0.0889887
Krankenhausbetten      0.0638877
pkw_dichte             0.0018925
HaushaltemitKindern    0.0493892
Straenverkehrsunfaelle 0.0002914
  1. Calculate the impacts

12.3.3 Esimate an FE model with SLX specification

Loops over years to generate WX

# All years where we have a balanced sample
years <- unique(inkar.df$year[which(complete.cases(inkar.df[, all.vars(fm)]))])

# All variables we want ot lag
vars <- all.vars(fm)

# create listw with the correct rownames (ID = Kennziffer)
kreise.spdf$Kennziffer <- kreise.spdf$ags
knn <- knearneigh(st_centroid(kreise.spdf), k = 10)
Warning: st_centroid assumes attributes are constant over
geometries
nb <- knn2nb(knn, row.names = kreise.spdf$Kennziffer)
listw <- nb2listw(nb, style = "W")

for(y in years){
  # Select singe year
  tmp <- inkar.df[inkar.df$year == y ,]
  # Select variables and make df
  x <- st_drop_geometry(tmp[, vars])
  # Add ID as rownames (we retreive them again later)
  rownames(x) <- tmp$Kennziffer
  # Perform lag transformation (rownames contian ids)
  w.tmp <- create_WX(as.matrix(x),
                    listw = listw,
                    prefix = "w",
                    zero.policy = TRUE) # NAs will get zero
  w.tmp <- as.data.frame(w.tmp)
  
  # add indices back
  w.tmp$Kennziffer <- row.names(w.tmp)
  w.tmp$year <- y
  
  if(y == years[1]){
    w.inkar.df <- w.tmp
  }else{
    w.inkar.df <- rbind(w.inkar.df, w.tmp)
  }
}
Warning: Setting row names on a tibble is deprecated.
Warning: Setting row names on a tibble is deprecated.
Setting row names on a tibble is deprecated.
Setting row names on a tibble is deprecated.
Setting row names on a tibble is deprecated.
Setting row names on a tibble is deprecated.
Setting row names on a tibble is deprecated.
Setting row names on a tibble is deprecated.
head(w.inkar.df)
      w.Lebenserwartung w.Beschaeftigtenquote w.Krankenhausbetten
01001            80.181                52.686               5.694
01002            80.193                53.122               6.087
01003            80.281                55.042               6.950
01004            80.325                53.877               5.631
01051            80.145                53.762               5.281
01053            80.236                56.863               6.155
      w.pkw_dichte w.HaushaltemitKindern w.Straenverkehrsunfaelle
01001      549.458                27.703                  512.763
01002      542.982                27.285                  519.162
01003      518.670                26.150                  518.684
01004      538.232                27.124                  516.199
01051      546.799                28.170                  494.705
01053      523.272                26.562                  513.071
      Kennziffer year
01001      01001 2013
01002      01002 2013
01003      01003 2013
01004      01004 2013
01051      01051 2013
01053      01053 2013
# Merge back 

inkar.df <- merge(inkar.df, w.inkar.df, by = c("Kennziffer", "year"))

Estimate a twoways FE panel model

fm2 <- Lebenserwartung ~ Beschaeftigtenquote + Krankenhausbetten + pkw_dichte + HaushaltemitKindern + Straenverkehrsunfaelle +
  w.Beschaeftigtenquote + w.Krankenhausbetten + w.pkw_dichte + w.HaushaltemitKindern + w.Straenverkehrsunfaelle

slx.fe <- plm(fm2,
              data = inkar.df,
              effects = "twoways",
              model = "within")

summary(slx.fe)
Oneway (individual) effect Within Model

Call:
plm(formula = fm2, data = inkar.df, model = "within", effects = "twoways")

Balanced Panel: n = 401, T = 8, N = 3208

Residuals:
       Min.     1st Qu.      Median     3rd Qu.        Max. 
-0.88591742 -0.12535845 -0.00049348  0.12743249  0.72005574 

Coefficients:
                            Estimate  Std. Error t-value  Pr(>|t|)
Beschaeftigtenquote       0.00553516  0.00753337  0.7348  0.462552
Krankenhausbetten         0.02732408  0.00960069  2.8461  0.004459
pkw_dichte                0.00058863  0.00021846  2.6945  0.007092
HaushaltemitKindern       0.02062436  0.00814937  2.5308  0.011435
Straenverkehrsunfaelle    0.00031042  0.00014097  2.2020  0.027748
w.Beschaeftigtenquote     0.06172600  0.00945493  6.5284 7.863e-11
w.Krankenhausbetten       0.02200257  0.03261281  0.6747  0.499947
w.pkw_dichte              0.00143784  0.00095524  1.5052  0.132382
w.HaushaltemitKindern    -0.01053720  0.01275188 -0.8263  0.408690
w.Straenverkehrsunfaelle -0.00091095  0.00022556 -4.0387 5.519e-05
                            
Beschaeftigtenquote         
Krankenhausbetten        ** 
pkw_dichte               ** 
HaushaltemitKindern      *  
Straenverkehrsunfaelle   *  
w.Beschaeftigtenquote    ***
w.Krankenhausbetten         
w.pkw_dichte                
w.HaushaltemitKindern       
w.Straenverkehrsunfaelle ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Total Sum of Squares:    250.08
Residual Sum of Squares: 132.01
R-Squared:      0.47212
Adj. R-Squared: 0.39474
F-statistic: 250.159 on 10 and 2797 DF, p-value: < 2.22e-16