Gengrui-Zhang / R2spa

An R Package for Two-Stage Path Analysis (2S-PA)
https://gengrui-zhang.github.io/R2spa/
Other
0 stars 0 forks source link

`update(..., meanstructure = TRUE)` does not work for object returned from `tspa()` #51

Open marklhc opened 1 year ago

marklhc commented 1 year ago
model <- ' 
  # latent variable definitions
     ind60 =~ x1 + x2 + x3
     dem60 =~ y1 + y2 + y3 + y4
     dem65 =~ y5 + y6 + y7 + y8

  # regressions
    dem60 ~ ind60
    dem65 ~ ind60 + dem60

  # # residual correlations
  #   y1 ~~ y5
  #   y2 ~~ y4 + y6
  #   y3 ~~ y7
  #   y4 ~~ y8
  #   y6 ~~ y8
'

fs_dat_ind60 <- get_fs(data = PoliticalDemocracy, 
                       model = "ind60 =~ x1 + x2 + x3")
fs_dat_dem60 <- get_fs(data = PoliticalDemocracy, 
                       model = "dem60 =~ y1 + y2 + y3 + y4")
fs_dat_dem65 <- get_fs(data = PoliticalDemocracy, 
                       model = "dem65 =~ y5 + y6 + y7 + y8")
fs_dat <- cbind(fs_dat_ind60, fs_dat_dem60, fs_dat_dem65)

tspa_fit_2 <- tspa(model = "dem60 ~ ind60
                          dem65 ~ ind60 + dem60", 
                      data = fs_dat, 
                      se = list(ind60 = 0.1213615, dem60 = 0.6756472, 
                                dem65 = 0.5724405))
latent_estimates <- data.frame(lavPredict(tspa_fit_2))
lavInspect(tspa_fit_2, what = "est")
update(tspa_fit_2, data = lavInspect(tspa_fit_2, what = "data"), meanstructure = TRUE)
marklhc commented 1 year ago

It works when specifying meanstructure = TRUE from start:

library(R2spa)
library(lavaan)
#> This is lavaan 0.6-15
#> lavaan is FREE software! Please report any bugs.
model <- "
  # latent variable definitions
     ind60 =~ x1 + x2 + x3
     dem60 =~ y1 + y2 + y3 + y4
     dem65 =~ y5 + y6 + y7 + y8

  # regressions
    dem60 ~ ind60
    dem65 ~ ind60 + dem60

  # # residual correlations
  #   y1 ~~ y5
  #   y2 ~~ y4 + y6
  #   y3 ~~ y7
  #   y4 ~~ y8
  #   y6 ~~ y8
"

fs_dat_ind60 <- get_fs(
    data = PoliticalDemocracy,
    model = "ind60 =~ x1 + x2 + x3"
)
fs_dat_dem60 <- get_fs(
    data = PoliticalDemocracy,
    model = "dem60 =~ y1 + y2 + y3 + y4"
)
fs_dat_dem65 <- get_fs(
    data = PoliticalDemocracy,
    model = "dem65 =~ y5 + y6 + y7 + y8"
)
fs_dat <- cbind(fs_dat_ind60, fs_dat_dem60, fs_dat_dem65)

tspa_fit_2 <- tspa(
    model = "dem60 ~ ind60
                          dem65 ~ ind60 + dem60",
    data = fs_dat,
    se = list(
        ind60 = 0.1213615, dem60 = 0.6756472,
        dem65 = 0.5724405
    ),
    meanstructure = TRUE
)

Created on 2023-06-30 with reprex v2.0.2