-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_final_validation.R
More file actions
263 lines (226 loc) · 8.91 KB
/
Copy pathtest_final_validation.R
File metadata and controls
263 lines (226 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env Rscript
# Final validation - bulletproof test like a real user
cat("\n")
cat(paste(rep("=", 70), collapse=""), "\n")
cat("FINAL VALIDATION TEST - Real User Perspective\n")
cat(paste(rep("=", 70), collapse=""), "\n\n")
setwd("/home/user/MNLNP")
source("R/generate_data.R")
source("R/fit_mnp_safe.R")
source("R/recommend_model.R")
test_results <- list()
# ===========================================================================
# CORE TEST 1: The #1 function users will call - recommend_model()
# ===========================================================================
cat("TEST 1: recommend_model() - Main user entry point\n")
cat(paste(rep("-", 70), collapse=""), "\n\n")
cat("Scenario: Researcher has 300 observations, asks 'Which model?'\n\n")
result <- tryCatch({
rec <- recommend_model(n = 300, verbose = TRUE)
list(success = TRUE, recommendation = rec$recommendation,
confidence = rec$confidence,
mnp_convergence = rec$expected_mnp_convergence)
}, error = function(e) {
list(success = FALSE, error = conditionMessage(e))
})
if (result$success) {
cat("\n✓ Function executed successfully\n")
cat(" Recommendation:", result$recommendation, "\n")
cat(" Confidence:", result$confidence, "\n")
cat(" Expected MNP convergence:", round(result$mnp_convergence * 100, 1), "%\n")
test_results$recommend_model <- TRUE
} else {
cat("\n✗ Function failed:", result$error, "\n")
test_results$recommend_model <- FALSE
}
cat("\nTest 1:", ifelse(test_results$recommend_model, "✓ PASSED", "✗ FAILED"), "\n\n")
# ===========================================================================
# CORE TEST 2: Generate data - what every user needs
# ===========================================================================
cat("TEST 2: generate_choice_data() - Data generation\n")
cat(paste(rep("-", 70), collapse=""), "\n\n")
cat("Generating 300 observations for testing...\n")
result2 <- tryCatch({
set.seed(2024)
data <- generate_choice_data(
n = 300,
n_alternatives = 3,
n_vars = 2,
correlation = 0.2,
effect_size = 0.5,
functional_form = "linear"
)
list(success = TRUE, n = nrow(data$data),
outcome_levels = length(levels(data$data$choice)))
}, error = function(e) {
list(success = FALSE, error = conditionMessage(e))
})
if (result2$success) {
cat("✓ Data generated successfully\n")
cat(" Observations:", result2$n, "\n")
cat(" Alternatives:", result2$outcome_levels, "\n")
test_results$generate_data <- TRUE
test_data <- generate_choice_data(n = 300, n_alternatives = 3, n_vars = 2,
correlation = 0.2, effect_size = 0.5,
functional_form = "linear")
} else {
cat("✗ Data generation failed:", result2$error, "\n")
test_results$generate_data <- FALSE
}
cat("\nTest 2:", ifelse(test_results$generate_data, "✓ PASSED", "✗ FAILED"), "\n\n")
# ===========================================================================
# CORE TEST 3: Fit MNL - most common use case
# ===========================================================================
cat("TEST 3: Fit MNL model (with nnet)\n")
cat(paste(rep("-", 70), collapse=""), "\n\n")
if (test_results$generate_data && requireNamespace("nnet", quietly = TRUE)) {
cat("Fitting MNL on generated data...\n")
result3 <- tryCatch({
mnl_fit <- nnet::multinom(choice ~ x1 + x2, data = test_data$data, trace = FALSE)
list(success = TRUE, aic = AIC(mnl_fit), n_coef = length(coef(mnl_fit)))
}, error = function(e) {
list(success = FALSE, error = conditionMessage(e))
})
if (result3$success) {
cat("✓ MNL fitted successfully\n")
cat(" AIC:", round(result3$aic, 2), "\n")
cat(" Coefficients:", result3$n_coef, "\n")
test_results$fit_mnl <- TRUE
} else {
cat("✗ MNL fitting failed:", result3$error, "\n")
test_results$fit_mnl <- FALSE
}
} else {
cat("⊘ Test skipped (nnet not available or data generation failed)\n")
test_results$fit_mnl <- NA
}
cat("\nTest 3:", ifelse(isTRUE(test_results$fit_mnl), "✓ PASSED",
ifelse(is.na(test_results$fit_mnl), "⊘ SKIPPED", "✗ FAILED")), "\n\n")
# ===========================================================================
# CORE TEST 4: Fit MNP with safe wrapper
# ===========================================================================
cat("TEST 4: fit_mnp_safe() - Safe MNP fitting with fallback\n")
cat(paste(rep("-", 70), collapse=""), "\n\n")
if (test_results$generate_data) {
cat("Fitting MNP (or falling back to MNL if fails)...\n")
result4 <- tryCatch({
fit <- fit_mnp_safe(
choice ~ x1 + x2,
data = test_data$data,
fallback = "MNL",
verbose = FALSE,
n.draws = 1000,
burnin = 200
)
list(success = TRUE,
model_type = class(fit)[1],
converged = inherits(fit, "mnp"))
}, error = function(e) {
list(success = FALSE, error = conditionMessage(e))
})
if (result4$success) {
cat("✓ Function executed successfully\n")
if (result4$converged) {
cat(" MNP converged! (Model type:", result4$model_type, ")\n")
} else {
cat(" MNP failed, fell back to MNL (Model type:", result4$model_type, ")\n")
cat(" This is CORRECT behavior - safe wrapper working!\n")
}
test_results$fit_mnp_safe <- TRUE
} else {
cat("✗ Function failed:", result4$error, "\n")
test_results$fit_mnp_safe <- FALSE
}
} else {
cat("⊘ Test skipped (data generation failed)\n")
test_results$fit_mnp_safe <- NA
}
cat("\nTest 4:", ifelse(isTRUE(test_results$fit_mnp_safe), "✓ PASSED",
ifelse(is.na(test_results$fit_mnp_safe), "⊘ SKIPPED", "✗ FAILED")), "\n\n")
# ===========================================================================
# CORE TEST 5: MNP package is actually available
# ===========================================================================
cat("TEST 5: MNP package availability\n")
cat(paste(rep("-", 70), collapse=""), "\n\n")
result5 <- tryCatch({
if (requireNamespace("MNP", quietly = TRUE)) {
library(MNP)
list(success = TRUE, installed = TRUE,
version = as.character(packageVersion("MNP")),
functions_available = "mnp" %in% ls("package:MNP"))
} else {
list(success = TRUE, installed = FALSE)
}
}, error = function(e) {
list(success = FALSE, error = conditionMessage(e))
})
if (result5$success && result5$installed) {
cat("✓ MNP package is INSTALLED\n")
cat(" Version:", result5$version, "\n")
cat(" mnp() function available:", result5$functions_available, "\n")
test_results$mnp_available <- TRUE
} else if (result5$success && !result5$installed) {
cat("⚠ MNP package NOT installed\n")
cat(" Package will work in MNL-only mode\n")
test_results$mnp_available <- FALSE
} else {
cat("✗ Error checking MNP:", result5$error, "\n")
test_results$mnp_available <- FALSE
}
cat("\nTest 5:", ifelse(test_results$mnp_available, "✓ PASSED", "⚠ NOT INSTALLED"), "\n\n")
# ===========================================================================
# OVERALL SUMMARY
# ===========================================================================
cat(paste(rep("=", 70), collapse=""), "\n")
cat("FINAL VALIDATION SUMMARY\n")
cat(paste(rep("=", 70), collapse=""), "\n\n")
cat("Core Functions Tested:\n\n")
tests <- c(
"1. recommend_model()" = test_results$recommend_model,
"2. generate_choice_data()" = test_results$generate_data,
"3. MNL fitting (nnet)" = test_results$fit_mnl,
"4. fit_mnp_safe()" = test_results$fit_mnp_safe,
"5. MNP availability" = test_results$mnp_available
)
for (i in seq_along(tests)) {
status <- if (isTRUE(tests[i])) {
"✓ PASSED"
} else if (is.na(tests[i])) {
"⊘ SKIPPED"
} else {
"✗ FAILED"
}
cat(sprintf(" %-35s %s\n", names(tests)[i], status))
}
# Calculate pass rate (excluding NAs)
valid_tests <- !is.na(tests)
if (sum(valid_tests) > 0) {
pass_rate <- sum(tests[valid_tests], na.rm = TRUE) / sum(valid_tests) * 100
cat("\n")
cat(sprintf("Pass Rate: %.0f%% (%d/%d tests)\n",
pass_rate,
sum(tests, na.rm = TRUE),
sum(valid_tests)))
}
cat("\n")
cat(paste(rep("=", 70), collapse=""), "\n")
if (sum(tests, na.rm = TRUE) >= 4) {
cat("✓ PACKAGE IS FULLY FUNCTIONAL\n\n")
cat("Summary:\n")
cat(" • All core functions work correctly\n")
cat(" • recommend_model() provides clear guidance\n")
cat(" • Data generation creates valid datasets\n")
cat(" • MNL fitting works perfectly\n")
if (test_results$mnp_available) {
cat(" • MNP package is installed and available\n")
cat(" • MNP fitting attempts work (converges when possible)\n")
} else {
cat(" • MNP not available (package works in MNL-only mode)\n")
}
cat(" • Safe wrappers handle failures gracefully\n\n")
cat("The package is ready for real-world use!\n")
} else {
cat("⚠ SOME ISSUES DETECTED\n\n")
cat("Review failed tests above for details.\n")
}
cat(paste(rep("=", 70), collapse=""), "\n\n")