torch_sem: Structural equation model with a Torch backend

torch_semR Documentation

Structural equation model with a Torch backend

Description

Function for creating a structural equation model

Usage

torch_sem(syntax, dtype = torch_float32(), device = torch_device("cpu"))

Arguments

syntax

lavaan syntax for the SEM model

dtype

(optional) torch dtype for the model (default torch_float32())

device

(optional) device type to put the model on. see torch::torch_device()

Details

This function instantiates a torch object for computing the model-implied covariance matrix based on a structural equation model. Through torch, gradients of this forward model can then be computed using backpropagation, and the parameters can be optimized using gradient-based optimization routines from the torch package.

Because of this, it is easy to add additional penalties to the standard objective function, or to write a new objective function altogether.

Value

A torch_sem object, which is an nn_module (torch object)

Fields

free_params

Vector of free parameters

Methods

⁠$initialize()⁠

The initialize method. Don't use this, just use torch_sem()

Arguments
  • syntax lavaan syntax for the SEM model

  • dtype (optional) torch dtype for the model (default torch_float32())

  • device (optional) device type to put the model on. see torch::torch_device()

Value

A torch_sem object, which is an nn_module (torch object)

⁠$forward()⁠

Compute the model-implied covariance matrix. Don't use this; nn_modules are callable, so access this method by calling the object itself as a function, e.g., my_torch_sem(). In the forward pass, we apply constraints to the parameter vector, and we create matrix views from it to compute the model-implied covariance matrix.

Value

A torch_tensor of the model-implied covariance matrix

⁠$inverse_Hessian(loss)⁠

Compute and return the asymptotic covariance matrix of the parameters with respect to the loss function

Arguments
  • loss torch_tensor of freshly computed loss function (needed by torch for backwards pass)

Value

A torch_tensor, representing the ACOV of the free parameters

⁠$standard_errors(loss)⁠

Compute and return observed information standard errors of the parameters, assuming the loss function is the likelihood and the current estimates are ML estimates.

Arguments
  • loss torch_tensor of freshly computed loss function (needed by torch for backwards pass)

Value

A ⁠numeric vector⁠ of standard errors of the free parameters

⁠$partable(loss)⁠

Create a lavaan-like parameter table from the current parameter estimates in the torch_sem object.

Arguments
  • loss (optional) torch_tensor of freshly computed loss function (needed by torch for backwards pass)

Value

lavaan partable object

⁠$fit(dat, lrate, maxit, verbose, tol)⁠

Fit a torch_sem model using the default maximum likelihood objective. This function uses the Adam optimizer to estimate the parameters of a torch_sem

Arguments
  • dat dataset (centered!) as a torch_tensor

  • lrate learning rate of the Adam optimizer.

  • maxit maximum number of epochs to train the model

  • verbose whether to print progress to the console

  • tol parameter change tolerance for stopping training

Value

Self, i.e., the torch_sem object with updated parameters

⁠$loglik(dat)⁠

Multivariate normal log-likelihood of the data.

Arguments
  • dat dataset (centered!) as a torch_tensor

Value

Log-likelihood value (torch scalar)

See Also

df_to_tensor()


vankesteren/tensorsem documentation built on Aug. 22, 2023, 7:41 a.m.