Skip to content

Instantly share code, notes, and snippets.

@Keiku
Last active February 10, 2017 01:49
Show Gist options
  • Save Keiku/d83ab52a4bc8963dadf9d2768ce69c21 to your computer and use it in GitHub Desktop.
Save Keiku/d83ab52a4bc8963dadf9d2768ce69c21 to your computer and use it in GitHub Desktop.
Chi-square testing in each group.
library(dplyr)
library(broom)
library(lazyeval)
df <- data_frame(
group = rep(letters[1:2], each = 50),
cat1 = letters[round(runif(100) * 5) + 1],
cat2 = letters[round(runif(100) * 3) + 1]
)
do_test <- function(.data, test_func, ...){
fun <- substitute(test_func)
.call <- lazyeval::make_call(fun, lazyeval::lazy_dots(...))
.call <- lazyeval::make_call(quote(list), .call)
summarise_(.data, test = .call) %>% rowwise %>% glance(test)
}
df %>%
group_by(group) %>%
do_test(., chisq.test, cat1, cat2, simulate.p.value = TRUE, B = 10)
# Source: local data frame [2 x 5]
# Groups: group [2]
#
# group statistic p.value parameter
# <chr> <dbl> <dbl> <lgl>
# 1 a 9.588062 0.7272727 NA
# 2 b 17.190908 0.6363636 NA
# ... with 1 more variables: method <fctr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment