[R-sig-Geo] Calculating impact measures for spatial panel models and trouble specifying SDEM model using spml

Roger Bivand Roger@B|v@nd @end|ng |rom nhh@no
Sun Jul 28 16:21:58 CEST 2019


On Thu, 25 Jul 2019, Letisha Sarah Fong Rui Zhen wrote:

> Dear Roger,
>
> Thank you for your quick response.
>
> I have uploaded the spatial weights matrix and sample dataset I'm 
> working with here: 
> https://drive.google.com/drive/folders/1NjCODKEix-_nA5CfiIos6uiKAUbGp_BZ?usp=sharing

Please only use built-in data - loading user-offered data is inherently 
unfortunate for many reasons.

>
> Reading the data in and transforming them into a pdataframe and listw, respectively:
> spatialweight <- read.csv("spatialweight.csv", header = T)
> row.names(spatialweight) <- spatialweight$X
> spatialweight <- spatialweight[, -1]
> spatialweight.mat <- as.matrix(spatialweight)
> mylistw <- mat2listw(spatialweight.mat, style = "M")
> mydata <- read.csv("sampledata.csv", header = T)
> mydata <- pdata.frame(mydata, index = c("Country", "Year"))
>

Hand crafting weights is typically error prone, also because the 
provenance of the weights (how they were constructed) is not known.

> I first ran a non-spatial model to determine the best specification for 
> fixed effects:
>
> nonspatial.pooledOLS <- plm(lnY ~ lnGDP + lnGDP2 + U + RE + IS + lnEI, data = mydata, model = "pooling")
> nonspatial.individualFE <- plm(lnY ~ lnGDP + lnGDP2 + U + RE + IS + lnEI, data = mydata, model = "within", effect = "individual")
> nonspatial.timeFE <- plm(lnY ~ lnGDP + lnGDP2 + U + RE + IS + lnEI, data = mydata, model = "within", effect = "time")
> nonspatial.twowayFE <- plm(lnY ~ lnGDP + lnGDP2 + U + RE + IS + lnEI, data = mydata, model = "within", effect = "twoways")
>
> I would like to compare these models based on log likelihood and AIC, 
> but the plm() function does not appear to provide a log likelihood or 
> AIC. I have read through the JSS plm article and it states that models 
> made with the plm() function are "estimated using the lm function to the 
> transformed data". I'm aware that we can use logLik() and AIC() for a 
> model estimated with the lm() function. However it doesn't seem to work 
> with the plm() function.

So either debug the function itself and find out where the object needed 
gets lost, or write to the package maintainer with a patch to return the 
value, see:

https://r-forge.r-project.org/scm/viewvc.php/pkg/R/est_plm.R?view=markup&root=plm

and its use of the internal function plm:::mylm(). It runs lm(), but only 
returns some of the components in the lm() object.

>
> For example, I did logLik(nonspatial.twowayFE) and 
> AIC(nonspatial.twowayFE) but the error message for both is:
>
> Error in UseMethod("logLik") :
>  no applicable method for 'logLik' applied to an object of class
>  "c('plm', 'panelmodel')"
>
> Please let me know if I'm calling the wrong function(s) and/or if you're 
> aware of a way to compare these models based on log likelihood and/or 
> AIC.
>
> For the spatial models, here is my code:
>
> spatial.SDEM <- spml(lnY ~ lnGDP + lnGDP2 + U + RE + IS + lnEI + slag(lnGDP, listw = mylistw) + slag(lnGDP2, listw = mylistw) + slag(lnEI, listw = mylistw), data = mydata, listw = mylistw, model = "within", effect = "twoways", lag = F, spatial.error = "b")
> spatial.SEM <- spml(lnY ~ lnGDP + lnGDP2 + U + RE + IS + lnEI, data = mydata, listw = mylistw, model = "within", effect = "individual", lag = F, spatial.error = "b")
> spatial.SLX <- plm(lnY ~ lnGDP + lnGDP2 + U + RE + IS + lnEI + slag(lnGDP, listw = mylistw) + slag(lnGDP2, listw = mylistw) + slag(lnEI, listw = mylistw), data = mydata, model = "within", effect = "individual")
>
> As in my original post, the SLX and SEM models ran OK but the error when 
> I try to run the SDEM model is:
>
> Error in UseMethod("slag") :
>  no applicable method for 'slag' applied to an object of class
>  "c('double', 'numeric')"

Please replicate with built-in data. If you cannot replicate, it is 
something in your data that you need to address.

Roger

