# NAG Library Function Documentnag_cov_to_corr (g02bwc)

## 1  Purpose

nag_cov_to_corr (g02bwc) calculates a matrix of Pearson product-moment correlation coefficients from sums of squares and cross-products of deviations about the mean.

## 2  Specification

#include <nag.h>
#include <nagg02.h>

void  nag_cov_to_corr (Integer m, double r[],  NagError *fail)

## 3  Description

nag_cov_to_corr (g02bwc) calculates a matrix of Pearson product-moment correlation coefficients from sums of squares and cross-products about the mean for observations on $m$ variables which can be computed by a single call to nag_sum_sqs (g02buc) or a series of calls to nag_sum_sqs_update (g02btc). The sums of squares and cross-products are stored in an array packed by column and are overwritten by the correlation coefficients.
Let ${c}_{jk}$ be the cross-product of deviations from the mean, for $\mathit{j}=1,2,\dots ,m$ and $\mathit{k}=j,\dots ,m$, then the product-moment correlation coefficient, ${r}_{jk}$ is given by

$rjk=cjkcjjckk .$

None.

## 5  Arguments

1:     m – IntegerInput
On entry: $m$, the number of variables.
Constraint: ${\mathbf{m}}\ge 1$.
2:     r[$\left({\mathbf{m}}×{\mathbf{m}}+{\mathbf{m}}\right)/2$] – doubleInput/Output
On entry: contains the upper triangular part of the sums of squares and cross-products matrix of deviations from the mean. These are stored packed by column, i.e., the cross-product between variable $j$ and $k$, $k\ge j$, is stored in ${\mathbf{r}}\left[\left(k×\left(k-1\right)/2+j\right)-1\right]$.
On exit: Pearson product-moment correlation coefficients.
These are stored packed by column corresponding to the input cross-products.
3:     fail – NagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_BAD_PARAM
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}\ge 1$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
NE_ZERO_VARIANCE
On entry, a variable has zero variance.

## 7  Accuracy

The accuracy of nag_cov_to_corr (g02bwc) is entirely dependent upon the accuracy of the elements of array r.

## 8  Further Comments

nag_cov_to_corr (g02bwc) may also be used to calculate the correlations between parameter estimates from the variance-covariance matrix of the parameter estimates as is given by several functions in this chapter.

## 9  Example

A program to calculate the correlation matrix from raw data. The sum of squares and cross-products about the mean are calculated from the raw data by a call to nag_sum_sqs (g02buc). The correlation matrix is then calculated from these values.

### 9.1  Program Text

Program Text (g02bwce.c)

### 9.2  Program Data

Program Data (g02bwce.d)

### 9.3  Program Results

Program Results (g02bwce.r)
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 16, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.6624400615692139, "perplexity_flag": "middle"}