Skip to content

Commit 7affa4f

Browse files
committed
regression namespace, for now with linear-regression function.
Adapted from Common Lisp: http://compbio.ucdenver.edu/Hunter_lab/Hunter/cl-statistics.lisp
1 parent 83677c4 commit 7affa4f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
(ns birdwatch.stats.regression)
2+
3+
(enable-console-print!)
4+
5+
(defn square [x] (* x x))
6+
(defn mean [xs]
7+
(let [cnt (count xs)]
8+
(when (pos? cnt) (/ (apply + xs) cnt))))
9+
10+
; adapted from from http://compbio.ucdenver.edu/Hunter_lab/Hunter/cl-statistics.lisp
11+
(defn linear-regression [ys]
12+
(let [n (count ys)]
13+
(when (pos? n)
14+
(let [xs (range n)
15+
x-bar (mean xs)
16+
y-bar (mean ys)
17+
Lxx (reduce + (map (fn [xi] (square (- xi x-bar))) xs))
18+
Lyy (reduce + (map (fn [yi] (square (- yi y-bar))) ys))
19+
Lxy (reduce + (map (fn [xi yi] (* (- xi x-bar) (- yi y-bar))) xs ys))
20+
slope (/ Lxy Lxx)
21+
intercept (- y-bar (* slope x-bar))
22+
reg-ss (* slope Lxy)
23+
res-ms (/ (- Lyy reg-ss) (- n 2))
24+
r (/ Lxy (Math/sqrt (* Lxx Lyy)))
25+
r2 (/ reg-ss Lyy)]
26+
[intercept slope r r2]))))

0 commit comments

Comments
 (0)