torch_sem | R Documentation |
Function for creating a structural equation model
torch_sem(syntax, dtype = torch_float32(), device = torch_device("cpu"))
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 |
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.
A torch_sem
object, which is an nn_module
(torch object)
free_params
Vector of free parameters
$initialize()
The initialize method. Don't use this, just use torch_sem()
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()
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.
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
loss
torch_tensor of freshly computed loss function (needed by torch
for backwards pass)
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.
loss
torch_tensor of freshly computed loss function (needed by torch
for backwards pass)
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.
loss
(optional) torch_tensor of freshly computed loss function (needed by torch
for backwards pass)
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
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
Self, i.e., the torch_sem
object with updated parameters
$loglik(dat)
Multivariate normal log-likelihood of the data.
dat
dataset (centered!) as a torch_tensor
Log-likelihood value (torch scalar)
df_to_tensor()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.