- 阅读权限
- 255
- 威望
- 1 级
- 论坛币
- 49655 个
- 通用积分
- 55.9937
- 学术水平
- 370 点
- 热心指数
- 273 点
- 信用等级
- 335 点
- 经验
- 57805 点
- 帖子
- 4005
- 精华
- 21
- 在线时间
- 582 小时
- 注册时间
- 2005-5-8
- 最后登录
- 2023-11-26
|
|
- ## Political Science 207
- ## panel data
- ## example of unobserved heterogeneity
- x1 <- rnorm(100,1,1)
- x2 <- rnorm(100,2,1)
- e1 <- rnorm(100,0,0.8)
- e2 <- rnorm(100,0,0.8)
- y1 <- 0 - x1 + e1
- y2 <- 5 - x2 + e2
- x <- append(x1,x2)
- y <- append(y1,y2)
- plot(y~x)
- reg.pooled <- lm(y~x)
- reg.1 <- lm(y1~x1)
- reg.2 <- lm(y2~x2)
- abline(reg.pooled)
- abline(reg.1, col="red")
- abline(reg.2, col="red")
- ## Basic Panel Data models in R ##
- library(foreign)
- BESData <- read.dta("bes.dta", convert.factors=FALSE)
- install.packages("plm")
- library(plm)
- pdim(BESData, index=c("serialno", "year"))
- ## Pooled model (regular OLS)
- pooled.model <- lm(right ~ manual + age + male + price + tory + labour, data=BESData, na.action=na.omit)
- summary(pooled.model)
- ## LSDV model
- lsdv.model <- lm(right ~ manual + age + male + price + tory + labour + as.factor(serialno), data=BESData, na.action=na.omit)
- summary(lsdv.model)
- ## F test for fixed effects
- anova(pooled.model, lsdv.model)
- ## Using the plm package ##
- pmodel1 <- plm(right ~ manual + age + male + price + tory + labour, data=BESData, index=c("serialno", "year"), na.action=na.omit, model="pooling")
- summary(pmodel1)
- ## Within model
- pmodel2 <- plm(right ~ manual + age + male + price + tory + labour, data=BESData, index=c("serialno", "year"), na.action=na.omit, model="within")
- summary(pmodel2)
- ## F test for fixed effects
- pFtest(pmodel2,pmodel1)
- ## Random effects model
- pmodel3 <- plm(right ~ manual + age + male + price + tory + labour, data=BESData, index=c("serialno", "year"), na.action=na.omit, model="random")
- summary(pmodel3)
- ## Breusch-Pagan test
- plmtest(pmodel3, effect="individual", type="bp")
- ## Hausman test
- phtest(pmodel2, pmodel3)
- ## First difference model -- not often used in political science panel data
- ## Differences from the within estimator usually indicate a violation of the strict exogeneity assumption
- pmodel.fd <- plm(right ~ manual + age + male + price + tory + labour, data=BESData, index=c("serialno", "year"), na.action=na.omit, model="fd")
- summary(pmodel.fd)
- #########################
- ## Example 2
- BankData <- read.dta("franzese.dta", convert.factors=FALSE)
- olsmodel2 <- lm(inf ~ cbilag1 + glag1 + tlag1 + flag1 + infalag1 + ulag1 + cwb, data=BankData, na.action=na.omit)
- summary(olsmodel2)
- pdim(BankData, index=c("ctry", "year"))
- ## Fixed effects model
- pmodel4 <- plm(inf ~ cbilag1 + glag1 + tlag1 + flag1 + infalag1 + ulag1 + cwb, data=BankData, index=c("ctry", "year"), na.action=na.omit, model="within")
- summary(pmodel4)
- ## F test for fixed effects
- pFtest(pmodel4,olsmodel2)
- ## Random effects model
- pmodel5 <- plm(inf ~ cbilag1 + glag1 + tlag1 + flag1 + infalag1 + ulag1 + cwb, data=BankData, index=c("ctry", "year"), na.action=na.omit, model="random")
- ## Crash because of negative estimated variance of individual effects
- ## Use different method of calculating variance of random effects
- ## random.method options only work on balanced panels
- pmodel5 <- plm(inf ~ cbilag1 + glag1 + tlag1 + flag1 + infalag1 + ulag1 + cwb, data=BankData, index=c("ctry", "year"), na.action=na.omit, model="random", random.method="walhus")
- summary(pmodel5)
- ## Breusch-Pagan test
- plmtest(pmodel5, effect="individual", type="bp")
- ## Hausman test
- phtest(pmodel4, pmodel5)
复制代码
|
|