| #' Add confidence interval and relative frequency variables |
| #' |
| #' Using \code{\link{prop.test}}, \code{ci} adds three columns to a data frame: |
| #' 1. relative frequency (\code{f}) |
| #' 2. lower bound of a confidence interval (\code{ci.low}) |
| #' 3. upper bound of a confidence interval |
| #' |
| #' |
| #' @seealso |
| #' \code{ci} is alread included in \code{\link{frequencyQuery}} |
| #' |
| #' @param df table with columns for absolute and total frequencies. |
| #' @param x column with the observed absolute frequency. |
| #' @param N column with the total frequncies |
| #' @param conf.level confidence level of the returned confidence interval. Must |
| #' be a single number between 0 and 1. |
| #' |
| #' @export |
| #' @importFrom stats prop.test |
| #' @importFrom tibble remove_rownames |
| #' @importFrom dplyr enquo rename starts_with |
| #' @examples |
| #' \donttest{ |
| #' library(ggplot2) |
| #' kco <- new("KorAPConnection", verbose=TRUE) |
| #' expand_grid(year=2015:2018, alternatives=c("Hate Speech", "Hatespeech")) %>% |
| #' bind_cols(corpusQuery(kco, .$alternatives, sprintf("pubDate in %d", .$year))) %>% |
| #' mutate(total=corpusStats(kco, vc=vc)$tokens) %>% |
| #' ci() %>% |
| #' ggplot(aes(x=year, y=f, fill=query, color=query, ymin=conf.low, ymax=conf.high)) + |
| #' geom_point() + geom_line() + geom_ribbon(alpha=.3) |
| #' } |
| ci <- function(df, |
| x = totalResults, |
| N = total, |
| conf.level = 0.95) { |
| x <- enquo(x) |
| N <- enquo(N) |
| nas <- df %>% |
| dplyr::filter(total <= 0) %>% |
| mutate(f = NA, conf.low = NA, conf.high = NA) |
| df %>% |
| dplyr::filter(total > 0) %>% |
| rowwise %>% |
| mutate(tst = list( |
| broom::tidy(prop.test(!!x,!!N, conf.level = conf.level)) %>% |
| select(estimate, conf.low, conf.high) %>% |
| rename(f = estimate) |
| )) %>% |
| tidyr::unnest(tst) %>% |
| bind_rows(nas) |
| } |
| |
| ## Mute notes: "Undefined global functions or variables:" |
| globalVariables(c("totalResults", "total", "estimate", "tst")) |
| |
| |
| # ci.old <- function(df, x = totalResults, N = total, conf.level = 0.95) { |
| # x <- deparse(substitute(x)) |
| # N <- deparse(substitute(N)) |
| # df <- data.frame(df) |
| # df$f <- df[,x] / df[,N] |
| # df[, c("conf.low", "conf.high")] <- t(sapply(Map(function(a, b) prop.test(a, b, conf.level = conf.level), df[,x], df[,N]), "[[","conf.int")) |
| # return(df) |
| # } |