Skip to content

Commit cc4e2ea

Browse files
committed
add r_crit()
1 parent 7bf8fcf commit cc4e2ea

File tree

5 files changed

+115
-2
lines changed

5 files changed

+115
-2
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: funtimes
22
Type: Package
33
Title: Functions for Time Series Analysis
4-
Version: 9.1
5-
Date: 2023-03-21
4+
Version: 9.2
5+
Date: 2023-04-28
66
Authors@R: c(person("Vyacheslav", "Lyubchich",
77
role = c("aut", "cre"),
88
comment = c(ORCID = "0000-0001-7936-4285"),

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export(notrend.test)
2121
export(notrend_test)
2222
export(purity)
2323
export(q.tails)
24+
export(r_crit)
2425
export(sync.cluster)
2526
export(sync.test)
2627
export(sync_cluster)
@@ -58,6 +59,7 @@ importFrom(stats,na.omit)
5859
importFrom(stats,pnorm)
5960
importFrom(stats,predict)
6061
importFrom(stats,qnorm)
62+
importFrom(stats,qt)
6163
importFrom(stats,quantile)
6264
importFrom(stats,residuals)
6365
importFrom(stats,rnorm)

R/r_crit.R

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#' Critical Value for Correlation Coefficient
2+
#'
3+
#' Calculate critical value for correlation coefficient,
4+
#' for a given sample size and confidence level.
5+
#' If absolute value of an observed correlation coefficient is higher
6+
#' than the critical value, then the correlation is statistically significant
7+
#' with the specified confidence.
8+
#' This approach is
9+
#' identical to using textbook tables of critical values and
10+
#' alternative to calculating \eqn{p}-values.
11+
#'
12+
#' @details
13+
#' Using Student's \eqn{t}-distribution, the critical value is
14+
#' \deqn{r_{crit} = \frac{t}{\sqrt{n - 2 + t^2}},}
15+
#' where \eqn{t} is a quantile of \eqn{t}-distribution with
16+
#' \eqn{n - 2} degrees of freedom for probability \eqn{1 - (1 - conf.level)/2}.
17+
#'
18+
#' @param n sample size(s) used to calculate correlations.
19+
#' Values of \eqn{n < 4} are omitted.
20+
#' @param conf.level confidence level for calculating the critical value(s).
21+
#' Default is 0.95 (i.e., confidence of 95%).
22+
#' If length of the input is higher than 1, only the first element is used.
23+
#' @param method Method for calculating the critical values.
24+
#' Currently only the method based on \eqn{t}-distribution is used.
25+
#'
26+
#' @return A vector of critical values.
27+
#'
28+
#' @references
29+
#' \insertAllCited{}
30+
#'
31+
#' @seealso \code{\link[stats]{cor.test}}, \code{\link[stats]{cor}}
32+
#'
33+
#' @keywords correlation threshold
34+
#'
35+
#' @author Vyacheslav Lyubchich
36+
#'
37+
#' @importFrom stats qt
38+
#' @export
39+
#' @examples
40+
#' r_crit(120)
41+
#' r_crit(20:30, conf.level = 0.9)
42+
#'
43+
r_crit <- function(n,
44+
conf.level = 0.95,
45+
method = c("t")) {
46+
if (any(n < 4)) {
47+
warning("Values of n < 4 were omitted.")
48+
n <- n[n >= 4]
49+
}
50+
alpha <- 1 - conf.level[1]
51+
t2 <- qt(p = 1 - alpha/2, df = n - 2)^2
52+
sqrt(t2 / (n - 2 + t2))
53+
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ library(funtimes)
1818
## Future work
1919

2020
- Remove defunct functions.
21+
- Describe the statistics in causality_pred()
2122

23+
## Current GitHub version
24+
25+
- Added new function `r_crit()` to compute critical values of correlations.
2226

2327
## CRAN version 9.1
2428

man/r_crit.Rd

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)