relcs

knitr::opts_chunk$set(
  comment = "#>",
  collapse = TRUE
)

What is this?

Random Effects Latent Change Score models.

Install

Obtain the latest package version from github:

install.packages("devtools")
devtools::install_github("brandmaier/relcs")

Load the relcs library:

library("relcs")

How To Use

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.

Simulation

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")

model fit

fit <- fitRELCS(data = simulated_data, type="stan")

fit$fit

visually inspect model posteriors

hist(rstan::extract(fit$fit)$residual_var,main = "Residual Variance")

Obtain individual beta estimates

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)

inspect STAN code

cat(fit$code)


brandmaier/relcs documentation built on March 27, 2021, 1:32 p.m.