[R-sig-Geo] Estimate impacts from a spgm - spatial panel regression model
Jose Funes
je|une@ @end|ng |rom gm@||@com
Thu Jun 1 17:16:42 CEST 2023
I loaded the spatiareg library and the function spatial weights matrix
powers traces (trMatc) worked. Still, the impact function gives me an error
message.
On Sat, May 27, 2023 at 11:37 AM Jose Funes <jefunes using gmail.com> wrote:
> Thanks, Professor Bivand and Josiah. The previous code had the variable
> time mistyped instead of time1. The code below corrects that. See below,
> basic info and code:
>
> # 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)
>
> # 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"
>
>
> On Sat, May 27, 2023 at 10:58 AM Jose Funes <jefunes using gmail.com> wrote:
>
>> Thanks Professor Bivand and Josiah. See below, basic info and code:
>>
>> # 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)
>>
>> # 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(time), listw2dgCMatrix(DCknn_7))
>> trMatc <- trW(s.lwtracts, type = "mult")
>>
>> Error in trW(s.lwtracts, type = "mult") : could not find function "trW"
>>
>> # durbin
>> 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"
>>
>>
>>
>>
>> On Fri, May 26, 2023 at 2:34 PM Roger Bivand <Roger.Bivand using nhh.no> wrote:
>>
>>> I think the example is using the splm package to fit a GMM model. It is
>>> now some years since spdep was split, with model fitting functions and
>>> methods moving to spatialreg. The latter has also been updated several
>>> times since then, so knowledge of versions of R and packages is essential,
>>> in addition to a reproducible example. Specifically,
>>> https://github.com/r-spatial/spatialreg/pull/16 changed impacts methods
>>> for sphet GMM, and other changes in impacts methods may have occurred:
>>> https://github.com/r-spatial/spatialreg/commits/main/R/impacts.R gives
>>> full details. Provide a fully reproducible example, best with built-in
>>> data, or raise an issue if you can see how any changes have impacted splm
>>> in ways that the splm maintainer did not anticipate.
>>>
>>> Hope this clarifies,
>>>
>>> Roger
>>>
>>> --
>>> Roger Bivand
>>> Emeritus Professor
>>> Norwegian School of Economics
>>> Postboks 3490 Ytre Sandviken, 5045 Bergen, Norway
>>> Roger.Bivand using nhh.no
>>>
>>> ________________________________________
>>> From: R-sig-Geo <r-sig-geo-bounces using r-project.org> on behalf of Josiah
>>> Parry <josiah.parry using gmail.com>
>>> Sent: 26 May 2023 20:14
>>> To: Jose Funes
>>> Cc: r-sig-geo using r-project.org
>>> Subject: Re: [R-sig-Geo] Estimate impacts from a spgm - spatial panel
>>> regression model
>>>
>>> Jose have you built the model that you want impacts for using the
>>> spatialreg package? Additionally, it would be great if you provided a
>>> reproducible example for folks to try and replicate the behavior you are
>>> experiencing.
>>>
>>> On Fri, May 26, 2023 at 12:26 PM Jose Funes <jefunes using gmail.com> wrote:
>>>
>>> > Hi,
>>> > I would like to estimate the impacts of a spatial lag model output. It
>>> used
>>> > to work, but it does not work anymore. I used to work fine with the
>>> impacts
>>> > function in the spdep package, but it seems the function does not work
>>> any
>>> > longer. The second option was to load the spatialreg library and use
>>> the
>>> > impacts function, but I get an error message "object type not
>>> recognized".
>>> > Please advise.
>>> >
>>> > Jose Funes
>>> > Economic geographer
>>> > DC Office of Planning
>>> > 1100 4th Street SW, 20024
>>> >
>>> > [[alternative HTML version deleted]]
>>> >
>>> > _______________________________________________
>>> > R-sig-Geo mailing list
>>> > R-sig-Geo using r-project.org
>>> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>> >
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo using r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list