Skip to content

Instantly share code, notes, and snippets.

@behrica
Created September 15, 2021 16:04
Show Gist options
  • Save behrica/f764b9c60b29a584680f574de2f12a59 to your computer and use it in GitHub Desktop.
Save behrica/f764b9c60b29a584680f574de2f12a59 to your computer and use it in GitHub Desktop.
;; calculation function
(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
(fastmath.stats/pearson-correlation (data col-1) (data col-2))
2 :DOWN)))})))
;; heatmap hanami template
(def heatmap-chart
{
:data :UDATA
:encoding {:x {:field :col-1 :sort :COLS}
:y {:field :col-2 :sort :COLS}}
:height :HEIGHT :width :WIDTH
:layer [{
:mark "rect"
:encoding
{
:color {:field :corr :type :quantitative
:scale {:scheme "redgrey" :reverse true}}}}
{:mark "text",
:encoding
{:text {:field :corr :type "quantitative"}
:color
{:condition {"test" "datum['corr'] < 0.2"
"value" "white"},
"value" "black"}}}]})
(def simple-data (ds/dataset {:x [1 2 3] :y [4 5 6] :z [9 8 7]}))
(def cols-to-use [:x :y :z])
^kind/vega
(hc/xform
heatmap-chart
:HEIGHT 500
:WIDTH 500
:COLS cols-to-use
:UDATA {:values (calc-correlations-matrix simple-data cols-to-use)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment