sem-in-r / seminr

Natural feeling domain-specific language for building structural equation models in R for estimation by covariance-based methods (like LISREL/Lavaan) or partial least squares (like SmartPLS)
58 stars 19 forks source link

Error when running moderator analysis #325

Open tomryjo opened 1 year ago

tomryjo commented 1 year ago

I got this error message when running moderator analysis:

Error in first_stage$construct_scores[, moderator] : subscript out of bounds

This is the code I use:

Define measurements with familiar terms: reflective, composite, multi-item constructs, etc.

mobi_mm <- constructs( composite("Overconfidence", multi_items("v1.", 1:7)), composite("Illusion of control bias", multi_items("v2.", 1:4)), composite("Self-attribution bias", multi_items("v3.", 1:9)), composite("Herding behavior", multi_items("v4.", 1:5)), composite("Information availability", multi_items("v5.", 1:5)), composite("Financial literacy", multi_items("v6.", 1:6)), interaction_term(iv= "Illusion of control bias", moderator = "Information availability",method = two_stage), interaction_term(iv= "Self-attribution bias", moderator = "Information availability",method = two_stage), interaction_term(iv= "Overconfidence", moderator = "Financial literacy",method = two_stage) )

Create structural model

overriding_effects_sm <- relationships( paths(from = c("Overconfidence","OverconfidenceFinancial literacy"), to = "Herding behavior"), paths(from = c("Illusion of control bias","Self-attribution bias","Information availability","Illusion of control biasInformation availability","Self-attribution bias*Information availability"), to = "Herding behavior") )

Plot the model

plot(overriding_effects_sm)

Models with reflective constructs are automatically estimated using PLSc

pls_model <- estimate_pls(data = my_data, mobi_mm, overriding_effects_sm , missing = mean_replacement , missing_value = "-99" ) model_summary <- summary(pls_model) model_summary


Please help as the deadline is coming! Many thanks.

NicholasDanks commented 1 year ago

Dear @tomryjo

thanks for sharing your code. I simulated data by munging the mobi dataset and renaming the columns. Ignore this part. But the subsequent code works. Essentially you forgot to use the "*" in the interaction term names. I also abbreviated the names for sanity.

# ignore the following 
my_data <- cbind(mobi,mobi[,1:12])
colnames(my_data) <- c(multi_items("v1.", 1:7),
                    multi_items("v2.", 1:4),
                    multi_items("v3.", 1:9),
                    multi_items("v4.", 1:5),
                    multi_items("v5.", 1:5),
                    multi_items("v6.", 1:6))
# ignore to here

mobi_mm <- constructs(
  composite("OC", multi_items("v1.", 1:7)),
  composite("ICB", multi_items("v2.", 1:4)),
  composite("SAB", multi_items("v3.", 1:9)),
  composite("HB", multi_items("v4.", 1:5)),
  composite("IA", multi_items("v5.", 1:5)),
  composite("FL", multi_items("v6.", 1:6)),
  interaction_term(iv= "ICB", moderator = "IA",method = two_stage),
  interaction_term(iv= "SAB", moderator = "IA",method = two_stage),
  interaction_term(iv= "OC", moderator = "FL",method = two_stage)
)

overriding_effects_sm <- relationships(
  paths(from = c("OC","OC*FL", "FL"), to = "HB"),
  paths(from = c("ICB","SAB","IA","ICB*IA","SAB*IA"), to = "HB")
)

plot(overriding_effects_sm)

pls_model <- estimate_pls(data = my_data, 
                          mobi_mm, 
                          overriding_effects_sm, 
                          missing = mean_replacement, 
                          missing_value = "-99"
)
model_summary <- summary(pls_model)
model_summary
NicholasDanks commented 1 year ago

Please close the issue if resolved.