您确实可以使用 slag 包含空间滞后的解释变量。但是,您需要确保您的数据属于 pdata.frame 类。
一个可重现的例子:
library(plm)
library(spatialreg)
library(splm)
# load data
data(Produc, package = "plm")
data(usaww, package = "splm")
d <- pdata.frame(Produc, index = c("state","year"), drop.index = FALSE)
# create a spatial explanatory variable
d$unemp_l <- slag(d$unemp, usaww)
# run model
m <- splm::spml(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp + unemp_l,
data = d, listw = mat2listw(usaww) , model="within")
summary(m)
Spatial panel fixed effects error model
Call:
splm::spml(formula = log(gsp) ~ log(pcap) + log(pc) + log(emp) +
unemp + unemp_l, data = d, listw = mat2listw(usaww), model = "within")
Residuals:
Min. 1st Qu. Median 3rd Qu. Max.
-0.1211492 -0.0234013 -0.0040218 0.0167919 0.1787587
Spatial error parameter:
Estimate Std. Error t-value Pr(>|t|)
rho 0.542254 0.033772 16.056 < 2.2e-16 ***
Coefficients:
Estimate Std. Error t-value Pr(>|t|)
log(pcap) 0.0090575 0.0251036 0.3608 0.71824
log(pc) 0.2152367 0.0234077 9.1951 < 2e-16 ***
log(emp) 0.7833003 0.0277672 28.2096 < 2e-16 ***
unemp -0.0014795 0.0011443 -1.2930 0.19603
unemp_l -0.0031210 0.0015790 -1.9766 0.04808 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
您可以验证空间解释是否正确。例如:
a <- usaww["ALABAMA",]
a <- a[a!=0]
a
FLORIDA GEORGIA MISSISSIPPI TENNESSE
0.25 0.25 0.25 0.25
mean(d[d$year=="1970" & d$state %in% names(a) , "unemp"])
[1] 4.525
d[d$state=="ALABAMA" & d$year=="1970", "unemp_l"]
ALABAMA-1970
4.525