| | |
| | library(relaimpo) |
| | library(readxl) |
| | library(readr) |
| | library(lavaan) |
| | library(leaps) |
| | library(dplyr) |
| | library(tidyr) |
| |
|
| | |
| | log_message <- function(message, output_text_file) { |
| | cat(message, "\n") |
| | write(message, file = output_text_file, append = TRUE) |
| | } |
| |
|
| | |
| | trust_driver_analysis <- function(model_formula, data, output_text_file, csv_file) { |
| | tryCatch({ |
| | |
| | model <- lm(model_formula, data = data) |
| |
|
| | |
| | calc_relaimpo <- calc.relimp(model, type = "lmg", rela = TRUE) |
| | |
| | average_importance <- mean(calc_relaimpo$lmg) |
| |
|
| | |
| | file_conn <- file(output_text_file, open = "a") |
| | |
| | full_output <- capture.output({ |
| | print("Trust Driver Analysis:\n") |
| | print(calc_relaimpo) |
| | cat("\nAverage Importance: ", average_importance, "\n") |
| | }) |
| | |
| | writeLines(full_output, file_conn) |
| | close(file_conn) |
| |
|
| | |
| | results <- data.frame(Predictor = names(calc_relaimpo$lmg), Importance = calc_relaimpo$lmg) |
| |
|
| | |
| | write.csv(results, file = csv_file, row.names = FALSE) |
| | }, error = function(e) { |
| | log_message(paste("Error in trust_driver_analysis:", e$message), output_text_file) |
| | }) |
| | } |
| |
|
| | |
| | trust_builder_analysis <- function(data, data_headers, output_text_file, csv_file) { |
| | tryCatch({ |
| | |
| | question_to_column <- setNames(as.list(data_headers[1, ]), as.character(data_headers[2, ])) |
| |
|
| | |
| | p <- 6 |
| |
|
| | |
| | bucket_columns <- c("Stability", "Development", "Relationship", "Benefit", "Vision", "Competence") |
| |
|
| | |
| | bucket <- data %>% select(all_of(bucket_columns)) |
| |
|
| | |
| | TB <- data %>% select(contains("TB")) |
| |
|
| | |
| | num_tb_statements <- ncol(TB) |
| |
|
| | |
| | coef <- matrix(NA, ncol = 6, nrow = num_tb_statements) |
| |
|
| | |
| | bucket_predictors <- list() |
| |
|
| | |
| | for (i in 1:6) { |
| | |
| | y <- as.matrix(pull(bucket[, i])) |
| |
|
| | |
| | x <- as.matrix(TB) |
| |
|
| | |
| | fit <- regsubsets(x, y, nbest = 1, nvmax = p) |
| |
|
| | |
| | fit_sum <- summary(fit) |
| |
|
| | |
| | coef[, i] <- fit_sum$outmat[p, ] |
| |
|
| | |
| | predictors <- names(which(fit_sum$outmat[p, ] == "*")) |
| |
|
| | |
| | bucket_predictors[[bucket_columns[i]]] <- predictors |
| | } |
| |
|
| | |
| | model_str <- sapply(names(bucket_predictors), function(col) { |
| | paste(col, "~", paste(bucket_predictors[[col]], collapse = "+")) |
| | }) |
| |
|
| | |
| | model_str <- c("Trust ~ Stability + Development + Relationship + Benefit + Vision + Competence", model_str) |
| |
|
| | |
| | fit <- sem(model_str, data = data) |
| | fit_summary <- summary(fit, standardized = TRUE, fit.measures = TRUE, rsquare = TRUE) |
| |
|
| | |
| | output <- fit_summary$pe[fit_summary$pe$op == "~", c("lhs", "rhs", "std.all")] |
| |
|
| | |
| | convert_to_percentage <- function(df) { |
| | df %>% |
| | group_by(lhs) %>% |
| | mutate(abs_std = abs(std.all), |
| | sum_abs_std = sum(abs_std), |
| | percent_std = (abs_std / sum_abs_std) * 100) %>% |
| | select(-abs_std, -sum_abs_std) %>% |
| | ungroup() |
| | } |
| |
|
| | |
| | percentage_output <- convert_to_percentage(output) |
| |
|
| | |
| | tb_column_names <- colnames(TB) |
| |
|
| | |
| | percentage_output_wide <- percentage_output %>% |
| | pivot_wider(names_from = lhs, values_from = percent_std) %>% |
| | rename_with(~ gsub("std.all\\.", "", .), starts_with("std.all")) |
| |
|
| | |
| | result_df <- data.frame(TB = tb_column_names) |
| |
|
| | |
| | result_df <- left_join(result_df, percentage_output_wide, by = c("TB" = "rhs")) |
| |
|
| | |
| | result_df[is.na(result_df)] <- 0 |
| |
|
| | |
| | result_df$Message <- sapply(result_df$TB, function(tb_col) question_to_column[[tb_col]]) |
| |
|
| | |
| | result_df$TB <- factor(result_df$TB, levels = paste0("TB", 1:37)) |
| |
|
| | |
| | result_df <- result_df %>% |
| | select(-std.all, -Trust) %>% |
| | group_by(TB) %>% |
| | summarise(across(everything(), ~ if(is.numeric(.)) sum(., na.rm = TRUE) else first(.))) %>% |
| | arrange(TB) |
| |
|
| | |
| | result_df <- result_df %>% |
| | select(TB, Message, everything()) |
| |
|
| | |
| | file_conn <- file(output_text_file, open = "a") |
| |
|
| | |
| | full_output <- capture.output({ |
| | print("Trust Builder Analysis:\n") |
| | print("Data header mapping:\n") |
| | print(question_to_column) |
| | print("Buckets:\n") |
| | print(bucket) |
| | print("Messages:\n") |
| | print(TB) |
| | print("Coefficients matrix (coef:\n") |
| | print(coef) |
| | print("Model:\n") |
| | cat(model_str, sep = "\n") |
| | print("Fit summary:\n") |
| | print(fit_summary) |
| | print("Output:\n") |
| | print(output) |
| | print("Output in percentage (%):\n") |
| | print(percentage_output) |
| | print("result_df:\n") |
| | print(result_df) |
| | }) |
| | |
| | writeLines(full_output, file_conn) |
| | close(file_conn) |
| |
|
| | |
| | results <- data.frame(result_df) |
| |
|
| | |
| | write.csv(results, file = csv_file, row.names = FALSE) |
| | }, error = function(e) { |
| | log_message(paste("Error in trust_builder_analysis:", e$message), output_text_file) |
| | }) |
| | } |
| |
|
| | |
| | args <- commandArgs(trailingOnly = TRUE) |
| | input_file <- args[1] |
| | output_text_file <- args[2] |
| | csv_output_path_trust <- args[3] |
| | csv_output_path_nps <- args[4] |
| | csv_output_path_loyalty <- args[5] |
| | csv_output_path_consideration <- args[6] |
| | csv_output_path_satisfaction <- args[7] |
| | csv_output_path_trustbuilder <- args[8] |
| | nps_present <- as.logical(tolower(args[9])) |
| | loyalty_present <- as.logical(tolower(args[10])) |
| | consideration_present <- as.logical(tolower(args[11])) |
| | satisfaction_present <- as.logical(tolower(args[12])) |
| | trustbuilder_present <- as.logical(tolower(args[13])) |
| |
|
| | |
| | log_message("Starting Trust Driver and Builder Analysis Script.", output_text_file) |
| |
|
| | |
| |
|
| | |
| | data_driver <- NULL |
| | if (grepl(".xlsx", input_file)) { |
| | |
| | data_driver <- read_excel(input_file, sheet = "Driver", skip = 3) |
| | } |
| |
|
| | |
| | trust_driver_analysis( |
| | Trust ~ Stability + Development + Relationship + Benefit + Vision + Competence, |
| | data_driver, |
| | output_text_file, |
| | csv_output_path_trust) |
| |
|
| | |
| | if (nps_present) { |
| | trust_driver_analysis( |
| | NPS ~ Stability + Development + Relationship + Benefit + Vision + Competence, |
| | data_driver, |
| | output_text_file, |
| | csv_output_path_nps) |
| | } |
| |
|
| | |
| | if (loyalty_present) { |
| | trust_driver_analysis( |
| | Loyalty ~ Stability + Development + Relationship + Benefit + Vision + Competence, |
| | data_driver, |
| | output_text_file, |
| | csv_output_path_loyalty) |
| | } |
| |
|
| | |
| | if (consideration_present) { |
| | trust_driver_analysis( |
| | Consideration ~ Stability + Development + Relationship + Benefit + Vision + Competence, |
| | data_driver, |
| | output_text_file, |
| | csv_output_path_consideration) |
| | } |
| |
|
| | |
| | if (satisfaction_present) { |
| | trust_driver_analysis( |
| | Satisfaction ~ Stability + Development + Relationship + Benefit + Vision + Competence, |
| | data_driver, |
| | output_text_file, |
| | csv_output_path_satisfaction) |
| | } |
| |
|
| | |
| |
|
| | if (trustbuilder_present) { |
| | data_builder <- NULL |
| |
|
| | if (grepl(".xlsx", input_file)) { |
| | |
| | data_builder_headers <- read_excel(input_file, sheet = "Builder", skip = 3, n_max = 2) |
| | |
| | data_builder_rows <- read_excel(input_file, sheet = "Builder", skip = 5) |
| | } |
| |
|
| | |
| | trust_builder_analysis(data_builder_rows, data_builder_headers, output_text_file, csv_output_path_trustbuilder) |
| |
|
| | } |
| |
|
| | |
| | log_message("Trust Driver and Builder Analysis Script Completed.", output_text_file) |