Skip to content

Commit

Permalink
Merge 54e7f7c into da42557
Browse files Browse the repository at this point in the history
  • Loading branch information
getzze committed Jan 21, 2020
2 parents da42557 + 54e7f7c commit 291cdf2
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/statmodels.jl
Expand Up @@ -216,13 +216,15 @@ 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})``.
- `:deviance_ratio`, defined as ``1 - D/D_0``.
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.
``D`` is the deviance of the model (from the saturated model),
``D_0`` is the deviance of the null model,
``n`` is the number of observations.
Cox and Snell'sshould match the classical R² for linear models.
The deviance ratiomatches the classical definition of R² for linear models.
"""
function r2(obj::StatisticalModel, variant::Symbol)
ll = loglikelihood(obj)
Expand All @@ -233,8 +235,12 @@ function r2(obj::StatisticalModel, variant::Symbol)
1 - exp(2 * (ll0 - ll) / nobs(obj))
elseif variant == :Nagelkerke
(1 - exp(2 * (ll0 - ll) / nobs(obj))) / (1 - exp(2 * ll0 / nobs(obj)))
elseif variant == :deviance_ratio
dev = deviance(obj)
dev0 = nulldeviance(obj)
1 - dev/dev0
else
error("variant must be one of :McFadden, :CoxSnell or :Nagelkerke")
error("variant must be one of :McFadden, :CoxSnell, :Nagelkerke or :deviance_ratio")
end
end

Expand All @@ -259,19 +265,30 @@ adjr2(obj::StatisticalModel) = error("adjr2 is not defined for $(typeof(obj)).")
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)``.
The only currently supported variants are `:MacFadden`, defined as ``1 - (\\log (L) - k)/\\log (L0)`` and
`:deviance_ration`, defined as ``1 - (D/dof)/(D_0/dof_0)``.
In this formula, ``L`` is the likelihood of the model, ``L0`` that of the null model
(the model including only the intercept), and ``k`` is the number of consumed degrees of freedom
of the model (as returned by [`dof`](@ref)).
(the model including only the intercept), ``D`` is the deviance of the model,
``D_0`` is the deviance of the null model, ``k`` is the number of consumed degrees of freedom
of the model (as returned by [`dof`](@ref)), ``dof`` is the number of degrees of freedom of
the model ([`nobs`](@ref) - k) and
``dof_0`` is the number of degrees of freedom of the null model ([`nobs`](@ref) - 1).
"""
function adjr2(obj::StatisticalModel, variant::Symbol)
ll = loglikelihood(obj)
ll0 = nullloglikelihood(obj)
k = dof(obj)
if variant == :McFadden
1 - (ll - k)/ll0
elseif variant == :deviance_ratio
n = nobs(obj)
# Number of explanatory variables
p = dof(obj)
dev = deviance(obj)/(n - p)
dev0 = nulldeviance(obj)/(n - 1)
1 - dev/dev0
else
error(":McFadden is the only currently supported variant")
error("variant must be one of :McFadden or :deviance_ratio")
end
end

Expand Down

0 comments on commit 291cdf2

Please sign in to comment.