-
Notifications
You must be signed in to change notification settings - Fork 4
/
lmq2.R
367 lines (135 loc) · 5.27 KB
/
lmq2.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#ESTIMATING A LINEAR REGRESSION USING MLE
#The purpose of this session is to introduce you to the MLE of the normal general linear model. This approach to linear regression” forms the statistical basis for hypothesis testing found in most econometrics textbooks. Thus, the normal general linear model is important from the standpoint of justifying least squares. More important, this model serves as a tool for understanding maximum likelihood estimation of many time series models, models with heteroskedastic disturbances, and models with non-normal disturbances.
# This file calculates a regression using maximum likelihood.
# Read in the Ostrom data
Ostrom <- read.table("C:/users/Wood/Documents/My Teaching/Maximum Likelihood/Data/ostrom.dat", header=TRUE)
attach(Ostrom)
Ostrom
summary(Ostrom)
# Generate time series plots of the data.
par(mfrow=c(2,1))
plot(year,USSR)
plot(year,US)
# Plot US versus USSR
plot(USSR,US)
# First, calculate a regression the easy way, plot the
# residuals, and do an F test for whether the coefficient on USSR=0
OLS.model <- lm(US ~ USSR , data=Ostrom)
summary(OLS.model)
plot(residuals(OLS.model))
library(car)
# Set up the F test
r <- c(0,1)
q <- c(0)
linearHypothesis(OLS.model, r, q)
# We can also get various diagnostics from the preceding model as follows.
# These are only a few. We explore diagnostics for heteroskedasticity and autocorrelation in the next lesson.
anova(OLS.model)
predict(OLS.model)
logLik(OLS.model)
AIC(OLS.model)
influence.measures(OLS.model)
library(lmtest)
reset(US ~ USSR, power=3, type='fitted')
# Now let's fit the Normal Maximum Likelihood model.
# First, put the data into matrices for the MLE procedure
x <- cbind(1,as.matrix(USSR))
y <- as.matrix(US)
ones <- x[,1]
# Calculate K and n for later use
K <- ncol(x)
K
K1 <- K + 1
n <- nrow(x)
n
# Define the function to be optimized
llik.regress <- function(par,X,Y) {
Y <- as.vector(y)
X <- as.matrix(x)
xbeta <- X%*%par[1:K]
Sig <- par[K1:K1]
sum(-(1/2)*log(2*pi)-(1/2)*log(Sig^2)-(1/(2*Sig^2))*(y-xbeta)^2)
}
llik.regress
# Now let's use the above function to estimate the model.
model <- optim(c(15,0.9,13),llik.regress, method = "BFGS", control = list(trace=1,maxit=100,fnscale = -1),
hessian = TRUE)
model
# Notice that the coefficients from this model are the same as those produced through OLS estimation
#Now let's get the variances, standard errors, Z statistics, and log likelihood from our model.
# Calculate the variance matrix from the Hessian matrix.
v <- -solve(model$hessian)
v
# Calculate the standard errors from the variance matrix.
se <- sqrt( diag(v))
se
# Calculate the z statistics from the coefficients and standard errors
b <- model$par
b
zstat <-b/se
zstat
# Calculate p-values for the z statistics
pzstat <- 2* (1 - pnorm(abs(zstat)))
pzstat
# Put the results together in a table.
table <- cbind(b,se,zstat,pzstat)
table
# Note that the estimate of the error variance is biased by n/n-K
# we can correct the preceding estimate of the standard error of #estimates as follows
Sig <- model$par[3]
Sig
Sigunb <- sqrt(n/(n-K)*Sig^2)
Sigunb
# The square of the z-statistic on USSR is a Wald test that USSR=0. We might want to
# test this hypothesis with other methods, including either a likelihood ratio or LaGrange
# multiplier. Therefore, we save the log-likelihood for later testing below.
LU <- model$value
LU
# Now let's do a likelihood ratio test that the coefficient on USSR=0 using "LU" from above.
# First, restimate the model with the restriction that USSR=0. Save the log likelihood from this model
llik.regressR <- function(par,X,Y) {
Y <- as.vector(y)
X <- as.matrix(ones)
xbeta <- X%*%par[1:K-1]
Sig <- par[K:K]
sum(-(1/2)*log(2*pi)-(1/2)*log(Sig^2)-(1/(2*Sig^2))*(y-xbeta)^2)
}
llik.regressR
# Now let's use the above function to estimate the model.
modelR <- optim(c(140,74),llik.regressR, method = "CG", control = list(trace=1,maxit=100,fnscale = -1),
hessian = TRUE)
modelR
LR <- modelR$value
LR
# Now calculate the likelihood ratio test using the saved values of the log likelihood function.*/
llratio <- -2*(LR-LU)
llratio
llratioPvalue <- 1-pchisq(llratio,K-1)
llratioPvalue
# In some instances it will be useful to perform matrix calculations on MLE outputs. For example,
# in computing quadratic forms. As an example of using matrices, let's illustrate the preceding
# regression using some of the matrix capabilities of R
# We defined the appropriate matrices for this above.
# Compute coefficient estimates using matrices.
bols <- solve(t(x) %*% x) %*% t(x) %*% y
bols
# Compute and list predicted values and residuals
yhat <- x %*% bols
yhat
errors <- y - yhat
errors
# Compute and display the maximum likelihood estimate of the error variance
SSE <- t(errors) %*% errors
SSE
S2 <- (1/n) * SSE
S2
# Compute the maximum likelihood covariance matrix of coefficients. The diagonal of this
# matrix contains the standard errors.
covb <- as.numeric(S2) * (solve(t(x) %*% x))
covb
# Extract the standard errors
se <- sqrt(diag(covb))
se
# Calculate t statistics
tstat <- bols/se
tstat