R/subtyper.R
analyze_longitudinal_change.Rd
This function fits a linear mixed-effects model to assess the relationship between a predictor and an outcome over time, while controlling for covariates. It returns the model object, key statistics, and two plots for visualization.
analyze_longitudinal_change(
data,
outcome_var,
predictor,
covariates = NULL,
random_effect = "eid"
)
A data frame containing the data.
A character string specifying the name of the outcome variable.
A character string specifying the main predictor of interest (e.g., "yearsbl").
A character vector of covariate names.
A character string for the name of the random grouping factor (e.g., "eid").
A list containing:
The fitted lmerMod
object.
The calculated Cohen's d for the main predictor.
The t-statistic for the main predictor.
The degrees of freedom for the t-statistic.
The p-value for the main predictor.
A ggplot object showing individual trajectories.
A ggplot object showing the model-predicted population trajectory.
Returns NULL
if the model fails to converge or an error occurs.
if (FALSE) { # \dontrun{
# Assuming `ukbbdL` is your data frame
result <- analyze_longitudinal_change(
data = ukbbdL,
outcome_var = "t1PC1",
predictor = "yearsbl",
covariates = c("Age", "Sex", "T1Hier_resnetGrade"),
random_effect = "eid"
)
if (!is.null(result)) {
gridExtra::grid.arrange(result$spaghetti_plot, result$population_plot, ncol = 2)
}
} # }