空间面板数据分析——R的s p l m包(任建辉,暨南大学)The splm 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’:载入需要的程辑包:spam载入需要的程辑包:gridSpam version 0.40-0 (2013-09-11) is loaded.Type 'help( Spam)' or 'demo( spam)' for a short introductionand overview of this package.Help for individual functions is also obtained by adding thesuffix '.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载入需要的程辑包:miscToolsPlease cite the 'maxLik' package as:Henningsen, Arne and Toomet, Ott (2011). maxLik: A package for maximum likelihood es timation 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, plea se use a forum or 'tracker' at maxLik's R-Forge site:Warning message:程辑包‘Matrix’是用R版本3.0.3 来建造的注意:在导入splm时,如果发现还有其他配套的包没有安装,需要先安装。
2、接着,查看数据及结构,命令如下:data(Produc,package=”Ecdat”)View(Produc)3、引入空间权重矩阵(spatial weights matrix),命令如下data(usaww)Views(usaww)4、空间面板数据的广义矩估计,命令spgmGM<-spgm(log(gsp)~log(pcap)+log(pc)+log(emp)+unemp, data=Produc, listw=usaww, moments=”fullweights”, spatial.error=TRUE)summary(GM)> GM<-spgm(log(gsp)~log(pcap)+log(pc)+log(emp)+unemp,data=Produc,+ listw=usaww,moments="fullweights",spatial.error=TRUE)> summary(GM)Spatial panel fixed effects GM modelCall: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.16800Estimated spatial coefficient, variance components and theta:Estimaterho 0.3277625sigma^2_v 0.0012179Coefficients:Estimate Std. Error t-value Pr(>|t|)log(pcap) -0.0022435 0.0262646 -0.0854 0.9319295log(pc) 0.2414979 0.0235826 10.2405 < 2.2e-16 ***log(emp) 0.7813276 0.0283855 27.5256 < 2.2e-16 ***unemp -0.0036026 0.0010094 -3.5691 0.0003582 ***---Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 15、空间面板极大似然估计,命令spmlfm<- log(gsp)~log(pcap)+log(pc)+log(emp)+unemp## fixed effects panel with spatial errorsFespaterr<-spml(fm,data=Produc,listw=mat2listw(usaww),model=”within ”,spatial.error=”b”,hess=FALSE)summary(Fespaterr)> fm<-log(gsp)~log(pcap)+log(pc)+log(emp)+unemp> Fespaterr<-spml(fm,data=Produc,listw=mat2listw(usaww),model="within",+ spatial.error="b",hess=FALSE)> summary(Fespaterr)Spatial panel fixed effects error modelCall: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.1880Coefficients:Estimate Std. Error t-value Pr(>|t|)rho 0.5574013 0.0329554 16.9138 < 2e-16 ***log(pcap) 0.0051438 0.0250724 0.2052 0.83745log(pc) 0.2053026 0.0231996 8.8494 < 2e-16 ***log(emp) 0.7822540 0.0278741 28.0638 < 2e-16 ***unemp -0.0022317 0.0010735 -2.0788 0.03764 *---Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1##random effects panal with spatial lagRespatlag<-spml(fm,data=Produc,listw=mat2listw(usaww),model=”random ”,spatial.error=”none”,lag=TRUE)summary(Respatlag)> Respatlag<-spml(fm,data=Produc,listw=mat2listw(usaww),model="random",+ spatial.error="none",lag=TRUE)> summary(Respatlag)Spatial panel random effects ML modelCall: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.802.13Error 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|)log(pcap) 0.01294505 0.02493997 0.5190 0.6037unemp -0.00579716 0.00089175 -6.5009 7.984e-11 ***---Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 16、伴有随机效应和序列误差相关的空间面板模型的极大似然估计,命令speml ##random effects panel with spatial lag and serial error correlation ##optimization method set to “BFGS“Sarsrmod<-spreml(fm,data=Froduc,w=usaww,error=”sr”,lag=TRUE,method =”BFGS”)summary(Sarsrmod)> Sarsrmod<-spreml(fm,data=Produc,w=usaww,error="sr",lag=TRUE,method="BFGS")> summary(Sarsrmod)Spatial panel random effects ML modelCall:spreml(formula = fm, data = Produc, w = usaww, lag = TRUE, errors = "sr",method = "BFGS")Residuals:Min. 1st Qu. Median Mean 3rd Qu. Max.2.663.02 3.18 3.18 3.31 3.77Error variance parameters:Estimate Std. Error t-value Pr(>|t|)Spatial autoregressive coefficient:Estimate Std. Error t-value Pr(>|t|)lambda 0.302942 0.030376 9.973 < 2.2e-16 ***Coefficients:Estimate Std. Error t-value Pr(>|t|)log(pcap) 0.08257977 0.03617371 2.2829 0.02244 *log(pc) 0.01509919 0.01977324 0.7636 0.44510unemp -0.00270962 0.00065851 -4.1148 3.875e-05 ***---Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 17、模型检验(1)bsjktest,Baltigi,Song,Jung, and Koh LM test for spatial panels > bsjktest(fm, data=Produc, listw = usaww, test="C.1")Baltagi, Song, Jung and Koh C.1 conditional testdata: log(gsp) ~ log(pcap) + log(pc) + log(emp) + unempLM = 0.2617, df = 1, p-value = 0.609alternative hypothesis: spatial dependence in error terms, sub RE and serial corr. (2)bsktest,Baltigi,Song and Koh LM test for spatial panels> bsktest(fm,data=Produc, listw = mat2listw(usaww),+ test="LM1", standardize=TRUE)Baltagi, Song and Koh SLM1 marginal testdata: log(gsp) ~ log(pcap) + log(pc) + log(emp) + unempSLM1 = 0.083, p-value = 0.9338alternative hypothesis: Random effects(3)Covariance extractor method for splm objects> sarremod<-spml(fm,data=Produc,listw=mat2listw(usaww),model="random",+ lag=TRUE,spatial.error="none")> library(lmtest)> coeftest(sarremod)z test of coefficients:Estimate Std. Error z value Pr(>|z|)log(pcap) 0.01294505 0.02493997 0.5190 0.6037unemp -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 testHypothesis:log(pcap) - log(pc) = 0Model 1: restricted modelModel 2: function (x, ...)UseMethod("formula")Df Chisq Pr(>Chisq)12 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-512.Baltagi,B.H.,Song,S.H and Koh, W.(2003) Testing panel data regression models with spatial error correlation. Journal of Econometrics,117,123-150lo,G.,Piras,G.(2012) splm:Spatial Panel Data Models in R. Journal of Statistical Software,47(1),1-38.URL4.Elhorst, J.P (2003) Specification and estimation of spatial panel data models, International Regional Science Review,26,pages 244-2685.Elhorst, J.P(2009) Spatial panel data models, In Fisher,M.M. and Getis,A.(eds),Handbook of Applied Spatial Analysis Springer,Berlin6.Giovanni Millo and Gaetano Carmeci,(2011) “Non-life insurance consumption in Italy: a subregional panel data analysis”, Journal of Geographical Systems,13:273-2987.Qu Feng and William C. Horrace,(2012)”Alternative Measures ofTechnical 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-1309.Mutl,J.,and Pfaffermayr,M.(2011) The Hausman test in a Cliff and Ord panel model,Econometrics Journal,14,pages 48-7610.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-53311. 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-122lo,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。