## Linear Regression

### June 10, 2016

Here’s our program:

```(define (regression xs ys)
(let loop ((xs xs) (ys ys) (n 0)
(x 0) (y 0) (xy 0) (xx 0))
(if (pair? xs)
(loop (cdr xs) (cdr ys) (+ n 1)
(+ x (car xs)) (+ y (car ys))
(+ xy (* (car xs) (car ys)))
(+ xx (* (car xs) (car xs))))
(let ((m (/ (- (* n xy) (* x y))
(- (* n xx) (* x x)))))
(list m (/ (- y (* m x)) n))))))```

And here’s the program run against the sample data:

```> (regression '(60 61 62 63 65) '(3.1 3.6 3.8 4 4.1))
(0.18783783783783292 -7.963513513513208)
> (- (* 0.1878 64) 7.9635)
4.0557```

You can run the program at http://ideone.com/9QJGUQ.

Pages: 1 2

### 5 Responses to “Linear Regression”

1. Daniel said

Here’s a solution in matlab. The same approach can be used for multiple regression, but X would have additional columns for the additional variables.

```x = [60;61;62;63;65];
y = [3.1;3.6;3.8;4.0;4.1];

X = [ones(size(x,1),1), x];

w = X \ y

y_hat = [1,64] * w
```

Output:

```w =
-7.9635
0.1878

y_hat =
4.0581
```
2. Jeff said

Shouldn’t the calculation for b be:

b = (Σy / n – m × Σx / n)

3. programmingpraxis said

Fixed. Thank you.

4. Jeff said