Created
September 20, 2021 15:27
-
-
Save behrica/0d73e3c28a14886073e83627ffb295aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns behrica.corr-matrix | |
| (:require [fastmath.stats :as stats] | |
| [aerial.hanami.common :as hc] | |
| [aerial.hanami.templates :as ht] | |
| [aerial.hanami.core :as hmi])) | |
| (defn round | |
| [n scale rm] | |
| (.setScale ^java.math.BigDecimal (bigdec n) | |
| (int scale) | |
| ^RoundingMode (if (instance? java.math.RoundingMode rm) | |
| rm | |
| (java.math.RoundingMode/valueOf | |
| (str (if (ident? rm) (symbol rm) rm)))))) | |
| (defn calc-correlations-matrix [data cols-to-use] | |
| (doall | |
| (for [col-1 cols-to-use col-2 cols-to-use] | |
| {:col-1 col-1 | |
| :col-2 col-2 | |
| :corr | |
| (Float/valueOf | |
| (str | |
| (round | |
| (stats/pearson-correlation (data col-1) (data col-2)) | |
| 2 :DOWN)))}))) | |
| (def text-encoding | |
| (-> ht/xy-encoding | |
| (assoc :text | |
| {:field :CORR | |
| :type "quantitative"} | |
| :color {:condition | |
| {"test" {:field :CORR :lt 0} "value" "black"} | |
| "value" "white"}) | |
| (dissoc :x :y :tooltip))) | |
| (def text-layer | |
| (-> ht/view-base | |
| (assoc :mark (merge ht/mark-base {:type "text"}) | |
| :encoding text-encoding) | |
| (dissoc :usermeta :data :height :width))) | |
| (def rect-layer | |
| (-> ht/view-base | |
| (assoc :mark (merge ht/mark-base {:type "rect"}) | |
| :encoding (-> ht/xy-encoding | |
| (dissoc :x :y :tooltip) | |
| (assoc :color {:field :CORR :type "quantitative" | |
| :scale {:scheme "redgrey" :reverse true}}))) | |
| (dissoc :usermeta :data :height :width))) | |
| (def heatmap-chart2 | |
| (assoc ht/layer-chart | |
| :encoding ht/xy-encoding | |
| :layer [rect-layer text-layer])) | |
| (def heatmap-chart | |
| { | |
| :data :UDATA | |
| :encoding :ENCODING | |
| :height :HEIGHT :width :WIDTH | |
| :layer [{ | |
| :mark "rect" | |
| :encoding | |
| { | |
| :color {:field :CORR-COL :type :quantitative | |
| :scale {:scheme "redgrey" :reverse true}}}} | |
| {:mark "text", | |
| :encoding | |
| {:text {:field :CORR-COL :type "quantitative"} | |
| :color | |
| {:condition {"test" {:field :CORR-COL :lt 0} "value" "green"}, | |
| "value" "blue"}}}]}) | |
| (def correlation-chart | |
| { | |
| :data :UDATA | |
| :height :HEIGHT :width :WIDTH | |
| :layer [{ | |
| :mark "point" | |
| :encoding | |
| {:x { :field :COL-NAME :type :quantitative} | |
| :y { :field :BASE-COL-NAME :type :quantitative}}} | |
| {:mark {:type :line :color :firebrick} | |
| :transform | |
| [{:regression :BASE-COL-NAME :on :COL-NAME}] | |
| :encoding | |
| {:x {:field :COL-NAME :type :quantitative} | |
| :y {:field :BASE-COL-NAME :type :quantitative}}} | |
| {:transform | |
| [{:regression :BASE-COL-NAME | |
| :on :COL-NAME | |
| :params true} | |
| {:calculate "'R²: '+format(datum.rSquared, '.2f')", :as "R2"}], | |
| :mark | |
| {:type "text", :color "firebrick", :x "width", :align "right", :y -5}, | |
| :encoding {:text {:type "nominal", :field "R2"}}}]}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment