Skip to content

Commit a688c0c

Browse files
committed
linear regression analysis used for determining ratio trend
1 parent d23abcd commit a688c0c

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

Clojure-Websockets/MainApp/src/cljs/birdwatch/charts/wordcount_chart.cljs

+12-10
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@
3030
[:polygon {:transform arrowTrans :stroke "none" :fill color :points points}]))
3131

3232
(defn bar [text cnt y h w idx]
33-
(let [pos-slope (get @pos-trends text)]
33+
(let [pos-slope (get @pos-trends text)
34+
ratio-slope (get @ratio-trends text)]
3435
[:g
3536
[:text {:y (+ y 8) :x 138 :stroke "none" :fill "black" :dy ".35em" :textAnchor "end"} text]
36-
[arrow 146 y (cond
37-
(pos? pos-slope) :UP
38-
(neg? pos-slope ) :DOWN
39-
:else :RIGHT)]
40-
[arrow 160 y :RIGHT]
37+
[arrow 146 y (cond (pos? pos-slope) :UP (neg? pos-slope ) :DOWN :else :RIGHT)]
38+
[arrow 160 y (cond (pos? ratio-slope) :RIGHT-UP (neg? ratio-slope ) :RIGHT-DOWN :else :RIGHT)]
4139
[:rect {:y y :x 168 :height 15 :width w :stroke "white" :fill "#428bca"}]
4240
(if (> w 50)
4341
[:text (merge text-defaults {:y (+ y 8) :x (+ w 160)}) cnt]
@@ -66,8 +64,12 @@
6664
"update wordcount chart"
6765
[words]
6866
(reset! items (vec (map-indexed vector words)))
69-
(doseq [[idx [text cnt]] @items]
70-
(let [slope (get (reg/linear-regression (take 3 (get @pos-items text))) 1)]
67+
(let [items @items
68+
total-cnt (apply + (map (fn [[_[_ cnt]]] cnt) items))]
69+
(doseq [[idx [text cnt]] items]
7170
(swap! pos-items update-in [text] conj idx)
72-
(swap! pos-trends assoc-in [text] slope))))
73-
71+
(swap! ratio-items update-in [text] conj (/ total-cnt cnt))
72+
(swap! pos-trends assoc-in [text]
73+
(get (reg/linear-regression (take 3 (get @pos-items text))) 1))
74+
(swap! ratio-trends assoc-in [text]
75+
(get (reg/linear-regression (take 1000 (get @ratio-items text))) 1)))))

0 commit comments

Comments
 (0)