@@ -216,6 +216,49 @@ and ``TSS`` the total sum of squares.
216
216
"""
217
217
function r2 end
218
218
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
+
219
262
const r² = r2
220
263
221
264
"""
@@ -230,4 +273,33 @@ coefficients (including the intercept). This definition is generally known as th
230
273
"""
231
274
function adjr2 end
232
275
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
+
233
305
const adjr² = adjr2
0 commit comments