Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better ncp ci optimization #629

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: effectsize
Title: Indices of Effect Size
Version: 0.8.6.5
Version: 0.8.6.6
Authors@R:
c(person(given = "Mattan S.",
family = "Ben-Shachar",
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

## Bug fixes

- Minor stability fix to ncp-based CI methods ( #628 )
- `nnt()` now properly accepts the `y` argument.

# effectsize 0.8.7
Expand Down
9 changes: 2 additions & 7 deletions R/docs_extra.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,8 @@
#' smaller than the tolerance of the optimizer, resulting in CIs of width 0.
#' This can also result in the estimated CIs excluding the point estimate.
#'
#' For example:
#' ```{r}
#' t_to_d(80, df_error = 4555555)
#' ```
#'
#' In these cases, consider an alternative optimizer, or an alternative method
#' for computing CIs, such as the bootstrap.
#' In these cases, consider an alternative method for computing CIs, such as the
#' bootstrap.
#'
#'
#' @references
Expand All @@ -140,7 +135,7 @@
#' \doi{10.1093/biomet/83.4.934}
#'
#' Rafi, Z., & Greenland, S. (2020).
#' Semantic and cognitive tools to aid statistical science: Replace confidence and significance by compatibility and surprise.

Check warning on line 138 in R/docs_extra.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/docs_extra.R,line=138,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 126 characters.
#' _BMC Medical Research Methodology, 20_(1), Article 244.
#' \doi{10.1186/s12874-020-01105-9}
#'
Expand All @@ -150,7 +145,7 @@
#' \doi{10.1017/CBO9781139046671}
#'
#' Steiger, J. H. (2004).
#' Beyond the _F_ test: Effect size confidence intervals and tests of close fit in the analysis of variance and contrast analysis.

Check warning on line 148 in R/docs_extra.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/docs_extra.R,line=148,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 130 characters.
#' _Psychological Methods, 9_(2), 164--182.
#' \doi{10.1037/1082-989x.9.2.164}
#'
Expand All @@ -166,7 +161,7 @@

#' `effectsize` API
#'
#' Read the [*Support functions for model extensions*](https://easystats.github.io/effectsize/articles/effectsize_API.html) vignette.

Check warning on line 164 in R/docs_extra.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/docs_extra.R,line=164,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 133 characters.
#'
#' @rdname effectsize_API
#' @name effectsize_API
Expand All @@ -177,7 +172,7 @@
#' `effectsize` options
#'
#' Currently, the following global options are supported:
#' - `es.use_symbols` [logical]: Should proper symbols be printed (`TRUE`) instead of transliterated effect size names (`FALSE`; default).

Check warning on line 175 in R/docs_extra.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/docs_extra.R,line=175,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 138 characters.
#'
#' @rdname effectsize_options
#' @name effectsize_options
Expand Down
41 changes: 17 additions & 24 deletions R/utils_ci.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
ncp <- suppressWarnings(stats::optim(
par = 1.1 * rep(lambda, 2),
fn = function(x) {
p <- stats::pf(q = f, df, df_error, ncp = x)

abs(max(p) - probs[2]) +
abs(min(p) - probs[1])
q <- stats::qf(p = probs, df, df_error, ncp = x)

Check warning on line 18 in R/utils_ci.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils_ci.R,line=18,col=7,[object_overwrite_linter] 'q' is an exported object from package 'base'. Avoid re-using such symbols.
sum(abs(q - f))
},
control = list(abstol = 1e-09)
))
Expand All @@ -37,56 +35,51 @@

#' @keywords internal
.get_ncp_t <- function(t, df_error, conf.level = 0.95) {
# # Note: these aren't actually needed - all t related functions would fail earlier
# if (!is.finite(t) || !is.finite(df_error)) {
# return(c(NA, NA))
# }
if (!is.finite(t) || !is.finite(df_error)) {
return(c(NA, NA))

Check warning on line 39 in R/utils_ci.R

View check run for this annotation

Codecov / codecov/patch

R/utils_ci.R#L39

Added line #L39 was not covered by tests
}

alpha <- 1 - conf.level
probs <- c(alpha / 2, 1 - alpha / 2)

ncp <- suppressWarnings(stats::optim(
par = 1.1 * rep(t, 2),
fn = function(x) {
p <- stats::pt(q = t, df = df_error, ncp = x)

abs(max(p) - probs[2]) +
abs(min(p) - probs[1])
q <- stats::qt(p = probs, df = df_error, ncp = x)

Check warning on line 48 in R/utils_ci.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils_ci.R,line=48,col=7,[object_overwrite_linter] 'q' is an exported object from package 'base'. Avoid re-using such symbols.
sum(abs(q - t))
},
control = list(abstol = 1e-09)
))

t_ncp <- unname(sort(ncp$par))

return(t_ncp)
}

#' @keywords internals
.get_ncp_chi <- function(chi, df, conf.level = 0.95) {
# # Note: these aren't actually needed - all chisq related functions would fail earlier
# if (!is.finite(chi) || !is.finite(df)) {
# return(c(NA, NA))
# }
.get_ncp_chi <- function(chisq, df, conf.level = 0.95) {
if (!is.finite(chisq) || !is.finite(df)) {
return(c(NA, NA))

Check warning on line 62 in R/utils_ci.R

View check run for this annotation

Codecov / codecov/patch

R/utils_ci.R#L62

Added line #L62 was not covered by tests
}

alpha <- 1 - conf.level
probs <- c(alpha / 2, 1 - alpha / 2)

ncp <- suppressWarnings(stats::optim(
par = 1.1 * rep(chi, 2),
par = 1.1 * rep(chisq, 2),
fn = function(x) {
p <- stats::pchisq(q = chi, df, ncp = x)

abs(max(p) - probs[2]) +
abs(min(p) - probs[1])
q <- stats::qchisq(p = probs, df, ncp = x)

Check warning on line 71 in R/utils_ci.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils_ci.R,line=71,col=7,[object_overwrite_linter] 'q' is an exported object from package 'base'. Avoid re-using such symbols.
sum(abs(q - chisq))
},
control = list(abstol = 1e-09)
))
chi_ncp <- sort(ncp$par)

if (chi <= stats::qchisq(probs[1], df)) {
if (chisq <= stats::qchisq(probs[1], df)) {
chi_ncp[2] <- 0
}

if (chi <= stats::qchisq(probs[2], df)) {
if (chisq <= stats::qchisq(probs[2], df)) {
chi_ncp[1] <- 0
}

Expand All @@ -107,7 +100,7 @@
ci > 1) {
insight::format_error("ci must be a single numeric value between (0, 1)")
}
return(TRUE)

Check warning on line 103 in R/utils_ci.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils_ci.R,line=103,col=3,[return_linter] Use implicit return behavior; explicit return() is not needed.
}

#' @keywords internal
Expand Down
2 changes: 1 addition & 1 deletion man/cohens_d.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/effectsize-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 2 additions & 10 deletions man/effectsize_CIs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading