forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 1
/
statmodels.jl
466 lines (369 loc) · 14.2 KB
/
statmodels.jl
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# Statistical Models
abstract type StatisticalModel end
"""
coef(obj::StatisticalModel)
Return the coefficients of the model.
"""
coef(obj::StatisticalModel) = error("coef is not defined for $(typeof(obj)).")
"""
coefnames(obj::StatisticalModel)
Return the names of the coefficients.
"""
coefnames(obj::StatisticalModel) = error("coefnames is not defined for $(typeof(obj)).")
"""
coeftable(obj::StatisticalModel)
Return a table of class `CoefTable` with coefficients and related statistics.
"""
coeftable(obj::StatisticalModel) = error("coeftable is not defined for $(typeof(obj)).")
"""
confint(obj::StatisticalModel)
Compute confidence intervals for coefficients.
"""
confint(obj::StatisticalModel) = error("confint is not defined for $(typeof(obj)).")
"""
deviance(obj::StatisticalModel)
Return the deviance of the model relative to a reference, which is usually when applicable
the saturated model. It is equal, *up to a constant*, to ``-2 \\log L``, with ``L``
the likelihood of the model.
"""
deviance(obj::StatisticalModel) = error("deviance is not defined for $(typeof(obj)).")
"""
islinear(obj::StatisticalModel)
Indicate whether the model is linear.
"""
islinear(obj::StatisticalModel) = error("islinear is not defined for $(typeof(obj)).")
"""
nulldeviance(obj::StatisticalModel)
Return the deviance of the null model, that is the one including only the intercept.
"""
nulldeviance(obj::StatisticalModel) = error("nulldeviance is not defined for $(typeof(obj)).")
"""
loglikelihood(obj::StatisticalModel)
Return the log-likelihood of the model.
"""
loglikelihood(obj::StatisticalModel) = error("loglikelihood is not defined for $(typeof(obj)).")
"""
loglikelihood(obj::StatisticalModel)
Return the log-likelihood of the null model corresponding to model `obj`.
This is usually the model containing only the intercept.
"""
nullloglikelihood(obj::StatisticalModel) = error("nullloglikelihood is not defined for $(typeof(obj)).")
"""
score(obj::StatisticalModel)
Return the score of the statistical model. The score is the gradient of the
log-likelihood with respect to the coefficients.
"""
score(obj::StatisticalModel) = error("score is not defined for $(typeof(obj)).")
"""
nobs(obj::StatisticalModel)
Return the number of independent observations on which the model was fitted. Be careful
when using this information, as the definition of an independent observation may vary
depending on the model, on the format used to pass the data, on the sampling plan
(if specified), etc.
"""
nobs(obj::StatisticalModel) = error("nobs is not defined for $(typeof(obj)).")
"""
dof(obj::StatisticalModel)
Return the number of degrees of freedom consumed in the model, including
when applicable the intercept and the distribution's dispersion parameter.
"""
dof(obj::StatisticalModel) = error("dof is not defined for $(typeof(obj)).")
"""
mss(obj::StatisticalModel)
Return the model sum of squares.
"""
mss(obj::StatisticalModel) = error("mss is not defined for $(typeof(obj)).")
"""
rss(obj::StatisticalModel)
Return the residual sum of squares.
"""
rss(obj::StatisticalModel) = error("rss is not defined for $(typeof(obj)).")
"""
informationmatrix(model::StatisticalModel; expected::Bool = true)
Return the information matrix. By default the Fisher information matrix is returned,
while the observed information matrix can be requested with `expected = false`.
"""
informationmatrix(model::StatisticalModel; expected::Bool = true) =
error("informationmatrix is not defined for $(typeof(obj)).")
"""
stderror(obj::StatisticalModel)
Return the standard errors for the coefficients of the model.
"""
stderror(obj::StatisticalModel) = sqrt.(diag(vcov(obj)))
"""
vcov(obj::StatisticalModel)
Return the variance-covariance matrix for the coefficients of the model.
"""
vcov(obj::StatisticalModel) = error("vcov is not defined for $(typeof(obj)).")
"""
weights(obj::StatisticalModel)
Return the weights used in the model.
"""
weights(obj::StatisticalModel) = error("weights is not defined for $(typeof(obj)).")
"""
isfitted(obj::StatisticalModel)
Indicate whether the model has been fitted.
"""
isfitted(obj::StatisticalModel) = error("isfitted is not defined for $(typeof(obj)).")
"""
Fit a statistical model.
"""
fit(obj::StatisticalModel, args...) = error("fit is not defined for $(typeof(obj)).")
"""
Fit a statistical model in-place.
"""
fit!(obj::StatisticalModel, args...) = error("fit! is not defined for $(typeof(obj)).")
"""
aic(obj::StatisticalModel)
Akaike's Information Criterion, defined as ``-2 \\log L + 2k``, with ``L`` the likelihood
of the model, and `k` its number of consumed degrees of freedom
(as returned by [`dof`](@ref)).
"""
aic(obj::StatisticalModel) = -2loglikelihood(obj) + 2dof(obj)
"""
aicc(obj::StatisticalModel)
Corrected Akaike's Information Criterion for small sample sizes (Hurvich and Tsai 1989),
defined as ``-2 \\log L + 2k + 2k(k-1)/(n-k-1)``, with ``L`` the likelihood of the model,
``k`` its number of consumed degrees of freedom (as returned by [`dof`](@ref)),
and ``n`` the number of observations (as returned by [`nobs`](@ref)).
"""
function aicc(obj::StatisticalModel)
k = dof(obj)
n = nobs(obj)
-2loglikelihood(obj) + 2k + 2k*(k+1)/(n-k-1)
end
"""
bic(obj::StatisticalModel)
Bayesian Information Criterion, defined as ``-2 \\log L + k \\log n``, with ``L``
the likelihood of the model, ``k`` its number of consumed degrees of freedom
(as returned by [`dof`](@ref)), and ``n`` the number of observations
(as returned by [`nobs`](@ref)).
"""
bic(obj::StatisticalModel) = -2loglikelihood(obj) + dof(obj)*log(nobs(obj))
"""
r2(obj::StatisticalModel)
r²(obj::StatisticalModel)
Coefficient of determination (R-squared).
For a linear model, the R² is defined as ``ESS/TSS``, with ``ESS`` the explained sum of squares
and ``TSS`` the total sum of squares.
"""
function r2(obj::StatisticalModel)
Base.depwarn("The default r² method for linear models is deprecated. " *
"Packages should define their own methods.", :r2)
mss(obj) / deviance(obj)
end
"""
r2(obj::StatisticalModel, variant::Symbol)
r²(obj::StatisticalModel, variant::Symbol)
Pseudo-coefficient of determination (pseudo R-squared).
For nonlinear models, one of several pseudo R² definitions must be chosen via `variant`.
Supported variants are:
- `:MacFadden` (a.k.a. likelihood ratio index), defined as ``1 - \\log (L)/\\log (L_0)``;
- `:CoxSnell`, defined as ``1 - (L_0/L)^{2/n}``;
- `:Nagelkerke`, defined as ``(1 - (L_0/L)^{2/n})/(1 - L_0^{2/n})``.
In the above formulas, ``L`` is the likelihood of the model,
``L_0`` is the likelihood of the null model (the model with only an intercept),
``n`` is the number of observations, ``y_i`` are the responses,
``\\hat{y}_i`` are fitted values and ``\\bar{y}`` is the average response.
Cox and Snell's R² should match the classical R² for linear models.
"""
function r2(obj::StatisticalModel, variant::Symbol)
ll = loglikelihood(obj)
ll0 = nullloglikelihood(obj)
if variant == :McFadden
1 - ll/ll0
elseif variant == :CoxSnell
1 - exp(2 * (ll0 - ll) / nobs(obj))
elseif variant == :Nagelkerke
(1 - exp(2 * (ll0 - ll) / obs(obj))) / (1 - exp(2 * ll0 / nobs(obj)))
else
error("variant must be one of :McFadden, :CoxSnell or :Nagelkerke")
end
end
const r² = r2
"""
adjr2(obj::StatisticalModel)
adjr²(obj::StatisticalModel)
Adjusted coefficient of determination (adjusted R-squared).
For linear models, the adjusted R² is defined as ``1 - (1 - (1-R^2)(n-1)/(n-p))``, with ``R^2``
the coefficient of determination, ``n`` the number of observations, and ``p`` the number of
coefficients (including the intercept). This definition is generally known as the Wherry Formula I.
"""
adjr2(obj::StatisticalModel) = error("adjr2 is not defined for $(typeof(obj)).")
"""
adjr2(obj::StatisticalModel, variant::Symbol)
adjr²(obj::StatisticalModel, variant::Symbol)
Adjusted pseudo-coefficient of determination (adjusted pseudo R-squared).
For nonlinear models, one of the several pseudo R² definitions must be chosen via `variant`.
The only currently supported variant is `:MacFadden`, defined as ``1 - (\\log (L) - k)/\\log (L0)``.
In this formula, ``L`` is the likelihood of the model, ``L0`` that of the null model
(the model including only the intercept). These two quantities are taken to be minus half
`deviance` of the corresponding models. ``k`` is the number of consumed degrees of freedom
of the model (as returned by [`dof`](@ref)).
"""
function adjr2(obj::StatisticalModel, variant::Symbol)
ll = loglikelihood(obj)
ll0 = nullloglikelihood(obj)
k = dof(obj)
if variant == :McFadden
1 - (ll - k)/ll0
else
error(":McFadden is the only currently supported variant")
end
end
const adjr² = adjr2
abstract type RegressionModel <: StatisticalModel end
"""
fitted(obj::RegressionModel)
Return the fitted values of the model.
"""
fitted(obj::RegressionModel) = error("fitted is not defined for $(typeof(obj)).")
"""
response(obj::RegressionModel)
Return the model response (a.k.a. the dependent variable).
"""
response(obj::RegressionModel) = error("response is not defined for $(typeof(obj)).")
"""
meanresponse(obj::RegressionModel)
Return the mean of the response.
"""
meanresponse(obj::RegressionModel) = error("meanresponse is not defined for $(typeof(obj)).")
"""
modelmatrix(obj::RegressionModel)
Return the model matrix (a.k.a. the design matrix).
"""
modelmatrix(obj::RegressionModel) = error("modelmatrix is not defined for $(typeof(obj)).")
"""
leverage(obj::RegressionModel)
Return the diagonal of the projection matrix.
"""
leverage(obj::RegressionModel) = error("leverage is not defined for $(typeof(obj)).")
"""
residuals(obj::RegressionModel)
Return the residuals of the model.
"""
residuals(obj::RegressionModel) = error("residuals is not defined for $(typeof(obj)).")
"""
predict(obj::RegressionModel, [newX])
Form the predicted response of model `obj`. An object with new covariate values `newX` can be supplied,
which should have the same type and structure as that used to fit `obj`; e.g. for a GLM
it would generally be a `DataFrame` with the same variable names as the original predictors.
"""
function predict end
predict(obj::RegressionModel) = error("predict is not defined for $(typeof(obj)).")
"""
predict!
In-place version of [`predict`](@ref).
"""
function predict! end
predict!(obj::RegressionModel) = error("predict! is not defined for $(typeof(obj)).")
"""
dof_residual(obj::RegressionModel)
Return the residual degrees of freedom of the model.
"""
dof_residual(obj::RegressionModel) = error("dof_residual is not defined for $(typeof(obj)).")
"""
params(obj)
Return all parameters of a model.
"""
params(obj) = error("params is not defined for $(typeof(obj))")
function params! end
## coefficient tables with specialized show method
## Nms are the coefficient names, corresponding to rows in the table
mutable struct CoefTable
cols::Vector
colnms::Vector
rownms::Vector
function CoefTable(cols::Vector,colnms::Vector,rownms::Vector)
nc = length(cols)
nrs = map(length,cols)
nr = nrs[1]
length(colnms) in [0,nc] || error("colnms should have length 0 or $nc")
length(rownms) in [0,nr] || error("rownms should have length 0 or $nr")
all(nrs .== nr) || error("Elements of cols should have equal lengths, but got $nrs")
new(cols,colnms,rownms)
end
function CoefTable(mat::Matrix,colnms::Vector,rownms::Vector,pvalcol::Int=0)
nc = size(mat,2)
cols = Any[mat[:, i] for i in 1:nc]
if pvalcol != 0 # format the p-values column
cols[pvalcol] = [PValue(cols[pvalcol][j])
for j in eachindex(cols[pvalcol])]
end
CoefTable(cols,colnms,rownms)
end
end
mutable struct PValue
v::Number
function PValue(v::Number)
0. <= v <= 1. || isnan(v) || error("p-values must be in [0.,1.]")
new(v)
end
end
function show(io::IO, pv::PValue)
v = pv.v
if isnan(v)
@printf(io,"%d", v)
elseif v >= 1e-4
@printf(io,"%.4f", v)
else
@printf(io,"<1e%2.2d", ceil(Integer, max(nextfloat(log10(v)), -99)))
end
end
function show(io::IO, ct::CoefTable)
cols = ct.cols; rownms = ct.rownms; colnms = ct.colnms;
nc = length(cols)
nr = length(cols[1])
if length(rownms) == 0
rownms = [lpad("[$i]",floor(Integer, log10(nr))+3) for i in 1:nr]
end
rnwidth = max(4,maximum([length(nm) for nm in rownms]) + 1)
rownms = [rpad(nm,rnwidth) for nm in rownms]
widths = [length(cn)::Int for cn in colnms]
str = String[isa(cols[j][i], AbstractString) ? cols[j][i] :
sprint(show, cols[j][i], context=:compact=>true) for i in 1:nr, j in 1:nc]
for j in 1:nc
for i in 1:nr
lij = length(str[i,j])
if lij > widths[j]
widths[j] = lij
end
end
end
widths .+= 1
println(io," " ^ rnwidth *
join([lpad(string(colnms[i]), widths[i]) for i = 1:nc], ""))
for i = 1:nr
print(io, rownms[i])
for j in 1:nc
print(io, lpad(str[i,j],widths[j]))
end
println(io)
end
end
"""
ConvergenceException(iters::Int, lastchange::Real=NaN, tol::Real=NaN)
The fitting procedure failed to converge in `iters` number of iterations,
i.e. the `lastchange` between the cost of the final and penultimate iteration was greater than
specified tolerance `tol`.
"""
struct ConvergenceException{T<:Real} <: Exception
iters::Int
lastchange::T
tol::T
function ConvergenceException{T}(iters, lastchange::T, tol::T) where T<:Real
if tol > lastchange
throw(ArgumentError("Change must be greater than tol."))
else
new(iters, lastchange, tol)
end
end
end
ConvergenceException(iters, lastchange::T=NaN, tol::T=NaN) where {T<:Real} =
ConvergenceException{T}(iters, lastchange, tol)
function Base.showerror(io::IO, ce::ConvergenceException)
print(io, "failure to converge after $(ce.iters) iterations.")
if !isnan(ce.lastchange)
print(io, " Last change ($(ce.lastchange)) was greater than tolerance ($(ce.tol)).")
end
end