library(lmtest)
library(jsonlite)
convert_categorical_to_numeric <- function(data, dataset_name) {
non_numeric_cols <- names(data)[sapply(data, function(x) !is.numeric(x))]
if (length(non_numeric_cols) > 0) {
cat(paste0("INFO: Dataset '", dataset_name, "' contains non-numeric columns: ",
paste(non_numeric_cols, collapse = ", "), "\n"))
cat("Converting categorical variables to numeric representations...\n")
for (col in non_numeric_cols) {
if (is.factor(data[[col]])) {
unique_vals <- length(unique(data[[col]]))
data[[col]] <- as.numeric(data[[col]])
cat(paste0(" ", col, ": ", unique_vals, " unique values -> integer level encoding\n"))
} else if (is.character(data[[col]])) {
unique_vals <- length(unique(data[[col]]))
temp_numeric <- as.numeric(data[[col]])
if (any(is.na(temp_numeric))) {
data[[col]] <- as.numeric(as.factor(data[[col]]))
cat(paste0(" ", col, ": ", unique_vals, " unique values -> integer level encoding\n"))
} else {
data[[col]] <- temp_numeric
cat(paste0(" ", col, ": ", unique_vals, " unique values -> numeric encoding\n"))
}
}
}
}
return(data)
}
args <- commandArgs(trailingOnly = TRUE)
default_csv <- "../../datasets/csv/mtcars.csv"
default_output <- "../../results/r"
default_powers <- "2:3"
default_type <- "fitted"
csv_path <- ifelse(length(args) >= 1, args[1], default_csv)
output_dir <- ifelse(length(args) >= 2, args[2], default_output)
powers_str <- ifelse(length(args) >= 3, args[3], default_powers)
type_str <- ifelse(length(args) >= 4, args[4], default_type)
if (grepl(":", powers_str)) {
power_range <- as.numeric(strsplit(powers_str, ":")[[1]])
powers <- power_range[1]:power_range[2]
} else {
powers <- as.numeric(strsplit(powers_str, ",")[[1]])
}
valid_types <- c("fitted", "regressor", "princomp")
if (!(type_str %in% valid_types)) {
stop(paste("Invalid type. Must be one of:", paste(valid_types, collapse = ", ")))
}
if (!file.exists(csv_path)) {
stop(paste("CSV file not found:", csv_path))
}
dataset_name <- tools::file_path_sans_ext(basename(csv_path))
data <- read.csv(csv_path)
data <- convert_categorical_to_numeric(data, dataset_name)
response_col <- names(data)[1]
predictor_cols <- names(data)[-1]
formula_str <- paste(response_col, "~", paste(predictor_cols, collapse = " + "))
formula <- as.formula(formula_str)
model <- lm(formula, data = data)
reset_result <- resettest(model, power = powers, type = type_str)
cat("RESET Test (R - lmtest::resettest)\n")
cat("==================================\n")
cat("Dataset:", dataset_name, "\n")
cat("Formula:", formula_str, "\n")
cat("Powers:", paste(powers, collapse = ", "), "\n")
cat("Type:", type_str, "\n")
cat("Statistic (F):", reset_result$statistic, "\n")
cat("p-value:", reset_result$p.value, "\n")
cat("Passed:", reset_result$p.value > 0.05, "\n\n")
output <- list(
test_name = "RESET Test (R - lmtest::resettest)",
dataset = dataset_name,
formula = formula_str,
statistic = as.numeric(reset_result$statistic),
p_value = as.numeric(reset_result$p.value),
passed = reset_result$p.value > 0.05,
power = as.list(powers),
type = type_str,
description = "Ramsey's RESET test for functional form misspecification. Tests whether powers of fitted values, regressors, or principal component significantly improve model fit."
)
if (!dir.exists(output_dir)) {
dir.create(output_dir, recursive = TRUE)
}
output_file <- file.path(output_dir, paste0(dataset_name, "_reset.json"))
write_json(output, output_file, pretty = TRUE, digits = 22)
cat("Results saved to:", output_file, "\n")