>
> The variables that I use the slag() function on are all numeric, so I 
> don't know what's wrong. I seem to be able to use slag() with plm() but 
> not with spml(), but I don't know why this is so.
>
> I need to compare the models to see if SDEM can be reduced to one of its 
> nested form. As was the case of the non-spatial models, I can't get the 
> log likelihood for models created with the plm() function, so any 
> suggestions are welcome. I've already read through the JSS articles for 
> splm and plm as well as both documentations and there's no information 
> on this (except that models built with the plm() function are estimated 
> using the lm function to the transformed data).
>
> Thanks for clarifying the impact measures for SDEM and SLX. Just to 
> check - when you say linear combination for standard errors do you mean 
> e.g. beta1*se + theta1*se = totalse (where beta1 is the coefficient of 
> the direct impact and theta1 is the coefficient of the indirect impact)?
>
> Thank you for your help!
>
> Best wishes,
> Sarah
>
> ________________________________
> From: Roger Bivand <Roger.Bivand using nhh.no>
> Sent: Wednesday, July 24, 2019 9:50:13 PM
> To: Letisha Sarah Fong Rui Zhen <geolsfrz using nus.edu.sg>
> Cc: r-sig-geo using r-project.org <r-sig-geo using r-project.org>
> Subject: Re: [R-sig-Geo] Calculating impact measures for spatial panel models and trouble specifying SDEM model using spml
>
> On Wed, 24 Jul 2019, Letisha Sarah Fong Rui Zhen wrote:
>
>> Dear all,
>
> Please do not post HTML-formated messages.
>
>>
>> I???m working with panel data of 9 countries and 18 years and I???m
>> running fixed effects SDEM, SLX and SEM with the splm package.
>>
>> I have three questions:
>>
>> 1. I can???t seem to get the SDEM model to run. My code for each of the
>>    3 models is:
>>
>> model.SDEM <- spml(ln(Y) ~ ln(GDP) + (ln(GDP))^2 + EI + RE +
>> slag(ln(GDP), listw = my.listw) + slag((ln(GDP))^2, listw = my.listw),
>> data = my.data, listw = my.listw, model = ???within???, effect =
>> ???individual???, lag = F, spatial.error = ???b???)
>>
>> model.SLX <- plm(ln(Y) ~ ln(GDP) + (ln(GDP))^2 + EI + RE + slag(ln(GDP),
>> listw = my.listw) + slag((ln(GDP))^2, listw = my.listw), data = my.data,
>> model = ???within???, effect = ???individual???)
>>
>> model.SEM <- spml(ln(Y) ~ ln(GDP) + (ln(GDP))^2 + EI + RE, data =
>> my.data, listw = my.listw, model = ???within???, effect =
>> ???individual???, lag = F, spatial.error = ???b???)
>>
>> I am able to run both SLX and SEM models without problem, but when I try
>> to run the SDEM model, the error message is:
>>
>> Error in UseMethod("slag") :
>>  no applicable method for 'slag' applied to an object of class
>>  "c('double', 'numeric')"
>>
>> I don???t understand what is wrong here, as I have no problems with the
>> slag() function in the SLX model. My data is a pdataframe and each
>> variable is a numeric pseries.
>
> My guess would be that you should protect your square with I() in general,
> but have no idea - this is not a reproducible example.
>
>>
>>
>> 2. How can I calculate impact measures (direct, indirect and total) for
>>    spatial panel models?
>>
>> The impacts() function in spdep doesn???t work anymore and the impacts()
>> function from the spatialreg package seems to work only for
>> cross-sectional data and not panel data.
>>
>> For example, I ran:
>>
>> spatialreg::impacts(model.SLX)
>>
>> And the error message is:
>>
>> Error in UseMethod("impacts", obj) :
>>  no applicable method for 'impacts' applied to an object of class
>>  "c('plm', 'panelmodel')"
>>
>> I have tried methods(impacts) but none of the functions seem to work for
>> my SLX model created with the splm package.
>
> But your SLX model is created with the plm package, isn't it? The only use
> of splm is for the manual lags with slag()?
>
>>
>> I also looked at some previous examples in the splm documentation and
>> more specifically the spml() function and the example provided
>> (specifically the impact measures) doesn???t work anymore:
>>
>> data(Produc, package = "plm")
>> data(usaww)
>> fm <- log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp
>> ## random effects panel with spatial lag
>> respatlag <- spml(fm, data = Produc, listw = mat2listw(usaww),
>>                  model="random", spatial.error="none", lag=TRUE)
>> summary(respatlag) ## calculate impact measures impac1 <-
>> impacts(respatlag, listw = mat2listw(usaww, style = "W"), time = 17)
>> summary(impac1, zstats=TRUE, short=TRUE)
>>
>
> The implemented impacts methods in splm apply to the case where the lagged
> response is included. For SDEM and SLX, you can get the impacts by
> addition for the total impactss, and by linear combination for their
> standard errors. This is not implemented in a method. Further, slag() does
> not give any impacts method any information on which variables have been
> lagged - in your illustration above EI and RE are not lagged.
>
>> The error message when I run the impacts() function is:
>>
>> Error in UseMethod("impacts", obj) :
>>  no applicable method for 'impacts' applied to an object of class "splm"
>>
>> My question is therefore, how do I go about calculating direct, indirect
>> and total impact measures for spatial panel data?
>>
>>
>> 3. How can I test if the SDEM model can be simplified to the SLX model
>>    (since I estimate the SDEM by maximum likelihood (spml function) and
>>    the SLX by ordinary linear regression (plm function))? From my
>>    understanding the plm() function does not compute a loglikelihood or
>>    AIC so I probably can???t do a likelihood ratio test to choose
>>    between models (I haven???t tried this out because I???m stuck at
>>    running the SDEM model).
>
> Do you know definitely that plm does not provide a log likelihood? I
> realise that it isn't OLS unless pooled. Have you reviewed the JSS plm and
> splm articles?
>
> Roger
>
>>
>> Any help or advice would be greatly appreciated. Thank you.
>>
>> Best wishes,
>> Sarah
>>
>>
>>
>> ________________________________
>>
>> Important: This email is confidential and may be privileged. If you are
>> not the intended recipient, please delete it and notify us immediately;
>> you should not copy or use it for any purpose, nor disclose its contents
>> to any other person. Thank you.
>>
>>        [[alternative HTML version deleted]]
>>
>>
>
> --
> 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 using nhh.no
> https://orcid.org/0000-0003-2392-6140
> https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en
>
> ________________________________
>
> Important: This email is confidential and may be privileged. If you are not the intended recipient, please delete it and notify us immediately; you should not copy or use it for any purpose, nor disclose its contents to any other person. Thank you.
>

-- 
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 using nhh.no
https://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en



More information about the R-sig-Geo mailing list