knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
Eq*
functions in the ramR
package require
a character string input
that specifies the associations between the variables.
Each line in the character string
is a structural equation linking two variables at a time.
Each line should follow the syntax below
lhs <space> op <space> rhs <space> par.label <space> par.start <\n> or <;>
The elements in the syntax are as follows:
lhs
is the variable on the left-hand side,rhs
is the variable on the right-hand side,op
is the operation between lhs
and rhs
,par.label
is the column of parameter label,par.start
is the column of starting values for estimation
(only for functions that require it), and\n
or ;
are line breaks.
Each line should end with a line break.The associations are defined by the following operations:
left-hand side
measured by
right-hand side
,left-hand side
regressed on
right-hand side
,left-hand side
covarying with
right-hand side
, andleft-hand side
regressed on 1
for mean structure.Each parameter should be labeled.
The par.label
should be a number for fixed parameters
and a character string for free parameters.
Equality contraints can be imposed by using the same par.label
.
Numerical values as starting values for estimation.
Starting values for fixed parameters should be NA
.
Starting values should be identical
for parameters constrained to be equal,
that is, parameters with the same par.label
.
The characters \n
and ;
can be used as line breaks.
Each line should end with a line break.
Comments can be written after a hash (#
) sign.
In the following example,
let y1
to y6
be observed variables,
eta[1]
and eta[2]
be latent variables,
and epsilon[1]
to epsilon[6]
be error terms.
Let y1
to y3
be indicators of eta[1]
, and
y4
to y6
of eta[2]
.
The first indicator of the latent variable has a factor loading of $1$.
The latent variables are correlated.
The factor loadings are labeled with lambda[*]
,
the intercepts are labeled with nu[*]
,
the error variances are labeled with theta[*]
,
the latent variable variances and covariances are labeled with psi[*]
,
and the latent variable means are constrained to zero.
You can use any string variable names and labels.
These names and labels were chosen to conform
to typical structural equation modeling notation.
The structural equations can be defined as follows.
eq <- " # items ## loadings eta[1] by y1 1 eta[1] by y2 lambda[y2] eta[1] by y3 lambda[y3] eta[2] by y4 1 eta[2] by y5 lambda[y5] eta[2] by y6 lambda[y6] ## intercepts y1 on 1 nu[y1] y2 on 1 nu[y2] y3 on 1 nu[y3] y4 on 1 nu[y4] y5 on 1 nu[y5] y6 on 1 nu[y6] # errors ## Asymmetric - fixed to 1 epsilon[1] by y1 1 epsilon[2] by y2 1 epsilon[3] by y3 1 epsilon[4] by y4 1 epsilon[5] by y5 1 epsilon[6] by y6 1 ## Symmetric - error variances epsilon[1] with epsilon[1] theta[epsilon[1]] epsilon[2] with epsilon[2] theta[epsilon[2]] epsilon[3] with epsilon[3] theta[epsilon[3]] epsilon[4] with epsilon[4] theta[epsilon[4]] epsilon[5] with epsilon[5] theta[epsilon[5]] epsilon[6] with epsilon[6] theta[epsilon[6]] # factors ## variances and covariances eta[1] with eta[1] psi[eta[11]] eta[1] with eta[2] psi[eta[12]] eta[2] with eta[2] psi[eta[22]] ## means eta[1] on 1 0 # There is no need to define equations for parameters constrained to zero. eta[2] on 1 0 # The following equations were added for completeness. "
eq <- " # items ## loadings eta[1] by y1 1 eta[1] by y2 lambda[y2] eta[1] by y3 lambda[y3] eta[2] by y4 1 eta[2] by y5 lambda[y5] eta[2] by y6 lambda[y6] ## intercepts y1 on 1 nu[y1] y2 on 1 nu[y2] y3 on 1 nu[y3] y4 on 1 nu[y4] y5 on 1 nu[y5] y6 on 1 nu[y6] # errors ## Asymmetric - fixed to 1 epsilon[1] by y1 1 epsilon[2] by y2 1 epsilon[3] by y3 1 epsilon[4] by y4 1 epsilon[5] by y5 1 epsilon[6] by y6 1 ## Symmetric - error variances epsilon[1] with epsilon[1] theta[epsilon[1]] epsilon[2] with epsilon[2] theta[epsilon[2]] epsilon[3] with epsilon[3] theta[epsilon[3]] epsilon[4] with epsilon[4] theta[epsilon[4]] epsilon[5] with epsilon[5] theta[epsilon[5]] epsilon[6] with epsilon[6] theta[epsilon[6]] # factors ## variances and covariances eta[1] with eta[1] psi[eta[11]] eta[1] with eta[2] psi[eta[12]] eta[2] with eta[2] psi[eta[22]] ## means eta[1] on 1 0 # There is no need to define equations for parameters constrained to zero. eta[2] on 1 0 # The following equations were added for completeness. "
knitr::include_graphics("figure1-1.png")
The EqParse()
function can parse the defined equation
and setup labels for the unknown parameters.
ramR::EqParse(eq)
The Eq2RAM()
function constructs
the RAM notation matrix representation of the model.
It also updates the parameter table to include
RAM notation information.
RAM <- ramR::Eq2RAM(eq)
RAM$par.table
cat( "\\begin{align*}", "\\mathrm{variables}=", yacR::as.tex(yacR::as.ysym.mat(RAM$variables)), "\\end{align*}", sep = "" )
cat( "\\begin{align*}", "\\mathbf{A}=", yacR::as.tex(RAM$A), "\\end{align*}", sep = "" )
cat( "\\begin{align*}", "\\mathbf{S}=", yacR::as.tex(RAM$S), "\\end{align*}", sep = "" )
cat( "\\begin{align*}", "\\mathbf{u}=", yacR::as.tex(RAM$u), "\\end{align*}", sep = "" )
cat( "\\begin{align*}", "\\mathbf{F}=", yacR::as.tex(RAM$Filter), "\\end{align*}", sep = "" )
The Eq2Expectations()
function constructs the
mean and covariance expectations.
Expectations <- ramR::Eq2Expectations(eq)
The covariance expectations for all the variables are given below.
latex <- ramR::Eq2Expectations(eq, format = "tex") cat( "\\begin{align*}", "\\mathbf{C}=", latex$C, "\\end{align*}", sep = "" )
The scaled/standardized $\mathbf{C}$ matrix is given below.
$$r latex$C.scaled
$$
The covariance expectations for the observed variables y1
to y6
are given below.
cat( "\\begin{align*}", "\\mathbf{M}=", latex$M, "\\end{align*}", sep = "" )
The scaled/standardized $\mathbf{M}$ matrix is given below.
$$r latex$M.scaled
$$
The mean expectations for all the variables are given below.
cat( "\\begin{align*}", "\\mathbf{v}=", latex$v, "\\end{align*}", sep = "" )
The mean expectations for the observed variables y1
to y6
are given below.
cat( "\\begin{align*}", "\\mathbf{g}=", latex$g, "\\end{align*}", sep = "" )
RAM.scaled <- ramR::RAMScaled( A = yacR::as.ysym(RAM$A), S = RAM$S, Filter = RAM$Filter, C = Expectations$C, C.scaled = Expectations$C.scaled )
$$
\mathbf{A}_{\mathrm{scaled}} = r yacR::as.tex(RAM.scaled$A.scaled)
$$
$$
\mathbf{S}_{\mathrm{scaled}} = r yacR::as.tex(RAM.scaled$S.scaled)
$$
Values can be placed on the par.label
column.
eq <- " # items ## loadings eta[1] by y1 1 eta[1] by y2 1 eta[1] by y3 1 eta[2] by y4 1 eta[2] by y5 1 eta[2] by y6 1 ## intercepts y1 on 1 0 y2 on 1 0 y3 on 1 0 y4 on 1 0 y5 on 1 0 y6 on 1 0 # errors ## Asymmetric - fixed to 1 epsilon[1] by y1 1 epsilon[2] by y2 1 epsilon[3] by y3 1 epsilon[4] by y4 1 epsilon[5] by y5 1 epsilon[6] by y6 1 ## Symmetric - error variances epsilon[1] with epsilon[1] 0.50 epsilon[2] with epsilon[2] 0.50 epsilon[3] with epsilon[3] 0.50 epsilon[4] with epsilon[4] 0.50 epsilon[5] with epsilon[5] 0.50 epsilon[6] with epsilon[6] 0.50 # factors ## variances and covariances eta[1] with eta[1] 1 eta[1] with eta[2] 0.50 eta[2] with eta[2] 1 ## means eta[1] on 1 0 eta[2] on 1 0 "
The Eq2RAM()
function constructs
the RAM notation matrix representation of the model.
RAM <- ramR::Eq2RAM(eq)
cat( "\\begin{align*}", "\\mathrm{variables}=", yacR::as.tex(yacR::as.ysym.mat(RAM$variables)), "\\end{align*}", sep = "" )
cat( "\\begin{align*}", "\\mathbf{A}=", yacR::as.tex(RAM$A), "\\end{align*}", sep = "" )
cat( "\\begin{align*}", "\\mathbf{S}=", yacR::as.tex(RAM$S), "\\end{align*}", sep = "" )
cat( "\\begin{align*}", "\\mathbf{u}=", yacR::as.tex(RAM$u), "\\end{align*}", sep = "" )
cat( "\\begin{align*}", "\\mathbf{F}=", yacR::as.tex(RAM$Filter), "\\end{align*}", sep = "" )
The Eq2Expectations()
function constructs the
mean and covariance expectations.
Expectations <- ramR::Eq2Expectations(eq)
The covariance expectations for all the variables are given below.
cat( "\\begin{align*}", "\\mathbf{C}=", yacR::as.tex(Expectations$C), "\\end{align*}", sep = "" )
The covariance expectations for the observed variables y1
to y6
are given below.
cat( "\\begin{align*}", "\\mathbf{M}=", yacR::as.tex(Expectations$M), "\\end{align*}", sep = "" )
The mean expectations for all the variables are given below.
cat( "\\begin{align*}", "\\mathbf{v}=", yacR::as.tex(Expectations$v), "\\end{align*}", sep = "" )
The mean expectations for the observed variables y1
to y6
are given below.
cat( "\\begin{align*}", "\\mathbf{g}=", yacR::as.tex(Expectations$g), "\\end{align*}", sep = "" )
RAM.scaled <- ramR::RAMScaled( A = RAM$A, S = RAM$S, Filter = RAM$Filter, C = Expectations$C, C.scaled = Expectations$C.scaled )
$$
\mathbf{A}_{\mathrm{scaled}} = r yacR::as.tex(RAM.scaled$A.scaled)
$$
$$
\mathbf{S}_{\mathrm{scaled}} = r yacR::as.tex(RAM.scaled$S.scaled)
$$
The Eq2Data()
function simulates data
from the multivariate normal distribution.
Data <- ramR::Eq2Data(eq, n = 100000) str(Data) cov(Data) colMeans(Data)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.