library(lavaan.pl)
#> ℹ Loading required package: lavaan
#> This is lavaan 0.6-19
#> lavaan is FREE software! Please report any bugs.
#>
#> ── Conflicts ─────────────────────────────────────────── lavaan.pl 0.1.0.9002 ──
#> ✖ lavaan.pl::cfa() masks lavaan::cfa()
Example taken from test files. Need to edit.
# p = number of items, q = number of latent variables, n = number of observations
p <- 30; q <- 8; n <- 1000
# Thresholds vector for each item
thr <- c(-1.5, 0, 1.5)
cat <- rep(length(thr)+1, p)
nthr <- sum(cat)-p
#### free correlation matrix and latent variances ######
set.seed(123)
stdlv <- FALSE
corrflag <- TRUE
# Simple loading matrix constraints
A <- build_constrMat(P = p, Q = q, STRUCT = 'simple')
llc <- NULL
A <- lavaan.pl:::check_cnstr_loadings(A, stdlv, LLC=llc)
# Draw some random loadings according to constraints
Load <- gen_loadings(CONSTRMAT = A, STDLV = stdlv, LLC=llc)
nload <- sum(is.na(A))
# Generate random latent correlation matrix
tcorrvec <- rep(0, q*(q-1)/2); if(corrflag) tcorrvec <- rnorm(q*(q-1)/2)
ncorr <- if(corrflag)q*(q-1)/2 else 0
R <- lavaan.pl:::cpp_latvar_vec2cmat(VEC=tcorrvec, NCORR=ncorr, Q=q)
# Generate random latent variances
constr_var <- rep(NA, q)
constr_lsd <- lavaan.pl:::check_cnstr_latvar(constr_var, q, stdlv)
nvar <- sum(is.na(constr_lsd))
tsdvec <- constr_lsd
tsdvec[is.na(constr_lsd)] <- rnorm(sum(is.na(constr_lsd)), -.25, .1)
Dmat <- diag(exp(tsdvec),q,q)
S <- Dmat %*% R %*% Dmat
# Dimensions
d <- nthr + nload + ncorr + nvar
d
#> [1] 148
#
theta <- get_theta(
THRESHOLDS = rep(thr, p),
LOADINGS = Load,
LATENT_COV = S,
CAT = cat,
CONSTRMAT = A,
CONSTRVAR = exp(constr_lsd)^2,
CORRFLAG = corrflag,
STDLV = stdlv
)
dat <- sim_data(
SAMPLE_SIZE = n,
LOADINGS = Load,
THRESHOLDS = thr,
LATENT_COV = S)
constr_list <- list(CONSTRMAT=A, CORRFLAG=corrflag, CONSTRVAR=exp(constr_lsd)^2, STDLV=stdlv, LLC=llc)
head(dat)
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> [1,] 1 1 2 1 2 2 1 1 1 1 2 2 2 2
#> [2,] 2 1 2 2 2 2 1 1 1 1 1 1 1 2
#> [3,] 1 1 1 1 1 1 1 1 2 3 1 0 2 1
#> [4,] 2 2 2 1 2 1 2 2 1 1 1 2 1 1
#> [5,] 1 1 1 1 3 1 1 1 1 2 3 2 3 2
#> [6,] 2 2 2 1 1 1 2 1 2 1 2 1 1 3
#> [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
#> [1,] 1 2 2 0 2 1 1 1 1 1 1 0
#> [2,] 2 1 1 1 3 2 2 2 1 2 1 3
#> [3,] 1 2 2 1 1 1 1 1 1 1 1 2
#> [4,] 1 2 2 1 2 2 1 2 0 2 2 2
#> [5,] 1 2 1 2 2 0 2 1 0 0 0 1
#> [6,] 1 2 3 2 2 2 1 1 1 2 1 1
#> [,27] [,28] [,29] [,30]
#> [1,] 2 2 1 2
#> [2,] 1 2 2 2
#> [3,] 2 1 1 2
#> [4,] 2 1 1 1
#> [5,] 0 1 2 0
#> [6,] 1 1 1 1