Skip to main content

LinearMixedModel

Struct LinearMixedModel 

Source
pub struct LinearMixedModel { /* private fields */ }
Expand description

A fitted random-intercept linear mixed model

yᵢⱼ = xᵢⱼᵀβ + bⱼ + εᵢⱼ, bⱼ ~ N(0, σ²_b), εᵢⱼ ~ N(0, σ²_e),

for observations i nested in groups j (one random intercept per group). The marginal covariance is V = σ²_e I + σ²_b ZZᵀ; because there is a single grouping factor, V⁻¹ is block-diagonal in closed form, so the whole fit reduces to a one-dimensional search over the variance ratio λ = σ²_b/σ²_e, profiling out β (by GLS) and σ²_e analytically at each λ. Method::Reml (default) gives unbiased variance components.

The headline diagnostics are the variance components, the intraclass correlation ICC = σ²_b/(σ²_b + σ²_e) (the share of variance between groups, and the correlation of two observations in the same group), and the BLUPs — shrinkage-predicted group intercepts.

Implementations§

Source§

impl LinearMixedModel

Source

pub fn new(x: Array2<f64>, y: Array1<f64>, groups: &[usize]) -> Result<Self>

Fit a random-intercept model of y on fixed-effect design X with group labels groups (one per observation; arbitrary integer labels are remapped internally), by REML.

X carries the fixed effects including any intercept column, as elsewhere in the crate.

§Errors
Source

pub fn with_method( x: Array2<f64>, y: Array1<f64>, groups: &[usize], method: Method, ) -> Result<Self>

Like LinearMixedModel::new with an explicit Method.

Source

pub fn n_observations(&self) -> usize

Number of observations.

Source

pub fn n_parameters(&self) -> usize

Number of fixed-effect coefficients.

Source

pub fn n_groups(&self) -> usize

Number of groups (levels of the random intercept).

Source

pub fn method(&self) -> Method

Estimation method used.

Source

pub fn coefficients(&self) -> ArrayView1<'_, f64>

Fixed-effect coefficients β̂ (GLS at the estimated variance ratio).

Source

pub fn covariance(&self) -> ArrayView2<'_, f64>

Fixed-effect covariance σ̂²_e (XᵀV⁻¹X)⁻¹.

Source

pub fn coefficient_standard_errors(&self) -> Array1<f64>

Fixed-effect standard errors.

Source

pub fn residual_variance(&self) -> f64

Residual (within-group) variance σ̂²_e.

Source

pub fn group_variance(&self) -> f64

Between-group (random-intercept) variance σ̂²_b.

Source

pub fn variance_ratio(&self) -> f64

Estimated variance ratio λ̂ = σ̂²_b / σ̂²_e.

Source

pub fn icc(&self) -> f64

Intraclass correlation ICC = σ̂²_b / (σ̂²_b + σ̂²_e) — the fraction of total variance attributable to between-group differences, equivalently the correlation between two observations in the same group.

Source

pub fn random_effects(&self) -> ArrayView1<'_, f64>

BLUPs — best linear unbiased predictors of the group random intercepts b̂_j, indexed by densified group order (first-seen order of the labels). Each is the group’s mean residual shrunk toward zero by (λ n_j)/(1 + λ n_j).

Source

pub fn log_likelihood(&self) -> f64

The profile log-likelihood (REML or ML per method) at the estimated variance components.

Source

pub fn aic(&self) -> f64

AIC. Under ML the parameter count is p + 2 (fixed effects plus the two variance components); under REML, where fixed effects are integrated out, only the two variance components are counted.

Trait Implementations§

Source§

impl Clone for LinearMixedModel

Source§

fn clone(&self) -> LinearMixedModel

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LinearMixedModel

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V