# Correlation coefficient calculator (Pearson and Spearman)

> Correlation coefficient calculator: Pearson's r or Spearman's rank correlation for paired data, with r², a t statistic and a two-sided p-value.

Phiên bản tương tác: https://www.calcopenly.com/vi/statistics/correlation-coefficient-calculator
Chủ đề: Máy tính thống kê và xác suất

Pearson's r measures how closely paired values follow a straight line: r = Sxy/√(Sxx·Syy), the sum of cross-products of deviations from the two means divided by the square root of the product of the two sums of squares. It runs from −1, a perfect falling line, through 0 to +1, a perfect rising line. Spearman's ρ is Pearson's r computed on the ranks, so it measures any steadily rising or falling trend and is less affected by outliers. The p-value comes from t = r√((n − 2)/(1 − r²)) with n − 2 degrees of freedom.

The default pairs hours studied with exam scores for 10 students: r = 0.9816, r² = 0.9636 and p = 4.9 × 10⁻⁷, a strong positive association.

A correlation near 0 rules out only a straight-line relationship: y = x² for x from −3 to 3 has r = 0 exactly.

## Dữ liệu đầu vào

- **x values**: Hours studied (default example).
- **y values**: Exam score for the same students, in the same order.
- **Coefficient** (lựa chọn: Pearson r, Spearman ρ)

## Kết quả

- Correlation coefficient — kết quả chính
- Strength
- Coefficient of determination r²
- t statistic
- Degrees of freedom
- p-value (two-sided)
- Pairs n

## Công thức

$$
r = \frac{\sum (x_i-\bar x)(y_i-\bar y)}{\sqrt{\sum (x_i-\bar x)^2 \sum (y_i-\bar y)^2}},\qquad t = r\sqrt{\frac{n-2}{1-r^2}}
$$

## Ví dụ có lời giải

### Hours studied vs score (default)

- x values: 1, 2, 2, 3, 4, 5, 5, 6, 7, 8
- y values: 52, 55, 61, 58, 66, 70, 68, 75, 79, 84
- Coefficient: Pearson r
- **Correlation coefficient: 0.981638**
- **Coefficient of determination r²: 0.963613**
- **t statistic: 14.555426**
- **Degrees of freedom: 8**
- **p-value (two-sided): 4.865 × 10⁻⁷**
- **Strength: Strong positive**
- Nguồn đối chiếu: Python fractions for Sxx, Syy, Sxy with a decimal square root; p from the closed-form Student t CDF for integer df (Abramowitz & Stegun 26.7.4)

### Negative association

- x values: 10, 20, 30, 40, 50, 60
- y values: 8.1, 7.4, 7.9, 6.2, 5.8, 6.0
- Coefficient: Pearson r
- **Correlation coefficient: -0.891042**
- **t statistic: -3.925982**
- **p-value (two-sided): 0.017161**
- **Strength: Strong negative**
- Nguồn đối chiếu: Python fractions with decimal square roots; p = 2·(1 − F_t(|t|; 4)) from the A&S 26.7.4 closed form

### Spearman with tied ranks

- x values: 1, 2, 2, 3, 4, 5, 5, 6
- y values: 3, 1, 4, 4, 5, 9, 2, 6
- Coefficient: Spearman ρ
- **Correlation coefficient: 0.575768**
- **t statistic: 1.724946**
- **p-value (two-sided): 0.135297**
- **Degrees of freedom: 6**
- **Strength: Strong positive**
- Nguồn đối chiếu: Python: ties averaged by hand (x ranks 1, 2.5, 2.5, 4, 5, 6.5, 6.5, 8), Pearson r of the ranks with fractions (Sxy = 95/4, Sxx = 41, Syy = 83/2), p from the A&S 26.7.4 t CDF

### Perfect straight line

- x values: 1, 2, 3, 4
- y values: 3, 5, 7, 9
- Coefficient: Pearson r
- **Correlation coefficient: 1**
- **Coefficient of determination r²: 1**
- **p-value (two-sided): 0**
- **Strength: Strong positive**
- Nguồn đối chiếu: y = 2x + 1 exactly, so r = 1 by definition and no sample could be more extreme

## Câu hỏi

### What does a correlation coefficient of 0.7 mean?

A fairly strong positive linear association: as x rises, y tends to rise, and r² = 0.49 says a straight line accounts for 49% of the variation in y. Cohen (1988) called r = 0.1 small, 0.3 medium and 0.5 large, and the Strength output uses those cut-offs. They are rough conventions from the behavioural sciences, so what counts as strong still depends on the field.

### What is the difference between Pearson and Spearman correlation?

Pearson's r measures linear association using the values themselves; Spearman's ρ applies the same formula to their ranks, so it measures any monotonic trend. For y = x³ with x from 1 to 10, Spearman gives exactly 1 but Pearson gives 0.928. Spearman also resists outliers and suits ordinal data such as ratings, while Pearson's r is the one that matches a least-squares line.

### Does correlation imply causation?

No. A correlation shows that two variables move together, not why. A third variable can drive both, as hot weather raises both ice-cream sales and drownings. The cause can also run the other way, or the pattern can be chance: test 20 unrelated pairs at α = 0.05 and about one will look significant. Showing cause takes a randomized experiment or a careful causal design.

### How do you test whether a correlation is significant?

Convert r to t = r√((n − 2)/(1 − r²)) and compare it with a t distribution with n − 2 degrees of freedom. The default data give r = 0.9816 with n = 10, so t = 14.56 on 8 df and p = 4.9 × 10⁻⁷. Significance depends heavily on n: with 1,000 pairs, r = 0.07 already gives p = 0.027, although it explains under 0.5% of the variation.

### “Correlation coefficient calculator (Pearson and Spearman)” chính xác đến mức nào?

Độ chính xác phụ thuộc vào dữ liệu nhập và giả định của phương pháp. Phép tính thập phân dùng 50 chữ số có nghĩa, nhưng ước lượng, phương pháp số và dữ liệu nguồn có thể kém chính xác hơn; làm tròn khi hiển thị không loại bỏ các giới hạn đó. Ví dụ có lời giải đã đối chiếu với nguồn độc lập: 4. Ví dụ, “Hours studied vs score (default)” được kiểm tra bằng Python fractions for Sxx, Syy, Sxy with a decimal square root; p from the closed-form Student t CDF for integer df (Abramowitz & Stegun 26.7.4).

### Phương pháp này lấy từ đâu?

NIST/SEMATECH e-Handbook of Statistical Methods, §7.1.3 / Dataplot CORRELATION (Pearson and rank correlation); Spearman (1904), The proof and measurement of association between two things, American Journal of Psychology 15; Cohen (1988), Statistical Power Analysis for the Behavioral Sciences, 2nd ed., §3.2 (r = 0.1, 0.3, 0.5).

## Nguồn

- [NIST/SEMATECH e-Handbook of Statistical Methods, §7.1.3 / Dataplot CORRELATION (Pearson and rank correlation)](https://www.itl.nist.gov/div898/handbook/prc/section1/prc13.htm)
- Spearman (1904), The proof and measurement of association between two things, American Journal of Psychology 15
- Cohen (1988), Statistical Power Analysis for the Behavioral Sciences, 2nd ed., §3.2 (r = 0.1, 0.3, 0.5)
