Debian Codegraph @

R/psych.R

raw
1 #' @templateVar class kappa
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `kappa` object returned from [psych::cohen.kappa()].
5 #' @template param_unused_dots
6 #'
7 #' @evalRd return_tidy(
8 #' type = "Either `weighted` or `unweighted`.",
9 #' "estimate",
10 #' "conf.low",
11 #' "conf.high"
12 #' )
13 #'
14 #' @details Note that confidence level (alpha) for the confidence interval
15 #' cannot be set in `tidy`. Instead you must set the `alpha` argument
16 #' to [psych::cohen.kappa()] when creating the `kappa` object.
17 #'
18 #' @examplesIf rlang::is_installed(c("psych", "ggplot2"))
19 #'
20 #' # load libraries for models and data
21 #' library(psych)
22 #'
23 #' # generate example data
24 #' rater1 <- 1:9
25 #' rater2 <- c(1, 3, 1, 6, 1, 5, 5, 6, 7)
26 #'
27 #' # fit model
28 #' ck <- cohen.kappa(cbind(rater1, rater2))
29 #'
30 #' # summarize model fit with tidiers + visualization
31 #' tidy(ck)
32 #'
33 #' # graph the confidence intervals
34 #' library(ggplot2)
35 #'
36 #' ggplot(tidy(ck), aes(estimate, type)) +
37 #' geom_point() +
38 #' geom_errorbarh(aes(xmin = conf.low, xmax = conf.high))
39 #'
40 #' @aliases kappa_tidiers psych_tidiers
41 #' @export
42 #' @seealso [tidy()], [psych::cohen.kappa()]
43 #'
44 tidy.kappa <- function(x, ...) {
45 ret <- as_tidy_tibble(
46 x$confid,
47 new_names = c("conf.low", "estimate", "conf.high"),
48 new_column = "type"
49 ) |>
50 dplyr::select(type, estimate, conf.low, conf.high) |>
51 mutate(type = gsub(" kappa", "", type))
52
53 ret
54 }