|
| 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 | +} |
0 commit comments