Skip to content

Commit

Permalink
Updated demo Chap 3 and 8. Cleaned some plot code and corrected some …
Browse files Browse the repository at this point in the history
…bugs
  • Loading branch information
NicholasDanks committed Oct 28, 2020
1 parent 24b792d commit a537201
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 98 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ export(report_paths)
export(rho_A)
export(simplePLS)
export(single_item)
export(slope_analysis)
export(two_stage)
59 changes: 59 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,69 @@
#' \item{mga_2}{Multi Group Analysis 2.}
#' \item{mga_3}{Multi Group Analysis 3.}
#' \item{mga_4}{Multi Group Analysis 4.}
#' \item{switch_1}{It takes me a great deal of time to switch to another company.}
#' \item{switch_2}{It costs me too much to switch to another company.}
#' \item{switch_3}{It takes a lot of effort to get used to a new company with its specific "rules" and practices.}
#' \item{switch_4}{In general, it would be a hassle switching to another company.}
#' }
#'
#' @details The data frame mobi contains the observed data for the model specified by Corporate Reputation.
#' @references Hair, J. F., Hult, G. T. M., Ringle, C. M., and Sarstedt, M. (2017). A Primer on Partial Least Squares Structural Equation Modeling (2nd ed.). Thousand Oakes, CA: Sage.
#' @examples data("corp_rep_data")
#'
"corp_rep_data"

#' A Second Measurement Instrument for the Corporate Reputation Model
#'
#' The data set is used as measurement instrument for corporate reputation.
#'
#' @format A data frame with 347 rows and 49 variables:
#' \describe{
#' \item{servicetype}{A categorical variable for the service type: 1=Postpaid plan; 2=Prepaid plan.}
#' \item{serviceprovider}{A categorical variable for the service provider: 1, 2, 3, or 4.}
#' \item{cusa}{If you consider your experiences with "company", how satisfied are you with "company"?}
#' \item{cusl_1}{I would recommend "the company" to friends and relatives.}
#' \item{cusl_2}{If I had to choose again, I would choose "the company" as my mobile phone services provider.}
#' \item{cusl_3}{I will remain a customer of "the company" in the future.}
#' \item{qual_1}{The products/services offered by "the company" are of high quality.}
#' \item{qual_2}{"The company" is an innovator, rather than an imitator with respect to the mobile phone service industry.}
#' \item{qual_3}{"The company's" services/products offer good quality for money.}
#' \item{qual_4}{The services "the company" offers are good.}
#' \item{qual_5}{Customer concerns are held in high regard at "the company".}
#' \item{qual_6}{"The company" is a reliable partner for customers.}
#' \item{qual_7}{"The company" is a trustworthy company.}
#' \item{qual_8}{I have a lot of respect for "the company".}
#' \item{perf_1}{"The company" is a very well managed company.}
#' \item{perf_2}{"The company" is an economically stable company.}
#' \item{perf_3}{The business risk of "the company" is reasonable compared to its competitors.}
#' \item{perf_4}{The growth of "the company" is promising.}
#' \item{perf_5}{"The company" has a clear vision about the future of the company.}
#' \item{csor_1}{"The company" behaves in a socially conscious way.}
#' \item{csor_2}{"The company" is honest in giving information to the public.}
#' \item{csor_3}{"The company" competes fairly in the indsutry.}
#' \item{csor_4}{"The company" cares for the preservation of the environment.}
#' \item{csor_5}{"The company" is doing more than just making profits.}
#' \item{attr_1}{"The company" is succesful in attracting high-quality employees.}
#' \item{attr_2}{I could see myself working at "the company".}
#' \item{attr_3}{I like the physical appearance of "the company" (company/buildings/shops, etc.).}
#' \item{comp_1}{"The company" is a top competitor in its market.}
#' \item{comp_2}{As far as I know, "the company" is recognized worldwide.}
#' \item{comp_3}{I believe that "the company" performs at a premium level.}
#' \item{like_1}{"The company" is a company that I can better identify with than other companies.}
#' \item{like_2}{When comparing with other companies, "The company" is the company I would regret more if it no longer existed.}
#' \item{like_3}{I regard "the company" as a likeable company.}
#' \item{qual_global}{Please assess the general quality of "the company".}
#' \item{perf_global}{Please assess the general performance of "the company".}
#' \item{csor_global}{Please assess the extent to which "the company" acts in socially conscious ways.}
#' \item{attr_global}{Please assess the attractiveness of "the company".}
#' \item{switch_1}{It takes me a great deal of time to switch to another mobile phone services provider.}
#' \item{switch_2}{It costs me too much to switch to another mobile phone services provider.}
#' \item{switch_3}{It takes a lot of effort to get used to a new mobile phone services provider with its specific "rules" and practices.}
#' \item{switch_4}{In general, it would be a hassle switching to another mobile phone services provider.}
#' }
#'
#' @details The data frame mobi contains the observed data for the model specified by Corporate Reputation.
#' @references Sarstedt, M., Hair Jr, J. F., Cheah, J. H., Becker, J. M., & Ringle, C. M. (2019). How to specify, estimate, and validate higher-order constructs in PLS-SEM. Australasian Marketing Journal (AMJ), 27(3), 197-211.
#' @examples data("corp_rep_data2")
#'
"corp_rep_data2"
3 changes: 2 additions & 1 deletion R/evaluate_measurement_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ reliability <- function(seminr_model) {
mat2 <- rho_A(seminr_model)
table <- cbind(alpha, mat1, mat2)
comment(table) <- "Alpha, rhoC, and rhoA should exceed 0.7 while AVE should exceed 0.5"
convert_to_table_output(table)
class(table) <- append(class(table), c("table_output","reliability_table"))
return(table)
}

