[R-sig-Geo] Spatial Panel Models Problem (Splm package)

Roger Bivand Roger@Biv@nd @ending from nhh@no
Mon May 7 21:13:07 CEST 2018


On Mon, 7 May 2018, felipe tavares wrote:

> Hi Roger.
>
> First of all, thank for your help.
>
> Yes, my first two colums are year and id (identification for cities).
>
> I really do not understand, because with the same data I run the regression
> in Stata.
>
> There is my full code:
> ""
> # Data
> data <- read.xlsx("RJ_database.xlsx", 1, header = TRUE)
> panel <- pdata.frame(data, index = c("id", "year"))
> time <- length(unique(data$year)) #Salving data time length
>
> pdim(panel)
> summary(panel)
>
> poly <- readOGR(dsn ="C:/Users/user/OneDrive/Resource Policy Paper", layer
> = "RJ") #Desktop
> RJ <- poly2nb(poly)
>
> W <- nb2listw(RJ, style = "W", glist = NULL)
> summary(W)
>
> ### Kronecker Product
> kronecker.W <- listw2dgCMatrix(W)
> W_queen2 <- kronecker(Diagonal(time), kronecker.W)
> W_queen <- mat2listw(W_queen2, style = "W")

This is the problem. You do not need to generate the Kronecker problem 
yourself (using the example in ?spml:

library(splm)
data(Produc, package = "plm")
data(usaww)
W <- mat2listw(usaww, style = "W")

# This is what you did:

years <- length(unique(Produc$year))
kronecker.W <- listw2dgCMatrix(W)
W_queen2 <- kronecker(Diagonal(years), kronecker.W)
W_queen <- mat2listw(W_queen2, style = "W")

fm <- log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp
SAR <- spml(fm, listw = W_queen, model="within", spatial.error= NULL,
   lag = TRUE, data=Produc)
# Error in lag.listw(listw, u, zero.policy = zero.policy) :
#   object lengths differ

# It works if you just pass the listw object:
SAR <- spml(fm, listw = W, model="within", spatial.error= NULL,lag = TRUE,
   data=Produc)

When reading documentation, please do not ignore the examples. They are 
tested once a day on more than 12 different platforms, and can be relied 
upon. This is also explained in the JSS article, which I assume you have 
read and will cite.

Roger

>
> SAR <- spml(gdp ~ oivrev, listw = rjq, model="within", spatial.error= NULL,
> lag = TRUE, data=panel)
> summary(W_queen)
> str(panel)
>
> OLS <- plm(gdp ~ oivrev, model = "within", data = panel)
> summary(OLS)
>
> ""
> And, weird the most is when I run OLS model panel works.
>
> Could you try in your own R to check it out?
>
> Thank you.
>
> On Sat, May 5, 2018 at 7:35 AM, Roger Bivand <Roger.Bivand at nhh.no> wrote:
>
>> On Sat, 5 May 2018, felipe tavares wrote:
>>
>> Good evening.
>>>
>>> I am trying to estimate a spatial panel data model through splm package.
>>>
>>> I am facing the error: Error in lag.listw(listw, u, zero.policy =
>>> zero.policy) :
>>>  object lengths differ
>>>
>>> However, my W matrix is NxN, my y vector is NTx1 and my X matrix is NTxK.
>>>
>>> My code is:
>>> poly <- readOGR(dsn ="C:/Users/fstavares/OneDrive/Resource Policy Paper",
>>> layer = "RJ")
>>> RJ <- poly2nb(poly)
>>> W <- nb2listw(RJ, zero.policy = TRUE, style = "W", glist = NULL)
>>> SEM <- spml(gdp ~ oivrev, listw = W, model="within", spatial.error="b",
>>> lag=F, data=panel)
>>>
>>>
>> Are the first two columns of panel as required (from ?spml):
>>
>>     data: an object of class ‘data.frame’ or ‘pdata.frame’. A data
>>           frame containing the variables in the model. When the object
>>           is a ‘data.frame’, the first two columns shall contain the
>>           indexes, unless otherwise specified. See ‘index’
>>
>> From ?index
>>
>>      Panel data are stored in a ‘"pdata.frame"’ which has an ‘"index"’
>>      attribute. Fitted models in ‘"plm"’ have a ‘"model"’ element which
>>      is also a ‘"pdata.frame"’ and therefore also has an ‘"index"’
>>      attribute. Finally, each series, once extracted from a
>>      ‘"pdata.frame"’, becomes of class ‘"pseries"’, which also has this
>>      ‘"index"’ attribute.  ‘"index"’ methods are available for all
>>      these objects.  The argument ‘"which"’ indicates which index
>>      should be extracted. If ‘which = NULL’, all indexes are extracted.
>>      ‘"which"’ can also be a vector of length 1, 2, or 3 (3 only if the
>>      pdata frame was constructed with an additional group index)
>>      containing either characters (the names of the individual variable
>>      and/or of the time variable and/or the group variable or ‘"id"’
>>      and ‘"time"’) and ‘"group"’ or integers (1 for the individual
>>      index, 2 for the time index, and 3 for the group index (the latter
>>      only if the pdata frame was constructed with such).)
>>
>> and:
>>
>> str(Produc)
>>>
>> 'data.frame':   816 obs. of  11 variables:
>>  $ state : Factor w/ 48 levels "ALABAMA","ARIZONA",..: 1 1 1 1 1 1 1 1 1 1
>> ...
>>  $ year  : int  1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 ...
>>  $ region: Factor w/ 9 levels "1","2","3","4",..: 6 6 6 6 6 6 6 6 6 6 ...
>>  $ pcap  : num  15033 15502 15972 16406 16763 ...
>>  $ hwy   : num  7326 7526 7765 7908 8026 ...
>>  $ water : num  1656 1721 1765 1742 1735 ...
>>  $ util  : num  6051 6255 6442 6756 7002 ...
>>  $ pc    : num  35794 37300 38670 40084 42057 ...
>>  $ gsp   : int  28418 29375 31303 33430 33749 33604 35764 37463 39964
>> 40979 ...
>>  $ emp   : num  1010 1022 1072 1136 1170 ...
>>  $ unemp : num  4.7 5.2 4.7 3.9 5.5 7.7 6.8 7.4 6.3 7.1 ...
>>
>> with the individual column first varying slowly, and the time column
>> second varying within the first column values. Since you do not provide a
>> reproducible example (not your data and code, an example using built-in
>> data), it is hard to know.
>>
>> Hope this clarifies,
>>
>> Roger
>>
>>
>>
>>> Does anyone have faced this problem?
>>>
>>> I can send database and code, if it somebody can help me.
>>>
>>>
>>>
>>>
>>>
>> --
>> Roger Bivand
>> Department of Economics, Norwegian Sc
>> <https://maps.google.com/?q=Norwegian+Sc&entry=gmail&source=g>hool of
>> Economics,
>> Helleveien 30, N-5045 Bergen, Norway.
>> voice: +47 55 95 93 55; e-mail: Roger.Bivand at nhh.no
>> http://orcid.org/0000-0003-2392-6140
>> https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en
>
>
>
>
>

-- 
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; e-mail: Roger.Bivand at nhh.no
http://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en


More information about the R-sig-Geo mailing list