forked from incanter/incanter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
2,369 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env bin/clj | ||
|
||
|
||
;;; test-incanter.clj -- Unit tests of Incanter functions | ||
|
||
;; by David Edgar Liebke http://incanter.org | ||
;; March 12, 2009 | ||
|
||
;; Copyright (c) David Edgar Liebke, 2009. All rights reserved. The use | ||
;; and distribution terms for this software are covered by the Eclipse | ||
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) | ||
;; which can be found in the file epl-v10.html at the root of this | ||
;; distribution. By using this software in any fashion, you are | ||
;; agreeing to be bound by the terms of this license. You must not | ||
;; remove this notice, or any other, from this software. | ||
|
||
;; CHANGE LOG | ||
;; March 12, 2009: First version | ||
|
||
|
||
(ns test.runtests | ||
(:use [clojure.test :only (run-tests)] | ||
(incanter tests))) | ||
|
||
(run) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
;; from the following blog post | ||
;; http://incanter-blog.org/2009/06/07/annotating-incanter-plots/ | ||
|
||
(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))") | ||
(add-pointer Math/PI (sin Math/PI) | ||
:text "(pi, (sin pi))" :angle :ne) | ||
(add-pointer (* 1/2 Math/PI) (sin (* 1/2 Math/PI)) | ||
:text "(pi/2, (sin pi/2))" :angle :south)) | ||
|
||
|
||
;; try the different angle options | ||
(doto plot | ||
(add-pointer 0 0 :text "north" :angle :north) | ||
(add-pointer 0 0 :text "nw" :angle :nw) | ||
(add-pointer 0 0 :text "ne" :angle :ne) | ||
(add-pointer 0 0 :text "west" :angle :west) | ||
(add-pointer 0 0 :text "east" :angle :east) | ||
(add-pointer 0 0 :text "south" :angle :south) | ||
(add-pointer 0 0 :text "sw" :angle :sw) | ||
(add-pointer 0 0 :text "se" :angle :se)) | ||
|
||
|
||
|
||
;; PCA chart example | ||
(use '(incanter core stats charts datasets)) | ||
;; load the iris dataset | ||
(def iris (to-matrix (get-dataset :iris))) | ||
;; run the pca | ||
(def pca (principal-components (sel iris :cols (range 4)))) | ||
;; extract the first two principal components | ||
(def pc1 (sel (:rotation pca) :cols 0)) | ||
(def pc2 (sel (:rotation pca) :cols 1)) | ||
|
||
;; project the first four dimension of the iris data onto the first | ||
;; two principal components | ||
(def x1 (mmult (sel iris :cols (range 4)) pc1)) | ||
(def x2 (mmult (sel iris :cols (range 4)) pc2)) | ||
|
||
;; now plot the transformed data, coloring each species a different color | ||
(def plot (scatter-plot x1 x2 | ||
:group-by (sel iris :cols 4) | ||
:x-label "PC1" :y-label "PC2" :title "Iris PCA")) | ||
|
||
(view plot) | ||
|
||
|
||
;; add some text annotations | ||
(doto plot | ||
(add-text -2.5 -6.5 "Setosa") | ||
(add-text -5 -5.5 "Versicolor") | ||
(add-text -8 -5.5 "Virginica")) | ||
|
||
;; put box around the first group | ||
(add-polygon plot [[-3.2 -6.3] [-2 -6.3] [-2 -3.78] [-3.2 -3.78]]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
;; from the following blog post: | ||
;; http://incanter-blog.org/2009/07/01/bayes-multinomial/ | ||
|
||
;; 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) | ||
|
||
|
||
(def theta (sample-multinomial-params 1000 y)) | ||
(def theta1 (sel theta :cols 0)) | ||
(def theta2 (sel theta :cols 1)) | ||
(def theta3 (sel theta :cols 2)) | ||
|
||
;; view means, 95% CI, and histograms of the proportion parameters | ||
(mean theta1) ;; 0.502 | ||
(sd theta1) ;; 0.0129 | ||
(quantile theta1 :probs [0.025 0.975]) ;; (0.476 0.528) | ||
(view (histogram theta1 | ||
:title "Bush, Sr. Support")) | ||
|
||
(mean theta2) ;; 0.403 | ||
(sd theta2) ;; 0.013 | ||
(quantile theta2 :probs [0.025 0.975]) ;; (0.376 0.427) | ||
(view (histogram theta2 | ||
:title "Dukakis Support")) | ||
|
||
(mean theta3) ;; 0.095 | ||
(sd theta3) ;; 0.008 | ||
(quantile theta3 :probs [0.025 0.975]) ;; (0.082 0.111) | ||
(view (histogram theta3 | ||
:title "Other/Undecided")) | ||
|
||
;; view a histogram of the difference in proportions between the first | ||
;; two candidates | ||
(view (histogram (minus theta1 theta2) | ||
:title "Bush, Sr. and Dukakis Diff")) | ||
|
||
|
||
|
||
;; sample the proportions directly from a Dirichlet distribution | ||
(def alpha [1 1 1]) | ||
(def props (sample-dirichlet 1000 (plus y alpha))) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
;; from the following blog post | ||
;; http://incanter-blog.org/2009/07/04/bootstrapping/ | ||
|
||
;; 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 | ||
;; now considered to be 33.02. | ||
|
||
(def x [28 -44 29 30 24 28 37 32 36 | ||
27 26 28 29 26 27 22 23 20 | ||
25 25 36 23 31 32 24 27 33 | ||
16 24 29 36 21 28 26 27 27 | ||
32 25 28 24 40 21 31 32 28 | ||
26 30 27 26 24 32 29 34 -2 | ||
25 19 36 29 30 22 28 33 39 | ||
25 16 23]) | ||
|
||
;; view histogram of data to see outlier observations | ||
(view (histogram x | ||
:nbins 20 | ||
:title "Newcomb's speed of light data")) | ||
|
||
|
||
|
||
;; calculate the median of the data | ||
(median x) ;; 27 | ||
|
||
;; define a function that converts Newcomb's data into speeds | ||
(defn to-speed | ||
"Converts Newcomb's data into speed (meters/sec)" | ||
([x] (div 7400 (div (plus 24800 x) 1E9)))) | ||
|
||
|
||
;; convert the data to speeds and calculate the median | ||
(median (to-speed x)) ;; 2.981E8 | ||
|
||
|
||
|
||
;; Draw 10000 bootstrap samples of the median | ||
(def t* (bootstrap x median :size 10000)) | ||
|
||
;; view a histogram of the bootstrap medians | ||
(view (histogram t* | ||
:density true | ||
:nbins 20 | ||
:title "Bootstrap sample of medians" | ||
:x-label "median")) | ||
|
||
;; Calculate the estimate of the median: 27.301 | ||
(mean t*) | ||
|
||
;; Convert bootstrap median estimate to speed: 2.981E8 | ||
(to-speed (mean t*)) | ||
|
||
;; Calculate a 95% CI for the median: (26.0 28.5) | ||
(quantile t* :probs [0.025 0.975]) | ||
|
||
;; Convert to speed and calculate 95% CI: (2.9804E8 2.9807E8) | ||
(quantile (to-speed t*) :probs [0.025 0.975]) | ||
|
||
;; estimate the standard error of the median: 0.681 | ||
(sd t*) | ||
|
||
;; estimate the bias of the sample median: -0.3 | ||
(- (mean t*) (median x)) | ||
|
||
|
||
;; draw 10000 smoothed bootstrap samples of the median | ||
(def smooth-t* (bootstrap x median :size 10000 :smooth true)) | ||
|
||
(view (histogram smooth-t* | ||
:density true | ||
:nbins 20 | ||
:title "Smoothed bootstrap sample of medians" | ||
:x-label "median"))(mean smooth-samp) | ||
|
||
;; Calculate the estimate of the median | ||
(mean smooth-t*) | ||
|
||
;; Calculate a 95% CI: (25.905 28.446) | ||
(quantile smooth-t* :probs [0.025 0.975]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
;; from the following blog post | ||
;; http://incanter-blog.org/2009/06/13/plotting-with-non-numeric-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"])) | ||
|
||
(view (line-chart ["a" "b" "c" "d" "e"] [20 10 30 25 40])) | ||
|
||
|
||
(def data (get-dataset :airline-passengers)) | ||
(def by-year (group-by data 0)) | ||
(view (bar-chart (sel (last by-year) :cols 2) | ||
(sel (last by-year) :cols 1) | ||
:title "Airline Travel in 1960" | ||
:y-label "Passengers" | ||
:x-label "Month")) | ||
|
||
|
||
;; line-charts | ||
(view (line-chart (sel (last by-year) :cols 2) | ||
(sel (last by-year) :cols 1) | ||
:title "Airline Travel in 1960" | ||
:y-label "Passengers" | ||
:x-label "Month")) | ||
|
||
(view (line-chart (sel data :cols 2) | ||
(sel data :cols 1) | ||
:group-by (sel data :cols 0) | ||
:title "Airline Travel in 1949-1960" | ||
:legend true | ||
:y-label "Passengers" | ||
:x-label "Month")) | ||
|
||
|
||
;; more bar-charts | ||
(view (bar-chart (sel data :cols 2) | ||
(sel data :cols 1) | ||
:group-by (sel data :cols 0) | ||
:title "Airline Travel in 1949-1960" | ||
:legend true | ||
:y-label "Passengers" | ||
:x-label "Year")) | ||
|
||
(view (bar-chart (sel data :cols 0) | ||
(sel data :cols 1) | ||
:group-by (sel data :cols 2) | ||
:title "Airline Travel in 1949-1960" | ||
:legend true | ||
:y-label "Passengers" | ||
:x-label "Year") | ||
:width 525) | ||
|
Oops, something went wrong.