-
Notifications
You must be signed in to change notification settings - Fork 4
/
Panel_Pooled_FE_RE.R
139 lines (99 loc) · 3.94 KB
/
Panel_Pooled_FE_RE.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
###########################################
## QUESTION 15.6
rm(list=ls(all=TRUE))
#library(multcomp)
library(data.table)
library(dplyr)
library(plotly)
#library(lmtest)
#library(sandwich)
#library(car)
library(AER)
library(plm)
context <- fread("mexican.csv")
## Part a
fixed.model <- plm(lnprice ~bar +street + nocondom +rich +regular +alcohol,
data = context,index =c("id", "trans") , model = "within")
# within means that varuation within one individual or entity
summary(fixed.model)
## part(i)
## part b
random.model <- plm(lnprice ~age+school+attractive+bar +street + nocondom +rich +regular +alcohol,
data = context,index =c("id", "trans"), model = "random")
summary(random.model)
##########################
rm(list=ls(all=TRUE))
#library(multcomp)
library(data.table)
library(dplyr)
library(plotly)
#library(lmtest)
#library(sandwich)
#library(car)
library(AER)
library(plm)
context <- fread("crime.csv")
context <- mutate(context,Lcrmrte = log(crmrte))
#(i) deterrence increases, will decrease crime rate
#(ii) wage increase in the private sector will decrease the crime rate
#(iii) population density increase, will increase the crime rate
#(iv) percentage of young males may increase the crime rate
## part b
OLS.model <- lm( Lcrmrte ~ log(prbarr) + log(prbconv)+ log(prbpris) + log(avgsen)+ log(wmfg), data=context)
summary(OLS.model)
## part c
FE.model <- plm( Lcrmrte ~ log(prbarr) + log(prbconv)+ log(prbpris) + log(avgsen)+ log(wmfg), data=context,
index=c("county","year"), model="within")
summary(FE.model)
## part d
# Unrestricted model will be FE model
# restricted model will be Pooled model
SSE.U = 16.149
pooled.model <- plm( Lcrmrte ~ log(prbarr) + log(prbconv)+ log(prbpris) + log(avgsen)+ log(wmfg), data=context,
model="pooling")
summary(pooled.model)
SSE.R = 106.81
n = 630
k = 95
## J = 89 because there are 50 more variables in FE the Unrestricted model
fVAL = ((SSE.R - SSE.U)/89)/ (SSE.U/(n-k))
fc <- qf(0.95,89,535)
## part e
## lets compare OLS model with FE model with dummy variables
OLS.model <- lm( Lcrmrte ~ log(prbarr) + log(prbconv)+ log(prbpris) + log(avgsen)+ log(wmfg)
+ ldensity + lpctymle+ d82 +d83 + d84 + d85+ d86 + d87, data=context)
summary(OLS.model)
FE.model <- plm( Lcrmrte ~ log(prbarr) + log(prbconv)+ log(prbpris) + log(avgsen)+ log(wmfg)
+ ldensity + lpctymle+ d82 +d83 + d84 + d85+ d86 + d87, data=context, index=c("county","year"),
model="within" )
summary(FE.model)
#(ii)
## compare OLS models with and without dummy variables
U.OLS.model <- lm( Lcrmrte ~ log(prbarr) + log(prbconv)+ log(prbpris) + log(avgsen)+ log(wmfg)
+ ldensity + lpctymle+ d82 +d83 + d84 + d85+ d86 + d87, data=context)
SSE.U = sum( (U.OLS.model$residuals)^2)
R.OLS.model <- lm( Lcrmrte ~ log(prbarr) + log(prbconv)+ log(prbpris) + log(avgsen)+ log(wmfg)
+ ldensity + lpctymle, data=context)
SSE.R = sum( (R.OLS.model$residuals)^2)
# restricted model is OLS with dummy variables
n = 630
k = 14
## J = 6
fVAL = ((SSE.R - SSE.U)/6)/ (SSE.U/(n-k))
fc <- qf(0.95,6,616)
## compare FE models with and without dummy variables
U.FE.model <- plm( Lcrmrte ~ log(prbarr) + log(prbconv)+ log(prbpris) + log(avgsen)+ log(wmfg)
+ ldensity + lpctymle+ d82 +d83 + d84 + d85+ d86 + d87,
data=context, index=c("county","year"), model = "within")
summary(U.FE.model)
SSE.U = 14.383
R.FE.model <- plm( Lcrmrte ~ log(prbarr) + log(prbconv)+ log(prbpris) + log(avgsen)+ log(wmfg)
+ ldensity + lpctymle,
data=context, index=c("county","year"), model = "within")
summary(R.FE.model)
SSE.R = 15.876
n = 630
k = 14
## J = 6
fVAL = ((SSE.R - SSE.U)/6)/ (SSE.U/(n-k))
fc <- qf(0.95,6,616)