File: xtab_corr.R

package info (click to toggle)
r-cran-effectsize 0.8.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,404 kB
  • sloc: sh: 17; makefile: 2
file content (217 lines) | stat: -rw-r--r-- 7,724 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#' \eqn{\phi} and Other Contingency Tables Correlations
#'
#' Compute phi (\eqn{\phi}), Cramer's *V*, Tschuprow's *T*, Cohen's *w*,
#' \ifelse{latex}{\eqn{Fei}}{פ (Fei)}, Pearson's contingency coefficient for
#' contingency tables or goodness-of-fit. Pair with any reported
#' [`stats::chisq.test()`].
#'
#' @inheritParams stats::chisq.test
#' @inheritParams chisq_to_phi
#' @param ... Ignored.
#'
#' @details
#'
#' phi (\eqn{\phi}), Cramer's *V*, Tschuprow's *T*, Cohen's *w*, and Pearson's
#' *C* are effect sizes for tests of independence in 2D contingency tables. For
#' 2-by-2 tables, phi, Cramer's *V*, Tschuprow's *T*, and Cohen's *w* are
#' identical, and are equal to the simple correlation between two dichotomous
#' variables, ranging between  0 (no dependence) and 1 (perfect dependence).
#' \cr\cr
#' For larger tables, Cramer's *V*, Tschuprow's *T* or Pearson's *C* should be
#' used, as they are bounded between 0-1. (Cohen's *w* can also be used, but
#' since it is not bounded at 1 (can be larger) its interpretation is more
#' difficult.) For square table, Cramer's *V* and Tschuprow's *T* give the same
#' results, but for non-square tables Tschuprow's *T* is more conservative:
#' while *V* will be 1 if either columns are fully dependent on rows (for each
#' column, there is only one non-0 cell) *or* rows are fully dependent on
#' columns, *T* will only be 1 if both are true.
#' \cr \cr
#' For goodness-of-fit in 1D tables Cohen's *W*, \ifelse{latex}{\eqn{Fei}}{פ (Fei)}
#' or Pearson's *C* can be used. Cohen's *w* has no upper bound (can be
#' arbitrarily large, depending on the expected distribution). *Fei* is an
#' adjusted Cohen's *w*, accounting for the expected distribution, making it
#' bounded between 0-1. Pearson's *C* is also bounded between 0-1.
#' \cr \cr
#' To summarize, for correlation-like effect sizes, we recommend:
#'
#' - For a 2x2 table, use `phi()`
#' - For larger tables, use `cramers_v()`
#' - For goodness-of-fit, use `fei()`
#'
#' @inheritSection effectsize_CIs Confidence (Compatibility) Intervals (CIs)
#' @inheritSection effectsize_CIs CIs and Significance Tests
#'
#' @return A data frame with the effect size (`Cramers_v`, `phi` (possibly with
#'   the suffix `_adjusted`), `Cohens_w`, `Fei`) and its CIs (`CI_low` and
#'   `CI_high`).
#'
#' @seealso [chisq_to_phi()] for details regarding estimation and CIs.
#' @family effect sizes for contingency table
#'
#' @examples
#'
#' ## 2-by-2 tables
#' ## -------------
#' data("RCT_table")
#' RCT_table # note groups are COLUMNS
#'
#' phi(RCT_table)
#' pearsons_c(RCT_table)
#'
#'
#'
#' ## Larger tables
#' ## -------------
#' data("Music_preferences")
#' Music_preferences
#'
#' cramers_v(Music_preferences)
#'
#' cohens_w(Music_preferences)
#'
#' pearsons_c(Music_preferences)
#'
#'
#'
#' ## Goodness of fit
#' ## ---------------
#' data("Smoking_FASD")
#' Smoking_FASD
#'
#' fei(Smoking_FASD)
#'
#' cohens_w(Smoking_FASD)
#'
#' pearsons_c(Smoking_FASD)
#'
#' # Use custom expected values:
#' fei(Smoking_FASD, p = c(0.015, 0.010, 0.975))
#'
#' cohens_w(Smoking_FASD, p = c(0.015, 0.010, 0.975))
#'
#' pearsons_c(Smoking_FASD, p = c(0.015, 0.010, 0.975))
#' @references
#' - Cohen, J. (1988). Statistical power analysis for the behavioral sciences (2nd Ed.). New York: Routledge.
#' - Johnston, J. E., Berry, K. J., & Mielke Jr, P. W. (2006). Measures of
#' effect size for chi-squared and likelihood-ratio goodness-of-fit tests.
#' Perceptual and motor skills, 103(2), 412-414.
#' - Rosenberg, M. S. (2010). A generalized formula for converting chi-square
#' tests to effect sizes for meta-analysis. PloS one, 5(4), e10059.
#'
#'
#' @importFrom stats chisq.test
#' @export
phi <- function(x, y = NULL,
                adjust = TRUE,
                ci = 0.95, alternative = "greater",
                ...) {
  alternative <- .match.alt(alternative, FALSE)

  if (.is_BF_of_type(x, "BFcontingencyTable", "Chi-squared")) {
    return(effectsize(x, type = "phi", adjust = adjust, ci = ci))
  } else if (!.is_htest_of_type(x, "Pearson's Chi-squared", "Chi-squared-test")) {
    x <- suppressWarnings(stats::chisq.test(x, y))
    x$data.name <- NULL
  }

  effectsize(x, type = "phi", adjust = adjust, ci = ci, alternative = alternative)
}

