-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_test.R
More file actions
173 lines (147 loc) · 5.46 KB
/
Copy pathfinal_test.R
File metadata and controls
173 lines (147 loc) · 5.46 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
#!/usr/bin/env Rscript
# Final test with correct function signatures
cat("\n=== FINAL PACKAGE VALIDATION ===\n\n")
# Source all R files
source_files <- list.files("R", pattern = "\\.R$", full.names = TRUE)
for (f in source_files) {
source(f)
}
tests_passed <- 0
tests_total <- 0
test <- function(name, expr) {
tests_total <<- tests_total + 1
cat(sprintf("[%02d] %-50s... ", tests_total, name))
result <- tryCatch({
expr
cat("✓\n")
tests_passed <<- tests_passed + 1
TRUE
}, error = function(e) {
cat(sprintf("✗\n Error: %s\n", e$message))
FALSE
})
result
}
# Test all core functions
test("recommend_model()", {
r <- recommend_model(n = 50, verbose = FALSE)
stopifnot(r$recommendation == "MNL")
})
test("generate_choice_data()", {
dat <- generate_choice_data(n = 100, seed = 123)
stopifnot(nrow(dat$data) == 100)
})
test("fit_mnp_safe() with smart starting values", {
data <- data.frame(
choice = factor(sample(1:3, 150, replace = TRUE)),
x1 = rnorm(150),
x2 = rnorm(150)
)
fit <- fit_mnp_safe(choice ~ x1 + x2, data = data, fallback = "MNL", verbose = FALSE)
stopifnot(!is.null(fit))
})
test("interpret_convergence_failure()", {
data <- data.frame(
choice = factor(sample(1:3, 50, replace = TRUE)),
x1 = rnorm(50),
x2 = rnorm(50)
)
diag <- interpret_convergence_failure(choice ~ x1 + x2, data = data, verbose = FALSE)
stopifnot(length(diag$likely_causes) > 0)
})
test("quantify_model_choice_consequences()", {
cons <- quantify_model_choice_consequences(n = 100, true_correlation = 0, n_sims = 10, verbose = FALSE)
stopifnot(!is.null(cons$summary))
})
test("compare_mnl_mnp()", {
dat <- generate_choice_data(n = 150, seed = 789)
comp <- compare_mnl_mnp(choice ~ x1 + x2, data = dat$data, fallback_mnp = TRUE, verbose = FALSE)
stopifnot(!is.null(comp$results))
})
test("compare_mnl_mnp_cv()", {
dat <- generate_choice_data(n = 200, seed = 111)
cv <- compare_mnl_mnp_cv(choice ~ x1 + x2, data = dat$data, k = 3, fallback_mnp = TRUE, verbose = FALSE)
stopifnot(!is.null(cv$results))
})
test("check_mnp_convergence()", {
mock_mnp <- list(param = matrix(rnorm(1000 * 5), ncol = 5))
class(mock_mnp) <- "mnp"
colnames(mock_mnp$param) <- paste0("beta", 1:5)
diag <- check_mnp_convergence(mock_mnp, diagnostic_plots = FALSE)
stopifnot(!is.null(diag$converged))
})
test("Visualization: plot_convergence_rates()", {
invisible(capture.output(r <- plot_convergence_rates()))
stopifnot(!is.null(r))
})
test("Visualization: plot_win_rates()", {
invisible(capture.output(r <- plot_win_rates()))
stopifnot(!is.null(r))
})
test("Visualization: plot_recommendation_regions()", {
invisible(capture.output(r <- plot_recommendation_regions()))
stopifnot(!is.null(r))
})
test("required_sample_size() - correct return value", {
result <- required_sample_size(model = "MNP", target_convergence = 0.90)
stopifnot(!is.null(result$minimum_n)) # Correct field name
stopifnot(is.numeric(result$minimum_n))
})
test("evaluate_performance()", {
n <- 100
true_probs <- matrix(runif(n * 3), ncol = 3)
true_probs <- true_probs / rowSums(true_probs)
pred_probs <- matrix(runif(n * 3), ncol = 3)
pred_probs <- pred_probs / rowSums(pred_probs)
perf <- evaluate_performance(pred_probs, true_probs)
stopifnot(!is.null(perf))
})
test("power_analysis_mnl() - correct parameters", {
# Actual parameters: effect_size, alpha, power, n_sims, seed, n_alternatives
power <- power_analysis_mnl(effect_size = 0.5, alpha = 0.05, power = 0.80, n_sims = 20)
stopifnot(!is.null(power$power_curve))
})
test("sample_size_table() - correct parameters", {
# Actual parameters: model, n_alternatives, print_table
invisible(capture.output(table <- sample_size_table(model = "MNL", n_alternatives = 3, print_table = FALSE)))
stopifnot(!is.null(table))
})
test("predict.mnp_safe()", {
train_data <- data.frame(
choice = factor(sample(1:3, 100, replace = TRUE)),
x1 = rnorm(100),
x2 = rnorm(100)
)
test_data <- data.frame(x1 = rnorm(20), x2 = rnorm(20))
fit <- fit_mnp_safe(choice ~ x1 + x2, data = train_data, fallback = "MNL", verbose = FALSE)
probs <- predict(fit, newdata = test_data, type = "probs")
stopifnot(is.matrix(probs))
})
test("run_benchmark_simulation() exists", {
stopifnot(exists("run_benchmark_simulation"))
stopifnot(is.function(run_benchmark_simulation))
})
test("model_summary_comparison()", {
dat <- generate_choice_data(n = 150, seed = 456)
mnl <- fit_mnp_safe(choice ~ x1 + x2, data = dat$data, fallback = "MNL", verbose = FALSE)
summary <- model_summary_comparison(mnl, mnp_fit = NULL, print_summary = FALSE)
stopifnot(!is.null(summary$mnl))
})
# Summary
cat(sprintf("\n%s\n", strrep("=", 70)))
cat(sprintf(" RESULTS: %d/%d tests passed (%.1f%%)\n",
tests_passed, tests_total, 100 * tests_passed / tests_total))
cat(sprintf("%s\n\n", strrep("=", 70)))
if (tests_passed == tests_total) {
cat("✓✓✓ ALL TESTS PASSED! ✓✓✓\n")
cat("\nPackage is fully functional and ready for use.\n")
cat("\nNew high-impact features confirmed working:\n")
cat(" • interpret_convergence_failure() - Diagnoses WHY MNP fails\n")
cat(" • quantify_model_choice_consequences() - Shows cost of wrong choice\n")
cat(" • fit_mnp_safe() - Smart MNL-based starting values\n")
cat(" • run_benchmark_simulation() - Empirical benchmark framework\n\n")
quit(status = 0)
} else {
cat(sprintf("✗ %d/%d tests failed\n\n", tests_total - tests_passed, tests_total))
quit(status = 1)
}