| Title: | Fit the Vector Autoregressive Model for Multiple Individuals |
|---|---|
| Description: | Fit the vector autoregressive model for multiple individuals using the 'OpenMx' package (Hunter, 2017 <doi:10.1080/10705511.2017.1369354>). |
| Authors: | Ivan Jacob Agaloos Pesigan [aut, cre, cph] (ORCID: <https://orcid.org/0000-0003-4818-8420>) |
| Maintainer: | Ivan Jacob Agaloos Pesigan <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.4 |
| Built: | 2026-06-06 00:19:34 UTC |
| Source: | https://github.com/jeksterslab/fitVARMxID |
Parameter Estimates
## S3 method for class 'varmxid' coef( object, mu = TRUE, alpha = TRUE, beta = TRUE, nu = TRUE, psi = TRUE, theta = TRUE, ncores = NULL, ... )## S3 method for class 'varmxid' coef( object, mu = TRUE, alpha = TRUE, beta = TRUE, nu = TRUE, psi = TRUE, theta = TRUE, ncores = NULL, ... )
object |
Object of class |
mu |
Logical.
If |
alpha |
Logical.
If |
beta |
Logical.
If |
nu |
Logical.
If |
psi |
Logical.
If |
theta |
Logical.
If |
ncores |
Positive integer. Number of cores to use. |
... |
additional arguments. |
Returns a list of vectors of parameter estimates.
Ivan Jacob Agaloos Pesigan
Confidence Intervals for the Parameter Estimates
## S3 method for class 'varmxid' confint( object, parm = NULL, level = 0.95, mu = TRUE, alpha = TRUE, beta = TRUE, nu = TRUE, psi = TRUE, theta = TRUE, robust = FALSE, ncores = NULL, ... )## S3 method for class 'varmxid' confint( object, parm = NULL, level = 0.95, mu = TRUE, alpha = TRUE, beta = TRUE, nu = TRUE, psi = TRUE, theta = TRUE, robust = FALSE, ncores = NULL, ... )
object |
Object of class |
parm |
Argument not used. |
level |
the confidence level required. |
mu |
Logical.
If |
alpha |
Logical.
If |
beta |
Logical.
If |
nu |
Logical.
If |
psi |
Logical.
If |
theta |
Logical.
If |
robust |
Logical.
If |
ncores |
Positive integer. Number of cores to use. |
... |
additional arguments. |
Returns list of matrices of confidence intervals.
Ivan Jacob Agaloos Pesigan
Determines whether each fitted OpenMx model in a varmxid object
meets convergence criteria based on (a) acceptable optimizer status and
gradient size, (b) a positive-definite Hessian, and (c) parameters not being
at their bounds.
converged(object, ...) ## S3 method for class 'varmxid' converged(object, prop = FALSE, ...)converged(object, ...) ## S3 method for class 'varmxid' converged(object, prop = FALSE, ...)
object |
A fit object. |
... |
Passed to and/or used by methods. |
prop |
Logical.
If |
For the varmxid method:
If prop = FALSE, a named logical vector indicating,
for each individual fit, whether the convergence criteria are met.
If prop = TRUE, the proportion of cases that converged.
Ivan Jacob Agaloos Pesigan
The function fits the first-order vector autoregressive model for each unit ID.
FitVARMxID( data, observed, id, time = NULL, ct = FALSE, center = TRUE, mu_fixed = FALSE, mu_free = NULL, mu_values = NULL, mu_lbound = NULL, mu_ubound = NULL, alpha_fixed = FALSE, alpha_free = NULL, alpha_values = NULL, alpha_lbound = NULL, alpha_ubound = NULL, beta_fixed = FALSE, beta_free = NULL, beta_values = NULL, beta_lbound = NULL, beta_ubound = NULL, psi_diag = FALSE, psi_fixed = FALSE, psi_d_free = NULL, psi_d_values = NULL, psi_d_lbound = NULL, psi_d_ubound = NULL, psi_d_equal = FALSE, psi_l_free = NULL, psi_l_values = NULL, psi_l_lbound = NULL, psi_l_ubound = NULL, nu_fixed = TRUE, nu_free = NULL, nu_values = NULL, nu_lbound = NULL, nu_ubound = NULL, theta_diag = TRUE, theta_fixed = TRUE, theta_d_free = NULL, theta_d_values = NULL, theta_d_lbound = NULL, theta_d_ubound = NULL, theta_d_equal = FALSE, theta_l_free = NULL, theta_l_values = NULL, theta_l_lbound = NULL, theta_l_ubound = NULL, mu0_fixed = TRUE, mu0_func = TRUE, mu0_free = NULL, mu0_values = NULL, mu0_lbound = NULL, mu0_ubound = NULL, sigma0_fixed = TRUE, sigma0_func = TRUE, sigma0_diag = FALSE, sigma0_d_free = NULL, sigma0_d_values = NULL, sigma0_d_lbound = NULL, sigma0_d_ubound = NULL, sigma0_d_equal = FALSE, sigma0_l_free = NULL, sigma0_l_values = NULL, sigma0_l_lbound = NULL, sigma0_l_ubound = NULL, robust = FALSE, seed = NULL, tries_explore = 100, tries_local = 100, max_attempts = 10, silent = FALSE, ncores = NULL )FitVARMxID( data, observed, id, time = NULL, ct = FALSE, center = TRUE, mu_fixed = FALSE, mu_free = NULL, mu_values = NULL, mu_lbound = NULL, mu_ubound = NULL, alpha_fixed = FALSE, alpha_free = NULL, alpha_values = NULL, alpha_lbound = NULL, alpha_ubound = NULL, beta_fixed = FALSE, beta_free = NULL, beta_values = NULL, beta_lbound = NULL, beta_ubound = NULL, psi_diag = FALSE, psi_fixed = FALSE, psi_d_free = NULL, psi_d_values = NULL, psi_d_lbound = NULL, psi_d_ubound = NULL, psi_d_equal = FALSE, psi_l_free = NULL, psi_l_values = NULL, psi_l_lbound = NULL, psi_l_ubound = NULL, nu_fixed = TRUE, nu_free = NULL, nu_values = NULL, nu_lbound = NULL, nu_ubound = NULL, theta_diag = TRUE, theta_fixed = TRUE, theta_d_free = NULL, theta_d_values = NULL, theta_d_lbound = NULL, theta_d_ubound = NULL, theta_d_equal = FALSE, theta_l_free = NULL, theta_l_values = NULL, theta_l_lbound = NULL, theta_l_ubound = NULL, mu0_fixed = TRUE, mu0_func = TRUE, mu0_free = NULL, mu0_values = NULL, mu0_lbound = NULL, mu0_ubound = NULL, sigma0_fixed = TRUE, sigma0_func = TRUE, sigma0_diag = FALSE, sigma0_d_free = NULL, sigma0_d_values = NULL, sigma0_d_lbound = NULL, sigma0_d_ubound = NULL, sigma0_d_equal = FALSE, sigma0_l_free = NULL, sigma0_l_values = NULL, sigma0_l_lbound = NULL, sigma0_l_ubound = NULL, robust = FALSE, seed = NULL, tries_explore = 100, tries_local = 100, max_attempts = 10, silent = FALSE, ncores = NULL )
data |
Data frame. A data frame object of data for potentially multiple subjects that contain a column of subject ID numbers (i.e., an ID variable), and at least one column of observed values. |
observed |
Character vector. A vector of character strings of the names of the observed variables in the data. |
id |
Character string. A character string of the name of the ID variable in the data. |
time |
Character string.
A character string of the name of the TIME variable in the data.
Used when |
ct |
Logical. If TRUE, fit a continuous-time vector autoregressive model. If FALSE, fit a discrete-time vector autoregressive model. |
center |
Logical.
If |
mu_fixed |
Logical.
If |
mu_free |
Logical vector indicating
which elements of |
mu_values |
Numeric vector of values for |
mu_lbound |
Numeric vector of lower bounds for |
mu_ubound |
Numeric vector of upper bounds for |
alpha_fixed |
Logical.
If |
alpha_free |
Logical vector indicating
which elements of |
alpha_values |
Numeric vector of values for |
alpha_lbound |
Numeric vector of lower bounds for |
alpha_ubound |
Numeric vector of upper bounds for |
beta_fixed |
Logical.
If |
beta_free |
Logical matrix indicating
which elements of |
beta_values |
Numeric matrix.
Values for |
beta_lbound |
Numeric matrix.
Lower bounds for |
beta_ubound |
Numeric matrix.
Upper bounds for |
psi_diag |
Logical.
If |
psi_fixed |
Logical.
If |
psi_d_free |
Logical vector
indicating free/fixed status of the elements of |
psi_d_values |
Numeric vector
with starting values for |
psi_d_lbound |
Numeric vector
with lower bounds for |
psi_d_ubound |
Numeric vector
with upper bounds for |
psi_d_equal |
Logical.
When |
psi_l_free |
Logical matrix
indicating which strictly-lower-triangular elements of |
psi_l_values |
Numeric matrix
of starting values
for the strictly-lower-triangular elements of |
psi_l_lbound |
Numeric matrix
with lower bounds for |
psi_l_ubound |
Numeric matrix
with upper bounds for |
nu_fixed |
Logical.
If |
nu_free |
Logical vector indicating
which elements of |
nu_values |
Numeric vector of values for |
nu_lbound |
Numeric vector of lower bounds for |
nu_ubound |
Numeric vector of upper bounds for |
theta_diag |
Logical.
If |
theta_fixed |
Logical.
If |
theta_d_free |
Logical vector
indicating free/fixed status of the diagonal parameters |
theta_d_values |
Numeric vector
with starting values for |
theta_d_lbound |
Numeric vector
with lower bounds for |
theta_d_ubound |
Numeric vector
with upper bounds for |
theta_d_equal |
Logical.
When |
theta_l_free |
Logical matrix
indicating which strictly-lower-triangular elements of |
theta_l_values |
Numeric matrix
of starting values
for the strictly-lower-triangular elements of |
theta_l_lbound |
Numeric matrix
with lower bounds for |
theta_l_ubound |
Numeric matrix
with upper bounds for |
mu0_fixed |
Logical.
If |
mu0_func |
Logical.
If |
mu0_free |
Logical vector indicating
which elements of |
mu0_values |
Numeric vector of values for |
mu0_lbound |
Numeric vector of lower bounds for |
mu0_ubound |
Numeric vector of upper bounds for |
sigma0_fixed |
Logical.
If |
sigma0_func |
Logical.
If |
sigma0_diag |
Logical.
If |
sigma0_d_free |
Logical vector
indicating free/fixed status of the elements of |
sigma0_d_values |
Numeric vector
with starting values for |
sigma0_d_lbound |
Numeric vector
with lower bounds for |
sigma0_d_ubound |
Numeric vector
with upper bounds for |
sigma0_d_equal |
Logical.
When |
sigma0_l_free |
Logical matrix
indicating which strictly-lower-triangular elements of |
sigma0_l_values |
Numeric matrix
of starting values
for the strictly-lower-triangular elements of |
sigma0_l_lbound |
Numeric matrix
with lower bounds for |
sigma0_l_ubound |
Numeric matrix
with upper bounds for |
robust |
Logical.
If |
seed |
Random seed for reproducibility. |
tries_explore |
Integer. Number of extra tries for the wide exploration phase. |
tries_local |
Integer. Number of extra tries for local polishing. |
max_attempts |
Integer. Maximum number of remediation attempts after the first Hessian computation fails the criteria. |
silent |
Logical.
If |
ncores |
Positive integer. Number of cores to use. |
By default, the measurement model is given by
However, the full measurement model can be parameterized as follows
where
,
,
and
are random variables
and
,
,
and
are model parameters.
represents a vector of observed random variables,
a vector of latent random variables,
and
a vector of random measurement errors,
at time and individual .
,
denotes a vector of intercepts (fixed to a null vector by default),
a matrix of factor loadings,
and
the covariance matrix of
.
In this model,
is an identity matrix and
is a diagonal matrix.
The dynamic structure is given by
where
,
,
and
are random variables,
and
,
,
and
are model parameters.
Here,
is a vector of latent variables
at time and individual ,
represents a vector of latent variables
at time and individual ,
and
represents a vector of dynamic noise
at time and individual .
denotes a vector of intercepts,
a matrix of autoregression
and cross regression coefficients,
and
the covariance matrix of
.
If center = TRUE, the dynamic structure is parameterized as follows
where
is equilibrium level of the latent state
toward which the system is pulled over time.
The continuous-time parameterization, when ct = TRUE,
for the dynamic structure is given by
note that
is a Wiener process or Brownian motion,
which represents random fluctuations.
If center = TRUE, the dynamic structure is parameterized as follows
Returns an object of class varmxid which is
a list with the following elements:
Function call.
List of function arguments.
Function used ("FitVARMxID").
A list of generated OpenMx models.
A list of fitted OpenMx models.
A logical vector indicating converged cases.
A list of output from OpenMx::imxRobustSE()
with argument details = TRUE for each id
if robust = TRUE.
Ivan Jacob Agaloos Pesigan
Hunter, M. D. (2017). State space modeling in an open source, modular, structural equation modeling environment. Structural Equation Modeling: A Multidisciplinary Journal, 25(2), 307–324. doi:10.1080/10705511.2017.1369354
Neale, M. C., Hunter, M. D., Pritikin, J. N., Zahery, M., Brick, T. R., Kirkpatrick, R. M., Estabrook, R., Bates, T. C., Maes, H. H., & Boker, S. M. (2015). OpenMx 2.0: Extended structural equation and statistical modeling. Psychometrika, 81(2), 535–549. doi:10.1007/s11336-014-9435-8
Other VAR Functions:
LDL(),
Softplus()
# Generate data using the simStateSpace package------------------------- library(simStateSpace) set.seed(42) n <- 5 time <- 100 p <- 2 alpha <- rep(x = 0, times = p) beta <- 0.50 * diag(p) psi <- 0.001 * diag(p) psi_l <- t(chol(psi)) mu0 <- SSMMeanEta( beta = beta, alpha = alpha ) sigma0 <- SSMCovEta( beta = beta, psi = psi ) sigma0_l <- t(chol(sigma0)) sim <- SimSSMVARFixed( n = n, time = time, mu0 = mu0, sigma0_l = sigma0_l, alpha = alpha, beta = beta, psi_l = psi_l ) data <- as.data.frame(sim) # Fit the model--------------------------------------------------------- # center = TRUE library(fitVARMxID) fit <- FitVARMxID( data = data, observed = paste0("y", seq_len(p)), id = "id", center = TRUE ) print(fit) summary(fit) coef(fit) vcov(fit) converged(fit) # Fit the model--------------------------------------------------------- # center = FALSE library(fitVARMxID) fit <- FitVARMxID( data = data, observed = paste0("y", seq_len(p)), id = "id", center = FALSE ) print(fit) summary(fit) coef(fit) vcov(fit) converged(fit) confint(fit) plot(fit)# Generate data using the simStateSpace package------------------------- library(simStateSpace) set.seed(42) n <- 5 time <- 100 p <- 2 alpha <- rep(x = 0, times = p) beta <- 0.50 * diag(p) psi <- 0.001 * diag(p) psi_l <- t(chol(psi)) mu0 <- SSMMeanEta( beta = beta, alpha = alpha ) sigma0 <- SSMCovEta( beta = beta, psi = psi ) sigma0_l <- t(chol(sigma0)) sim <- SimSSMVARFixed( n = n, time = time, mu0 = mu0, sigma0_l = sigma0_l, alpha = alpha, beta = beta, psi_l = psi_l ) data <- as.data.frame(sim) # Fit the model--------------------------------------------------------- # center = TRUE library(fitVARMxID) fit <- FitVARMxID( data = data, observed = paste0("y", seq_len(p)), id = "id", center = TRUE ) print(fit) summary(fit) coef(fit) vcov(fit) converged(fit) # Fit the model--------------------------------------------------------- # center = FALSE library(fitVARMxID) fit <- FitVARMxID( data = data, observed = paste0("y", seq_len(p)), id = "id", center = FALSE ) print(fit) summary(fit) coef(fit) vcov(fit) converged(fit) confint(fit) plot(fit)
Performs an LDL' factorization of a symmetric positive-definite
matrix , such that
where is unit lower-triangular (ones on the diagonal)
and is diagonal.
LDL(x, epsilon = 1e-10) InvLDL(s_l, uc_d)LDL(x, epsilon = 1e-10) InvLDL(s_l, uc_d)
x |
Numeric matrix.
Assumed symmetric positive-definite (not checked).
Note: |
epsilon |
Numeric.
Small positive value used to replace exactly zero diagonal entries
of |
s_l |
Matrix.
Strictly lower-triangular part of |
uc_d |
Vector.
Unconstrained vector such that |
LDL() returns both the unit lower-triangular factor
and the diagonal factor .
The strictly lower-triangular part of is also provided
for convenience.
The function additionally computes an unconstrained vector uc_d
such that Softplus(uc_d) = d. This uses a numerically stable inverse
softplus implementation based on log(expm1(d)) (and a large-d rewrite),
rather than the unstable expression .
InvLDL() returns a symmetric positive definite matrix
from the strictly lower-triangular part of
and the unconstrained vector uc_d. The reconstructed matrix is
symmetrized as to reduce numerical
asymmetry.
LDL(): a list with components:
l: a unit lower-triangular matrix
s_l: a strictly lower-triangular part of
d: a vector of diagonal entries of
uc_d: unconstrained vector with
x: input matrix (with diagonal zeros possibly replaced by epsilon)
epsilon: the epsilon value used
InvLDL(): a symmetric positive definite matrix
Other VAR Functions:
FitVARMxID(),
Softplus()
set.seed(123) x <- crossprod(matrix(rnorm(16), 4, 4)) + diag(1e-6, 4) ldl <- LDL(x = x) ldl inv_ldl <- InvLDL(s_l = ldl$s_l, uc_d = ldl$uc_d) inv_ldl max(abs(x - inv_ldl))set.seed(123) x <- crossprod(matrix(rnorm(16), 4, 4)) + diag(1e-6, 4) ldl <- LDL(x = x) ldl inv_ldl <- InvLDL(s_l = ldl$s_l, uc_d = ldl$uc_d) inv_ldl max(abs(x - inv_ldl))
varmxid
Plot Method for an Object of Class varmxid
## S3 method for class 'varmxid' plot( x, level = 0.95, mu = TRUE, alpha = TRUE, beta = TRUE, nu = TRUE, psi = TRUE, theta = TRUE, robust = FALSE, ncores = NULL, ... )## S3 method for class 'varmxid' plot( x, level = 0.95, mu = TRUE, alpha = TRUE, beta = TRUE, nu = TRUE, psi = TRUE, theta = TRUE, robust = FALSE, ncores = NULL, ... )
x |
Object of class |
level |
the confidence level required. |
mu |
Logical.
If |
alpha |
Logical.
If |
beta |
Logical.
If |
nu |
Logical.
If |
psi |
Logical.
If |
theta |
Logical.
If |
robust |
Logical.
If |
ncores |
Positive integer. Number of cores to use. |
... |
additional arguments. |
Displays plots of point estimates and confidence intervals.
Ivan Jacob Agaloos Pesigan
varmxid
Print Method for Object of Class varmxid
## S3 method for class 'varmxid' print( x, means = FALSE, mu = TRUE, alpha = TRUE, beta = TRUE, nu = TRUE, psi = TRUE, theta = TRUE, digits = 4, ... )## S3 method for class 'varmxid' print( x, means = FALSE, mu = TRUE, alpha = TRUE, beta = TRUE, nu = TRUE, psi = TRUE, theta = TRUE, digits = 4, ... )
x |
an object of class |
means |
Logical.
If |
mu |
Logical.
If |
alpha |
Logical.
If |
beta |
Logical.
If |
nu |
Logical.
If |
psi |
Logical.
If |
theta |
Logical.
If |
digits |
Integer indicating the number of decimal places to display. |
... |
further arguments. |
Prints means or raw estimates
depending the the value of the argument means.
Ivan Jacob Agaloos Pesigan
The softplus transformation maps unconstrained real values to the positive real line. This is useful when parameters (e.g., variances) must be positive. The inverse softplus transformation recovers the unconstrained value from a strictly positive input.
Softplus(x) InvSoftplus(x)Softplus(x) InvSoftplus(x)
x |
Numeric vector or matrix. Input values to be transformed. |
Mathematical definitions:
Softplus(x) = log(1 + exp(x))
InvSoftplus(x) = log(exp(x) - 1)
Numerical implementation (stable forms):
Softplus(x) = max(x, 0) + log1p(exp(-abs(x)))
InvSoftplus(y) uses log(expm1(y)), and for large y uses the rewrite
y + log1p(-exp(-y)) for stability.
For numerical stability, these functions use log1p() and expm1()
internally. InvSoftplus() requires strictly positive input and will
error if any values are <= 0.
Softplus(): numeric vector or matrix of nonnegative values
(mathematically strictly positive for finite inputs, but can underflow
to 0 for very negative values).
InvSoftplus(): numeric vector or matrix of unconstrained values.
Ivan Jacob Agaloos Pesigan
Other VAR Functions:
FitVARMxID(),
LDL()
# Apply softplus to unconstrained values x <- c(-5, 0, 5) y <- Softplus(x) # Recover unconstrained values x_recovered <- InvSoftplus(y) y x_recovered# Apply softplus to unconstrained values x <- c(-5, 0, 5) y <- Softplus(x) # Recover unconstrained values x_recovered <- InvSoftplus(y) y x_recovered
varmxid
Summary Method for Object of Class varmxid
## S3 method for class 'varmxid' summary( object, means = FALSE, mu = TRUE, alpha = TRUE, beta = TRUE, nu = TRUE, psi = TRUE, theta = TRUE, digits = 4, ncores = NULL, ... )## S3 method for class 'varmxid' summary( object, means = FALSE, mu = TRUE, alpha = TRUE, beta = TRUE, nu = TRUE, psi = TRUE, theta = TRUE, digits = 4, ncores = NULL, ... )
object |
an object of class |
means |
Logical.
If |
mu |
Logical.
If |
alpha |
Logical.
If |
beta |
Logical.
If |
nu |
Logical.
If |
psi |
Logical.
If |
theta |
Logical.
If |
digits |
Integer indicating the number of decimal places to display. |
ncores |
Positive integer. Number of cores to use. |
... |
further arguments. |
Returns means or raw estimates
depending the the value of the argument means.
Ivan Jacob Agaloos Pesigan
Sampling Covariance Matrix of the Parameter Estimates
## S3 method for class 'varmxid' vcov( object, mu = TRUE, alpha = TRUE, beta = TRUE, nu = TRUE, psi = TRUE, theta = TRUE, robust = FALSE, se = FALSE, ncores = NULL, ... )## S3 method for class 'varmxid' vcov( object, mu = TRUE, alpha = TRUE, beta = TRUE, nu = TRUE, psi = TRUE, theta = TRUE, robust = FALSE, se = FALSE, ncores = NULL, ... )
object |
Object of class |
mu |
Logical.
If |
alpha |
Logical.
If |
beta |
Logical.
If |
nu |
Logical.
If |
psi |
Logical.
If |
theta |
Logical.
If |
robust |
Logical.
If |
se |
Logical.
Option to return standard errors.
Default value is |
ncores |
Positive integer. Number of cores to use. |
... |
additional arguments. |
Returns a list of sampling variance-covariance matrices.
Ivan Jacob Agaloos Pesigan