空间面板数据分析——R 的splm 包
(任建辉,暨南大学)
The package provides methods for fitting spatial panel data by maximum likelihood and GM.
安装R 软件及其编辑器Rstudio
网址:
下载好Rstudio 以后,操作都可以Rstudio 中完成了,包括命令的编写、命令运行、图形展示,最方便的要数查看数据了。
R 界面
Rstudio 界面,形如
matlab
下面进入正题,了解splm 包中的数据、命令及结果展示。所有命令都写在编辑窗口(studio 左上区域),可以单独的运行每行命令,也可选取一段一起执行,点run 按钮。
1、首先,安装splm 包并导入,命令如下:
intall.packages(“splm ”) ,选择最近的下载点
library(splm)
> library(splm)
载入需要的程辑包:MASS
载入需要的程辑包:nlme
载入需要的程辑包:spdep
载入需要的程辑包:sp
载入需要的程辑包:Matrix
载入需要的程辑包:plm
载入需要的程辑包:bdsmatrix
载入程辑包:‘bdsmatrix’
下列对象被屏蔽了from ‘package:base’:
backsolve
载入需要的程辑包:Formula
载入需要的程辑包:sandwich
载入需要的程辑包:zoo
载入程辑包:‘zoo’
下列对象被屏蔽了from ‘package:base’:
as.Date, as.Date.numeric
载入需要的程辑包:spam
载入需要的程辑包:grid Spam version 0.40-0 (2013-09-11) is loaded. Type 'help( Spam)' or 'demo( spam)' for a short introduction and overview of this package. Help for individual functions is also obtained by adding the
suffix '.spam' to the function name, e.g. 'help( chol.spam)'.
载入程辑包:‘spam’
下列对象被屏蔽了from ‘package:bdsmatrix’:
backsolve
下列对象被屏蔽了from ‘package:base’:
backsolve, forwardsolve
载入需要的程辑包:ibdreg
载入需要的程辑包:car
载入需要的程辑包:lmtest
载入需要的程辑包:Ecdat
载入程辑包:‘Ecdat’
下列对象被屏蔽了from ‘package:car’:
Mroz
下列对象被屏蔽了from ‘package:nlme’:
Gasoline
下列对象被屏蔽了from ‘package:MASS’:
SP500
下列对象被屏蔽了from ‘package:datasets’:
Orange
载入需要的程辑包:maxLik
载入需要的程辑包:miscTools Please cite the 'maxLik' package as: Henningsen, Arne and Toomet, Ott (2011). maxLik: A package for maximum likelihood estimation in R. Computational Statistics 26(3), 443-458. DOI 10.1007/s00180-010-0217-1. If you have questions, suggestions, or comments regarding the 'maxLik' package, please use a forum or 'tracker' at maxLik's R-Forge site: https://r-forge.r-project.org/projects/maxlik/ Warning message:
程辑包‘Matrix’是用R 版本3.0.3 来建造的
注意:在导入splm 时,如果发现还有其他配套的包没有安装,需要先安装。
2、接着,查看数据及结构,命令如下:
data(Produc,package=”Ecdat ”)
View(Produc)
3、引入空间权重矩阵(spatial weights matrix),命令如下
data(usaww)
Views(usaww)
4、空间面板数据的广义矩估计,命令spgm
GM
listw=usaww, moments=”fullweights ”, spatial.error=TRUE)
summary(GM)
> GM
+ listw=usaww,moments="fullweights",spatial.error=TRUE)
> summary(GM)
Spatial panel fixed effects GM model Call: spgm(formula = log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, data = Produc, listw = usaww, spatial.error = TRUE, moments = "fullweights") Residuals: Min. 1st Qu. Median 3rd Qu. Max.
-0.14000 -0.01950 -0.00316 0.01530 0.16800 Estimated spatial coefficient, variance components and theta: Estimate rho 0.3277625
sigma^2_v 0.0012179 Coefficients: Estimate Std. Error t-value Pr(>|t|) log(pcap) -0.0022435 0.0262646 -0.0854 0.9319295 log(pc) 0.2414979 0.0235826 10.2405
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
5、空间面板极大似然估计,命令spml
fm
## fixed effects panel with spatial errors
Fespaterr
spatial.error=”b ”,hess=FALSE)
summary(Fespaterr)
> fm
> Fespaterr
> summary(Fespaterr)
Spatial panel fixed effects error model Call: spml(formula = fm, data = Produc, listw = mat2listw(usaww), model = "within", spatial.error = "b", hess = FALSE) Residuals: Min. 1st Qu. Median 3rd Qu. Max.
-0.1250 -0.0238 -0.0035 0.0171 0.1880 Coefficients: Estimate Std. Error t-value Pr(>|t|) rho 0.5574013 0.0329554 16.9138
S ignif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
##random effects panal with spatial lag
Respatlag
spatial.error=”none ”,lag=TRUE)
summary(Respatlag)
> Respatlag
> summary(Respatlag)
Spatial panel random effects ML model Call: spreml(formula = formula, data = data, index = index, w = listw2mat(listw), w2 = listw2mat(listw2), lag = lag, errors = errors, cl = cl) Residuals: Min. 1st Qu. Median Mean 3rd Qu. Max.
1.38 1.57 1.70 1.70 1.80 2.13 Error variance parameters: Estimate Std. Error t-value Pr(>|t|)
phi 21.3175 8.3017 2.5678 0.01023 *
Spatial autoregressive coefficient:
Estimate Std. Error t-value Pr(>|t|)
lambda 0.161615 0.029099 5.554 2.793e-08 *** Coefficients: Estimate Std. Error t-value Pr(>|t|) (Intercept) 1.65814995 0.15071855 11.0016
S ignif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
6、伴有随机效应和序列误差相关的空间面板模型的极大似然估计,命令speml
##random effects panel with spatial lag and serial error correlation
##optimization method set to “BFGS “
Sarsrmod
summary(Sarsrmod)
> Sarsrmod summary(Sarsrmod)
Spatial panel random effects ML model Call: spreml(formula = fm, data = Produc, w = usaww, lag = TRUE, errors = "sr", method = "BFGS") Residuals: Min. 1st Qu. Median Mean 3rd Qu. Max.
2.66 3.02 3.18 3.18 3.31 3.77 Error variance parameters: Estimate Std. Error t-value Pr(>|t|)
psi 0.99726353 0.00082138 1214.1 |t|)
lambda 0.302942 0.030376 9.973 |t|) (Intercept) 1.23670293 0.22777554 5.4295 5.652e-08 ***
log(pcap) 0.08257977 0.03617371 2.2829 0.02244 *
log(pc) 0.01509919 0.01977324 0.7636 0.44510 log(emp) 0.73882021 0.02934144 25.1801
Signi f. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
7、模型检验
(1)bsjktest ,Baltigi,Song,Jung, and Koh LM test for spatial panels
> bsjktest(fm, data=Produc, listw = usaww, test="C.1")
data: log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp LM = 0.2617, df = 1, p-value = 0.609 alternative hypothesis: spatial dependence in error terms, sub RE and serial corr.
Baltagi, Song, Jung and Koh C.1 conditional test (2)bsktest ,Baltigi,Song and Koh LM test for spatial panels
> bsktest(fm,data=Produc, listw = mat2listw(usaww),
+ test="LM1", standardize=TRUE)
data: log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp SLM1 = 0.083, p-value = 0.9338
alternative hypothesis: Random effects
Baltagi, Song and Koh SLM1 marginal test (3)Covariance extractor method for splm objects
> sarremod
> library(lmtest)
> coeftest(sarremod)
z test of coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 1.65814995 0.15071855 11.0016
log(emp) 0.67081075 0.02642113 25.3892
unemp -0.00579716 0.00089175 -6.5009 7.984e-11 *** ---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> library(car)
> lht(sarremod,"log(pcap)=log(pc)")
Linear hypothesis test Hypothesis:
log(pcap) - log(pc) = 0 Model 1: restricted model Model 2: function (x, ...)
UseMethod("formula") Df Chisq Pr(>Chisq) 1 2 1 36.268 1.719e-09 *** ---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(更多详情请查看splm 的help 文档以及文后列的参考文献)
参考文献
1.Baltagi,B.H.,Song,S.H.,Jung B. and Koh, W.(2007) Testing panel data regression models with spatial and serial error correlation. Journal of Econometrics,140,5-51
2.Baltagi,B.H.,Song,S.H and Koh, W.(2003) Testing panel data regression models with spatial error correlation. Journal of Econometrics,117,123-150
3.Millo,G.,Piras,G.(2012) splm:Spatial Panel Data Models in R. Journal of Statistical Software,47(1),1-38.URL
4.Elhorst, J.P (2003) Specification and estimation of spatial panel data models, International
Regional Science Review,26,pages 244-268
5.Elhorst, J.P(2009) Spatial panel data models, In Fisher,M.M. and Getis,A.(eds),Handbook of Applied Spatial Analysis Springer,Berlin
6.Giovanni Millo and Gaetano Carmeci,(2011) “Non-life insurance consumption in Italy: a subregional panel data analysis”, Journal of Geographical Systems,13:273-298
7.Qu Feng and William C. Horrace,(2012)”Alternative Measures of Technical Efficiency :Skew,Bias and Scale”, Journal of Applied Econometrics, Forthcoming.
8.Kapoor,M.,Kelejian,H.H. and Prucha,I.R.(2007) Panel data model with spatially correlated error components,Journal of Econometrics,140,pages 97-130
9.Mutl,J.,and Pfaffermayr,M.(2011) The Hausman test in a Cliff and Ord panel model,Econometrics Journal,14,pages 48-76
10.Kelejian,H.H. and Prucha,I.R.(1999) A Generalied Moments Estimator for the Autoregressive Parameter in a Spatial Model, International Economic Reviews,40, pages509-533
11. Kelejian,H.H. and Prucha,I.R.(1999) A Generalied Spatial Two Stage Least Square Procedure for Estimating a Spatial Autoregressive Model with Autoregressive Disturbances,Journal of Real Estate Finance and Economics,17,pages99-122
12.Millo,G.(2013) Maximum likelihood estimation of spatially and serially correlated panel with random effects. Computational Statistics and Data Analysis, forthcoming.
13.Zeileis,A(2006)Object-Oriented Computation of Sandwich Estimators. Journal of Statistical Software,16(9),1-16
空间面板数据分析——R 的splm 包
(任建辉,暨南大学)
The package provides methods for fitting spatial panel data by maximum likelihood and GM.
安装R 软件及其编辑器Rstudio
网址:
下载好Rstudio 以后,操作都可以Rstudio 中完成了,包括命令的编写、命令运行、图形展示,最方便的要数查看数据了。
R 界面
Rstudio 界面,形如
matlab
下面进入正题,了解splm 包中的数据、命令及结果展示。所有命令都写在编辑窗口(studio 左上区域),可以单独的运行每行命令,也可选取一段一起执行,点run 按钮。
1、首先,安装splm 包并导入,命令如下:
intall.packages(“splm ”) ,选择最近的下载点
library(splm)
> library(splm)
载入需要的程辑包:MASS
载入需要的程辑包:nlme
载入需要的程辑包:spdep
载入需要的程辑包:sp
载入需要的程辑包:Matrix
载入需要的程辑包:plm
载入需要的程辑包:bdsmatrix
载入程辑包:‘bdsmatrix’
下列对象被屏蔽了from ‘package:base’:
backsolve
载入需要的程辑包:Formula
载入需要的程辑包:sandwich
载入需要的程辑包:zoo
载入程辑包:‘zoo’
下列对象被屏蔽了from ‘package:base’:
as.Date, as.Date.numeric
载入需要的程辑包:spam
载入需要的程辑包:grid Spam version 0.40-0 (2013-09-11) is loaded. Type 'help( Spam)' or 'demo( spam)' for a short introduction and overview of this package. Help for individual functions is also obtained by adding the
suffix '.spam' to the function name, e.g. 'help( chol.spam)'.
载入程辑包:‘spam’
下列对象被屏蔽了from ‘package:bdsmatrix’:
backsolve
下列对象被屏蔽了from ‘package:base’:
backsolve, forwardsolve
载入需要的程辑包:ibdreg
载入需要的程辑包:car
载入需要的程辑包:lmtest
载入需要的程辑包:Ecdat
载入程辑包:‘Ecdat’
下列对象被屏蔽了from ‘package:car’:
Mroz
下列对象被屏蔽了from ‘package:nlme’:
Gasoline
下列对象被屏蔽了from ‘package:MASS’:
SP500
下列对象被屏蔽了from ‘package:datasets’:
Orange
载入需要的程辑包:maxLik
载入需要的程辑包:miscTools Please cite the 'maxLik' package as: Henningsen, Arne and Toomet, Ott (2011). maxLik: A package for maximum likelihood estimation in R. Computational Statistics 26(3), 443-458. DOI 10.1007/s00180-010-0217-1. If you have questions, suggestions, or comments regarding the 'maxLik' package, please use a forum or 'tracker' at maxLik's R-Forge site: https://r-forge.r-project.org/projects/maxlik/ Warning message:
程辑包‘Matrix’是用R 版本3.0.3 来建造的
注意:在导入splm 时,如果发现还有其他配套的包没有安装,需要先安装。
2、接着,查看数据及结构,命令如下:
data(Produc,package=”Ecdat ”)
View(Produc)
3、引入空间权重矩阵(spatial weights matrix),命令如下
data(usaww)
Views(usaww)
4、空间面板数据的广义矩估计,命令spgm
GM
listw=usaww, moments=”fullweights ”, spatial.error=TRUE)
summary(GM)
> GM
+ listw=usaww,moments="fullweights",spatial.error=TRUE)
> summary(GM)
Spatial panel fixed effects GM model Call: spgm(formula = log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, data = Produc, listw = usaww, spatial.error = TRUE, moments = "fullweights") Residuals: Min. 1st Qu. Median 3rd Qu. Max.
-0.14000 -0.01950 -0.00316 0.01530 0.16800 Estimated spatial coefficient, variance components and theta: Estimate rho 0.3277625
sigma^2_v 0.0012179 Coefficients: Estimate Std. Error t-value Pr(>|t|) log(pcap) -0.0022435 0.0262646 -0.0854 0.9319295 log(pc) 0.2414979 0.0235826 10.2405
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
5、空间面板极大似然估计,命令spml
fm
## fixed effects panel with spatial errors
Fespaterr
spatial.error=”b ”,hess=FALSE)
summary(Fespaterr)
> fm
> Fespaterr
> summary(Fespaterr)
Spatial panel fixed effects error model Call: spml(formula = fm, data = Produc, listw = mat2listw(usaww), model = "within", spatial.error = "b", hess = FALSE) Residuals: Min. 1st Qu. Median 3rd Qu. Max.
-0.1250 -0.0238 -0.0035 0.0171 0.1880 Coefficients: Estimate Std. Error t-value Pr(>|t|) rho 0.5574013 0.0329554 16.9138
S ignif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
##random effects panal with spatial lag
Respatlag
spatial.error=”none ”,lag=TRUE)
summary(Respatlag)
> Respatlag
> summary(Respatlag)
Spatial panel random effects ML model Call: spreml(formula = formula, data = data, index = index, w = listw2mat(listw), w2 = listw2mat(listw2), lag = lag, errors = errors, cl = cl) Residuals: Min. 1st Qu. Median Mean 3rd Qu. Max.
1.38 1.57 1.70 1.70 1.80 2.13 Error variance parameters: Estimate Std. Error t-value Pr(>|t|)
phi 21.3175 8.3017 2.5678 0.01023 *
Spatial autoregressive coefficient:
Estimate Std. Error t-value Pr(>|t|)
lambda 0.161615 0.029099 5.554 2.793e-08 *** Coefficients: Estimate Std. Error t-value Pr(>|t|) (Intercept) 1.65814995 0.15071855 11.0016
S ignif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
6、伴有随机效应和序列误差相关的空间面板模型的极大似然估计,命令speml
##random effects panel with spatial lag and serial error correlation
##optimization method set to “BFGS “
Sarsrmod
summary(Sarsrmod)
> Sarsrmod summary(Sarsrmod)
Spatial panel random effects ML model Call: spreml(formula = fm, data = Produc, w = usaww, lag = TRUE, errors = "sr", method = "BFGS") Residuals: Min. 1st Qu. Median Mean 3rd Qu. Max.
2.66 3.02 3.18 3.18 3.31 3.77 Error variance parameters: Estimate Std. Error t-value Pr(>|t|)
psi 0.99726353 0.00082138 1214.1 |t|)
lambda 0.302942 0.030376 9.973 |t|) (Intercept) 1.23670293 0.22777554 5.4295 5.652e-08 ***
log(pcap) 0.08257977 0.03617371 2.2829 0.02244 *
log(pc) 0.01509919 0.01977324 0.7636 0.44510 log(emp) 0.73882021 0.02934144 25.1801
Signi f. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
7、模型检验
(1)bsjktest ,Baltigi,Song,Jung, and Koh LM test for spatial panels
> bsjktest(fm, data=Produc, listw = usaww, test="C.1")
data: log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp LM = 0.2617, df = 1, p-value = 0.609 alternative hypothesis: spatial dependence in error terms, sub RE and serial corr.
Baltagi, Song, Jung and Koh C.1 conditional test (2)bsktest ,Baltigi,Song and Koh LM test for spatial panels
> bsktest(fm,data=Produc, listw = mat2listw(usaww),
+ test="LM1", standardize=TRUE)
data: log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp SLM1 = 0.083, p-value = 0.9338
alternative hypothesis: Random effects
Baltagi, Song and Koh SLM1 marginal test (3)Covariance extractor method for splm objects
> sarremod
> library(lmtest)
> coeftest(sarremod)
z test of coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 1.65814995 0.15071855 11.0016
log(emp) 0.67081075 0.02642113 25.3892
unemp -0.00579716 0.00089175 -6.5009 7.984e-11 *** ---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> library(car)
> lht(sarremod,"log(pcap)=log(pc)")
Linear hypothesis test Hypothesis:
log(pcap) - log(pc) = 0 Model 1: restricted model Model 2: function (x, ...)
UseMethod("formula") Df Chisq Pr(>Chisq) 1 2 1 36.268 1.719e-09 *** ---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(更多详情请查看splm 的help 文档以及文后列的参考文献)
参考文献
1.Baltagi,B.H.,Song,S.H.,Jung B. and Koh, W.(2007) Testing panel data regression models with spatial and serial error correlation. Journal of Econometrics,140,5-51
2.Baltagi,B.H.,Song,S.H and Koh, W.(2003) Testing panel data regression models with spatial error correlation. Journal of Econometrics,117,123-150
3.Millo,G.,Piras,G.(2012) splm:Spatial Panel Data Models in R. Journal of Statistical Software,47(1),1-38.URL
4.Elhorst, J.P (2003) Specification and estimation of spatial panel data models, International
Regional Science Review,26,pages 244-268
5.Elhorst, J.P(2009) Spatial panel data models, In Fisher,M.M. and Getis,A.(eds),Handbook of Applied Spatial Analysis Springer,Berlin
6.Giovanni Millo and Gaetano Carmeci,(2011) “Non-life insurance consumption in Italy: a subregional panel data analysis”, Journal of Geographical Systems,13:273-298
7.Qu Feng and William C. Horrace,(2012)”Alternative Measures of Technical Efficiency :Skew,Bias and Scale”, Journal of Applied Econometrics, Forthcoming.
8.Kapoor,M.,Kelejian,H.H. and Prucha,I.R.(2007) Panel data model with spatially correlated error components,Journal of Econometrics,140,pages 97-130
9.Mutl,J.,and Pfaffermayr,M.(2011) The Hausman test in a Cliff and Ord panel model,Econometrics Journal,14,pages 48-76
10.Kelejian,H.H. and Prucha,I.R.(1999) A Generalied Moments Estimator for the Autoregressive Parameter in a Spatial Model, International Economic Reviews,40, pages509-533
11. Kelejian,H.H. and Prucha,I.R.(1999) A Generalied Spatial Two Stage Least Square Procedure for Estimating a Spatial Autoregressive Model with Autoregressive Disturbances,Journal of Real Estate Finance and Economics,17,pages99-122
12.Millo,G.(2013) Maximum likelihood estimation of spatially and serially correlated panel with random effects. Computational Statistics and Data Analysis, forthcoming.
13.Zeileis,A(2006)Object-Oriented Computation of Sandwich Estimators. Journal of Statistical Software,16(9),1-16