#' @rdname phi
#' @importFrom stats chisq.test
#' @export
cramers_v <- function(x, y = NULL,
                      adjust = TRUE,
                      ci = 0.95, alternative = "greater",
                      ...) {
  alternative <- .match.alt(alternative, FALSE)

  if (.is_BF_of_type(x, "BFcontingencyTable", "Chi-squared")) {
    return(effectsize(x, type = "cramers_v", adjust = adjust, ci = ci))
  } else if (!.is_htest_of_type(x, "Pearson's Chi-squared", "Chi-squared-test")) {
    x <- suppressWarnings(stats::chisq.test(x, y))
    x$data.name <- NULL
  }

  effectsize(x, type = "cramers_v", adjust = adjust, ci = ci, alternative = alternative)
}


#' @rdname phi
#' @importFrom stats chisq.test
#' @export
tschuprows_t <- function(x, y = NULL,
                         ci = 0.95, alternative = "greater",
                         ...) {
  alternative <- .match.alt(alternative, FALSE)

  if (.is_BF_of_type(x, "BFcontingencyTable", "Chi-squared")) {
    return(effectsize(x, type = "tschuprows_t", ci = ci))
  } else if (!.is_htest_of_type(x, "Pearson's Chi-squared", "Chi-squared-test")) {
    x <- suppressWarnings(stats::chisq.test(x, y))
    x$data.name <- NULL
  }

  effectsize(x, type = "tschuprows_t", ci = ci, alternative = alternative)
}

#' @rdname phi
#' @importFrom stats chisq.test
#' @export
cohens_w <- function(x, y = NULL, p = rep(1, length(x)),
                     ci = 0.95, alternative = "greater",
                     ...) {
  alternative <- .match.alt(alternative, FALSE)

  if (.is_BF_of_type(x, "BFcontingencyTable", "Chi-squared")) {
    return(effectsize(x, type = "cohens_w", ci = ci))
  } else if (!.is_htest_of_type(
    x, "(Pearson's Chi-squared|Chi-squared test for given probabilities)",
    "Chi-squared-test"
  )) {
    x <- suppressWarnings(stats::chisq.test(x, y, p = p, rescale.p = TRUE))
    x$data.name <- NULL
  }

  effectsize(x, type = "cohens_w", ci = ci, alternative = alternative)
}


#' @rdname phi
#' @importFrom stats chisq.test
#' @export
fei <- function(x, p = rep(1, length(x)),
                ci = 0.95, alternative = "greater",
                ...) {
  alternative <- .match.alt(alternative, FALSE)

  if (inherits(x, "BFBayesFactor")) {
    insight::format_error("Fei is only applicable to goodness of fit tests.")
  } else if (!.is_htest_of_type(x, "Chi-squared test for given probabilities", "Chi-squared-test")) {
    x <- suppressWarnings(stats::chisq.test(x, y = NULL, p = p, rescale.p = TRUE))
    x$data.name <- NULL
  }

  effectsize(x, type = "fei", ci = ci, alternative = alternative)
}

#' @rdname phi
#' @importFrom stats chisq.test
#' @export
pearsons_c <- function(x, y = NULL, p = rep(1, length(x)),
                       ci = 0.95, alternative = "greater",
                       ...) {
  alternative <- .match.alt(alternative, FALSE)

  if (.is_BF_of_type(x, "BFcontingencyTable", "Chi-squared")) {
    return(effectsize(x, type = "pearsons_c", ci = ci))
  } else if (!.is_htest_of_type(
    x, "(Pearson's Chi-squared|Chi-squared test for given probabilities)",
    "Chi-squared-test"
  )) {
    x <- suppressWarnings(stats::chisq.test(x, y, p = p, rescale.p = TRUE))
    x$data.name <- NULL
  }

  effectsize(x, type = "pearsons_c", ci = ci, alternative = alternative)
}