Wajahat698 commited on
Commit
efacef2
·
verified ·
1 Parent(s): 3f202d2

Update process_data.R

Browse files
Files changed (1) hide show
  1. process_data.R +21 -20
process_data.R CHANGED
@@ -154,25 +154,26 @@ if (grepl("\\.xlsx$", input_file)) {
154
  } else if (grepl("\\.csv$", input_file)) {
155
  data_driver <- read.csv(input_file, stringsAsFactors = FALSE)
156
  }
 
 
 
157
 
158
- # Trust model
159
- trust_driver_analysis(
160
- Trust ~ Stability + Development + Relationship + Benefit + Vision + Competence,
161
- data_driver,
162
- output_text_file,
163
- csv_output_path_trust)
164
-
165
- # Conditional models
166
- if (nps_present) trust_driver_analysis(NPS ~ Stability + Development + Relationship + Benefit + Vision + Competence, data_driver, output_text_file, csv_output_path_nps)
167
- if (loyalty_present) trust_driver_analysis(Loyalty ~ Stability + Development + Relationship + Benefit + Vision + Competence, data_driver, output_text_file, csv_output_path_loyalty)
168
- if (consideration_present) trust_driver_analysis(Consideration ~ Stability + Development + Relationship + Benefit + Vision + Competence, data_driver, output_text_file, csv_output_path_consideration)
169
- if (satisfaction_present) trust_driver_analysis(Satisfaction ~ Stability + Development + Relationship + Benefit + Vision + Competence, data_driver, output_text_file, csv_output_path_satisfaction)
170
-
171
- # TrustBuilder only for Excel files
172
- if (trustbuilder_present && grepl("\\.xlsx$", input_file)) {
173
- data_builder_headers <- read_excel(input_file, sheet = "Builder", skip = 3, n_max = 2)
174
- data_builder_rows <- read_excel(input_file, sheet = "Builder", skip = 5)
175
- trust_builder_analysis(data_builder_rows, data_builder_headers, output_text_file, csv_output_path_trustbuilder)
176
- }
177
 
178
- log_message("Trust Driver and Builder Analysis Script Completed.", output_text_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  } else if (grepl("\\.csv$", input_file)) {
155
  data_driver <- read.csv(input_file, stringsAsFactors = FALSE)
156
  }
157
+ trust_driver_analysis <- function(model_formula, data, output_text_file, csv_file) {
158
+ tryCatch({
159
+ data <- data[complete.cases(data), ] # ✅ <---- ADD THIS LINE HERE
160
 
161
+ model <- lm(model_formula, data = data)
162
+ calc_relaimpo <- calc.relimp(model, type = "lmg", rela = TRUE)
163
+ average_importance <- mean(calc_relaimpo$lmg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
 
165
+ file_conn <- file(output_text_file, open = "a")
166
+ full_output <- capture.output({
167
+ print("Trust Driver Analysis:\n")
168
+ print(calc_relaimpo)
169
+ cat("\nAverage Importance: ", average_importance, "\n")
170
+ })
171
+ writeLines(full_output, file_conn)
172
+ close(file_conn)
173
+
174
+ results <- data.frame(Predictor = names(calc_relaimpo$lmg), Importance = calc_relaimpo$lmg)
175
+ write.csv(results, file = csv_file, row.names = FALSE)
176
+ }, error = function(e) {
177
+ log_message(paste("Error in trust_driver_analysis:", e$message), output_text_file)
178
+ })
179
+ }