@@ -11,6 +11,10 @@ lav_sem_miiv_internal <- function(lavmodel = NULL, lavh1 = NULL,
1111 stopifnot(iv.method %in% " 2SLS" )
1212 iv.varcov.method <- toupper(lavoptions $ estimator.args $ iv.varcov.method )
1313 iv.samplestats <- lavoptions $ estimator.args $ iv.samplestats
14+ if (lavdata @ data.type == " moment" ) {
15+ # force iv.samplestats = TRUE
16+ iv.samplestats <- TRUE
17+ }
1418 iv.vcov.stage1 <- tolower(lavoptions $ estimator.args $ iv.vcov.stage1 )
1519 iv.sargan <- lavoptions $ estimator.args $ iv.sargan
1620 # just in case
@@ -814,6 +818,7 @@ lav_sem_miiv_vcov <- function(lavmodel = NULL, lavsamplestats = NULL,
814818
815819 # nblocks
816820 nblocks <- lavmodel @ nblocks
821+ stopifnot(lavmodel @ nblocks == 1L ) # for now...
817822
818823 # eqs?
819824 if (is.null(eqs )) {
@@ -847,38 +852,36 @@ lav_sem_miiv_vcov <- function(lavmodel = NULL, lavsamplestats = NULL,
847852 }
848853
849854 # do we need gamma_big?
850- if (iv.vcov.stage1 == " gamma" ||
851- (iv.vcov.stage2 != " none" && length(free.undirected.idx ) > 0L )) {
852- gamma_g <- vector(" list" , lavmodel @ ngroups )
853- for (g in seq_len(lavmodel @ ngroups )) {
854- if (! is.null(lavsamplestats @ NACOV [[g ]])) {
855- gamma_g [[g ]] <- lavsamplestats @ NACOV [[g ]]
856- } else {
857- if (iv.vcov.gamma.modelbased ) {
858- mean_g <- lavimplied $ mean [[g ]]
859- cov_g <- lavimplied $ cov [[g ]]
860- } else {
861- mean_g <- lavh1 $ implied $ mean [[g ]]
862- cov_g <- lavh1 $ implied $ cov [[g ]]
863- }
864- # NT version (for now), model-based
865- gamma_g [[g ]] <- lav_samplestats_Gamma_NT(
866- COV = cov_g ,
867- MEAN = mean_g ,
868- x.idx = lavsamplestats @ x.idx [[g ]],
869- fixed.x = lavmodel @ fixed.x ,
870- conditional.x = lavmodel @ conditional.x ,
871- meanstructure = lavmodel @ meanstructure ,
872- slopestructure = lavmodel @ conditional.x
873- )
874- # weight by (group) sample size
875- fg <- lavsamplestats @ nobs [[g ]] / lavsamplestats @ ntotal
876- gamma_g [[g ]] <- fg * gamma_g [[g ]]
855+ if (iv.vcov.stage1 == " gamma" ||
856+ (iv.vcov.stage2 != " none" && length(free.undirected.idx ) > 0L )) {
857+ # gamma_g <- vector("list", lavmodel@ngroups)
858+ for (g in seq_len(lavmodel @ ngroups )) {
859+ # if (!is.null(lavsamplestats@NACOV[[g]])) {
860+ # gamma_g[[g]] <- lavsamplestats@NACOV[[g]]
861+ # } else {
862+ if (iv.vcov.gamma.modelbased ) {
863+ cov_g <- lavimplied $ cov [[g ]]
864+ } else {
865+ cov_g <- lavh1 $ implied $ cov [[g ]]
866+ }
867+ # # NT version (for now), model-based
868+ # gamma_g[[g]] <- lav_samplestats_Gamma_NT(
869+ # COV = cov_g,
870+ # MEAN = mean_g,
871+ # x.idx = lavsamplestats@x.idx[[g]],
872+ # fixed.x = lavmodel@fixed.x,
873+ # conditional.x = lavmodel@conditional.x,
874+ # meanstructure = lavmodel@meanstructure,
875+ # slopestructure = lavmodel@conditional.x
876+ # )
877+ # # weight by (group) sample size
878+ # fg <- lavsamplestats@nobs[[g]] / lavsamplestats@ntotal
879+ # gamma_g[[g]] <- fg * gamma_g[[g]]
880+ # }
877881 }
882+ # gamma_big <- lav_matrix_bdiag(gamma_g)
878883 }
879- gamma_big <- lav_matrix_bdiag(gamma_g )
880- }
881-
884+
882885 # stage 1: directed effects
883886 if (length(free.directed.idx ) > 0L && iv.vcov.stage1 != " none" ) {
884887 if (iv.vcov.stage1 == " gamma" ) {
@@ -905,8 +908,13 @@ lav_sem_miiv_vcov <- function(lavmodel = NULL, lavsamplestats = NULL,
905908 free.directed.idx = free.directed.idx
906909 )
907910 }
911+ # K %*% Gamma_NT %*% t(K)
912+ # k_gammant_kt <- jac_k %*% gamma_big %*% t(jac_k)
913+ x.idx <- if (lavmodel @ fixed.x ) lavsamplestats @ x.idx [[1 ]] else integer(0L )
914+ k_gammant_kt <- lav_matrix_k_gammant_kt(K = jac_k , S = cov_g ,
915+ meanstructure = lavmodel @ meanstructure , x.idx = x.idx )
908916 vcov [free.directed.idx , free.directed.idx ] <-
909- ( jac_k %*% gamma_big %*% t( jac_k )) / lavsamplestats @ ntotal
917+ k_gammant_kt / lavsamplestats @ ntotal
910918 } else {
911919 for (b in seq_len(nblocks )) {
912920 neqs <- length(eqs [[b ]])
@@ -992,8 +1000,12 @@ lav_sem_miiv_vcov <- function(lavmodel = NULL, lavsamplestats = NULL,
9921000 } else {
9931001 tmp <- h2
9941002 }
1003+ x.idx <- if (lavmodel @ fixed.x ) lavsamplestats @ x.idx [[1 ]] else integer(0L )
1004+ tmp_gammant_tmpt <- lav_matrix_k_gammant_kt(K = tmp , S = cov_g ,
1005+ meanstructure = lavmodel @ meanstructure , x.idx = x.idx )
1006+ # tmp_gammant_tmpt_bis <- tmp %*% gamma_big %*% t(tmp)
9951007 vcov [free.undirected.idx , free.undirected.idx ] <-
996- ( tmp %*% gamma_big %*% t( tmp )) / lavsamplestats @ ntotal
1008+ tmp_gammant_tmpt / lavsamplestats @ ntotal
9971009
9981010 # iv.vcov.stage2 == "delta"
9991011 } else {
@@ -1100,8 +1112,11 @@ lav_sem_miiv_vcov <- function(lavmodel = NULL, lavsamplestats = NULL,
11001112 iv.varcov.method = iv.varcov.method
11011113 )
11021114 }
1103-
1104- vcov_b <- (jac_b %*% gamma_big %*% t(jac_b )) / lavsamplestats @ ntotal
1115+ x.idx <- if (lavmodel @ fixed.x ) lavsamplestats @ x.idx [[1 ]] else integer(0L )
1116+ jacb_gammant_jacbt <- lav_matrix_k_gammant_kt(K = jac_b , S = cov_g ,
1117+ meanstructure = lavmodel @ meanstructure , x.idx = x.idx )
1118+ # jacb_gammant_jacbt_bis <- jac_b %*% gamma_big %*% t(jac_b)
1119+ vcov_b <- jacb_gammant_jacbt / lavsamplestats @ ntotal
11051120 vcov_ab <- vcov_a + vcov_b
11061121 vcov [free.undirected.idx , free.undirected.idx ] <- vcov_ab
11071122 } # continuous
0 commit comments