Skip to contents

Generates a HTML table of bivariate Chi squared and Fisher Test analysis for 2 categories. Display a table arranged dataframe with Chi squared statistic, minimum expected frecuencies, Chi squared p value, Fisher Test p value, and Odds ratio with 95 confidence levels. Note that you must recode factors and level the database factors in order to compute exact p values. Variable names can be assigned using table1::label() function.

Usage

dichotomous_2k_2sid(data, referencevar, flextableformat = TRUE)

Arguments

data

Data frame from which variables will be extractred

referencevar

Reference variable. Must have exactly 2 levels

flextableformat

Logical operator to indicate the output desired. Default is TRUE. When FALSE, function will return a dataframe format.

Value

Returns a dataframe or flextable containing statistical values for Chi squared tests or Fisher's test.

Author

JAFG

Examples

  # Not run

 # Create a sample dataframe
 df <- data.frame(
   has = c("Yes", "No", "Yes", "Yes", "No", "No", "Yes"),
   smoke = c("Yes", "No", "No", "Yes", "No", "Yes", "No"),
   gender = c("Male", "Female", "Male", "Female", "Female", "Male", "Male"))

 df$has <- as.factor(df$has)
 df$smoke <- as.factor(df$smoke)
 df$gender <- as.factor(df$gender)

# Set a value as reference level
 df$has <- relevel(df$has, ref= "Yes")
 df$smoke <- relevel(df$smoke, ref= "Yes")
 df$gender <- relevel(df$gender, ref= "Female")

 # Apply function
dichotomous_2k_2sid(df, referencevar="has")
#> Warning: Chi-squared approximation may be incorrect
#> Warning: Chi-squared approximation may be incorrect
#> Warning: Chi-squared approximation may be incorrect
#> Warning: Chi-squared approximation may be incorrect

Variable

Chi_Squared

Min_Expected

P_Chi

P_Fisher

Odds_Ratio

CI_Lower

CI_Upper

smoke

0.00

1.29

1.00

1.00

2.00

0.09

44.35

gender

0.11

1.29

0.74

0.49

0.17

0.01

4.51

dichotomous_2k_2sid(df, referencevar="has", flextableformat = FALSE) #> Warning: Chi-squared approximation may be incorrect #> Warning: Chi-squared approximation may be incorrect #> Warning: Chi-squared approximation may be incorrect #> Warning: Chi-squared approximation may be incorrect #> Variable Chi_Squared Min_Expected P_Chi P_Fisher Odds_Ratio CI_Lower #> 1 smoke 0.00000 1.28571 1.00000 1.00000 2.00000 0.09019 #> 2 gender 0.10937 1.28571 0.74086 0.48571 0.16667 0.00615 #> CI_Upper #> 1 44.35043 #> 2 4.51476 # Set names to variables if(requireNamespace("table1")){ table1::label(df$has) <- "Hypertension" table1::label(df$smoke) <- "Smoking Habits" table1::label(df$gender) <- "Gender" dichotomous_2k_2sid(df, referencevar="has", flextableformat = FALSE) } #> Warning: Chi-squared approximation may be incorrect #> Warning: Chi-squared approximation may be incorrect #> Warning: Chi-squared approximation may be incorrect #> Warning: Chi-squared approximation may be incorrect #> Variable Chi_Squared Min_Expected P_Chi P_Fisher Odds_Ratio CI_Lower #> 1 Smoking Habits 0.00000 1.28571 1.00000 1.00000 2.00000 0.09019 #> 2 Gender 0.10937 1.28571 0.74086 0.48571 0.16667 0.00615 #> CI_Upper #> 1 44.35043 #> 2 4.51476