correlation_pvalue
C++ Function Reference
1 Signature
double BigDataStatMeth::correlation_pvalue(double r, int n)2 Description
Compute p-value for correlation coefficient - R equivalent implementation.
3 Parameters
r(double): Correlation coefficient [-1, 1]n(int): Sample size (number of observations)
4 Returns
Two-tailed p-value [0, 1], or NaN if invalid input
5 Details
Implements the exact equivalent of R’s correlation test: t<-(rsqrt(n-2))/sqrt(1-r^2)p<-2(pt(abs(t),(n-2),lower.tail=FALSE))
6 Call Graph
7 Source Code
NoteImplementation
File: inst/include/hdf5Algebra/matrixCorrelation.hpp • Lines 355-372
inline double correlation_pvalue(double r, int n) {
if (std::isnan(r) || n < 3 || std::abs(r) >= 1.0) {
return std::numeric_limits<double>::quiet_NaN();
}
// Calculate t-statistic exactly as in R: t <- (r*sqrt(n-2))/sqrt(1-r^2)
double df = n - 2;
double t_stat = (r * std::sqrt(df)) / std::sqrt(1.0 - r * r);
// Calculate p-value exactly as in R: p <- 2*(pt(abs(t),(n-2), lower.tail=FALSE))
// pt(abs(t), df, lower.tail=FALSE) = 1 - pt(abs(t), df, lower.tail=TRUE)
double abs_t = std::abs(t_stat);
double cdf_value = t_distribution_cdf(abs_t, df);
double p_value = 2.0 * (1.0 - cdf_value);
// Ensure p-value is in valid range [0, 1]
return std::max(0.0, std::min(1.0, p_value));
}8 Usage Example
#include "BigDataStatMeth.hpp"
// Example usage
auto result = correlation_pvalue(...);