Total, Direct, and Indirect Effects in CT-Med

Author

Ivan Jacob Agaloos Pesigan

Published

February 2, 2026

Installing Required Packages

Before proceeding, ensure that the necessary package are installed and loaded. We use cTMed for calculating total, direct, and indirect effects in continuous-time mediation model.

library(cTMed)

Extracting Matrices for Use in cTMed

We begin by defining the drift matrix phi (\(\boldsymbol{\Phi}\)).

phi <- matrix(
  data = c(
    -0.357, 0.771, -0.450,
    0.000, -0.511, 0.729,
    0.000, 0.000, -0.693
  ),
  nrow = 3,
  ncol = 3
)
phi
       [,1]   [,2]   [,3]
[1,] -0.357  0.000  0.000
[2,]  0.771 -0.511  0.000
[3,] -0.450  0.729 -0.693

Next, we assign row and column names to phi (\(\boldsymbol{\Phi}\)), which will be used to index the matrix.

colnames(phi) <- rownames(phi) <- c("x", "m", "y")
phi
       x      m      y
x -0.357  0.000  0.000
m  0.771 -0.511  0.000
y -0.450  0.729 -0.693

We need the process noise covariance matrix sigma (\(\boldsymbol{\Sigma}\)) to calculate the standardized total, direct, and indirect effects.

sigma <- matrix(
  data = c(
    0.24455556, 0.02201587, -0.05004762,
    0.02201587, 0.07067800, 0.01539456,
    -0.05004762, 0.01539456, 0.07553061
  ),
  nrow = 3,
  ncol = 3
)
sigma
            [,1]       [,2]        [,3]
[1,]  0.24455556 0.02201587 -0.05004762
[2,]  0.02201587 0.07067800  0.01539456
[3,] -0.05004762 0.01539456  0.07553061

We then create a sequence of time intervals delta_t (\(\Delta t\)) ranging from 0.01 to 10, with a total of 1,000 equally spaced values.

delta_t <- seq(from = 0.01, to = 10, length.out = 1000)

Total, Direct, and Indirect Effects in CT-Med

Now, we compute the total, direct, and indirect effects across the sequence of delta_t (\(\Delta t\)), and visualize their dynamic behavior.

med <- Med(
  phi = phi,
  delta_t = delta_t,
  from = "x",
  to = "y",
  med = "m"
)
plot(med)

Standardized Total, Direct, and Indirect Effects in CT-Med

Let’s calculate the standardized total, direct, and indirect effects for the sequence of delta_t (\(\Delta t\)). We plot to visualize the dynamics.

med_std <- MedStd(
  phi = phi,
  sigma = sigma,
  delta_t = delta_t,
  from = "x",
  to = "y",
  med = "m"
)
plot(med_std)