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

Getting an error amid attempting to run 'plot' command #302

Closed sergey-kazakov closed 1 year ago

sergey-kazakov commented 1 year ago

Dear Developer Team.

Thank you very much for developing seminr package, I find it very practical solution for SEM PLS modelling.

I've updated to Rstudio most recently 2022.07.1 Build 554 and R 4.2.1 and in my most recent project I notoriously receive an error code when trying to get a pic of the model as follows:

Error in model$outer_loadings[indicator, construct] : subscript out of bounds

This error occurs on estimated normal and bootstrapped models in en effort to visualise them. Both measurement and structural models are drawn fine when using the 'plot' command.

I appreciate your advice on the matter. Sincerely, Sergio

soumyaray commented 1 year ago

Hi @sergey-kazakov, could you give us an idea of what your model looks like? does it have 2nd order constructs? ideally, it would be nice to have your measurement and structural model code.

sergey-kazakov commented 1 year ago

Dear Soumya, Thank you for your response. I'm attaching the script below for your convenience. All script runs fine except for the plotting. Regards, Sergio.

library(lavaan) library(readxl) library(seminr) library(semPlot)

data_IMC = read_xlsx('/Users/skazakov/Documents/!Научная Работа/Мои публикации/2022/JEFAS-Vera-Olga/Omni22-STATA_SEM_data_only.xlsx')