# Validity ----
Expand Down
30 changes: 29 additions & 1 deletion R/plot_results.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plot.reliability_table <- function(x, ...) {
stopifnot(inherits(x, "reliability_table"))

metrics <- cbind(1:nrow(x), x)
plot(metrics[,1:2], xlim=c(1, nrow(metrics[,-1])), ylim=c(min(metrics[,-1] - 0.1), max(metrics[,-1])),
plot(metrics[,1:2], xlim=c(0.7, nrow(metrics[,-1])+0.3), ylim=c(min(metrics[,-1] - 0.1), max(metrics[,-1])),
frame.plot = FALSE, xaxt='n', ylab='', xlab = '', pch='')

# Grid
Expand Down Expand Up @@ -52,3 +52,31 @@ plot.reliability_table <- function(x, ...) {

invisible(x)
}

#' @export
slope_analysis <- function(moderated_model, dv, moderator, iv, leg_place = "bottomright") {
mat <- matrix(c(-1, 1, -1,
-1, 0, 0,
-1,-1, 1,
0, 0, -1,
0, 0, 0,
0, 0, 1,
1,-1, -1,
1, 0, 0,
1, 1, 1), nrow = 9, ncol = 3, byrow = TRUE)
res <- mat %*% moderated_model$path_coef[c(iv,paste(iv,"*",moderator, sep = ""),moderator),dv]

plot(c(-1,0,1), res[c(1,4,7)], type="n", xlab = iv, ylab = dv,
xlim = c(-1,1), ylim = c(min(res),max(res)))
lines(c(-1,0,1), res[c(1,4,7)], lty = 2)
lines(c(-1,0,1), res[c(2,5,8)], lty = 1)
lines(c(-1,0,1), res[c(3,6,9)], lty = 3)
grid()
legend(leg_place, c("Mod at -1SD", "Mod at Mean", "Mod at 1SD"), lty=c(2,1,3),
horiz=FALSE, bty="n", cex = 0.8
)
}




Binary file modified data/corp_rep_data.rda
Binary file not shown.
Binary file added data/corp_rep_data2.rda
Binary file not shown.
2 changes: 2 additions & 0 deletions demo/00Index
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ seminr-pls-interaction A demonstration of modeling an interaction using SEMinR
seminr-alternative-models A demonstration of alternate model specification, quick and easy
seminr-pls-higher_order A demonstration of modeling a higher order construct in SEMinR syntax
seminr-cbsem-cfa-ecsi A demonstration of the CBSEM estimation of the ECSI model using SEMinR syntax
seminr-primer-chap3 A demonstration of the code used in Chapter 3 of the PLS Primer for R Workbook
seminr-primer-chap4 A demonstration of the code used in Chapter 4 of the PLS Primer for R Workbook
seminr-primer-chap5 A demonstration of the code used in Chapter 5 of the PLS Primer for R Workbook
seminr-primer-chap6 A demonstration of the code used in Chapter 6 of the PLS Primer for R Workbook
seminr-primer-chap7 A demonstration of the code used in Chapter 7 of the PLS Primer for R Workbook
seminr-primer-chap8 A demonstration of the code used in Chapter 8 of the PLS Primer for R Workbook
63 changes: 63 additions & 0 deletions demo/seminr-primer-chap3.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
### Accompanying Code for the PLS Primer in R Workbook
## Chapter 3: Introduction to SEMinR

# Load the SEMinR library
library(seminr)

# Inspect the head of corp_rep_data dataset ----
head(corp_rep_data)

# Create measurement model ----
simple_mm <- constructs(
composite("COMP", multi_items("comp_", 1:3), weights = mode_A),
composite("CSOR", multi_items("csor_", 1:5), weights = mode_B),
composite("ATTR", multi_items("attr_", 1:3), weights = mode_B)
)

# Create structural model ----
simple_sm <- relationships(
paths(from = c("CSOR", "ATTR"), to = "COMP")
)

# Estimate the model
corp_rep_simple_model <- estimate_pls(data = corp_rep_data,
measurement_model = simple_mm,
structural_model = simple_sm,
inner_weights = path_weighting,
missing = mean_replacement,
missing_value = "-99")

# Estimate the model with default settings
corp_rep_simple_model <- estimate_pls(data = corp_rep_data,
measurement_model = simple_mm,
structural_model = simple_sm)

# Summarize the model results
summary_simple_corp_rep <- summary(corp_rep_simple_model)

# Inspect the structural paths
summary_simple_corp_rep$paths

# Inspect the construct reliability metrics
summary_simple_corp_rep$reliability

# Bootstrap the model
boot_simple_corp_rep <- bootstrap_model(seminr_model = corp_rep_simple_model,
nboot = 1000,
cores = NULL,
seed = 123)

# Store the summary of the bootstrapped model
sum_boot_simple_corp_rep <- summary(boot_simple_corp_rep)

# Inspect the bootstrapped structural paths
sum_boot_simple_corp_rep$bootstrapped_paths

# Inspect the bootstrapped outer weights
sum_boot_simple_corp_rep$bootstrapped_weights

# Write the bootstrapped paths object to csv file
write.csv(x = sum_boot_simple_corp_rep$bootstrapped_weights, file = "boot_paths.csv")

# Generate the plot for exporting
plot(summary_simple_corp_rep$reliability)
108 changes: 12 additions & 96 deletions demo/seminr-primer-chap7.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,104 +42,20 @@ boot_corp_rep_ext <- bootstrap_model(seminr_model = corp_rep_pls_model_ext,
summary_boot_corp_rep_ext <- summary(boot_corp_rep_ext,
alpha = 0.05)

# Inspect the structural model collinearity VIF
summary_corp_rep_ext$vif_antecedents
# Inspect indirect effects
summary_corp_rep_ext$total_indirect_effects

# Inspect the structural paths
summary_boot_corp_rep_ext$bootstrapped_paths

# Inspect the total effects
summary_boot_corp_rep_ext$bootstrapped_total_paths
# Obtain confidence intervals for indirect effects
total_indirect_ci(boot_corp_rep_ext, from = "COMP", to = "CUSL", alpha = 0.05)
total_indirect_ci(boot_corp_rep_ext, from = "LIKE", to = "CUSL", alpha = 0.05)

# Inspect the model RSquares
# Inspect the direct effects
summary_corp_rep_ext$paths

# Inspect the effect sizes
summary_corp_rep_ext$fSquare

# Generate the model predictions
predict_corp_rep_ext <- predict_pls(
model = corp_rep_pls_model_ext,
technique = predict_DA,
noFolds = 10,
reps = 10)

# Summarize the prediction results
sum_predict_corp_rep_ext <- summary(predict_corp_rep_ext)

# Analyze the distribution of prediction error
par(mfrow=c(1,3))
plot(sum_predict_corp_rep_ext, indicator = "cusl_1")
plot(sum_predict_corp_rep_ext, indicator = "cusl_2")
plot(sum_predict_corp_rep_ext, indicator = "cusl_3")

# Inspect the results of PLSpredict
sum_predict_corp_rep_ext

# Estimate alternative models
# Create measurement model ----
measurement_model <- constructs(
composite("QUAL", multi_items("qual_", 1:8), weights = mode_B),
composite("PERF", multi_items("perf_", 1:5), weights = mode_B),
composite("CSOR", multi_items("csor_", 1:5), weights = mode_B),
composite("ATTR", multi_items("attr_", 1:3), weights = mode_B),
composite("COMP", multi_items("comp_", 1:3)),
composite("LIKE", multi_items("like_", 1:3)),
composite("CUSA", single_item("cusa")),
composite("CUSL", multi_items("cusl_", 1:3))
)

# Create structural models
# Model 1
structural_model1 <- relationships(
paths(from = c("QUAL","PERF","CSOR","ATTR"), to = c("COMP", "LIKE")),
paths(from = c("COMP","LIKE"), to = c("CUSA", "CUSL")),
paths(from = "CUSA", to = c("CUSL"))
)
# Model 2
structural_model2 <- relationships(
paths(from = c("QUAL","PERF","CSOR","ATTR"), to = c("COMP", "LIKE")),
paths(from = c("COMP","LIKE"), to = c("CUSA")),
paths(from = c("LIKE"), to = c("CUSL")),
paths(from = "CUSA", to = c("CUSL"))
)
# Model 3
structural_model3 <- relationships(
paths(from = c("QUAL","PERF","CSOR","ATTR"), to = c("COMP", "LIKE")),
paths(from = c("COMP","LIKE"), to = c("CUSA")),
paths(from = "CUSA", to = c("CUSL"))
)

# Estimate and summarize the models ----
pls_model1 <- estimate_pls(data = corp_rep_data,
measurement_model = measurement_model,
structural_model = structural_model1)
sum_model1 <- summary(pls_model1)
pls_model2 <- estimate_pls(data = corp_rep_data,
measurement_model = measurement_model,
structural_model = structural_model2)
sum_model2 <- summary(pls_model2)
pls_model3 <- estimate_pls(data = corp_rep_data,
measurement_model = measurement_model,
structural_model = structural_model3)
sum_model3 <- summary(pls_model3)

# Inspect the IT Criteria matrix of Model1
sum_model1$it_criteria

# Subset the matrix to only return the BIC row and CUSL column
sum_model1$it_criteria["BIC", "CUSL"]

# Collect the vector of BIC values for CUSL
itcriteria_vector <- c(sum_model1$it_criteria["BIC","CUSL"],
sum_model2$it_criteria["BIC","CUSL"],
sum_model3$it_criteria["BIC","CUSL"])

# Assign the model names to IT Criteria vector
names(itcriteria_vector) <- c("Model1", "Model2", "Model3")

# Inspect the IT Criteria vector for competing models.
itcriteria_vector
# Inspect the confidence intervals for direct effects
summary_boot_corp_rep_ext$bootstrapped_paths

# Calculate the model BIC Akaike Weights
compute_itcriteria_weights(itcriteria_vector)
# Calculate the sign of p1*p2*p3
summary_corp_rep_ext$paths["LIKE", "CUSL"] *
summary_corp_rep_ext$paths["LIKE","CUSA"] *
summary_corp_rep_ext$paths["CUSA","CUSL"]
53 changes: 53 additions & 0 deletions demo/seminr-primer-chap8.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
library(seminr)

# Load the data
corp_rep_data <- corp_rep_data

# Create measurement model ----
corp_rep_mm_mod <- constructs(
composite("QUAL", multi_items("qual_", 1:8), weights = mode_B),
composite("PERF", multi_items("perf_", 1:5), weights = mode_B),
composite("CSOR", multi_items("csor_", 1:5), weights = mode_B),
composite("ATTR", multi_items("attr_", 1:3), weights = mode_B),
composite("COMP", multi_items("comp_", 1:3)),
composite("LIKE", multi_items("like_", 1:3)),
composite("CUSA", single_item("cusa")),
composite("SC", multi_items("switch_", 1:4)),
composite("CUSL", multi_items("cusl_", 1:3)),
interaction_term(iv = "CUSA", moderator = "SC", method = two_stage, weights = mode_A)
)

# Create structural model ----
corp_rep_sm_mod <- relationships(
paths(from = c("QUAL", "PERF", "CSOR", "ATTR"), to = c("COMP", "LIKE")),
paths(from = c("COMP", "LIKE"), to = c("CUSA", "CUSL")),
paths(from = c("CUSA", "SC", "CUSA*SC"), to = c("CUSL"))
)

# Estimate the model ----
corp_rep_pls_model_mod <- estimate_pls(data = corp_rep_data,
measurement_model = corp_rep_mm_mod,
structural_model = corp_rep_sm_mod,
missing = mean_replacement,
missing_value = "-99")

# Extract the summary
sum_corp_rep_mod <- summary(corp_rep_pls_model_mod)

# Bootstrap the model ----
boot_corp_rep_mod <- bootstrap_model(seminr_model = corp_rep_pls_model_mod,
nboot = 10000)

# Summarize the results of the bootstrap
sum_boot_corp_rep_mod <- summary(boot_corp_rep_mod,
alpha = 0.05)

# Inspect the bootstrapped structural paths
sum_boot_corp_rep_mod$bootstrapped_paths

# Simple slope analysis plot
slope_analysis(moderated_model = corp_rep_pls_model_mod,
dv = "CUSL",
moderator = "SC",
iv = "CUSA",
leg_place = "bottomright")
4 changes: 4 additions & 0 deletions man/corp_rep_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a537201

Please sign in to comment.