# tests/testthat/test-logistic_analysis.R library(testthat) # Load the test dataset data(iris) # Split the data into training and testing sets (70% training, 30% testing) set.seed(123) # Set a seed for reproducibility train_indices <- sample(seq_len(nrow(iris)), size = 0.7 * nrow(iris)) train_data <- iris[train_indices, ] test_data <- iris[-train_indices, ] # Test logistic_analysis function test_that("logistic_analysis fits a multinomial logistic regression model", { # Define the formula formula <- Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width # Fit the model using logistic_analysis on the training set model <- logistic_analysis(train_data, formula) # Check that the model is of class 'multinom' expect_s3_class(model, "multinom") # Check if the model has coefficients expect_true(length(coef(model)) > 0) }) # Test logistic_predict function test_that("logistic_predict makes predictions", { # Fit the model using logistic_analysis on the training set formula <- Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width model <- logistic_analysis(train_data, formula) # Make predictions on the testing data predictions <- logistic_predict(model, test_data) # Check that predictions are of class 'factor' expect_s3_class(predictions, "factor") # Check that the predicted classes are in the levels of the iris Species expect_true(all(predictions %in% levels(iris$Species))) }) # Test logistic_plot function test_that("logistic_plot generates a plot", { # Fit the model using logistic_analysis on the training set formula <- Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width model <- logistic_analysis(train_data, formula) # Create a plot file png(filename = "logistic_plot_test.png") # Call logistic_plot to generate the plot expect_error(logistic_plot(model, train_data, formula), NA) # Close the plot device dev.off() # Check if the plot file was created expect_true(file.exists("logistic_plot_test.png")) # Optionally, clean up the file after the test file.remove("logistic_plot_test.png") })