Skip to content

Commit 00ce15f

Browse files
authored
Move and adjr2 implementations from StatsBase (#7)
These are the only methods for `StatisticalModel` still defined in StatsBase. Given that definitions are quite trivial and based only on functions defined in StatsAPI, it makes more sense for them to live here too.
1 parent 5ce3601 commit 00ce15f

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "StatsAPI"
22
uuid = "82ae8749-77ed-4fe6-ae5f-f523153014b0"
33
authors = ["Milan Bouchet-Valat <[email protected]"]
4-
version = "1.1.0"
4+
version = "1.2.0"
55

66
[compat]
77
julia = "1"

src/statisticalmodel.jl

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,49 @@ and ``TSS`` the total sum of squares.
216216
"""
217217
function r2 end
218218

219+
"""
220+
r2(model::StatisticalModel, variant::Symbol)
221+
r²(model::StatisticalModel, variant::Symbol)
222+
223+
Pseudo-coefficient of determination (pseudo R-squared).
224+
225+
For nonlinear models, one of several pseudo R² definitions must be chosen via `variant`.
226+
Supported variants are:
227+
- `:MacFadden` (a.k.a. likelihood ratio index), defined as ``1 - \\log (L)/\\log (L_0)``;
228+
- `:CoxSnell`, defined as ``1 - (L_0/L)^{2/n}``;
229+
- `:Nagelkerke`, defined as ``(1 - (L_0/L)^{2/n})/(1 - L_0^{2/n})``.
230+
- `:devianceratio`, defined as ``1 - D/D_0``.
231+
232+
In the above formulas, ``L`` is the likelihood of the model,
233+
``L_0`` is the likelihood of the null model (the model with only an intercept),
234+
``D`` is the deviance of the model (from the saturated model),
235+
``D_0`` is the deviance of the null model,
236+
``n`` is the number of observations (given by [`nobs`](@ref)).
237+
238+
The Cox-Snell and the deviance ratio variants both match the classical definition of R²
239+
for linear models.
240+
"""
241+
function r2(model::StatisticalModel, variant::Symbol)
242+
loglikbased = (:McFadden, :CoxSnell, :Nagelkerke)
243+
if variant in loglikbased
244+
ll = loglikelihood(model)
245+
ll0 = nullloglikelihood(model)
246+
if variant == :McFadden
247+
1 - ll/ll0
248+
elseif variant == :CoxSnell
249+
1 - exp(2 * (ll0 - ll) / nobs(model))
250+
elseif variant == :Nagelkerke
251+
(1 - exp(2 * (ll0 - ll) / nobs(model))) / (1 - exp(2 * ll0 / nobs(model)))
252+
end
253+
elseif variant == :devianceratio
254+
dev = deviance(model)
255+
dev0 = nulldeviance(model)
256+
1 - dev/dev0
257+
else
258+
error("variant must be one of $(join(loglikbased, ", ")) or :devianceratio")
259+
end
260+
end
261+
219262
const= r2
220263

221264
"""
@@ -230,4 +273,33 @@ coefficients (including the intercept). This definition is generally known as th
230273
"""
231274
function adjr2 end
232275

276+
"""
277+
adjr2(model::StatisticalModel, variant::Symbol)
278+
adjr²(model::StatisticalModel, variant::Symbol)
279+
280+
Adjusted pseudo-coefficient of determination (adjusted pseudo R-squared).
281+
For nonlinear models, one of the several pseudo R² definitions must be chosen via `variant`.
282+
The only currently supported variants are `:MacFadden`, defined as ``1 - (\\log (L) - k)/\\log (L0)`` and
283+
`:devianceratio`, defined as ``1 - (D/(n-k))/(D_0/(n-1))``.
284+
In these formulas, ``L`` is the likelihood of the model, ``L0`` that of the null model
285+
(the model including only the intercept), ``D`` is the deviance of the model,
286+
``D_0`` is the deviance of the null model, ``n`` is the number of observations (given by [`nobs`](@ref)) and
287+
``k`` is the number of consumed degrees of freedom of the model (as returned by [`dof`](@ref)).
288+
"""
289+
function adjr2(model::StatisticalModel, variant::Symbol)
290+
k = dof(model)
291+
if variant == :McFadden
292+
ll = loglikelihood(model)
293+
ll0 = nullloglikelihood(model)
294+
1 - (ll - k)/ll0
295+
elseif variant == :devianceratio
296+
n = nobs(model)
297+
dev = deviance(model)
298+
dev0 = nulldeviance(model)
299+
1 - (dev*(n-1))/(dev0*(n-k))
300+
else
301+
error("variant must be one of :McFadden or :devianceratio")
302+
end
303+
end
304+
233305
const adjr² = adjr2

0 commit comments

Comments
 (0)