Re: [R-sig-Geo] Estimate impacts from a spgm - spatial panel regression model

2023-06-10 Thread Jose Funes
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
>> day01
>> svn rev81115
>> 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]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


Re: [R-sig-Geo] Estimate impacts from a spgm - spatial panel regression model

2023-06-01 Thread Jose Funes
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  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.biv...@nhh.no
>>>
>>> 
>>> From: R-sig-Geo  on behalf of Josiah
>>> Parry 
>>> Sent: 26 May 2023 20:14
>>> To: Jose Funes
>>> Cc: r-sig-geo@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  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@r-project.org
>>> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>> >
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ___
>>> R-sig-Geo mailing list
>>> R-sig-Geo@r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>

[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


Re: [R-sig-Geo] Estimate impacts from a spgm - spatial panel regression model

2023-05-27 Thread Jose Funes
)
>
> # 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  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.biv...@nhh.no
>>
>> 
>> From: R-sig-Geo  on behalf of Josiah
>> Parry 
>> Sent: 26 May 2023 20:14
>> To: Jose Funes
>> Cc: r-sig-geo@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  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@r-project.org
>> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> >
>>
>> [[alternative HTML version deleted]]
>>
>> ___
>> R-sig-Geo mailing list
>> R-sig-Geo@r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>

[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


Re: [R-sig-Geo] Estimate impacts from a spgm - spatial panel regression model

2023-05-27 Thread Jose Funes
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
day01
svn rev81115
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  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.biv...@nhh.no
>
> 
> From: R-sig-Geo  on behalf of Josiah
> Parry 
> Sent: 26 May 2023 20:14
> To: Jose Funes
> Cc: r-sig-geo@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  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]]
&g

Re: [R-sig-Geo] Estimate impacts from a spgm - spatial panel regression model

2023-05-26 Thread Roger Bivand
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.biv...@nhh.no


From: R-sig-Geo  on behalf of Josiah Parry 

Sent: 26 May 2023 20:14
To: Jose Funes
Cc: r-sig-geo@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  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@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


Re: [R-sig-Geo] Estimate impacts from a spgm - spatial panel regression model

2023-05-26 Thread Josiah Parry
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  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@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo