Skip to content

Instantly share code, notes, and snippets.

;; Chi-square test of independence:
;; testing the independence of eye and hair color
;; for both males and females
(use '(incanter core stats charts datasets))
(def by-gender (group-by (get-dataset :hair-eye-color) 2))
(def male-data (first by-gender))
(view male-data)
;; plotting categorical data
(use '(incanter core charts datasets))
;; bar-charts
(view (bar-chart ["a" "b" "c" "d" "e"] [10 20 30 25 20]))
(view (bar-chart ["a" "a" "b" "b" "c" "c" ] [10 20 30 10 40 20]
:legend true
:group-by ["I" "II" "I" "II" "I" "II"]))
(use '(incanter core charts))
(def x (range (* -2 Math/PI) (* 2 Math/PI) 0.01))
(def plot (xy-plot x (sin x)))
(view plot)
;; annotate the plot
(doto plot
(add-pointer (- Math/PI) (sin (- Math/PI))
:text "(-pi, (sin -pi))")
(use '(incanter core optimize datasets charts))
;; Chwirut data set from NIST
;; http://www.itl.nist.gov/div898/strd/nls/data/LINKS/DATA/Chwirut1.dat
(def data (to-matrix (get-dataset :chwirut)))
(def x (sel data :cols 1))
(def y (sel data :cols 0))
;; define model function: y = exp(-b1*x)/(b2+b3*x) + e
(defn f [theta x]
;; load the necessary libraries
(use '(incanter core stats charts datasets))
;; load the NIST filip data set
;; see information on this data set at http://www.itl.nist.gov/div898/strd/lls/data/Filip.shtml
(def data (to-matrix (get-dataset :filip)))
(def y (sel data :cols 0))
;; use the sweep function to center the x values to reduce collinearity of the polynomial terms
;; compare the means of different treatments in the plant-growth data set
(use '(incanter core stats charts datasets))
;; load the plant-growth data
(def plant-growth (to-matrix (get-dataset :plant-growth)))
;; create box-plots of the three treatment groups
(view (box-plot (sel plant-growth :cols 0)
:group-by (sel plant-growth :cols 1)))
;; example of testing the significance of a correlation
;; value with a permutation test
(use '(incanter core stats charts datasets))
;; load the data
(def data (to-matrix (get-dataset :us-arrests)))
(def assault (sel data :cols 2))
(def urban-pop (sel data :cols 3))
;; example of performing PCA on Fisher's iris data
(use '(incanter core stats charts datasets))
(def iris (to-matrix (get-dataset :iris)))
(view iris)
(def X (sel iris :cols (range 4)))
(def species (sel iris :cols 4))
(def pca (principal-components X))
;; plot a user defined function and its derivative
(use '(incanter core charts optimize))
;; define the function, x^3 + 2x^2 + 2x + 3
(defn cubic [x] (+ (* x x x) (* 2 x x) (* 2 x) 3))
;; use the derivative function to get a function
;; that approximates the derivative of cubic
(def deriv-cubic (derivative cubic))
(use '(incanter core charts))
;; plot the sine and cosine functions with
;; the function-plot and add-function functions
(doto (function-plot sin (- Math/PI) Math/PI)
(add-function cos (- Math/PI) Math/PI)
view)