pub struct GlmFit<F: Family> { /* private fields */ }Expand description
A generalized linear model fit by iteratively reweighted least squares
(Fisher scoring) for an exponential-dispersion Family.
One solver serves every family. Each IRLS step forms the working response
z = η + (y − μ)/(dμ/dη) and working weights w = (dμ/dη)² / V(μ), then
solves the weighted normal equations (XᵀWX) β = XᵀWz. At convergence the
score equations Xᵀ(y − μ) · (dμ/dη)/V = 0 hold and φ·(XᵀWX)⁻¹ is the
coefficient covariance, with φ = 1 for the fixed-dispersion families
(Poisson, negative binomial) and the Pearson estimate φ̂ = χ²/(n − p) for
Gamma.
See the module docs for the response conventions and the link choice.
Implementations§
Source§impl<F: Family> GlmFit<F>
impl<F: Family> GlmFit<F>
Sourcepub fn new(family: F, x: Array2<f64>, y: Array1<f64>) -> Result<Self>
pub fn new(family: F, x: Array2<f64>, y: Array1<f64>) -> Result<Self>
Fit y ~ X under family (default: up to 100 IRLS iterations, tolerance
1e-10 on the maximum coefficient step).
The caller owns the design matrix, including any intercept column, exactly
as with OlsFit and LogisticFit.
§Errors
RegressionError::EmptyInput/RegressionError::ShapeMismatch.RegressionError::InvalidResponseifyis outside the family’s support (negative counts, non-positive Gamma responses).RegressionError::RankDeficientif the weighted design is singular.RegressionError::NotConvergedif IRLS fails to converge.
Sourcepub fn with_options(
family: F,
x: Array2<f64>,
y: Array1<f64>,
max_iter: usize,
tol: f64,
) -> Result<Self>
pub fn with_options( family: F, x: Array2<f64>, y: Array1<f64>, max_iter: usize, tol: f64, ) -> Result<Self>
Like GlmFit::new with an explicit iteration cap and step tolerance.
Sourcepub fn n_observations(&self) -> usize
pub fn n_observations(&self) -> usize
Number of observations.
Sourcepub fn n_parameters(&self) -> usize
pub fn n_parameters(&self) -> usize
Number of coefficients (design columns, intercept included).
Sourcepub fn has_intercept(&self) -> bool
pub fn has_intercept(&self) -> bool
Whether a constant (intercept) column was detected in the design.
Sourcepub fn iterations(&self) -> usize
pub fn iterations(&self) -> usize
IRLS iterations taken to converge.
Sourcepub fn design_matrix(&self) -> ArrayView2<'_, f64>
pub fn design_matrix(&self) -> ArrayView2<'_, f64>
The design matrix as fitted.
Sourcepub fn response(&self) -> ArrayView1<'_, f64>
pub fn response(&self) -> ArrayView1<'_, f64>
The response.
Sourcepub fn coefficients(&self) -> ArrayView1<'_, f64>
pub fn coefficients(&self) -> ArrayView1<'_, f64>
Estimated coefficients (link scale), aligned to the design columns.
Sourcepub fn linear_predictor(&self) -> ArrayView1<'_, f64>
pub fn linear_predictor(&self) -> ArrayView1<'_, f64>
Linear predictor η = Xβ.
Sourcepub fn fitted_means(&self) -> ArrayView1<'_, f64>
pub fn fitted_means(&self) -> ArrayView1<'_, f64>
Fitted means μᵢ = g⁻¹(ηᵢ).
Sourcepub fn weights(&self) -> ArrayView1<'_, f64>
pub fn weights(&self) -> ArrayView1<'_, f64>
IRLS working weights wᵢ = (dμ/dη)² / V(μ) at the MLE.
Sourcepub fn dispersion(&self) -> f64
pub fn dispersion(&self) -> f64
Estimated dispersion φ: 1 for Poisson/negative binomial, the Pearson
estimate χ²/(n − p) for Gamma.
Sourcepub fn log_likelihood(&self) -> f64
pub fn log_likelihood(&self) -> f64
Maximized log-likelihood (evaluated at the estimated dispersion for Gamma).
Sourcepub fn covariance(&self) -> Array2<f64>
pub fn covariance(&self) -> Array2<f64>
Coefficient covariance φ · (XᵀWX)⁻¹.
Sourcepub fn coefficient_standard_errors(&self) -> Array1<f64>
pub fn coefficient_standard_errors(&self) -> Array1<f64>
Coefficient standard errors √diag(φ · (XᵀWX)⁻¹).
Sourcepub fn wald_statistics(&self) -> Array1<f64>
pub fn wald_statistics(&self) -> Array1<f64>
Wald statistics βⱼ / seⱼ.
The reference distribution is the standard normal when the dispersion is
known (Poisson, negative binomial) and Student’s t with n − p degrees
of freedom when it is estimated (Gamma) — see GlmFit::p_values.
Sourcepub fn p_values(&self) -> Array1<f64>
pub fn p_values(&self) -> Array1<f64>
Two-sided Wald p-values.
Uses the standard normal for fixed-dispersion families and Student’s t
with n − p degrees of freedom when the dispersion is estimated (the R
glm() convention).
Sourcepub fn predict_mean(&self, x: ArrayView2<'_, f64>) -> Array1<f64>
pub fn predict_mean(&self, x: ArrayView2<'_, f64>) -> Array1<f64>
Predicted means for a new design matrix x (same column layout as the
training design): μ = g⁻¹(xβ).
Source§impl<F: Family> GlmFit<F>
impl<F: Family> GlmFit<F>
Sourcepub fn goodness_of_fit(&self) -> GoodnessOfFit
pub fn goodness_of_fit(&self) -> GoodnessOfFit
Overall goodness-of-fit summary: null/residual deviance, dispersion, McFadden’s pseudo-R², and AIC/BIC.
The residual deviance is the sum of per-observation unit deviances
(equivalently the sum of squared deviance residuals). The null model
is the mean-only fit, whose MLE mean is the sample mean ȳ for all
log-link families here, so the null deviance is computed directly without
a second IRLS solve.
The information criteria count k = p parameters when the dispersion is
fixed (Poisson, negative binomial) and k = p + 1 when it is estimated
(Gamma), charging the extra degree of freedom for φ̂. McFadden’s
pseudo-R² compares the fitted log-likelihood to the mean-only model at the
same dispersion.
Trait Implementations§
Auto Trait Implementations§
impl<F> Freeze for GlmFit<F>where
F: Freeze,
impl<F> RefUnwindSafe for GlmFit<F>where
F: RefUnwindSafe,
impl<F> Send for GlmFit<F>where
F: Send,
impl<F> Sync for GlmFit<F>where
F: Sync,
impl<F> Unpin for GlmFit<F>where
F: Unpin,
impl<F> UnsafeUnpin for GlmFit<F>where
F: UnsafeUnpin,
impl<F> UnwindSafe for GlmFit<F>where
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.