File: effectsize.BFBayesFactor.R

package info (click to toggle)
r-cran-effectsize 0.8.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,404 kB
  • sloc: sh: 17; makefile: 2
file content (155 lines) | stat: -rw-r--r-- 4,495 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
#' @export
#' @rdname effectsize
#' @inheritParams bayestestR::describe_posterior
#' @importFrom insight get_data get_parameters check_if_installed
#' @importFrom bayestestR describe_posterior
effectsize.BFBayesFactor <- function(model, type = NULL, ci = 0.95, test = NULL, verbose = TRUE, ...) {
  insight::check_if_installed("BayesFactor")

  if (length(model) > 1) {
    if (verbose) {
      insight::format_warning("Multiple models detected. Using first only.")
    }
    model <- model[1]
  }

  if (inherits(model@numerator[[1]], "BFcontingencyTable")) {
    pars <- .effectsize_contingencyTableBF(model, type = type, verbose = verbose, ...)
  } else if (inherits(model@numerator[[1]], c("BFoneSample", "BFindepSample"))) {
    pars <- .effectsize_ttestBF(model, type = type, verbose = verbose)
  } else if (inherits(model@numerator[[1]], "BFcorrelation")) {
    pars <- .effectsize_correlationBF(model, type = type, verbose = verbose)
  } else if (inherits(model@numerator[[1]], "BFproportion")) {
    pars <- .effectsize_proportionBF(model, type = type, verbose = verbose)
  } else {
    insight::format_error("No effect size for this type of 'BayesFactor' object.")
  }

  # Clean up
  out <- bayestestR::describe_posterior(pars$res, ci = ci, test = test, ...)
  if (isTRUE(type == "cles")) {
    colnames(out)[2] <- "Coefficient"
  } else {
    colnames(out)[2] <- out$Parameter
    out$Parameter <- NULL
  }

  class(out) <- c(pars$xtra_class, "effectsize_table", "see_effectsize_table", class(out))
  .someattributes(out) <- pars$attr
  .someattributes(out) <- list(
    ci = out$CI,
    approximate = FALSE,
    alternative = "two.sided"
  )
  out
}

#' @keywords internal
.effectsize_contingencyTableBF <- function(model, type = NULL, verbose = TRUE, adjust = TRUE, ...) {
  if (is.null(type)) type <- "cramers_v"

  f <- switch(tolower(type),
    v = ,
    cramers_v = cramers_v,
    t = ,
    tschuprows_t = tschuprows_t,
    w = ,
    cohens_w = cohens_w,
    phi = phi,
    c = ,
    pearsons_c = pearsons_c,
    h = ,
    cohens_h = cohens_h,
    or = ,
    oddsratio = oddsratio,
    rr = ,
    riskratio = riskratio
  )
  data <- insight::get_data(model)
  posts <- insight::get_parameters(model)

  ES <- apply(posts, 1, function(a) {
    M <- matrix(a, nrow = nrow(data))
    f(M, ci = NULL, adjust = adjust)[[1]]
  })

  res <- data.frame(ES)
  colnames(res) <- colnames(f(data, ci = NULL, adjust = adjust))

  list(
    res = res,
    attr = NULL,
    xtra_class = NULL
  )
}


#' @keywords internal
.effectsize_ttestBF <- function(model, type = NULL, verbose = TRUE) {
  if (is.null(type) || tolower(type) == "cohens_d") {
    type <- "d"
  }

  samps <- as.data.frame(BayesFactor::posterior(model, iterations = 4000, progress = FALSE))

  paired <- inherits(model@numerator[[1]], "BFoneSample")
  if (!paired) {
    mu <- 0
    D <- samps$delta
  } else {
    mu <- model@numerator[[1]]@prior$mu
    D <- (samps$mu - mu) / sqrt(samps$sig2)
  }

  res <- data.frame(Cohens_d = D)

  if (type == "d") {
    xtra_class <- "effectsize_difference"
  } else if (tolower(type) %in% c("p_superiority", "u1", "u2", "u3", "overlap")) {
    if (paired && type != "p_superiority") insight::format_error("CLES only applicable to two independent samples.")

    converter <- match.fun(paste0("d_to_", tolower(type)))
    if (grepl("^(u|U)", type)) type <- paste0("Cohens_", toupper(type))

    res <- data.frame(converter(res$Cohens_d), check.names = FALSE)
    colnames(res) <- type
    xtra_class <- NULL
  }

  list(
    res = res,
    attr = list(mu = mu, paired = paired, pooled_sd = TRUE),
    xtra_class = xtra_class
  )
}


# Others ------------------------------------------------------------------
# Wrappers

#' @keywords internal
.effectsize_correlationBF <- function(model, type = NULL, verbose = TRUE) {
  rho <- insight::get_parameters(model)[["rho"]]
  res <- data.frame(rho = rho)

  list(
    res = res,
    attr = NULL,
    xtra_class = NULL
  )
}


#' @keywords internal
.effectsize_proportionBF <- function(model, type = NULL, verbose = TRUE) {
  res <- insight::get_parameters(model)

  p0 <- model@denominator@identifier[["p0"]]
  xtra_footer <- list(c(sprintf("\n- Against the null: p = %s.", p0), "cyan"))

  list(
    res = res,
    attr = list(xtra_footer = xtra_footer),
    xtra_class = NULL
  )
}