[R-sig-Geo] Estimate impacts from a spgm - spatial panel regression model
Jose Funes
je|une@ @end|ng |rom gm@||@com
Sat Jun 10 18:28:18 CEST 2023
Hi,
I would like to know if there is an alternative package to the splm package
for carrying out spatial panel analysis. I am still having problems with
the splm package, as I posted a few days ago.
Thank you in advance.
Jose
>>
>> # Rstudio version and PC info
>>
>> platform x86_64-w64-mingw32
>> arch x86_64
>> os mingw32
>> system x86_64, mingw32
>> status
>> major 4
>> minor 1.2
>> year 2021
>> month 11
>> day 01
>> svn rev 81115
>> language R
>> version.string R version 4.1.2 (2021-11-01)
>>
>>
>>
>> ####### code #####
>>
>> # Load necessary libraries
>> library(tigris)
>> library(splm)
>> library(spdep)
>> library(Matrix)
>> library(plm)
>>
>>
>> # Load necessary libraries
>> library(tigris)
>> library(splm)
>> library(spdep)
>> library(Matrix)
>> library(plm)
>>
> library(spatialreg)
>
>> # census tracts
>> dc_tracts_2018 <- tracts(state="DC",cb=TRUE, year=2018)
>> dc_tracts_2018$row.id <- 1:179
>>
>> # Spatial matrix
>> coords <- coordinates(dc_tracts_2018)
>> DC.knn_7 <- knearneigh(coords, k=7)
>> DC7_nb <- knn2nb(DC.knn_7)
>> DCknn_7 <- nb2listw(DC7_nb, style="W")
>>
>> # reproducible example
>> set.seed(123)
>> df2010 <- data.frame(
>> y = rnorm(179),
>> x = rnorm(179, mean=2),
>> x2 = rnorm(179, mean = 3),
>> year=2010,
>> row.id = 1:179
>> )
>>
>> df2015 <- data.frame(
>> y = rnorm(179),
>> x = rnorm(179, mean=2.5),
>> x2 = rnorm(179, mean = 3),
>> year=2015,
>> row.id = 1:179
>> )
>>
>> df2020 <- data.frame(
>> y = rnorm(179),
>> x = rnorm(179, mean=1.5),
>> x2 = rnorm(179, mean = 3.5),
>> year=2020,
>> row.id = 1:179
>> )
>>
>>
>> # drop geom
>> st_geometry(dc_tracts_2018) <- NULL
>>
>> #list of data frame
>> df <- list(df2010,df2015,df2020)
>>
>> for (i in seq_along(df)) {
>> df[[i]] <- merge(df[[i]], dc_tracts_2018, by = "row.id")
>>
>> }
>>
>> df2010 <- df[1]
>> df2015 <- df[2]
>> df2020 <- df[3]
>>
>> # merging
>> panel_dc_2010_2020 <- rbind(df2010[[1]], df2015[[1]], df2020[[1]])
>>
>> #### setting lag variables
>> panel_dc_2010_2020.pd<-pdata.frame(panel_dc_2010_2020,
>> index=c("GEOID","year"))
>>
>> # lag variables
>> panel_dc_2010_2020.pd$lag_x <- slag(panel_dc_2010_2020.pd$x,listw=DCknn_7)
>> panel_dc_2010_2020.pd$lag_x2 <-
>> slag(panel_dc_2010_2020.pd$x2,listw=DCknn_7)
>>
>> ## impacts
>> time1 <- length(unique(panel_dc_2010_2020$year))
>> s.lwtracts <- kronecker(Diagonal(time1), listw2dgCMatrix(DCknn_7))
>> trMatc <- trW(s.lwtracts, type = "mult")
>>
>> Error in trW(s.lwtracts, type = "mult") : could not find function "trW"
>>
>> # durbing
>> model_fix_durbin <- spgm(y ~ x + x2 +lag_x + lag_x2,
>> data=panel_dc_2010_2020.pd, listw = DCknn_7, model = "within",lag =
>> TRUE,spatial.error = FALSE,moments="fullweights",method="ec2sls")
>>
>> # estimating impacts
>> impacts(model_fix_durbin,tr=trMatc, R=200)
>>
>> Error in impacts(model_fix_durbin, tr = a, R = 200) :
>> could not find function "impacts"
>>
>>
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list