Title: | Symbolic Computation for Structural Equation Models |
---|---|
Description: | A collection of functions for symbolic computation using the 'caracas' package for structural equation models and other statistical analyses. Among its features is the ability to calculate the model-implied covariance (and correlation) matrix and the sampling covariance matrix of variable functions using the delta method. |
Authors: | Mike Cheung [aut, cre] |
Maintainer: | Mike Cheung <[email protected]> |
License: | GPL (>=2) |
Version: | 0.4 |
Built: | 2024-11-13 05:30:15 UTC |
Source: | https://github.com/mikewlcheung/symsem |
A collection of functions for symbolic computation using 'caracas' package for structural equation models and other statistical analyses. Among its features is the ability to calculate the model-implied covariance (and correlation) matrix and the sampling covariance matrix of variable functions using the delta method.
As 'caracas' uses 'SymPy" in the backend. Reserved words in SymP, such as"lambda" and "I" are converted to some random strings first. These random strings are converted back to R.
It computes the variance-covariance matrix of functions using the first-order delta method.
deltamethod(fn, Covvars, vars, Var.name = "V", Cov.name = "C", simplify = TRUE)
deltamethod(fn, Covvars, vars, Var.name = "V", Cov.name = "C", simplify = TRUE)
fn |
A function in character strings or a vector of functions. |
Covvars |
Variance-covariance matrix of the variables. Users must
ensure the order of variables is the same as that in |
vars |
A vector of characters of the random variables. If the random variables are not listed in 'vars', they are treated as constants. If 'vars' is missing, all names in 'RAM' are treated as random variables. |
Var.name |
Name of the variances. |
Cov.name |
Name of the covariances. |
simplify |
Attempt to simplify the output. |
Variance-covariance matrix of the functions.
Mike W.-L. Cheung <[email protected]>
## Not run: #### Fisher-z-transformation fn <- "0.5*log((1+r)/(1-r))" ## Sampling variance of r Covvars <- "(1-r^2)^2/n" deltamethod(fn=fn, Covvars=Covvars, vars="r") ## $fn ## [,1] ## fn1 "0.5*log((r+1)/(1-r))" ## $Covfn ## fn1 ## fn1 "1/n" ## $vars ## [1] "r" ## $Covvars ## r ## r "(1-r^2)^2/n" ## $Jmatrix ## r ## fn1 "(0.5*(1-r+r+1)*(1-r))/((1-r)^2*(r+1))" #### Raw mean difference: y_treatment - y_control fn <- "yt - yc" ## Sampling covariance matrix ## S2p: pooled variance ## nt: n_treatment ## nc: n_control Covvars <- matrix(c("S2p/nt", 0, 0, "S2p/nc"), ncol=2, nrow=2) deltamethod(fn=fn, Covvars=Covvars, vars=c("yt", "yc")) ## $fn ## [,1] ## fn1 "yt-yc" ## $Covfn ## fn1 ## fn1 "(S2p*nt+S2p*nc)/(nt*nc)" ## $vars ## [1] "yt" "yc" ## $Covvars ## yt yc ## yt "S2p/nt" "0" ## yc "0" "S2p/nc" ## $Jmatrix ## yt yc ## fn1 "1" "-1" #### log(odds) fn <- "log(p/(1-p))" ## Sampling variance of p Covvars <- "p*(1-p)/n" ## Though it is correct, the simplification does not work well. deltamethod(fn=fn, Covvars=Covvars, vars="p") ## $fn ## [,1] ## fn1 "log(p/(1-p))" ## $Covfn ## fn1 ## fn1 "(3*p^2-p^3-3*p+1)/((p^4-4*p^3+6*p^2-4*p+1)*p*n)" ## $vars ## [1] "p" ## $Covvars ## p ## p "(p*(1-p))/n" ## $Jmatrix ## p ## fn1 "((1-p+p)*(1-p))/((1-p)^2*p)" ## End(Not run)
## Not run: #### Fisher-z-transformation fn <- "0.5*log((1+r)/(1-r))" ## Sampling variance of r Covvars <- "(1-r^2)^2/n" deltamethod(fn=fn, Covvars=Covvars, vars="r") ## $fn ## [,1] ## fn1 "0.5*log((r+1)/(1-r))" ## $Covfn ## fn1 ## fn1 "1/n" ## $vars ## [1] "r" ## $Covvars ## r ## r "(1-r^2)^2/n" ## $Jmatrix ## r ## fn1 "(0.5*(1-r+r+1)*(1-r))/((1-r)^2*(r+1))" #### Raw mean difference: y_treatment - y_control fn <- "yt - yc" ## Sampling covariance matrix ## S2p: pooled variance ## nt: n_treatment ## nc: n_control Covvars <- matrix(c("S2p/nt", 0, 0, "S2p/nc"), ncol=2, nrow=2) deltamethod(fn=fn, Covvars=Covvars, vars=c("yt", "yc")) ## $fn ## [,1] ## fn1 "yt-yc" ## $Covfn ## fn1 ## fn1 "(S2p*nt+S2p*nc)/(nt*nc)" ## $vars ## [1] "yt" "yc" ## $Covvars ## yt yc ## yt "S2p/nt" "0" ## yc "0" "S2p/nc" ## $Jmatrix ## yt yc ## fn1 "1" "-1" #### log(odds) fn <- "log(p/(1-p))" ## Sampling variance of p Covvars <- "p*(1-p)/n" ## Though it is correct, the simplification does not work well. deltamethod(fn=fn, Covvars=Covvars, vars="p") ## $fn ## [,1] ## fn1 "log(p/(1-p))" ## $Covfn ## fn1 ## fn1 "(3*p^2-p^3-3*p+1)/((p^4-4*p^3+6*p^2-4*p+1)*p*n)" ## $vars ## [1] "p" ## $Covvars ## p ## p "(p*(1-p))/n" ## $Jmatrix ## p ## fn1 "((1-p+p)*(1-p))/((1-p)^2*p)" ## End(Not run)
It computes a symbolic model-implied covariance (or correlation) matrix in SEM using the RAM specification inputs.
impliedS(RAM, corr = FALSE, replace.constraints = FALSE, convert = TRUE)
impliedS(RAM, corr = FALSE, replace.constraints = FALSE, convert = TRUE)
RAM |
A RAM object including a list of matrices of the model returned
from |
corr |
Whether the model implied matrix is covariance (default) or correlation structure. |
replace.constraints |
Whether to replace the parameters with the constraints
in the |
convert |
Whether to convert random strings back to parameters. For internal use only. Users unlikely need to use this argument. |
A list of object with class implieS
. It stores the A, S, and F
matrices and the model implied covariance (or correlation) matrix and the
vector of the means.
Mike W.-L. Cheung <[email protected]>
## Not run: #### A mediation model model1 <- "y ~ c*x + b*m m ~ a*x ## Means y ~ b0*1 m ~ m0*1 x ~ x0*1" RAM1 <- metaSEM::lavaan2RAM(model1) ## Model-implied covariance matrix and mean structure impliedS(RAM1, corr=FALSE) ## Model-implied correlation matrix impliedS(RAM1, corr=TRUE) #### A CFA model model2 <- "f =~ x1 + x2 + x3 + x4 ## Mean f ~ fmean*1" RAM2 <- metaSEM::lavaan2RAM(model2) ## Model-implied covariance matrix impliedS(RAM2, corr=FALSE) ## Model-implied correlation matrix impliedS(RAM2, corr=TRUE) ## End(Not run)
## Not run: #### A mediation model model1 <- "y ~ c*x + b*m m ~ a*x ## Means y ~ b0*1 m ~ m0*1 x ~ x0*1" RAM1 <- metaSEM::lavaan2RAM(model1) ## Model-implied covariance matrix and mean structure impliedS(RAM1, corr=FALSE) ## Model-implied correlation matrix impliedS(RAM1, corr=TRUE) #### A CFA model model2 <- "f =~ x1 + x2 + x3 + x4 ## Mean f ~ fmean*1" RAM2 <- metaSEM::lavaan2RAM(model2) ## Model-implied covariance matrix impliedS(RAM2, corr=FALSE) ## Model-implied correlation matrix impliedS(RAM2, corr=TRUE) ## End(Not run)
It computes a symbolic Jacobian matrix of the model-implied covariance (or correlation) matrix in SEM using the RAM specification.
JacobianRAM(RAM, vars, corr = FALSE)
JacobianRAM(RAM, vars, corr = FALSE)
RAM |
A RAM object including a list of matrices of the model returned
from |
vars |
A vector of characters of the random variables. If the random variables are not listed in 'vars', they are treated as constants. If 'vars' is missing, all names in 'RAM' are treated as random variables. |
corr |
Whether the model implied matrix is covariance (default) or correlation structure. |
A Jacobian matrix.
Mike W.-L. Cheung <[email protected]>
## Not run: #### A mediation model model1 <- "y ~ c*x + b*m m ~ a*x ## Means y ~ b0*1 m ~ m0*1 x ~ x0*1" RAM1 <- metaSEM::lavaan2RAM(model1) ## Model-implied covariance matrix and mean structure JacobianRAM(RAM1, corr=FALSE) ## Model-implied correlation matrix JacobianRAM(RAM1, corr=TRUE) #### A CFA model model2 <- "f =~ x1 + x2 + x3 + x4#' ## Mean f ~ fmean*1" RAM2 <- metaSEM::lavaan2RAM(model2) ## Model-implied covariance matrix JacobianRAM(RAM2, corr=FALSE) ## Model-implied correlation matrix JacobianRAM(RAM2, corr=TRUE) ## End(Not run)
## Not run: #### A mediation model model1 <- "y ~ c*x + b*m m ~ a*x ## Means y ~ b0*1 m ~ m0*1 x ~ x0*1" RAM1 <- metaSEM::lavaan2RAM(model1) ## Model-implied covariance matrix and mean structure JacobianRAM(RAM1, corr=FALSE) ## Model-implied correlation matrix JacobianRAM(RAM1, corr=TRUE) #### A CFA model model2 <- "f =~ x1 + x2 + x3 + x4#' ## Mean f ~ fmean*1" RAM2 <- metaSEM::lavaan2RAM(model2) ## Model-implied covariance matrix JacobianRAM(RAM2, corr=FALSE) ## Model-implied correlation matrix JacobianRAM(RAM2, corr=TRUE) ## End(Not run)