Skip to content

Instantly share code, notes, and snippets.

(use '(incanter core charts io))
;; create points to plot
(def x1 (range 0.1 5 0.1))
;; highlight a couple different sets of critical points
(def crit-pts1 [0.5 2 3])
(def crit-pts2 [0.25 2.5 3.5])
;; define a reciprocal function
(defn reciprocal [x] (div 1 x))
(use '(incanter stats charts io))
(doto (box-plot (sample-gamma 1000 :shape 1 :rate 2)
:title "Gamma Boxplot"
:legend true)
(add-box-plot (sample-gamma 1000 :shape 2 :rate 2))
(add-box-plot (sample-gamma 1000 :shape 3 :rate 2))
(add-box-plot (sample-gamma 1000 :shape 5 :rate 1))
(add-box-plot (sample-gamma 1000 :shape 9 :rate 0.5))
;; Examples of plots from the Gamma Distribution page at
;; Wikipedia (
(use '(incanter stats charts io))
(def x (range 0 20 0.1))
(def gamma-plot (xy-plot x (pdf-gamma x :shape 1 :rate 2)
:legend true
:title "Gamma PDF"
:y-label "Density"))
(use '(incanter core datasets charts io))
(def flow-meter (to-matrix (get-dataset :flow-meter)))
(def x1 (sel flow-meter :cols 1))
(def x2 (sel flow-meter :cols 3))
(doto (bland-altman-plot x1 x2)
(save "/tmp/bland_altman_plot.png"))
(use '(incanter core datasets stats bayes charts))
(def survey-data (to-matrix (get-dataset :survey)))
(def x (sel survey-data (range 0 2313) (range 1 10)))
(def y (sel survey-data (range 0 2313) 10))
(def sample-params (sample-model-params 5000 (linear-model y x :intercept false)))
(view (trace-plot (:var sample-params)))
(view (trace-plot (sel (:coefs sample-params) :cols 0)))
(use '(incanter stats charts)
;; Multi-line plots can be created using Clojure's doto macro
(def x (range 0 20 0.1))
(doto (xy-plot x (cdf-gamma x :shape 1 :rate 2)
:title "Gamma CDF"
:legend true
:y-label "Probability")
(use '(incanter core stats charts io))
;; make a histogram of a sample of 1000 Gamma deviates
(doto (histogram (sample-gamma 1000 :shape 1 :rate 2)
:title "Gamma Histogram")
(save "/tmp/gamma_hist.png"))
(use '(incanter stats charts))
;; qq-plot of normal distributed sample data
(view (qq-plot (sample-normal 100)))
;; qq-plot of exponential distributed sample data
(view (qq-plot (sample-exp 100)))
(use '(incanter core stats charts))
;; generate a sample from a bi-variate normal distribution
(def mvn-samp (sample-mvn 1000 :mean [7 5] :sigma (matrix [[2 1.5] [1.5 3]])))
(def x (sel mvn-samp :cols 0))
(def y (sel mvn-samp :cols 1))
;; add regression line to scatter plot
(def lm (linear-model y x))
(use '(incanter stats charts))
;; plot the normal pdf function
(def x (range -3 3 0.1))
(view (xy-plot x (pdf-normal x)))