File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
Clojure-Websockets/MainApp/src/cljs/birdwatch/stats Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
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]))))
You can’t perform that action at this time.
0 commit comments