Title: | Mixture of Longitudinal Factor Analysis Methods |
---|---|
Description: | Provides a function for the estimation of mixture of longitudinal factor analysis models using the iterative expectation-maximization algorithm (Ounajim, Slaoui, Louis, Billot, Frasca, Rigoard (2023) <doi:10.1002/sim.9804>) and several tools for visualizing and interpreting the models' parameters. |
Authors: | Amine Ounajim [aut, cre], Yousri Slaoui [aut], Omar Lahbabi [aut], Philippe Rigoard [ctb] |
Maintainer: | Amine Ounajim <[email protected]> |
License: | GPL-3 |
Version: | 1.0.0 |
Built: | 2024-12-17 07:02:21 UTC |
Source: | CRAN |
This function extracts the fixed effect coefficients from the results obtained from a
Mixture of Longitudinal Factor Analyzers (MLFA) model for a specified class and factor.
Fixed_coef(res_MLFA, C, d)
Fixed_coef(res_MLFA, C, d)
res_MLFA |
list containing the MLFA model parameters returned by the MLFA function. |
C |
an integer giving the number of mixture components. |
d |
an integer giving the factor index from which to extract the coefficients. This corresponds to the specific latent factor of interest. |
The function first determines the number of predictor variables () by evaluating the number of columns in the
predictor matrix
that was used in the MLFA. It then extracts the relevant coefficients from the estimated fixed effects
vector
associated with the specified class
and factor
. The
vector is structured such that coefficients
for each factor are stored in contiguous blocks; this function selects the appropriate block corresponding to the
factor
within the class
.
A numeric vector of length ncol(X) (number of fixed covariates in the MLFA model) containing the coefficients for the specified class and factor.
# Load the necessary datasets data(simulated_MLFA) # Load a simulated dataset based on the MLFA model # Extract matrices from the list # Extract matrix Y of outcomes of interest for the factor analysis model Y <- simulated_MLFA$Y # Extract matrix X of fixed effect covariates for describing the latent factors X <- simulated_MLFA$X # Extract matrix Z of random effect covariates for describing the latent factors Z <- simulated_MLFA$Z # Extract matrix id containing subject identifiers. id <-simulated_MLFA$id #' # Run the MLFA (Mixture of Longitudinal Factor Analyzers) function with: # C: number of classes or clusters in our simulated data was set to 2. # d: number of latent factors in our simulated data was set to 1. # max_it: maximum number of iterations is set to 50 for a quick test. # Estimation of the parameters of the MLFA model using the simulated data. result_MLFA <- MLFA(C = 2, d = 2, X, Y, Z, id, max_it = 50, fixed_factor = c(1,6)) # Extract the fixed effect coefficients for the latent factor 1 in cluster 1 coef_vector <- Fixed_coef(result_MLFA, C=1, d=1)
# Load the necessary datasets data(simulated_MLFA) # Load a simulated dataset based on the MLFA model # Extract matrices from the list # Extract matrix Y of outcomes of interest for the factor analysis model Y <- simulated_MLFA$Y # Extract matrix X of fixed effect covariates for describing the latent factors X <- simulated_MLFA$X # Extract matrix Z of random effect covariates for describing the latent factors Z <- simulated_MLFA$Z # Extract matrix id containing subject identifiers. id <-simulated_MLFA$id #' # Run the MLFA (Mixture of Longitudinal Factor Analyzers) function with: # C: number of classes or clusters in our simulated data was set to 2. # d: number of latent factors in our simulated data was set to 1. # max_it: maximum number of iterations is set to 50 for a quick test. # Estimation of the parameters of the MLFA model using the simulated data. result_MLFA <- MLFA(C = 2, d = 2, X, Y, Z, id, max_it = 50, fixed_factor = c(1,6)) # Extract the fixed effect coefficients for the latent factor 1 in cluster 1 coef_vector <- Fixed_coef(result_MLFA, C=1, d=1)
This function generates a heatmap for visualizing the factor loadings from the MLFA model results.
generate_heatmap(res_MLFA, C)
generate_heatmap(res_MLFA, C)
res_MLFA |
a list containing the MLFA model parameters returned by the MLFA function. |
C |
an integer Class to display |
No return value. This function generates a heatmap plot of the factor loadings matrix.
# Load the necessary datasets data(simulated_MLFA) # Load a simulated dataset based on the MLFA model # Extract matrices from the list # Extract matrix Y of outcomes of interest for the factor analysis model Y <- simulated_MLFA$Y # Extract matrix X of fixed effect covariates for describing the latent factors X <- simulated_MLFA$X # Extract matrix Z of random effect covariates for describing the latent factors Z <- simulated_MLFA$Z # Extract matrix id containing subject identifiers. id <-simulated_MLFA$id #' # Run the MLFA (Mixture of Longitudinal Factor Analyzers) function with: # C: number of classes or clusters in our simulated data was set to 2. # d: number of latent factors in our simulated data was set to 1. # max_it: maximum number of iterations is set to 50 for a quick test. # Estimation of the parameters of the MLFA model using the simulated data. result_MLFA <- MLFA(C = 2, d = 2, X, Y, Z, id, max_it = 50, fixed_factor = c(1,6)) # Generate a heatmap of the factor loadings of the first cluster generate_heatmap(result_MLFA,C=1)
# Load the necessary datasets data(simulated_MLFA) # Load a simulated dataset based on the MLFA model # Extract matrices from the list # Extract matrix Y of outcomes of interest for the factor analysis model Y <- simulated_MLFA$Y # Extract matrix X of fixed effect covariates for describing the latent factors X <- simulated_MLFA$X # Extract matrix Z of random effect covariates for describing the latent factors Z <- simulated_MLFA$Z # Extract matrix id containing subject identifiers. id <-simulated_MLFA$id #' # Run the MLFA (Mixture of Longitudinal Factor Analyzers) function with: # C: number of classes or clusters in our simulated data was set to 2. # d: number of latent factors in our simulated data was set to 1. # max_it: maximum number of iterations is set to 50 for a quick test. # Estimation of the parameters of the MLFA model using the simulated data. result_MLFA <- MLFA(C = 2, d = 2, X, Y, Z, id, max_it = 50, fixed_factor = c(1,6)) # Generate a heatmap of the factor loadings of the first cluster generate_heatmap(result_MLFA,C=1)
This function performs a mixture of longitudinal factor analyzers on multivariate longitudinal data as described by Ounajim et al (2023). The MLFA model is written a two equations representing a measurement factor model describing the link between the outcomes / indicators of interest and one or several latent factors
, and a structural mixed effect model describing the link between the latent factors and a set of explanatory variables in the design matrices
and
for fixed and random effects respectively:
where i is the subject index, t is the measurement time index, j is the outcome index and k is latent factor index. The model parameters are estimated using the Expectation-Maximization (EM) algorithm.
MLFA(C, d, X, Y, Z, id, max_it, fixed_factor = 1:d, seed = 1, scale = TRUE)
MLFA(C, d, X, Y, Z, id, max_it, fixed_factor = 1:d, seed = 1, scale = TRUE)
C |
an integer giving the number of mixture components. |
d |
an integer giving the number of latent factors in the factor analysis model. |
X |
a matrix containing the design matrix for fixed effects covariates for the mixed effect model explaining the latent factors (an unit column can be included to estimate an intercept). |
Y |
a matrix containing the observed outcomes / indicators of interest for the factor model. |
Z |
a matrix containing the design matrix for random effects covariates for the mixed effect model explaining the latent factors. |
id |
a vector containing subject identifiers. |
max_it |
an integer giving the maximum number of iterations for the expectation-maximization algorithm for parameter estimation. The algorithm might stop before |
fixed_factor |
a vector of integers of length d containing the columns in Y with factor loadings fixed to 1. |
seed |
a seed value for the random initialization of parameters. |
scale |
an optional Boolean indicating whether the matrix Y needs to be scaled or not. |
a list with the following components:
a list of length containing the estimated factor loading matrix for each cluster.
a list of length containing the estimated fixed effect coefficients numeric vector of length p*d where p is the number of fixed effect and d is the number of latent factors. The function fixed_coef can be used to extract the fixed effect coefficient for a given cluster and for a given latent factor.
a list of length containing the estimated covariance matrices of the random effects.
a list of length containing the estimated covariance matrices of the error term in the mixed effect model (both intra and inter-factor covariances).
a list of length containing the vector of estimated variances of the error terms
in the factor 'measurement' model.
a numeric vector containing the estimated proportion of each cluster.
a numeric value of the Bayesian Information Criterion for model selection for either the number of clusters or the number of latent factors.
a numeric value of the Akaike Information Criterion for model selection.
a numeric value of the Integrated Completed Likelihood for model selection.
a matrix containig the the probability of class membership for each subject.
a Boolean indicating whether the model generated Nan values or not.
Ounajim, A., Slaoui, Y., Louis, P. Y., Billot, M., Frasca, D., & Rigoard, P. (2023). Mixture of longitudinal factor analyzers and their application to the assessment of chronic pain. Statistics in medicine, 42(18), 3259–3282. https://doi.org/10.1002/sim.9804
# Load the necessary datasets data(simulated_MLFA) # Load a simulated dataset based on the MLFA model # Extract matrices from the list # Extract matrix Y of outcomes of interest for the factor analysis model Y <- simulated_MLFA$Y # Extract matrix X of fixed effect covariates for describing the latent factors X <- simulated_MLFA$X # Extract matrix Z of random effect covariates for describing the latent factors Z <- simulated_MLFA$Z # Extract matrix id containing subject identifiers. id <-simulated_MLFA$id #' # Run the MLFA (Mixture of Longitudinal Factor Analyzers) function with: # C: number of classes or clusters in our simulated data was set to 2. # d: number of latent factors in our simulated data was set to 1. # max_it: maximum number of iterations is set to 50 for a quick test. # Estimation of the parameters of the MLFA model using the simulated data. result_MLFA <- MLFA(C = 2, d = 2, X, Y, Z, id, max_it = 50, fixed_factor = c(1,6)) # Print the resulting factor loading matrices and fixed effect coefficients print(result_MLFA$Lam) print(result_MLFA$beta)
# Load the necessary datasets data(simulated_MLFA) # Load a simulated dataset based on the MLFA model # Extract matrices from the list # Extract matrix Y of outcomes of interest for the factor analysis model Y <- simulated_MLFA$Y # Extract matrix X of fixed effect covariates for describing the latent factors X <- simulated_MLFA$X # Extract matrix Z of random effect covariates for describing the latent factors Z <- simulated_MLFA$Z # Extract matrix id containing subject identifiers. id <-simulated_MLFA$id #' # Run the MLFA (Mixture of Longitudinal Factor Analyzers) function with: # C: number of classes or clusters in our simulated data was set to 2. # d: number of latent factors in our simulated data was set to 1. # max_it: maximum number of iterations is set to 50 for a quick test. # Estimation of the parameters of the MLFA model using the simulated data. result_MLFA <- MLFA(C = 2, d = 2, X, Y, Z, id, max_it = 50, fixed_factor = c(1,6)) # Print the resulting factor loading matrices and fixed effect coefficients print(result_MLFA$Lam) print(result_MLFA$beta)
This function applies an oblimin rotation (from the package GPArotation
) <doi:10.1177/0013164404272507> to the factor loadings from the results.
of a Mixture of Longitudinal Factor Analyzers (MLFA) model. The oblimin default parameters are used.
Oblimin_Rotation(res_MLFA)
Oblimin_Rotation(res_MLFA)
res_MLFA |
a list containing the MLFA model parameters returned by the MLFA function. |
A list similar to 'res_MLFA', but with the factor loadings rotated using the oblimin method, which is a method for obtaining oblique rotations used to transform factor loading vectors or matrices to simple structure (i.e. a pattern of factor loadings such that each variable loads highly onto one and only one factor).
# Load the necessary datasets data(simulated_MLFA) # Load a simulated dataset based on the MLFA model # Extract matrices from the list # Extract matrix Y of outcomes of interest for the factor analysis model Y <- simulated_MLFA$Y # Extract matrix X of fixed effect covariates for describing the latent factors X <- simulated_MLFA$X # Extract matrix Z of random effect covariates for describing the latent factors Z <- simulated_MLFA$Z # Extract matrix id containing subject identifiers. id <-simulated_MLFA$id #' # Run the MLFA (Mixture of Longitudinal Factor Analyzers) function with: # C: number of classes or clusters in our simulated data was set to 2. # d: number of latent factors in our simulated data was set to 1. # max_it: maximum number of iterations is set to 50 for a quick test. # Estimation of the parameters of the MLFA model using the simulated data. result_MLFA <- MLFA(C = 2, d = 2, X, Y, Z, id, max_it = 50, fixed_factor = c(1,6)) # Apply the oblimin rotation to the factor loadings rotated_result <- Oblimin_Rotation(result_MLFA)
# Load the necessary datasets data(simulated_MLFA) # Load a simulated dataset based on the MLFA model # Extract matrices from the list # Extract matrix Y of outcomes of interest for the factor analysis model Y <- simulated_MLFA$Y # Extract matrix X of fixed effect covariates for describing the latent factors X <- simulated_MLFA$X # Extract matrix Z of random effect covariates for describing the latent factors Z <- simulated_MLFA$Z # Extract matrix id containing subject identifiers. id <-simulated_MLFA$id #' # Run the MLFA (Mixture of Longitudinal Factor Analyzers) function with: # C: number of classes or clusters in our simulated data was set to 2. # d: number of latent factors in our simulated data was set to 1. # max_it: maximum number of iterations is set to 50 for a quick test. # Estimation of the parameters of the MLFA model using the simulated data. result_MLFA <- MLFA(C = 2, d = 2, X, Y, Z, id, max_it = 50, fixed_factor = c(1,6)) # Apply the oblimin rotation to the factor loadings rotated_result <- Oblimin_Rotation(result_MLFA)
This function plots the Bayesian Information Criterion (BIC) values across iterations for convergence evaluation.
plot_BIC(res_MLFA)
plot_BIC(res_MLFA)
res_MLFA |
a list containing the MLFA model parameters returned by the MLFA function. |
No return value. This function generates a plot representing BIC values (y-axis) for different iterations (x-axis).
# Load the necessary datasets data(simulated_MLFA) # Load a simulated dataset based on the MLFA model # Extract matrices from the list # Extract matrix Y of outcomes of interest for the factor analysis model Y <- simulated_MLFA$Y # Extract matrix X of fixed effect covariates for describing the latent factors X <- simulated_MLFA$X # Extract matrix Z of random effect covariates for describing the latent factors Z <- simulated_MLFA$Z # Extract matrix id containing subject identifiers. id <-simulated_MLFA$id #' # Run the MLFA (Mixture of Longitudinal Factor Analyzers) function with: # C: number of classes or clusters in our simulated data was set to 2. # d: number of latent factors in our simulated data was set to 1. # max_it: maximum number of iterations is set to 50 for a quick test. # Estimation of the parameters of the MLFA model using the simulated data. result_MLFA <- MLFA(C = 2, d = 2, X, Y, Z, id, max_it = 50, fixed_factor = c(1,6)) # plot the BIC from iteration 1 to iteration \eqn{max_it}. plot_BIC(result_MLFA)
# Load the necessary datasets data(simulated_MLFA) # Load a simulated dataset based on the MLFA model # Extract matrices from the list # Extract matrix Y of outcomes of interest for the factor analysis model Y <- simulated_MLFA$Y # Extract matrix X of fixed effect covariates for describing the latent factors X <- simulated_MLFA$X # Extract matrix Z of random effect covariates for describing the latent factors Z <- simulated_MLFA$Z # Extract matrix id containing subject identifiers. id <-simulated_MLFA$id #' # Run the MLFA (Mixture of Longitudinal Factor Analyzers) function with: # C: number of classes or clusters in our simulated data was set to 2. # d: number of latent factors in our simulated data was set to 1. # max_it: maximum number of iterations is set to 50 for a quick test. # Estimation of the parameters of the MLFA model using the simulated data. result_MLFA <- MLFA(C = 2, d = 2, X, Y, Z, id, max_it = 50, fixed_factor = c(1,6)) # plot the BIC from iteration 1 to iteration \eqn{max_it}. plot_BIC(result_MLFA)
This function calculates the predicted latent factor conditional expectations given the covariates for new observations. The predictions are obtained using the estimated fixed effect coefficients of the mixed effect model in the MLFA model.
Predict(res_MLFA, C, d, X = res_MLFA$X)
Predict(res_MLFA, C, d, X = res_MLFA$X)
res_MLFA |
a list containing the MLFA model parameters returned by the MLFA function. |
C |
an integer giving the number of mixture components. |
d |
an integer giving the number of latent factors in the factor analysis model. |
X |
a matrix containing the design matrix for fixed effects covariates for the mixed effect model explaining the latent factors (an unit column can be included to estimate an intercept). Default is the matrix used to estimate the model. |
The function extracts the relevant coefficients from the matrix for the specified class
and factor
.
These coefficients are then multiplied by the predictor matrix
to compute the predicted values.
The function does not perform any plotting; it simply returns the estimated conditional expectation of the latent factor
.
A matrix containing the predicted latent factor scores given the fixed effects and a cluster indicator.
# Load the necessary datasets data(simulated_MLFA) # Load a simulated dataset based on the MLFA model # Extract matrices from the list # Extract matrix Y of outcomes of interest for the factor analysis model Y <- simulated_MLFA$Y # Extract matrix X of fixed effect covariates for describing the latent factors X <- simulated_MLFA$X # Extract matrix Z of random effect covariates for describing the latent factors Z <- simulated_MLFA$Z # Extract matrix id containing subject identifiers. id <-simulated_MLFA$id #' # Run the MLFA (Mixture of Longitudinal Factor Analyzers) function with: # C: number of classes or clusters in our simulated data was set to 2. # d: number of latent factors in our simulated data was set to 1. # max_it: maximum number of iterations is set to 50 for a quick test. # Estimation of the parameters of the MLFA model using the simulated data. result_MLFA <- MLFA(C = 2, d = 2, X, Y, Z, id, max_it = 50, fixed_factor = c(1,6)) # predict the latent factor scores from the resulting MLFA model. Predict(result_MLFA, 1, 1)
# Load the necessary datasets data(simulated_MLFA) # Load a simulated dataset based on the MLFA model # Extract matrices from the list # Extract matrix Y of outcomes of interest for the factor analysis model Y <- simulated_MLFA$Y # Extract matrix X of fixed effect covariates for describing the latent factors X <- simulated_MLFA$X # Extract matrix Z of random effect covariates for describing the latent factors Z <- simulated_MLFA$Z # Extract matrix id containing subject identifiers. id <-simulated_MLFA$id #' # Run the MLFA (Mixture of Longitudinal Factor Analyzers) function with: # C: number of classes or clusters in our simulated data was set to 2. # d: number of latent factors in our simulated data was set to 1. # max_it: maximum number of iterations is set to 50 for a quick test. # Estimation of the parameters of the MLFA model using the simulated data. result_MLFA <- MLFA(C = 2, d = 2, X, Y, Z, id, max_it = 50, fixed_factor = c(1,6)) # predict the latent factor scores from the resulting MLFA model. Predict(result_MLFA, 1, 1)
This dataset contains a list of matrices, each with a specific purpose or
structure. The list includes four matrices: ,
,
, and
. simulated using the following parameters (described in the simulation study in (Ounajim et al., 2023)):
data(simulated_MLFA)
data(simulated_MLFA)
A list with four elements:
the observed outcomes matrix with 10 columns and 500 rows (100 subjects with 5 observations each).
the fixed effects design matrix with two columns (two explanatory variables for explaining the factor loadings variation) and 500 rows.
the random effect design matrix similar to X.
a vector of length 500 containing subject identifiers
Ounajim, A., Slaoui, Y., Louis, P. Y., Billot, M., Frasca, D., & Rigoard, P. (2023). Mixture of longitudinal factor analyzers and their application to the assessment of chronic pain. Statistics in medicine, 42(18), 3259–3282. https://doi.org/10.1002/sim.9804
# Load the necessary datasets data(simulated_MLFA) # Load a simulated dataset based on the MLFA model # Extract matrices from the list # Extract matrix Y of outcomes of interest for the factor analysis model Y <- simulated_MLFA$Y # Extract matrix X of fixed effect covariates for describing the latent factors X <- simulated_MLFA$X # Extract matrix Z of random effect covariates for describing the latent factors Z <- simulated_MLFA$Z # Extract matrix id containing subject identifiers. id <-simulated_MLFA$id #' # Run the MLFA (Mixture of Longitudinal Factor Analyzers) function with: # C: number of classes or clusters in our simulated data was set to 2. # d: number of latent factors in our simulated data was set to 1. # max_it: maximum number of iterations is set to 50 for a quick test. # Estimation of the parameters of the MLFA model using the simulated data. result_MLFA <- MLFA(C = 2, d = 2, X, Y, Z, id, max_it = 50, fixed_factor = c(1,6))
# Load the necessary datasets data(simulated_MLFA) # Load a simulated dataset based on the MLFA model # Extract matrices from the list # Extract matrix Y of outcomes of interest for the factor analysis model Y <- simulated_MLFA$Y # Extract matrix X of fixed effect covariates for describing the latent factors X <- simulated_MLFA$X # Extract matrix Z of random effect covariates for describing the latent factors Z <- simulated_MLFA$Z # Extract matrix id containing subject identifiers. id <-simulated_MLFA$id #' # Run the MLFA (Mixture of Longitudinal Factor Analyzers) function with: # C: number of classes or clusters in our simulated data was set to 2. # d: number of latent factors in our simulated data was set to 1. # max_it: maximum number of iterations is set to 50 for a quick test. # Estimation of the parameters of the MLFA model using the simulated data. result_MLFA <- MLFA(C = 2, d = 2, X, Y, Z, id, max_it = 50, fixed_factor = c(1,6))
This function generates uniqueness plots (proportion of variance in the outcome variables in Y that is not explained by the factor analysis model) based on the estimated error variance.
stdUnique(res_MLFA, C)
stdUnique(res_MLFA, C)
res_MLFA |
a list containing the MLFA model parameters returned by the MLFA function. |
C |
an integer giving the number of mixture components. |
No return value. This function generates a plot of the uniqueness of the factor analysis model for a given cluster (variance that is 'unique' to the outcome variable and not explained by the common factors).
# Load the necessary datasets data(simulated_MLFA) # Load a simulated dataset based on the MLFA model # Extract matrices from the list # Extract matrix Y of outcomes of interest for the factor analysis model Y <- simulated_MLFA$Y # Extract matrix X of fixed effect covariates for describing the latent factors X <- simulated_MLFA$X # Extract matrix Z of random effect covariates for describing the latent factors Z <- simulated_MLFA$Z # Extract matrix id containing subject identifiers. id <-simulated_MLFA$id #' # Run the MLFA (Mixture of Longitudinal Factor Analyzers) function with: # C: number of classes or clusters in our simulated data was set to 2. # d: number of latent factors in our simulated data was set to 1. # max_it: maximum number of iterations is set to 50 for a quick test. # Estimation of the parameters of the MLFA model using the simulated data. result_MLFA <- MLFA(C = 2, d = 2, X, Y, Z, id, max_it = 50, fixed_factor = c(1,6)) # Generate the uniqueness plots for the first cluster stdUnique(result_MLFA, C=1)
# Load the necessary datasets data(simulated_MLFA) # Load a simulated dataset based on the MLFA model # Extract matrices from the list # Extract matrix Y of outcomes of interest for the factor analysis model Y <- simulated_MLFA$Y # Extract matrix X of fixed effect covariates for describing the latent factors X <- simulated_MLFA$X # Extract matrix Z of random effect covariates for describing the latent factors Z <- simulated_MLFA$Z # Extract matrix id containing subject identifiers. id <-simulated_MLFA$id #' # Run the MLFA (Mixture of Longitudinal Factor Analyzers) function with: # C: number of classes or clusters in our simulated data was set to 2. # d: number of latent factors in our simulated data was set to 1. # max_it: maximum number of iterations is set to 50 for a quick test. # Estimation of the parameters of the MLFA model using the simulated data. result_MLFA <- MLFA(C = 2, d = 2, X, Y, Z, id, max_it = 50, fixed_factor = c(1,6)) # Generate the uniqueness plots for the first cluster stdUnique(result_MLFA, C=1)