Skip to content

Instantly share code, notes, and snippets.

;; 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"]))
;; 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)
;; significance testing with randomization
(use '(incanter core stats datasets charts))
(def data (to-matrix (get-dataset :plant-growth)))
;; Break the first column of the data into groups based on
;; treatment type (second column) using the group-by function.
(def groups (group-by data 1 :cols 0))
(t-test (first groups) :y (second groups))
;; Chi-square goodness-of-fit analysis of 2009 Iranian election and Benford's law
(use '(incanter core stats charts io))
(def votes (read-dataset "data/iran_election_2009.csv"
:header true))
(view votes)
(def regions (sel votes :cols "Region"))
;; Monty Hall problem (Let's Make a Deal gameshow)
;; http://www.marilynvossavant.com/articles/gameshow.html
(use '(incanter core stats charts))
;; set a simulation sample size
(def n 10000)
;; generate samples of initial-guesses, prize-doors, and switch decisions
(def initial-guesses (sample [1 2 3] :size n))
(def prize-doors (sample [1 2 3] :size n))
;; Bayesian inference of multinomial distribution parameters
(use '(incanter core stats bayes charts))
(def y [727 583 137])
(div y 1447.) ;; (0.502 0.403 0.095)
;; example with smoothing
;; Newcomb's speed of light data
(use '(incanter core stats charts))
;; A numeric vector giving the Third Series of measurements of the
;; passage time of light recorded by Newcomb in 1882. The given
;; values divided by 1000 plus 24 give the time in millionths of a
;; second for light to traverse a known distance. The 'true' value is
(use '(incanter core processing))
;; simple interactive Processing example taken from processingjs.org website:
;; http://processingjs.org/source/basic-example/processingjs_basic-example.html
;; set up variable references to use in the sketch object
(let [radius (ref 50.0)
X (ref nil)
Y (ref nil)
@echo off
setLocal EnableDelayedExpansion
set CLASSPATH="
for /R ./lib %%a in (*.jar) do (
set CLASSPATH=!CLASSPATH!;%%a
)
set CLASSPATH=!CLASSPATH!"
set CLASSPATH=%CLASSPATH%;src;test;config;data
echo CLASSPATH=%CLASSPATH%
java -Xmx1G -cp %CLASSPATH% jline.ConsoleRunner clojure.main -e "(use '[clojure.contrib.duck-streams :only (spit read-lines reader writer)] '[clojure.contrib def ns-utils pprint repl-utils shell-out]) (require '[clojure.contrib.str-utils2 :as s])" -r
(use 'incanter.core)
;; basic arithmetic
($= 7 + 8 - 2 * 6 / 2) ; => 9
;; add 5 to each element of the vector [1 2 3]
($= [1 2 3] + 5) ; => (6 7 8)