combined_contingency_2x2_functions | R Documentation |
A comprehensive set of functions to:
Generate contingency tables and summary tables.
Perform 2x2 contingency table significance tests.
epi_stats_contingency_2x2_df(df, x_var, y_var)
epi_stats_contingency_2x2_tables(df, x_var)
rename_contingency_2x2_cols(contingency_2x2_list, df, x_var)
epi_stats_contingency_2x2_test(
df,
target_var,
other_var,
test_type = "fisher.test"
)
epi_stats_contingency_2x2_cols(df, min_unique = 2)
epi_stats_contingency_2x2_all(df, target_var, test_type = "fisher.test")
df |
A data frame containing the data to analyze. |
x_var |
A string or numeric index specifying the independent variable (column in |
y_var |
A string or numeric index specifying the dependent variable (column in |
target_var |
The name of the target variable (e.g., dependent variable) as a string. |
other_var |
The name of the other variable to test against the target variable. |
test_type |
The type of test to perform. Either |
min_unique |
Minimum number of unique values required to include a column for testing (default: 2). |
output_file |
(Optional) File path to save the aggregated test results. |
These functions include:
epi_stats_contingency_2x2_df()
: Creates a raw contingency table between two variables.
epi_stats_contingency_2x2_tables()
: Generates a named list of contingency tables for a target variable and all other variables.
epi_stats_rename_contingency_2x2_cols()
: Renames columns in a list of contingency tables for clarity.
epi_stats_contingency_2x2_test()
: Performs a statistical test (Fisher's exact or Chi-squared) on a 2x2 table.
epi_stats_contingency_2x2_cols()
: Selects suitable columns for contingency table tests.
epi_stats_contingency_2x2_all()
: Iteratively applies the tests and aggregates results into a data frame.
epi_stats_contingency_2x2_df()
: A data frame representing the contingency table with raw frequencies.
epi_stats_contingency_2x2_tables()
: A named list of data frames, each representing a contingency table.
epi_stats_rename_contingency_2x2_cols()
: A list of data frames with renamed columns.
epi_stats_contingency_2x2_test
: A tidy data frame with test results for a single 2x2 table.
epi_stats_contingency_2x2_cols()
: A character vector of column names that meet the criteria for testing.
epi_stats_contingency_2x2_all()
: A data frame containing test results for all selected columns.
# Example dataset
set.seed(42)
col_facts <- data.frame(
State = sample(c("Active", "Inactive"), 100, replace = TRUE),
Gender = sample(c("Male", "Female"), 100, replace = TRUE),
Something = sample(c("Yes", "No"), 100, replace = TRUE),
Another = sample(c("Yes", "No", "Unknown"), 100, replace = TRUE),
Type = sample(c("Type1", "Type2", "Type3"), 100, replace = TRUE),
SingleValue = rep("Same", 100)
)
# Generate a contingency table for two variables
contingency_2x2_df <- epi_stats_contingency_2x2_df(col_facts, x_var = "State", y_var = "Gender")
print(contingency_2x2_df)
# Generate contingency tables for all variables
contingency_2x2_list <- epi_stats_contingency_2x2_tables(col_facts, x_var = "State")
print(contingency_2x2_list[[1]])
# Rename columns in contingency tables
renamed_list <- rename_contingency_2x2_cols(contingency_2x2_list, col_facts, x_var = "State")
print(renamed_list[[1]])
# Perform a single 2x2 test
result <- epi_stats_contingency_2x2_test(col_facts, "State", "Gender")
print(result)
# Select columns for testing
testable_columns <- epi_stats_contingency_2x2_cols(col_facts)
print(testable_columns)
# Run tests on all testable columns
results_df <- epi_stats_contingency_2x2_all(col_facts, "State")
print(results_df)
# Save results
write.table(results_df, "fishers_results.txt", sep = "\t", row.names = FALSE, quote = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.