0
# Regression Analysis
1
2
Functions for linear regression analysis and correlation calculations.
3
4
## Core Imports
5
6
```typescript
7
import {
8
linearRegression,
9
linearRegressionLine,
10
rSquared,
11
sampleCorrelation,
12
sampleCovariance
13
} from "simple-statistics";
14
```
15
16
## Linear Regression
17
18
### linearRegression { .api }
19
20
```typescript { .api }
21
interface RegressionResult {
22
m: number; // slope
23
b: number; // y-intercept
24
}
25
26
function linearRegression(data: Array<[number, number]>): RegressionResult;
27
```
28
29
Performs simple linear regression using the least squares method to find the best-fit line through a set of points.
30
31
**Parameters:**
32
- `data: Array<[number, number]>` - Array of [x, y] coordinate pairs
33
34
**Returns:** `RegressionResult` - Object containing slope (m) and y-intercept (b)
35
36
**Formula:** y = mx + b
37
38
```typescript
39
import { linearRegression } from "simple-statistics";
40
41
const data = [[1, 1], [2, 2], [3, 1.3], [4, 3.75], [5, 2.25]];
42
const regression = linearRegression(data);
43
// { m: 0.425, b: 0.785 }
44
// Line equation: y = 0.425x + 0.785
45
```
46
47
### linearRegressionLine { .api }
48
49
```typescript { .api }
50
function linearRegressionLine(mb: RegressionResult): (x: number) => number;
51
```
52
53
Creates a function from regression parameters that can predict y values for given x values.
54
55
**Parameters:**
56
- `mb: RegressionResult` - Object with slope (m) and y-intercept (b)
57
58
**Returns:** `(x: number) => number` - Function that predicts y from x
59
60
```typescript
61
import { linearRegression, linearRegressionLine } from "simple-statistics";
62
63
const data = [[1, 1], [2, 2], [3, 1.3], [4, 3.75], [5, 2.25]];
64
const regression = linearRegression(data);
65
const predictY = linearRegressionLine(regression);
66
67
const prediction = predictY(6); // 3.335
68
const prediction2 = predictY(0); // 0.785 (y-intercept)
69
```
70
71
### rSquared { .api }
72
73
```typescript { .api }
74
function rSquared(data: Array<[number, number]>, func: (x: number) => number): number;
75
```
76
77
Calculates the coefficient of determination (R²) which measures how well the regression line fits the data.
78
79
**Parameters:**
80
- `data: Array<[number, number]>` - Array of [x, y] coordinate pairs
81
- `func: (x: number) => number` - Regression function (from linearRegressionLine)
82
83
**Returns:** `number` - R² value between 0 and 1 (higher is better fit)
84
85
```typescript
86
import { linearRegression, linearRegressionLine, rSquared } from "simple-statistics";
87
88
const data = [[1, 1], [2, 2], [3, 1.3], [4, 3.75], [5, 2.25]];
89
const regression = linearRegression(data);
90
const line = linearRegressionLine(regression);
91
const fit = rSquared(data, line); // 0.261...
92
93
// R² = 0.261 means 26.1% of variance is explained by the regression
94
```
95
96
## Correlation Analysis
97
98
### sampleCorrelation { .api }
99
100
```typescript { .api }
101
function sampleCorrelation(x: number[], y: number[]): number;
102
```
103
104
Calculates the Pearson correlation coefficient between two variables, measuring linear relationship strength.
105
106
**Parameters:**
107
- `x: number[]` - First variable values
108
- `y: number[]` - Second variable values (same length as x)
109
110
**Returns:** `number` - Correlation coefficient between -1 and 1
111
- 1: Perfect positive correlation
112
- 0: No linear correlation
113
- -1: Perfect negative correlation
114
115
```typescript
116
import { sampleCorrelation } from "simple-statistics";
117
118
const heights = [60, 65, 70, 75, 80];
119
const weights = [120, 140, 160, 180, 200];
120
const correlation = sampleCorrelation(heights, weights); // 1.0 (perfect positive)
121
122
const negativeExample = [1, 2, 3, 4, 5];
123
const negativeY = [5, 4, 3, 2, 1];
124
const negCorr = sampleCorrelation(negativeExample, negativeY); // -1.0 (perfect negative)
125
```
126
127
### sampleCovariance { .api }
128
129
```typescript { .api }
130
function sampleCovariance(x: number[], y: number[]): number;
131
```
132
133
Calculates how much two variables change together. Covariance indicates direction of linear relationship but not strength.
134
135
**Parameters:**
136
- `x: number[]` - First variable values
137
- `y: number[]` - Second variable values (same length as x)
138
139
**Returns:** `number` - Covariance value
140
- Positive: Variables tend to increase together
141
- Negative: One increases as other decreases
142
- Zero: No linear relationship
143
144
```typescript
145
import { sampleCovariance } from "simple-statistics";
146
147
const x = [1, 2, 3, 4, 5];
148
const y = [2, 4, 6, 8, 10];
149
const covariance = sampleCovariance(x, y); // 5.0 (positive covariance)
150
```
151
152
## Usage Examples
153
154
### Complete Regression Analysis
155
156
```typescript
157
import {
158
linearRegression,
159
linearRegressionLine,
160
rSquared,
161
sampleCorrelation
162
} from "simple-statistics";
163
164
// Sales data: [advertising_spend, sales_revenue]
165
const salesData = [
166
[1000, 50000], [1500, 55000], [2000, 65000],
167
[2500, 70000], [3000, 80000], [3500, 85000]
168
];
169
170
// Perform regression
171
const regression = linearRegression(salesData);
172
console.log(`Slope: ${regression.m}`); // Revenue increase per $ of advertising
173
console.log(`Intercept: ${regression.b}`); // Base revenue with no advertising
174
175
// Create prediction function
176
const predictRevenue = linearRegressionLine(regression);
177
const predicted4000 = predictRevenue(4000); // Predict revenue for $4000 advertising
178
179
// Measure fit quality
180
const rSquaredValue = rSquared(salesData, predictRevenue);
181
console.log(`R²: ${rSquaredValue}`); // How well the model explains variance
182
183
// Calculate correlation
184
const advertising = salesData.map(d => d[0]);
185
const revenue = salesData.map(d => d[1]);
186
const correlation = sampleCorrelation(advertising, revenue);
187
console.log(`Correlation: ${correlation}`); // Strength of linear relationship
188
```
189
190
### Comparing Regression Models
191
192
```typescript
193
import { linearRegression, linearRegressionLine, rSquared } from "simple-statistics";
194
195
const data = [[1, 2], [2, 5], [3, 7], [4, 10], [5, 15]];
196
197
// Linear model
198
const linearModel = linearRegression(data);
199
const linearPredict = linearRegressionLine(linearModel);
200
const linearR2 = rSquared(data, linearPredict);
201
202
console.log(`Linear R²: ${linearR2}`);
203
// Compare with other models to find best fit
204
```