Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions R/analyze_linear_selection.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ analyze_linear_selection <- function(data, fitness_col, trait_cols, fitness_type

if (fitness_type == "continuous") {
resp <- if ("relative_fitness" %in% names(data)) {
"relative_fitness"
} else {
fitness_col
}
Expand Down
14 changes: 8 additions & 6 deletions R/selection_coefficients.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ selection_coefficients <- function(data, fitness_col, trait_cols,

fitness_type <- match.arg(fitness_type)

# 3 decide which fitness column to model
# binary fitness must always be on absolute values 0 or 1
# continuous fitness may use relative fitness
rel_col <- paste0(fitness_col, "_relative")

# 1 prepare data
df <- prepare_selection_data(
data = data,
fitness_col = fitness_col,
trait_cols = trait_cols,
standardize = standardize,
add_relative = TRUE,
na_action = "warn"
na_action = "warn",
name_relative = rel_col
)

# 2 detect family if auto
Expand All @@ -28,11 +34,6 @@ selection_coefficients <- function(data, fitness_col, trait_cols,
}
family_obj <- if (fitness_type == "binary") binomial("logit") else gaussian()

# 3 decide which fitness column to model
# binary fitness must always be on absolute values 0 or 1
# continuous fitness may use relative fitness
rel_col <- paste0(fitness_col, "relative")

model_fitness_col <-
if (fitness_type == "binary") {
if (use_relative_for_fit) {
Expand Down Expand Up @@ -84,3 +85,4 @@ selection_coefficients <- function(data, fitness_col, trait_cols,

return(all_coefs)
}

24 changes: 18 additions & 6 deletions R/tests scripts/test_all_functions.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# ---- Packages ----
if (!requireNamespace("ggplot2", quietly = TRUE)) install.packages("ggplot2")
library(ggplot2)
# Ensure other likely dependencies are loaded if needed by the sourced functions
for (pkg in c("mgcv", "fields", "dplyr", "tidyr")) {
if (requireNamespace(pkg, quietly = TRUE)) library(pkg, character.only = TRUE)
}

cat("1. Loading all function files...\n")
function_files <- c(
"prepare_selection_data.R",
Expand All @@ -18,8 +26,11 @@ for (file in function_files) {
if (file.exists(file)) {
source(file)
cat("Loaded:", file, "\n")
} else if (file.exists(file.path("..", file))) {
source(file.path("..", file))
cat("Loaded:", file.path("..", file), "\n")
} else {
cat("ile not found:", file, "\n")
cat("File not found:", file, "\n")
}
}

Expand Down Expand Up @@ -124,7 +135,6 @@ tps_result <- correlational_tps(
data = df_continuous,
fitness_col = "fitness",
trait_cols = c("size", "speed"),
use_relative = TRUE,
grid_n = 30
)

Expand Down Expand Up @@ -288,7 +298,6 @@ for (traits in trait_combinations) {
data = df_continuous,
fitness_col = "fitness",
trait_cols = traits,
use_relative = TRUE,
grid_n = 30
)

Expand All @@ -312,7 +321,6 @@ for (traits in trait_combinations[1:2]) {
data = df_binary,
fitness_col = "fitness",
trait_cols = traits,
use_relative = FALSE,
grid_n = 25
)

Expand Down Expand Up @@ -361,10 +369,10 @@ validate_plot <- function(plot, plot_name) {
)

if (all(checks)) {
cat(lot_name, "PASS\n")
cat(plot_name, "PASS\n")
return(TRUE)
} else {
cat(lot_name, "FAIL - Issues:", paste(names(checks)[!checks], collapse = ", "), "\n")
cat(plot_name, "FAIL - Issues:", paste(names(checks)[!checks], collapse = ", "), "\n")
return(FALSE)
}
}
Expand Down Expand Up @@ -395,6 +403,10 @@ cat("Correlation plots:", length(correlation_plots), "\n")

cat("\n11. Updated final summary...\n")

# Calculate passed and total based on the 'results' list from Section 5
total <- length(results)
passed <- sum(grepl("^pass", unlist(results)))

cat("Functions tested:", length(function_files), "\n")
cat("Tests passed:", passed, "/", total, "\n")
cat("Plots created:", total_plots, "\n")
Expand Down