參考一:https://www.cnblogs.com/laoketeng/p/11268581.html
library(plm)
library(psych)
library(xts)
library(tseries)
library(lmtest)
## import dataset
datas<-read.table("data.txt",header =TRUE)
## adf test
pcgdp<-xts(datas$PCGDP,as.Date(datas$year))
adf.test(pcgdp)
# result: stationary
ltax<-xts(datas$Ltax,as.Date(datas$year))
adf.test(ltax)
# result: stationary
hp<-xts(datas$hp,as.Date(datas$year))
adf.test(hp)
# result: stationary
lp<-xts(datas$lp,as.Date(datas$year))
adf.test(lp)
# result: stationary
## 協(xié)整檢驗(yàn)
# Engle-Granger
reg<-lm(datas$hp~datas$lp+datas$Ltax+datas$PCGDP)
summary(reg)
error<-residuals(reg)
adf.test(error)
# result: residuals stationary
### 面板數(shù)據(jù)回歸
hpdatas<-plm.data(datas,index=c("city","year"))
# Pooled Regression Model
hp_pool<-plm(hp~lp+Ltax+PCGDP+PP,data=hpdatas,model = "pooling")
# Fixed Effects Regression Model
hp_fe<-plm(hp~lp+Ltax+PCGDP+PP,data=hpdatas,model = "within")
# F-test :
pFtest(hp_fe,hp_pool)
# result: significant effects
# Random Effects Regression Model
hp_re<-plm(hp~lp+Ltax+PCGDP,data=hpdatas,model="random",random.method = "swar")
# Hausman test
phtest(hp_fe,hp_re)
# if p<0.05,then use fixed effects
# result: p=0.6785>0.05,use random ffects
# Random Effects Regression Model
hp_re<-plm(hp~lp+Ltax+PCGDP,data=hpdatas,model="random",random.method = "swar")
summary(hp_re)
# 顯著水平 a=0.01
# result: fp:房價(jià)與 lp:地價(jià)正相關(guān),且顯著;
# fp:房價(jià)與 Ltax: 地稅收入正相關(guān)吨枉,且顯著恐锦;
# fp:房價(jià)與 PCGDP: 人均GDP 正相關(guān),且顯著刑然;
參考二:https://zhuanlan.zhihu.com/p/24877529
Panel Data Models in R
library(plm)
mydata <- read.csv("panel_wage.csv")
attach(mydata)
Y <- cbind(lwage)
X <- cbind(exp, exp2, wks, ed)
summary(Y)
summary(X)
ols <- lm(Y ~ X)
summary(ols)
聲明面板
Stata: xtset
pdata <- plm.data(mydata,indexes = c("id","t"))
混合回歸
Stata: reg
pooling <- plm(Y ~ X, data = pdata,model = "pooling")
summary(pooling)
固定效應(yīng)
Stata: xtreg ,fe
fixed <- plm(Y ~ X,data = pdata,model = "within")
summary(fixed)
隨機(jī)效應(yīng)
Stata: xtreg ,re
random <- plm(Y ~ X,data = pdata,model = "random")
summary(random)
不同模型的比較
random vs ols
plmtest(pooling)
fixed vs ols
pFtest(fixed, pooling)
random vs fixed
phtest(random, fixed)