楼主: ReneeBK
2318 10

【Lecture Notes】Political Science 207 [推广有奖]

11
ReneeBK 发表于 2017-1-22 03:07:58
  1. ## Political Science 207
  2. ## panel data

  3. ## example of unobserved heterogeneity

  4. x1 <- rnorm(100,1,1)
  5. x2 <- rnorm(100,2,1)

  6. e1 <- rnorm(100,0,0.8)
  7. e2 <- rnorm(100,0,0.8)

  8. y1 <- 0 - x1 + e1
  9. y2 <- 5 - x2 + e2

  10. x <- append(x1,x2)
  11. y <- append(y1,y2)

  12. plot(y~x)

  13. reg.pooled <- lm(y~x)
  14. reg.1 <- lm(y1~x1)
  15. reg.2 <- lm(y2~x2)

  16. abline(reg.pooled)
  17. abline(reg.1, col="red")
  18. abline(reg.2, col="red")

  19. ## Basic Panel Data models in R ##

  20. library(foreign)

  21. BESData <- read.dta("bes.dta",  convert.factors=FALSE)

  22. install.packages("plm")
  23. library(plm)

  24. pdim(BESData, index=c("serialno", "year"))

  25. ## Pooled model (regular OLS)

  26. pooled.model <- lm(right ~ manual + age + male + price + tory + labour, data=BESData, na.action=na.omit)
  27. summary(pooled.model)

  28. ## LSDV model

  29. lsdv.model <- lm(right ~ manual + age + male + price + tory + labour + as.factor(serialno), data=BESData, na.action=na.omit)
  30. summary(lsdv.model)

  31. ## F test for fixed effects

  32. anova(pooled.model, lsdv.model)

  33. ## Using the plm package ##

  34. pmodel1 <- plm(right ~ manual + age + male + price + tory + labour, data=BESData, index=c("serialno", "year"), na.action=na.omit, model="pooling")
  35. summary(pmodel1)

  36. ## Within model

  37. pmodel2 <- plm(right ~ manual + age + male + price + tory + labour, data=BESData, index=c("serialno", "year"), na.action=na.omit, model="within")
  38. summary(pmodel2)

  39. ## F test for fixed effects

  40. pFtest(pmodel2,pmodel1)

  41. ## Random effects model

  42. pmodel3 <- plm(right ~ manual + age + male + price + tory + labour, data=BESData, index=c("serialno", "year"), na.action=na.omit, model="random")
  43. summary(pmodel3)

  44. ## Breusch-Pagan test

  45. plmtest(pmodel3, effect="individual", type="bp")

  46. ## Hausman test

  47. phtest(pmodel2, pmodel3)

  48. ## First difference model -- not often used in political science panel data
  49. ## Differences from the within estimator usually indicate a violation of the strict exogeneity assumption

  50. pmodel.fd <- plm(right ~ manual + age + male + price + tory + labour, data=BESData, index=c("serialno", "year"), na.action=na.omit, model="fd")
  51. summary(pmodel.fd)


  52. #########################
  53. ## Example 2



  54. BankData <- read.dta("franzese.dta",  convert.factors=FALSE)

  55. olsmodel2 <- lm(inf ~ cbilag1 + glag1 + tlag1 + flag1 + infalag1 + ulag1 + cwb, data=BankData, na.action=na.omit)
  56. summary(olsmodel2)

  57. pdim(BankData, index=c("ctry", "year"))

  58. ## Fixed effects model

  59. pmodel4 <- plm(inf ~ cbilag1 + glag1 + tlag1 + flag1 + infalag1 + ulag1 + cwb, data=BankData, index=c("ctry", "year"), na.action=na.omit, model="within")
  60. summary(pmodel4)

  61. ## F test for fixed effects

  62. pFtest(pmodel4,olsmodel2)

  63. ## Random effects model

  64. pmodel5 <- plm(inf ~ cbilag1 + glag1 + tlag1 + flag1 + infalag1 + ulag1 + cwb, data=BankData, index=c("ctry", "year"), na.action=na.omit, model="random")

  65. ## Crash because of negative estimated variance of individual effects

  66. ## Use different method of calculating variance of random effects
  67. ## random.method options only work on balanced panels

  68. 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")
  69. summary(pmodel5)

  70. ## Breusch-Pagan test

  71. plmtest(pmodel5, effect="individual", type="bp")

  72. ## Hausman test

  73. phtest(pmodel4, pmodel5)
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-22 23:59