blob: d3b11027d0c2577fc7c0126e87d3fb76fe4489fa [file] [log] [blame]
Marc Kupietzdcc1de62019-10-04 09:10:36 +02001#' Add confidence interval and relative frequency variables
2#'
3#' Using \code{\link{prop.test}}, \code{ci} adds three columns to a data frame:
Marc Kupietz3f575282019-10-04 14:46:04 +02004#' 1. relative frequency (\code{f})
5#' 2. lower bound of a confidence interval (\code{ci.low})
6#' 3. upper bound of a confidence interval
7#'
Marc Kupietz97a1bca2019-10-04 22:52:09 +02008#'
Marc Kupietz3f575282019-10-04 14:46:04 +02009#' @seealso
10#' \code{ci} is alread included in \code{\link{frequencyQuery}}
Marc Kupietzdcc1de62019-10-04 09:10:36 +020011#'
12#' @param df table with columns for absolute and total frequencies.
13#' @param x column with the observed absolute frequency.
14#' @param N column with the total frequncies
15#' @param conf.level confidence level of the returned confidence interval. Must
16#' be a single number between 0 and 1.
17#'
18#' @export
19#' @importFrom stats prop.test
Marc Kupietz3f575282019-10-04 14:46:04 +020020#' @importFrom tibble remove_rownames
Marc Kupietz97a1bca2019-10-04 22:52:09 +020021#' @importFrom dplyr enquo rename starts_with
Marc Kupietzdcc1de62019-10-04 09:10:36 +020022#' @examples
Marc Kupietz05b22772020-02-18 21:58:42 +010023#' \donttest{
Marc Kupietzdcc1de62019-10-04 09:10:36 +020024#' library(ggplot2)
25#' kco <- new("KorAPConnection", verbose=TRUE)
26#' expand_grid(year=2015:2018, alternatives=c("Hate Speech", "Hatespeech")) %>%
27#' bind_cols(corpusQuery(kco, .$alternatives, sprintf("pubDate in %d", .$year))) %>%
Marc Kupietz71d6e052019-11-22 18:42:10 +010028#' mutate(total=corpusStats(kco, vc=vc)$tokens) %>%
Marc Kupietzdcc1de62019-10-04 09:10:36 +020029#' ci() %>%
30#' ggplot(aes(x=year, y=f, fill=query, color=query, ymin=conf.low, ymax=conf.high)) +
31#' geom_point() + geom_line() + geom_ribbon(alpha=.3)
Marc Kupietz05b22772020-02-18 21:58:42 +010032#' }
Marc Kupietz53c1b502020-02-03 22:48:30 +010033ci <- function(df,
34 x = totalResults,
35 N = total,
36 conf.level = 0.95) {
Marc Kupietzdcc1de62019-10-04 09:10:36 +020037 x <- enquo(x)
38 N <- enquo(N)
Marc Kupietz53c1b502020-02-03 22:48:30 +010039 nas <- df %>%
40 dplyr::filter(total <= 0) %>%
41 mutate(f = NA, conf.low = NA, conf.high = NA)
Marc Kupietzdcc1de62019-10-04 09:10:36 +020042 df %>%
Marc Kupietz53c1b502020-02-03 22:48:30 +010043 dplyr::filter(total > 0) %>%
Marc Kupietzdcc1de62019-10-04 09:10:36 +020044 rowwise %>%
Marc Kupietz53c1b502020-02-03 22:48:30 +010045 mutate(tst = list(
46 broom::tidy(prop.test(!!x,!!N, conf.level = conf.level)) %>%
47 select(estimate, conf.low, conf.high) %>%
48 rename(f = estimate)
Marc Kupietzdcc1de62019-10-04 09:10:36 +020049 )) %>%
Marc Kupietz53c1b502020-02-03 22:48:30 +010050 tidyr::unnest(tst) %>%
51 bind_rows(nas)
Marc Kupietzdcc1de62019-10-04 09:10:36 +020052}
53
Marc Kupietz7d613872019-10-04 22:47:20 +020054## Mute notes: "Undefined global functions or variables:"
Marc Kupietz71d6e052019-11-22 18:42:10 +010055globalVariables(c("totalResults", "total", "estimate", "tst"))
Marc Kupietzdcc1de62019-10-04 09:10:36 +020056
57
Marc Kupietz71d6e052019-11-22 18:42:10 +010058# ci.old <- function(df, x = totalResults, N = total, conf.level = 0.95) {
Marc Kupietzdcc1de62019-10-04 09:10:36 +020059# x <- deparse(substitute(x))
60# N <- deparse(substitute(N))
61# df <- data.frame(df)
62# df$f <- df[,x] / df[,N]
63# 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"))
64# return(df)
65# }