Estimate or extract degrees of freedom of models parameters.

## Usage

degrees_of_freedom(model, ...)

# S3 method for default
degrees_of_freedom(model, method = "analytical", ...)

dof(model, ...)

## Arguments

model

A statistical model.

...

Currently not used.

method

Can be "analytical" (default, DoFs are estimated based on the model type), "residual" in which case they are directly taken from the model if available (for Bayesian models, the goal (looking for help to make it happen) would be to refit the model as a frequentist one before extracting the DoFs), "ml1" (see dof_ml1()), "betwithin" (see dof_betwithin()), "satterthwaite" (see dof_satterthwaite()), "kenward" (see dof_kenward()) or "any", which tries to extract DoF by any of those methods, whichever succeeds. See 'Details'.

## Details

Methods for calculating degrees of freedom:

• "analytical" for models of class lmerMod, Kenward-Roger approximated degrees of freedoms are calculated, for other models, n-k (number of observations minus number of parameters).

• "residual" tries to extract residual degrees of freedom, and returns Inf if residual degrees of freedom could not be extracted.

• "any" first tries to extract residual degrees of freedom, and if these are not available, extracts analytical degrees of freedom.

• "nokr" same as "analytical", but does not Kenward-Roger approximation for models of class lmerMod. Instead, always uses n-k to calculate df for any model.

• "normal" returns Inf.

• "wald" returns residual df for models with t-statistic, and Inf for all other models.

• "kenward" calls dof_kenward().

• "satterthwaite" calls dof_satterthwaite().

• "ml1" calls dof_ml1().

• "betwithin" calls dof_betwithin().

For models with z-statistic, the returned degrees of freedom for model parameters is Inf (unless method = "ml1" or method = "betwithin"), because there is only one distribution for the related test statistic.

## Note

In many cases, degrees_of_freedom() returns the same as df.residuals(), or n-k (number of observations minus number of parameters). However, degrees_of_freedom() refers to the model's parameters degrees of freedom of the distribution for the related test statistic. Thus, for models with z-statistic, results from degrees_of_freedom() and df.residuals() differ. Furthermore, for other approximation methods like "kenward" or "satterthwaite", each model parameter can have a different degree of freedom.

## Examples

model <- lm(Sepal.Length ~ Petal.Length * Species, data = iris)
dof(model)
#> [1] 144 144 144 144 144 144

model <- glm(vs ~ mpg * cyl, data = mtcars, family = "binomial")
dof(model)
#> [1] Inf
# \dontrun{
if (require("lme4", quietly = TRUE)) {
model <- lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris)
dof(model)
}
#> [1]   2.53354 144.66692

if (require("rstanarm", quietly = TRUE)) {
model <- stan_glm(
Sepal.Length ~ Petal.Length * Species,
data = iris,
chains = 2,
refresh = 0
)
dof(model)
}
#> This is rstanarm version 2.21.3
#> - See https://mc-stan.org/rstanarm/articles/priors for changes to default priors!
#> - Default priors may change, so it's safest to specify priors, even if equivalent to the defaults.
#> - For execution on a local, multicore CPU with excess RAM we recommend calling
#>   options(mc.cores = parallel::detectCores())
#>
#> Attaching package: ‘rstanarm’
#> The following object is masked from ‘package:psych’:
#>
#>     logit
#> The following object is masked from ‘package:boot’:
#>
#>     logit
#> The following object is masked from ‘package:parameters’:
#>
#>     compare_models
#> [1] 143 143 143 143 143 143 143
# }