mm_IMC = constructs(

sm_IMC = relationships(

IMCmod_est = estimate_pls(

boot_ICMmod_estimate = bootstrap_model(seminr_model = IMCmod_est, +nboot = 2000, +cores = NULL, +seed = 1,2,3) summary_boot_ICMmod_estimate = summary(boot_ICMmod_estimate) summary_boot_ICMmod_estimate$bootstrapped_paths ummary_boot_ICMmod_estimate$bootstrapped_loadings summary_boot_ICMmod_estimate$bootstrapped_weights

summary_ICMmod_estimate$total_indirect_effects summary_ICMmod_estimate$total_effects

write.csv(x = summary_ICMmod_estimate$total_effects, file = "IMC_total_effects.csv") write.csv(x = summary_ICMmod_estimate$total_indirect_effects, file = "IMC_indirect_effects.csv") write.csv(x = summary_boot_ICMmod_estimate$bootstrapped_loadings, file = "IMC_bootstrapped_loadings.csv") write.csv(x = summary_boot_ICMmod_estimate$bootstrapped_paths, file = "IMC_bootstrapped_paths.csv") write.csv(x = summary_ICMmod_estimate$reliability, file = "IMC_reliability.csv") write.csv(x = summary_ICMmod_estimate$paths, file = "IMC_path_estimates.csv")

plot(boot_ICMmod_estimate) Error in model$outer_loadings[indicator, construct] : subscript out of bounds

NicholasDanks commented 1 year ago

Dear Sergey-kazakov,

I hope you are well. I have the following code from your comment:

library(readxl)
library(seminr)
library(semPlot)

var_names <- c(multi_items("IMC", 1:7),
               multi_items("IMC", 8:12),
               multi_items("IMC", 13:16),
               multi_items("PS", 1:4),
               multi_items("SR", 1:5),
               multi_items("RI", 1:3),
               multi_items("CD", 1:6))
data_IMC = cbind(mobi,mobi[,1:10])
colnames(data_IMC) <- var_names
mm_IMC = constructs(
  composite("imc_co", multi_items("IMC", 1:7)),
  composite("imc_i", multi_items("IMC", 8:12)),
  composite("imc_om", multi_items("IMC", 13:16)),
  composite("ps", multi_items("PS", 1:4)),
  composite("sr", multi_items("SR", 1:5)),
  composite("ri", multi_items("RI", 1:3)),
  composite("cd", multi_items("CD", 1:6))
)
sm_IMC = relationships(

  paths(from = c("imc_co"), to = c("ps")),
  paths(from = c("imc_co"), to = c("sr")),
  paths(from = c("imc_om"), to = c("ps")),
  paths(from = c("imc_om"), to = c("sr")),
  paths(from = c("imc_i"), to = c("ps")),
  paths(from = c("imc_i"), to = c("sr")),
  paths(from = c("ps"), to = c("ri")),
  paths(from = c("sr"), to = c("ri"))
)
IMCmod_est = estimate_pls(
  data = data_IMC,
  measurement_model = mm_IMC,
  structural_model = sm_IMC,
  inner_weights = path_weighting,
  missing = mean_replacement,
  missing_value = NA
)
summary_ICMmod_estimate = summary(IMCmod_est)
summary_ICMmod_estimate$paths
summary_ICMmod_estimate$reliability

boot_ICMmod_estimate = bootstrap_model(seminr_model = IMCmod_est,
                                       nboot = 200,
                                       cores = NULL,
                                       seed = 123)
summary_boot_ICMmod_estimate = summary(boot_ICMmod_estimate)
summary_boot_ICMmod_estimate$bootstrapped_paths
summary_boot_ICMmod_estimate$bootstrapped_loadings
summary_boot_ICMmod_estimate$bootstrapped_weights

summary_ICMmod_estimate$total_indirect_effects
summary_ICMmod_estimate$total_effects

plot(boot_ICMmod_estimate)

For me this code has no problem to run. If you are still having trouble, you can try to install the latest bugfix version by using the following code:

devtools::install_github(repo = "https://github.com/sem-in-r/seminr", ref = "bugfixes_aug_2022")

Please let us know if this addresses your issue, and close the issue if it is now resolved.

Many thanks! Nick

NicholasDanks commented 1 year ago

@sergey-kazakov is this issue resolved?

sergey-kazakov commented 1 year ago

Hi Nicolas,

My apologies, since I’ve thought I already had posted my thank you message on gt.  

Thank you so much, this line:

devtools::install_github(repo = "https://github.com/sem-in-r/seminr", ref = "bugfixes_aug_2022")

made a trick. 

Please accept my gigantic gratitude for creating and supporting such an amazingly powerful and practical tool. 

Best Regards, Sergey 

Sent with Unibox

On Sep 22, 2022, at 4:23 PM, Nicholas Danks @.***> wrote:

@sergey-kazakov is this issue resolved?

— Reply to this email directly, view it on GitHub , or unsubscribe . You are receiving this because you were mentioned.

sergey-kazakov commented 1 year ago

Hi Nick! I’ve installed the bug fix as you recommended and it worked quite well for me.  Thank you so much for your help and ongoing support! Have a lovely week.  Best Regards, Sergey

Sent with Unibox

On Aug 22, 2022, at 5:19 PM, Nicholas Danks @.***> wrote:

Dear Sergey-kazakov,

I hope you are well. I have the following code from your comment:

library(readxl) library(seminr) library(semPlot)

var_names <- c(multi_items("IMC", 1:7), multi_items("IMC", 8:12), multi_items("IMC", 13:16), multi_items("PS", 1:4), multi_items("SR", 1:5), multi_items("RI", 1:3), multi_items("CD", 1:6)) data_IMC = cbind(mobi,mobi[,1:10]) colnames(data_IMC) <- var_names mm_IMC = constructs( composite("imc_co", multi_items("IMC", 1:7)), composite("imc_i", multi_items("IMC", 8:12)), composite("imc_om", multi_items("IMC", 13:16)), composite("ps", multi_items("PS", 1:4)), composite("sr", multi_items("SR", 1:5)), composite("ri", multi_items("RI", 1:3)), composite("cd", multi_items("CD", 1:6)) ) sm_IMC = relationships(

paths(from = c("imc_co"), to = c("ps")), paths(from = c("imc_co"), to = c("sr")), paths(from = c("imc_om"), to = c("ps")), paths(from = c("imc_om"), to = c("sr")), paths(from = c("imc_i"), to = c("ps")), paths(from = c("imc_i"), to = c("sr")), paths(from = c("ps"), to = c("ri")), paths(from = c("sr"), to = c("ri")) ) IMCmod_est = estimate_pls( data = data_IMC, measurement_model = mm_IMC, structural_model = sm_IMC, inner_weights = path_weighting, missing = mean_replacement, missing_value = NA ) summary_ICMmod_estimate = summary(IMCmod_est) summary_ICMmod_estimate$paths summary_ICMmod_estimate$reliability

boot_ICMmod_estimate = bootstrap_model(seminr_model = IMCmod_est, nboot = 200, cores = NULL, seed = 123) summary_boot_ICMmod_estimate = summary(boot_ICMmod_estimate) summary_boot_ICMmod_estimate$bootstrapped_paths summary_boot_ICMmod_estimate$bootstrapped_loadings summary_boot_ICMmod_estimate$bootstrapped_weights

summary_ICMmod_estimate$total_indirect_effects summary_ICMmod_estimate$total_effects

plot(boot_ICMmod_estimate)

For me this code has no problem to run. If you are still having trouble, you can try to install the latest bugfix version by using the following code:

devtools::install_github(repo = "https://github.com/sem-in-r/seminr", ref = "bugfixes_aug_2022")

Please let us know if this addresses your issue, and close the issue if it is now resolved.

Many thanks! Nick

— Reply to this email directly, view it on GitHub , or unsubscribe . You are receiving this because you were mentioned.