R语言与面板数据
1 安装包Paneldata说明
Paneldata用于线性面板数据模型回归,包括固定效应与随机效应。语法格式为
Paneldata(formula, data = list(), n, t, model = c(“fe”, “re”), …)
其中formula表示回归公式;data 表示需要用到的面板数据;n:截面个数;t:时间长度;model表示回归方法,可以选择fe固定效应或re随机效应。此外,还可以直接适用固定效应函数与随机效应函数回归。固定效应函数语法格式
fixed(y, x, n, t)
y表示被解释变量向量;x表示解释变量矩阵;n为截面数,t是时间长度。随机效应函数语法格式
Rand(y, x, n, t)
2 举例操作
# install.packages("Paneldata") 第一次适用记得安装
library(Paneldata)
pib<-as.matrix(c(12,3,4,0.4,0.7,5,0.7,0.3,0.6,89,7,8,45,7,4,5,0.5,5),
nrows=18,ncols=1)
tir<-as.matrix(c(12,0.3,4,0.4,7,12,3.0,6.0,45,7.0,0.8,44,65,23,4,6,76,9),
nrows=18,ncols=1)
inf<-as.matrix(c(1.2,3.6,44,1.4,0.78,54,0.34,0.66,12,0.7,8.0,12,65,43,5,76,65,8),
nrows=18,ncols=1)
npl<-as.matrix(c(0.2,3.8,14,2.4,1.7,43,0.2,0.5,23,7.8,88,36,65,3,44,65,7,34),
nrows=18,ncols=1)
# create a data frame
mdata<-data.frame(p=pib,t=tir,int=inf,np=npl)
# fit the fixed function
fx<-Paneldata(t~p+int+np,mdata,n=6,t=3,model="fe")
summary(fx)
#----------下面是结果---------
$call
Paneldata.formula(formula = t ~ p + int + np, data = mdata, n = 6,
t = 3, model = "fe")
$coefficients
Estimate Std.Err T value Pr(>z)
(Intercept) -1.661707e-34 7.712676e-26 -2.154514e-09 1.00000000
p -7.320181e-02 3.438750e-01 -2.128733e-01 0.83449425
int 5.643656e-01 3.035607e-01 1.859152e+00 0.08415063
np -2.936620e-01 2.961002e-01 -9.917655e-01 0.33814463
# fit the random function
rx<-Paneldata(t~p+int+np,mdata,n=6,t=3,model="re")
summary(rx)
#----------下面是结果---------
$call
Paneldata.formula(formula = t ~ p + int + np, data = mdata, n = 6,
t = 3, model = "re")
$coefficients
Estimate Std.Err T value Pr(>z)
(Intercept) 12.78988444 19.4115730 0.6588793 0.52066339
p -0.04515299 0.2715156 -0.1662998 0.87029887
int 0.55080874 0.2406516 2.2888222 0.03814986
np -0.26792475 0.2342418 -1.1437954 0.27188959
固定效应函数
# create data frame
mdata<-data.frame(p=pib,ti=tir,int=inf,np=npl)
# create the designed matrix for the model
d<-matrix(c(mdata$p,mdata$int,mdata$np),18, 3)
# Fit a fixed model
fx<-fixed(mdata$p,d,n=6,t=3)
fx
#------------下面是结果----------------
$coefficients 回归系数
[,1]
[1,] 1.000000e+00
[2,] -8.326673e-17
[3,] -1.110223e-16
$vcov 系数的方差协方差矩阵,对角线是方差,非对角协方差
[,1] [,2] [,3]
[1,] 4.381833e-32 -7.959139e-33 1.163005e-32
[2,] -7.959139e-33 3.414647e-32 -1.338142e-32
[3,] 1.163005e-32 -1.338142e-32 3.248868e-32
$df 自由度
[1] 15
随机效应函数
mdata<-data.frame(p=pib,t=tir,int=inf,np=npl)
# create the designed matrix for the model
d<-matrix(c(mdata$p,mdata$int,mdata$np),18, 3)
# Fit a random model
rx<-Rand(mdata$p,d,n=6,t=3)
rx
#----------下面是结果---------
$coefficients
[,1]
[1,] 1.000000e+00
[2,] -1.249001e-16
[3,] -5.551115e-17
$Std 标准差
[1] 1.880880e-16 1.660374e-16 1.619568e-16
$stat student值,即t值
[,1]
[1,] 5.316662e+15
[2,] -7.522406e-01
[3,] -3.427529e-01
$vcov 系数方差协方差矩阵
[,1] [,2] [,3]
[1,] 3.537708e-32 -6.425874e-33 9.389612e-33
[2,] -6.425874e-33 2.756843e-32 -1.080360e-32
[3,] 9.389612e-33 -1.080360e-32 2.623000e-32
$df
[1] 15
参考文献
https://cran.r-project.org/web/packages/Paneldata/Paneldata.pdf
Amemiyia, T. (1971) The estimation of the variances in a variance–components model, International Economic Review, 12, pp.1–13.
Baltagi, B.H. (1981) Simultaneous equations with error components, Journal of econometrics, 17, pp.21–49.
Baltagi, B.H. (2001) Econometric Analysis of Panel Data. John Wiley and sons. ltd