knitr::opts_chunk$set( comment = "#>", collapse = TRUE )
Random Effects Latent Change Score models.
Obtain the latest package version from github:
install.packages("devtools") devtools::install_github("brandmaier/relcs")
Load the relcs
library:
library("relcs")
The relcs
package offers functions to simulate data (either from an OpenMx model or from specialized code) and to fit a model using either STAN or OpenMx.
Simulate 100 cases from a RELCS model:
library(relcs) library(tidyverse) library(ggplot2) simulated_data <- simulateDataFromRELCS(N = 100, num.obs = 5, residualerrorvariance = .3, selffeedback.mean = .5, selffeedback.variance = .01, interceptmu = 0, interceptvariance = 1, has.slope=FALSE)
Plot the first 20 simulated trajectories:
simulated_data %>% mutate(id=1:nrow(simulated_data)) %>% filter(id < 20) %>% pivot_longer(-id) %>% ggplot(aes(x=name,y=value,group=id,color=factor(id)))+ geom_line()+ theme_minimal()+ xlab("Time")+ylab("Value")+ ggx::gg_("hide legend")
fit <- fitRELCS(data = simulated_data, type="stan") fit$fit
hist(rstan::extract(fit$fit)$residual_var,main = "Residual Variance")
Re-fit the model with option beta.as.parameter
which makes the person-specific parameters
additional parameters, one for each person.
fit_with_beta <- fitRELCS(data = simulated_data, type="stan",beta.as.parameter = TRUE)
Get the beta values from the model and obtain summary.
betas <- get_beta_estimates(fit_with_beta) summary(betas) hist(betas)
cat(fit$code)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.