-
Notifications
You must be signed in to change notification settings - Fork 1
/
statistical_visualization.clj
84 lines (68 loc) · 2.13 KB
/
statistical_visualization.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
;; # Statistical Visualization (experimental 🛠)
;; author: Daniel Slutsky
(ns noj-book.statistical-visualization
(:require [aerial.hanami.templates :as ht]
[noj-book.datasets :as datasets]
[scicloj.kindly.v4.kind :as kind]
[scicloj.noj.v1.stats :as stats]
[scicloj.noj.v1.vis.hanami :as vis.hanami]
[scicloj.noj.v1.vis.stats :as vis.stats]
[tablecloth.api :as tc]))
;; ## Linear regression
(-> datasets/mtcars
(stats/add-predictions :mpg [:wt]
{:model-type :smile.regression/ordinary-least-square})
(vis.hanami/combined-plot
ht/layer-chart
{:X :wt
:MSIZE 200
:HEIGHT 200}
:LAYER [[ht/point-chart
{:Y :mpg
:WIDTH 200}]
[ht/line-chart
{:Y :mpg-prediction
:MSIZE 5
:MCOLOR "purple"
:YTITLE :mpg}]]))
;; Alternatively:
(-> datasets/mtcars
(vis.stats/linear-regression-plot
:mpg :wt
{:HEIGHT 200
:WIDTH 200
:point-options {:MSIZE 200}
:line-options {:MSIZE 5
:MCOLOR "purple"}}))
;; And in a grouped dataset case:
(-> datasets/mtcars
(tc/group-by [:gear])
(vis.stats/linear-regression-plot
:mpg :wt
{:HEIGHT 200
:WIDTH 200
:point-options {:MSIZE 200}
:line-options {:MSIZE 5
:MCOLOR "purple"}}))
;; ## Histogram
;; A [histogram](https://en.wikipedia.org/wiki/Histogram) groups values in bins,
;; counts them,
;; and creates a corresponding bar-chart.
;;
;; The `vis.stats/histogram` functions does that behind the scenes,
;; and generates a Vega-Lite spec using Hanami.
(-> datasets/iris
(vis.stats/histogram :sepal-width
{:nbins 10}))
(-> datasets/iris
(vis.stats/histogram :sepal-width
{:nbins 10})
kind/pprint)
;; The resulting spec can be customized further:
(-> datasets/iris
(vis.stats/histogram :sepal-width
{:nbins 10})
;; varying the resulting vega-lite spec:
(assoc :height 125
:width 175))
:bye