├── src
├── .gitignore
└── incanter
│ └── main.clj
├── modules
├── incanter-core
│ ├── .gitignore
│ ├── project.clj
│ ├── test
│ │ └── incanter
│ │ │ ├── symbolic_tests.clj
│ │ │ ├── dataset_tests.clj
│ │ │ └── infix_tests.clj
│ ├── java
│ │ └── Weibull.java
│ └── src
│ │ └── incanter
│ │ ├── internal.clj
│ │ ├── infix.clj
│ │ ├── censored.clj
│ │ └── som.clj
├── incanter-io
│ ├── .gitignore
│ ├── project.clj
│ ├── test
│ │ └── incanter
│ │ │ └── io_tests.clj
│ └── src
│ │ └── incanter
│ │ └── io.clj
├── incanter-pdf
│ ├── .gitignore
│ ├── test
│ │ └── incanter_pdf
│ │ │ └── pdf_tests.clj
│ ├── project.clj
│ └── src
│ │ └── incanter
│ │ └── pdf.clj
├── incanter-charts
│ ├── .gitignore
│ └── project.clj
├── incanter-latex
│ ├── .gitignore
│ ├── test
│ │ └── incanter
│ │ │ └── latex_tests.clj
│ ├── README
│ ├── project.clj
│ └── src
│ │ └── incanter
│ │ └── latex.clj
├── incanter-mongodb
│ ├── .gitignore
│ ├── test
│ │ └── incanter
│ │ │ └── mongodb_tests.clj
│ ├── project.clj
│ └── src
│ │ └── incanter
│ │ └── mongodb.clj
├── incanter-processing
│ ├── .gitignore
│ ├── project.clj
│ └── test
│ │ └── incanter
│ │ └── processing_tests.clj
├── incanter-zoo
│ ├── .gitignore
│ ├── src
│ │ └── incanter
│ │ │ └── backstage
│ │ │ └── zoo_commons.clj
│ ├── test
│ │ └── incanter
│ │ │ ├── backstage
│ │ │ └── zoo_commons_test.clj
│ │ │ └── zoo_test.clj
│ ├── orig.project.clj
│ └── project.clj
└── incanter-excel
│ ├── project.clj
│ ├── src
│ └── incanter
│ │ ├── excel
│ │ ├── workbook.clj
│ │ └── cells.clj
│ │ └── excel.clj
│ └── test
│ └── incanter
│ └── excel_tests.clj
├── docs
└── incanter-cheat-sheet.pdf
├── examples
├── benfry
│ ├── images
│ │ └── map.png
│ ├── random.tsv
│ └── locations.tsv
├── processing
│ ├── data
│ │ ├── Ziggurat.vlw
│ │ ├── Eureka-90.vlw
│ │ ├── Univers45.vlw
│ │ ├── CourierNew-12.vlw
│ │ ├── CourierNew36.vlw
│ │ ├── Univers66.vlw.gz
│ │ ├── CourierNewPSMT-24.vlw
│ │ ├── ScalaSans-Caps-32.vlw
│ │ ├── TheSans-Plain-12.vlw
│ │ ├── AmericanTypewriter-24.vlw
│ │ ├── UniversLTStd-Light-48.vlw
│ │ └── Ziggurat-HTF-Black-32.vlw
│ ├── text_tests.clj
│ ├── simple_interation.clj
│ └── simple_rgb_cube.clj
├── blog
│ ├── projects
│ │ ├── pdf-chart
│ │ │ ├── pdf-chart.pdf
│ │ │ ├── src
│ │ │ │ └── pdf_chart.clj
│ │ │ └── project.clj
│ │ ├── newtheme
│ │ │ ├── project.clj
│ │ │ └── src
│ │ │ │ └── newtheme
│ │ │ │ └── core.clj
│ │ ├── incanter-helloworld
│ │ │ ├── src
│ │ │ │ └── hello.clj
│ │ │ ├── project.clj
│ │ │ └── README
│ │ ├── string_template
│ │ │ ├── README
│ │ │ ├── project.clj
│ │ │ └── src
│ │ │ │ └── hello.clj
│ │ ├── swank
│ │ │ ├── project.clj
│ │ │ └── README
│ │ └── simple_web_app
│ │ │ ├── project.clj
│ │ │ └── src
│ │ │ └── simple_web_app.clj
│ ├── pca.clj
│ ├── t_test.clj
│ ├── corr_perm_test.clj
│ ├── non_linear_models.clj
│ ├── bayesian_multinomial.clj
│ ├── mahalanobis.clj
│ ├── regression_high_order.clj
│ ├── categorical_plots.clj
│ ├── annotations.clj
│ ├── monte_hall.clj
│ ├── processing_intro.clj
│ ├── chisq_test_indep.clj
│ ├── datasets_mongodb.clj
│ ├── bootstrapping.clj
│ ├── chisq_goodness_of_fit.clj
│ ├── kmeans.clj
│ └── signif_testing.clj
├── run_prob_plots.clj
├── bayes.clj
└── mixture_em.clj
├── script
├── swank
├── swank.bat
├── clean
├── repl
├── package
├── install
├── run.clj
├── repl.bat
├── test
├── push
├── clj
└── clj.bat
├── .gitignore
├── data
├── thurstone.dat
├── plant_growth.csv
├── flow_meter.dat
├── cars.dat
├── cars.csv
├── hair_eye_color.csv
├── cars.tdd
├── longley.dat
├── pontius.dat
├── math_prog.csv
├── austres.csv
├── filip.dat
├── us_arrests.dat
├── airline_passengers.csv
├── co2.csv
├── iran_election_2009.csv
├── iris.dat
└── Chwirut1.dat
└── project.clj
/src/.gitignore:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/modules/incanter-core/.gitignore:
--------------------------------------------------------------------------------
1 | pom.xml
2 | *jar
3 | lib
4 | classes
--------------------------------------------------------------------------------
/modules/incanter-io/.gitignore:
--------------------------------------------------------------------------------
1 | pom.xml
2 | *jar
3 | lib
4 | classes
--------------------------------------------------------------------------------
/modules/incanter-pdf/.gitignore:
--------------------------------------------------------------------------------
1 | pom.xml
2 | *jar
3 | lib
4 | classes
--------------------------------------------------------------------------------
/modules/incanter-charts/.gitignore:
--------------------------------------------------------------------------------
1 | pom.xml
2 | *jar
3 | lib
4 | classes
--------------------------------------------------------------------------------
/modules/incanter-latex/.gitignore:
--------------------------------------------------------------------------------
1 | pom.xml
2 | *jar
3 | lib
4 | classes
--------------------------------------------------------------------------------
/modules/incanter-mongodb/.gitignore:
--------------------------------------------------------------------------------
1 | pom.xml
2 | *jar
3 | lib
4 | classes
--------------------------------------------------------------------------------
/modules/incanter-processing/.gitignore:
--------------------------------------------------------------------------------
1 | pom.xml
2 | *jar
3 | lib
4 | classes
--------------------------------------------------------------------------------
/docs/incanter-cheat-sheet.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sandbox/incanter/master/docs/incanter-cheat-sheet.pdf
--------------------------------------------------------------------------------
/examples/benfry/images/map.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sandbox/incanter/master/examples/benfry/images/map.png
--------------------------------------------------------------------------------
/examples/processing/data/Ziggurat.vlw:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sandbox/incanter/master/examples/processing/data/Ziggurat.vlw
--------------------------------------------------------------------------------
/script/swank:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | export LABREPL_SWANK="(require 'swank.swank) (swank.swank/start-repl 4005)"
3 | script/repl
4 |
5 |
--------------------------------------------------------------------------------
/examples/processing/data/Eureka-90.vlw:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sandbox/incanter/master/examples/processing/data/Eureka-90.vlw
--------------------------------------------------------------------------------
/examples/processing/data/Univers45.vlw:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sandbox/incanter/master/examples/processing/data/Univers45.vlw
--------------------------------------------------------------------------------
/examples/processing/data/CourierNew-12.vlw:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sandbox/incanter/master/examples/processing/data/CourierNew-12.vlw
--------------------------------------------------------------------------------
/examples/processing/data/CourierNew36.vlw:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sandbox/incanter/master/examples/processing/data/CourierNew36.vlw
--------------------------------------------------------------------------------
/examples/processing/data/Univers66.vlw.gz:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sandbox/incanter/master/examples/processing/data/Univers66.vlw.gz
--------------------------------------------------------------------------------
/modules/incanter-pdf/test/incanter_pdf/pdf_tests.clj:
--------------------------------------------------------------------------------
1 | (ns incanter.pdf
2 | (:use [incanter.pdf])
3 | (:use [clojure.test]))
4 |
5 |
--------------------------------------------------------------------------------
/modules/incanter-latex/test/incanter/latex_tests.clj:
--------------------------------------------------------------------------------
1 | (ns incanter.latex
2 | (:use [incanter.latex])
3 | (:use [clojure.test]))
4 |
5 |
--------------------------------------------------------------------------------
/modules/incanter-zoo/.gitignore:
--------------------------------------------------------------------------------
1 | /pom.xml
2 | *jar
3 | /lib
4 | /classes
5 | /native
6 | /.lein-failures
7 | /checkouts
8 | /.lein-deps-sum
9 |
--------------------------------------------------------------------------------
/script/swank.bat:
--------------------------------------------------------------------------------
1 | @echo off
2 | setlocal
3 | set LABREPL_SWANK="(require 'swank.swank) (swank.swank/start-repl 4005)"
4 | call %~dp0repl.bat
5 |
--------------------------------------------------------------------------------
/examples/blog/projects/pdf-chart/pdf-chart.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sandbox/incanter/master/examples/blog/projects/pdf-chart/pdf-chart.pdf
--------------------------------------------------------------------------------
/examples/processing/data/CourierNewPSMT-24.vlw:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sandbox/incanter/master/examples/processing/data/CourierNewPSMT-24.vlw
--------------------------------------------------------------------------------
/examples/processing/data/ScalaSans-Caps-32.vlw:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sandbox/incanter/master/examples/processing/data/ScalaSans-Caps-32.vlw
--------------------------------------------------------------------------------
/examples/processing/data/TheSans-Plain-12.vlw:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sandbox/incanter/master/examples/processing/data/TheSans-Plain-12.vlw
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .*.swp
2 | *~
3 | pom.xml
4 | build
5 | target
6 | lib
7 | classes
8 | *.jar
9 | *.log
10 | <<<<<<< HEAD
11 | .cake
12 | .lein*
13 |
--------------------------------------------------------------------------------
/examples/processing/data/AmericanTypewriter-24.vlw:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sandbox/incanter/master/examples/processing/data/AmericanTypewriter-24.vlw
--------------------------------------------------------------------------------
/examples/processing/data/UniversLTStd-Light-48.vlw:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sandbox/incanter/master/examples/processing/data/UniversLTStd-Light-48.vlw
--------------------------------------------------------------------------------
/examples/processing/data/Ziggurat-HTF-Black-32.vlw:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sandbox/incanter/master/examples/processing/data/Ziggurat-HTF-Black-32.vlw
--------------------------------------------------------------------------------
/examples/blog/projects/pdf-chart/src/pdf_chart.clj:
--------------------------------------------------------------------------------
1 | (ns pdf-chart)
2 |
3 |
4 | (use '(incanter core charts pdf))
5 |
6 | (save-pdf (function-plot sin -4 4) "./pdf-chart.pdf")
7 |
8 |
9 |
--------------------------------------------------------------------------------
/modules/incanter-mongodb/test/incanter/mongodb_tests.clj:
--------------------------------------------------------------------------------
1 | (ns incanter.mongodb-tests
2 | (:use clojure.test
3 | (incanter core mongodb)))
4 |
5 | ;; just load the namespace for now
6 |
--------------------------------------------------------------------------------
/script/clean:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | cd modules
4 | for f in core io charts processing mongodb pdf latex excel zoo ; do
5 | cd incanter-$f
6 | lein clean
7 | cd ..
8 | done
9 | cd ..
10 |
11 |
--------------------------------------------------------------------------------
/examples/blog/projects/newtheme/project.clj:
--------------------------------------------------------------------------------
1 | (defproject newtheme "1.0.0-SNAPSHOT"
2 | :description "Examples of Incanter's new default chart theme"
3 | :dependencies [[incanter "1.2.1-SNAPSHOT"]])
4 |
5 |
6 |
--------------------------------------------------------------------------------
/examples/blog/projects/incanter-helloworld/src/hello.clj:
--------------------------------------------------------------------------------
1 | (ns hello
2 | (:gen-class)
3 | (:use (incanter core stats charts)))
4 |
5 | (defn -main [& args]
6 | (view (histogram (sample-normal 1000))))
7 |
8 |
9 |
--------------------------------------------------------------------------------
/modules/incanter-latex/README:
--------------------------------------------------------------------------------
1 | # incanter-latex
2 |
3 | FIXME: write description
4 |
5 | ## Usage
6 |
7 | FIXME: write
8 |
9 | ## Installation
10 |
11 | FIXME: write
12 |
13 | ## License
14 |
15 | FIXME: write
16 |
--------------------------------------------------------------------------------
/script/repl:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | CLASSPATH=src:test:config:data:.
3 |
4 | for f in lib/*.jar; do
5 | CLASSPATH="$CLASSPATH":$f
6 | done
7 |
8 | java -Xmx1G -cp "$CLASSPATH" jline.ConsoleRunner clojure.main -i script/run.clj -r
9 |
--------------------------------------------------------------------------------
/examples/blog/projects/pdf-chart/project.clj:
--------------------------------------------------------------------------------
1 | (defproject pdf-chart "1.0.0-SNAPSHOT"
2 | :description "An example of creating a PDF chart with incanter.pdf."
3 | :dependencies [[incanter "1.2.1-SNAPSHOT"]])
4 |
5 |
--------------------------------------------------------------------------------
/examples/blog/projects/string_template/README:
--------------------------------------------------------------------------------
1 | # myproject
2 |
3 | FIXME: write description
4 |
5 | ## Usage
6 |
7 | FIXME: write
8 |
9 | ## Installation
10 |
11 | FIXME: write
12 |
13 | ## License
14 |
15 | FIXME: write
16 |
--------------------------------------------------------------------------------
/script/package:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | echo "** Make sure to run lein deps first **"
4 | echo "Packaging executable incanter.jar file..."
5 | [ -f incanter.jar ] && rm incanter.jar
6 | lein uberjar && mv incanter-*-standalone.jar incanter.jar
7 |
8 |
--------------------------------------------------------------------------------
/examples/blog/projects/swank/project.clj:
--------------------------------------------------------------------------------
1 | (defproject incanter-swank "0.1.0"
2 | :description "A Swank Server for Incanter"
3 | :dependencies [[incanter "1.2.1-SNAPSHOT"]]
4 | :dev-dependencies [[leiningen/lein-swank "1.0.0-SNAPSHOT"]])
5 |
6 |
--------------------------------------------------------------------------------
/examples/blog/projects/incanter-helloworld/project.clj:
--------------------------------------------------------------------------------
1 | (defproject incanter-helloworld "0.1.0"
2 | :description "The Incanter version of hello world."
3 | :dependencies [[incanter "1.2.1-SNAPSHOT"]]
4 | :main hello)
5 |
6 |
--------------------------------------------------------------------------------
/src/incanter/main.clj:
--------------------------------------------------------------------------------
1 | (ns incanter.main
2 | (:require org.dipert.swingrepl.main)
3 | (:gen-class))
4 |
5 | (defn -main
6 | [& args]
7 | (org.dipert.swingrepl.main/make-repl-jframe
8 | {:on-close javax.swing.JFrame/EXIT_ON_CLOSE}))
9 |
10 |
--------------------------------------------------------------------------------
/examples/blog/projects/simple_web_app/project.clj:
--------------------------------------------------------------------------------
1 |
2 | (defproject incanter-webapp "0.1.0"
3 | :description "A simple Incanter web application."
4 | :dependencies [[incanter "1.2.1-SNAPSHOT"]
5 | [compojure "0.3.2"]]
6 | :main simple_web_app)
7 |
8 |
9 |
--------------------------------------------------------------------------------
/modules/incanter-zoo/src/incanter/backstage/zoo_commons.clj:
--------------------------------------------------------------------------------
1 | (ns incanter.backstage.zoo-commons)
2 |
3 | (defn partialsums [start coll]
4 | (lazy-seq
5 | (if (seq coll)
6 | (cons start (partialsums (+ start (first coll)) (rest coll)))
7 | (list start))))
--------------------------------------------------------------------------------
/examples/blog/projects/string_template/project.clj:
--------------------------------------------------------------------------------
1 | (defproject myproject "1.0.0-SNAPSHOT"
2 | :description "Example Incanter project"
3 | :dependencies [[incanter "1.2.1-SNAPSHOT"]
4 | [antlr/stringtemplate "2.2"]]
5 | :main hello)
6 |
--------------------------------------------------------------------------------
/data/thurstone.dat:
--------------------------------------------------------------------------------
1 | y x
2 | 39 10
3 | 51 20
4 | 60 30
5 | 64 40
6 | 73 50
7 | 83 60
8 | 90 70
9 | 93 80
10 | 99 90
11 | 105 100
12 | 110 110
13 | 111 120
14 | 113 130
15 | 117 140
16 | 120 150
17 | 125 160
18 | 130 170
19 | 133 180
20 | 133 190
21 | 134 200
22 | 138 210
23 | 140 220
24 | 145 230
25 | 146 240
26 | 148 250
27 |
28 |
--------------------------------------------------------------------------------
/script/install:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | lein clean
4 | cd modules
5 | for f in core io charts processing mongodb pdf latex excel zoo; do
6 | cd incanter-$f
7 | lein clean
8 | lein deps
9 | jsrc=`find src -name \*.java`
10 | if [ -n "$jsrc" ] ; then
11 | lein javac
12 | fi
13 | lein install
14 | cd ..
15 | done
16 | cd ..
17 | lein deps
18 |
--------------------------------------------------------------------------------
/script/run.clj:
--------------------------------------------------------------------------------
1 | (when-let [run-swank (System/getenv "LABREPL_SWANK")]
2 | (println "Starting swank...")
3 | ;; Drop the enclosing double quotes from the environment variable and eval it.
4 | (load-string (if-let [found (re-find #"^\"(.*)\"$" run-swank)]
5 | (second found)
6 | run-swank)))
7 |
8 | (use 'clojure.repl)
9 | (set! *print-length* 500)
10 |
--------------------------------------------------------------------------------
/modules/incanter-zoo/test/incanter/backstage/zoo_commons_test.clj:
--------------------------------------------------------------------------------
1 | (ns incanter.backstage.zoo-commons-test
2 | (:use clojure.test
3 | incanter.backstage.zoo-commons))
4 |
5 | (def test-coll [2 5 1 3 8 0 6 7])
6 |
7 | (deftest partialsums-test
8 | (is (= [11 17 12 17 21]
9 | (partialsums (apply + (take 4 test-coll))
10 | (map - (drop 4 test-coll) test-coll)))))
--------------------------------------------------------------------------------
/script/repl.bat:
--------------------------------------------------------------------------------
1 | @echo off
2 | setLocal EnableDelayedExpansion
3 | set CLASSPATH="
4 | for /R ./lib %%a in (*.jar) do (
5 | set CLASSPATH=!CLASSPATH!;%%a
6 | )
7 | set CLASSPATH=!CLASSPATH!"
8 | set CLASSPATH=%CLASSPATH%;src;test;config;data;.
9 | echo CLASSPATH=%CLASSPATH%
10 |
11 | @rem jline breaks inferior-lisp.
12 | if not defined LABREPL_SWANK set JLINE=jline.ConsoleRunner
13 |
14 | java -Xmx1G -cp %CLASSPATH% %JLINE% clojure.main -i script/run.clj -r
15 |
--------------------------------------------------------------------------------
/data/plant_growth.csv:
--------------------------------------------------------------------------------
1 | weight,group
2 | 4.17, ctrl
3 | 5.58, ctrl
4 | 5.18, ctrl
5 | 6.11, ctrl
6 | 4.50, ctrl
7 | 4.61, ctrl
8 | 5.17, ctrl
9 | 4.53, ctrl
10 | 5.33, ctrl
11 | 5.14, ctrl
12 | 4.81, trt1
13 | 4.17, trt1
14 | 4.41, trt1
15 | 3.59, trt1
16 | 5.87, trt1
17 | 3.83, trt1
18 | 6.03, trt1
19 | 4.89, trt1
20 | 4.32, trt1
21 | 4.69, trt1
22 | 6.31, trt2
23 | 5.12, trt2
24 | 5.54, trt2
25 | 5.50, trt2
26 | 5.37, trt2
27 | 5.29, trt2
28 | 4.92, trt2
29 | 6.15, trt2
30 | 5.80, trt2
31 | 5.26, trt2
32 |
33 |
--------------------------------------------------------------------------------
/data/flow_meter.dat:
--------------------------------------------------------------------------------
1 | "Subject" "Wright 1st PEFR" "Wright 2nd PEFR" "Mini Wright 1st PEFR" "Mini Wright 2nd PEFR"
2 | 1 494 490 512 525
3 | 2 395 397 430 415
4 | 3 516 512 520 508
5 | 4 434 401 428 444
6 | 5 476 470 500 500
7 | 6 557 611 600 625
8 | 7 413 415 364 460
9 | 8 442 431 380 390
10 | 9 650 638 658 642
11 | 10 433 429 445 432
12 | 11 417 420 432 420
13 | 12 656 633 626 605
14 | 13 267 275 260 227
15 | 14 478 492 477 467
16 | 15 178 165 259 268
17 | 16 423 372 350 370
18 | 17 427 421 451 443
19 |
20 |
--------------------------------------------------------------------------------
/examples/benfry/random.tsv:
--------------------------------------------------------------------------------
1 | AL 0.1
2 | AK -5.3
3 | AZ 3
4 | AR 7
5 | CA 11
6 | CO 1.5
7 | CT -6.7
8 | DE -4
9 | FL 9
10 | GA 2
11 | HI -3.3
12 | ID 6.6
13 | IL 7.2
14 | IN 7.1
15 | IA 6.9
16 | KS 6
17 | KY 1.8
18 | LA 7.5
19 | ME -4
20 | MD 0.1
21 | MA -6
22 | MI 1.7
23 | MN -2
24 | MS -4.4
25 | MO -2
26 | MT 1.0
27 | NE 1.2
28 | NV 1.6
29 | NH 0.5
30 | NJ 0.2
31 | NM 8.8
32 | NY 1.4
33 | NC 9.7
34 | ND 5.4
35 | OH 3.2
36 | OK 6
37 | OR -4
38 | PA -7
39 | RI -2
40 | SC 1
41 | SD 6
42 | TN 5
43 | TX -3.4
44 | UT 2.3
45 | VT 4.8
46 | VA 3
47 | WA 2.2
48 | WV 5.4
49 | WI 3.1
50 | WY -6
--------------------------------------------------------------------------------
/examples/blog/projects/incanter-helloworld/README:
--------------------------------------------------------------------------------
1 |
2 |
3 | First make sure Leiningen is installed:
4 |
5 | $ cd ~/bin
6 | $ wget http://github.com/technomancy/leiningen/raw/stable/bin/lein
7 | $ chmod +x lein
8 | $ lein self-install
9 |
10 | Then download and install the project dependencies:
11 |
12 | $ lein deps
13 |
14 | Compile it:
15 |
16 | $ lein compile
17 |
18 | And create a uberjar that contains all of the project files and dependencies:
19 |
20 | $ lein uberjar
21 |
22 | And finally, run incanter-helloworld:
23 |
24 | $ java -jar incanter-helloworld-standalone.jar
25 |
26 |
--------------------------------------------------------------------------------
/modules/incanter-mongodb/project.clj:
--------------------------------------------------------------------------------
1 | (defproject incanter/incanter-mongodb "1.3.0"
2 | :description "Incanter-mongodb is the MongoDB module of the Incanter project."
3 | :dependencies [[org.clojure/clojure "1.3.0"]
4 | [incanter/incanter-core "1.3.0"
5 | :exclusions [org.clojure/clojure
6 | org.clojure/clojure-contrib]]
7 | [congomongo "0.1.7"
8 | :exclusions [org.clojure/clojure
9 | org.clojure/clojure-contrib]]]
10 | :dev-dependencies [[lein-clojars "0.7.0"]])
11 |
--------------------------------------------------------------------------------
/modules/incanter-pdf/project.clj:
--------------------------------------------------------------------------------
1 | (defproject incanter/incanter-pdf "1.3.0"
2 | :description "Incanter-pdf is the PDF module of the Incanter project."
3 | :dependencies [[org.clojure/clojure "1.3.0"]
4 | [incanter/incanter-charts "1.3.0"
5 | :exclusions [org.clojure/clojure
6 | org.clojure/clojure-contrib]]
7 | [com.lowagie/itext "1.4"]]
8 | :dev-dependencies [[lein-clojars "0.7.0"
9 | :exclusions [org.clojure/clojure
10 | org.clojure/clojure-contrib]]])
11 |
--------------------------------------------------------------------------------
/script/test:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | cd ./modules/incanter-core
4 | lein deps && lein test && lein install
5 |
6 | cd ../incanter-io
7 | lein deps && lein test && lein install
8 |
9 | cd ../incanter-charts
10 | lein deps && lein test && lein install
11 |
12 | cd ../incanter-processing
13 | lein deps && lein test
14 |
15 | cd ../incanter-mongodb
16 | lein deps && lein test
17 |
18 | cd ../incanter-pdf
19 | lein deps && lein test
20 |
21 | cd ../incanter-latex
22 | lein deps && lein test
23 |
24 | cd ../incanter-excel
25 | lein deps && lein test
26 |
27 | cd ../incanter-zoo
28 | lein deps && lein test
29 |
--------------------------------------------------------------------------------
/examples/blog/projects/swank/README:
--------------------------------------------------------------------------------
1 |
2 | This is a simple project that is used to start a Swank server configured with Incanter and its dependencies using Leiningen. For more details see the following blog post: http://incanter-blog.org/2009/12/22/lein-swank/
3 |
4 | To build:
5 |
6 | Download the necessary dependencies:
7 | $ lein deps
8 |
9 | and start the Swank server:
10 | $ lein swank
11 |
12 | Now connect to the server within Emacs with the following command:
13 | M-x slime-connect
14 |
15 | That's it, you should have a live Clojure REPL in Emacs now, and can evaluate your Clojure code with 'C-x C-e'.
16 |
17 |
18 |
--------------------------------------------------------------------------------
/modules/incanter-latex/project.clj:
--------------------------------------------------------------------------------
1 | (defproject incanter/incanter-latex "1.3.0"
2 | :description "Library for rendering LaTeX math equations using the jLateXMath library."
3 | :dependencies [[org.clojure/clojure "1.3.0"]
4 | [incanter/incanter-charts "1.3.0"
5 | :exclusions [org.clojure/clojure
6 | org.clojure/clojure-contrib]]
7 | [org.scilab.forge/jlatexmath "0.9.6"]]
8 | :dev-dependencies [[lein-clojars "0.7.0"
9 | :exclusions [org.clojure/clojure
10 | org.clojure/clojure-contrib]]])
11 |
--------------------------------------------------------------------------------
/script/push:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | cd ./modules/incanter-core
4 | lein deps
5 | lein push
6 |
7 | cd ../incanter-io
8 | lein deps
9 | lein push
10 |
11 | cd ../incanter-charts
12 | lein deps
13 | lein push
14 |
15 | cd ../incanter-processing
16 | lein deps
17 | lein push
18 |
19 | cd ../incanter-mongodb
20 | lein deps
21 | lein push
22 |
23 | cd ../incanter-pdf
24 | lein deps
25 | lein push
26 |
27 | cd ../incanter-latex
28 | lein deps
29 | lein push
30 |
31 | cd ../incanter-excel
32 | lein deps
33 | lein push
34 |
35 | cd ../incanter-zoo
36 | lein deps
37 | lein push
38 |
39 | cd ../..
40 | lein deps
41 | lein push
42 |
43 |
44 |
--------------------------------------------------------------------------------
/modules/incanter-io/project.clj:
--------------------------------------------------------------------------------
1 | (defproject incanter/incanter-io "1.3.0"
2 | :description "Incanter-io is the I/O module of the Incanter project."
3 | :dependencies [[org.clojure/clojure "1.3.0"]
4 | [incanter/incanter-core "1.3.0"
5 | :exclusions [org.clojure/clojure
6 | org.clojure/clojure-contrib]]
7 | [net.sf.opencsv/opencsv "2.0.1"]
8 | [org.danlarkin/clojure-json "1.1"
9 | :exclusions [org.clojure/clojure
10 | org.clojure/clojure-contrib]]]
11 | :dev-dependencies [[lein-clojars "0.7.0"]])
12 |
--------------------------------------------------------------------------------
/modules/incanter-excel/project.clj:
--------------------------------------------------------------------------------
1 | (defproject incanter/incanter-excel "1.3.0"
2 | :description "Incanter-excel provides access to reading and writing Excel files."
3 | :dependencies [[org.clojure/clojure "1.3.0"]
4 | [incanter/incanter-core "1.3.0"
5 | :exclusions [org.clojure/clojure
6 | org.clojure/clojure-contrib]]
7 | [org.apache.poi/poi "3.6"]
8 | [org.apache.poi/poi-ooxml "3.6"]]
9 | :dev-dependencies [[lein-clojars "0.7.0"
10 | :exclusions [org.clojure/clojure
11 | org.clojure/clojure-contrib]]])
12 |
--------------------------------------------------------------------------------
/data/cars.dat:
--------------------------------------------------------------------------------
1 | speed dist
2 | 4 2
3 | 4 10
4 | 7 4
5 | 7 22
6 | 8 16
7 | 9 10
8 | 10 18
9 | 10 26
10 | 10 34
11 | 11 17
12 | 11 28
13 | 12 14
14 | 12 20
15 | 12 24
16 | 12 28
17 | 13 26
18 | 13 34
19 | 13 34
20 | 13 46
21 | 14 26
22 | 14 36
23 | 14 60
24 | 14 80
25 | 15 20
26 | 15 26
27 | 15 54
28 | 16 32
29 | 16 40
30 | 17 32
31 | 17 40
32 | 17 50
33 | 18 42
34 | 18 56
35 | 18 76
36 | 18 84
37 | 19 36
38 | 19 46
39 | 19 68
40 | 20 32
41 | 20 48
42 | 20 52
43 | 20 56
44 | 20 64
45 | 22 66
46 | 23 54
47 | 24 70
48 | 24 92
49 | 24 93
50 | 24 120
51 | 25 85
52 |
53 |
--------------------------------------------------------------------------------
/script/clj:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | INCANTER_HOME=`dirname $0`/..
4 |
5 | CLASSPATH=src:test:config:data:.
6 |
7 | for f in lib/*.jar; do
8 | CLASSPATH=$CLASSPATH:$f
9 | done
10 |
11 | REPL_JAVA_OPTS="-server -Xmx1G"
12 | SCRIPT_JAVA_OPTS="-server -Xmx1G -XX:+AggressiveOpts"
13 |
14 |
15 | if [ -z "$1" ]; then
16 | java $REPL_JAVA_OPTS -Dclojure.compile.path=$CLASSES_DIR -Dincanter.home=$INCANTER_HOME -cp $CLASSPATH jline.ConsoleRunner clojure.main -i script/run.clj -r
17 | else
18 | scriptname=$1
19 | java $SCRIPT_JAVA_OPTS -Dclojure.compile.path=$CLASSES_DIR -Dincanter.home=$INCANTER_HOME -cp $CLASSPATH jline.ConsoleRunner clojure.main $scriptname -- $*
20 | fi
21 |
22 |
--------------------------------------------------------------------------------
/data/cars.csv:
--------------------------------------------------------------------------------
1 | speed,dist
2 | 4, 2
3 | 4, 10
4 | 7, 4
5 | 7, 22
6 | 8, 16
7 | 9, 10
8 | 10, 18
9 | 10, 26
10 | 10, 34
11 | 11, 17
12 | 11, 28
13 | 12, 14
14 | 12, 20
15 | 12, 24
16 | 12, 28
17 | 13, 26
18 | 13, 34
19 | 13, 34
20 | 13, 46
21 | 14, 26
22 | 14, 36
23 | 14, 60
24 | 14, 80
25 | 15, 20
26 | 15, 26
27 | 15, 54
28 | 16, 32
29 | 16, 40
30 | 17, 32
31 | 17, 40
32 | 17, 50
33 | 18, 42
34 | 18, 56
35 | 18, 76
36 | 18, 84
37 | 19, 36
38 | 19, 46
39 | 19, 68
40 | 20, 32
41 | 20, 48
42 | 20, 52
43 | 20, 56
44 | 20, 64
45 | 22, 66
46 | 23, 54
47 | 24, 70
48 | 24, 92
49 | 24, 93
50 | 24, 120
51 | 25, 85
52 |
53 |
--------------------------------------------------------------------------------
/modules/incanter-processing/project.clj:
--------------------------------------------------------------------------------
1 | (defproject incanter/incanter-processing "1.3.0"
2 | :description "Incanter-processing is the Processing module of the Incanter project."
3 | :dependencies [[org.clojure/clojure "1.3.0"]
4 | [incanter/incanter-core "1.3.0"
5 | :exclusions [org.clojure/clojure
6 | org.clojure/clojure-contrib]]
7 | [incanter/processing-core "1.1"
8 | :exclusions [org.clojure/clojure
9 | org.clojure/clojure-contrib]]]
10 | :dev-dependencies [[lein-clojars "0.7.0"
11 | :exclusions [org.clojure/clojure
12 | org.clojure/clojure-contrib]]])
13 |
--------------------------------------------------------------------------------
/modules/incanter-zoo/orig.project.clj:
--------------------------------------------------------------------------------
1 | (defproject incanter-zoo "1.3.0-SNAPSHOT"
2 | :description "Incanter Zoo is a port of Zoo from R."
3 | :dependencies [[org.clojure/clojure "1.3.0"]
4 | [incanter/incanter-core "1.3.0-SNAPSHOT"
5 | :exclusions [org.clojure/clojure
6 | org.clojure/clojure-contrib]]
7 | [incanter/incanter-io "1.3.0-SNAPSHOT"
8 | :exclusions [org.clojure/clojure
9 | org.clojure/clojure-contrib]]
10 | [clj-time "0.3.5"]]
11 | :dev-dependencies [[lein-clojars "0.7.0"
12 | :exclusions [org.clojure/clojure
13 | org.clojure/clojure-contrib]]])
--------------------------------------------------------------------------------
/script/clj.bat:
--------------------------------------------------------------------------------
1 | @echo off
2 | setLocal EnableDelayedExpansion
3 | set INCANTER_HOME=.
4 | set CLASSES_DIR=%INCANTER_HOME%\classes
5 |
6 | set CLASSPATH="
7 | for /R ./lib %%a in (*.jar) do (
8 | set CLASSPATH=!CLASSPATH!;%%a
9 | )
10 | set CLASSPATH=!CLASSPATH!"
11 | set CLASSPATH=%CLASSPATH%;src;test;config;data;.
12 | echo CLASSPATH=%CLASSPATH%
13 |
14 | @rem jline breaks inferior-lisp.
15 | if not defined LABREPL_SWANK set JLINE=jline.ConsoleRunner
16 |
17 |
18 | IF (%1)==() (
19 | java -Xmx1G -cp %CLASSPATH% %JLINE% -Dclojure.compile.path=%CLASSES_DIR% -Dincanter.home=%INCANTER_HOME% clojure.main -i script/run.clj -r
20 | ) ELSE (
21 | java -Xmx1G -cp %CLASSPATH% %JLINE% -Dclojure.compile.path=%CLASSES_DIR% -Dincanter.home=%INCANTER_HOME% clojure.main %1 -- %*
22 | )
23 |
--------------------------------------------------------------------------------
/examples/benfry/locations.tsv:
--------------------------------------------------------------------------------
1 | AL 439 270
2 | AK 94 325
3 | AZ 148 241
4 | AR 368 247
5 | CA 56 176
6 | CO 220 183
7 | CT 576 120
8 | DE 556 166
9 | FL 510 331
10 | GA 478 267
11 | HI 232 380
12 | ID 143 101
13 | IL 405 168
14 | IN 437 165
15 | IA 357 147
16 | KS 302 194
17 | KY 453 203
18 | LA 371 302
19 | ME 595 59
20 | MD 538 162
21 | MA 581 108
22 | MI 446 120
23 | MN 339 86
24 | MS 406 274
25 | MO 365 197
26 | MT 194 61
27 | NE 286 151
28 | NV 102 157
29 | NH 580 89
30 | NJ 561 143
31 | NM 208 245
32 | NY 541 107
33 | NC 519 221
34 | ND 283 65
35 | OH 472 160
36 | OK 309 239
37 | OR 74 86
38 | PA 523 144
39 | RI 589 117
40 | SC 506 251
41 | SD 286 109
42 | TN 441 229
43 | TX 291 299
44 | UT 154 171
45 | VT 567 86
46 | VA 529 189
47 | WA 92 38
48 | WV 496 178
49 | WI 392 103
50 | WY 207 125
51 |
--------------------------------------------------------------------------------
/examples/blog/pca.clj:
--------------------------------------------------------------------------------
1 | ;; from the following blog post:
2 | ;; http://incanter-blog.org/2009/05/31/principal-components-analysis/
3 |
4 | ;; example of performing PCA on Fisher's iris data
5 |
6 | (use '(incanter core stats charts datasets))
7 |
8 | (def iris (to-matrix (get-dataset :iris)))
9 | (view iris)
10 |
11 | (def X (sel iris :cols (range 4)))
12 | (def species (sel iris :cols 4))
13 | (def pca (principal-components X))
14 | (def components (:rotation pca))
15 | (def pc1 (sel components :cols 0))
16 | (def pc2 (sel components :cols 1))
17 | (def x1 (mmult X pc1))
18 | (def x2 (mmult X pc2))
19 |
20 | (view (scatter-plot x1 x2
21 | :group-by species
22 | :x-label "PC1"
23 | :y-label "PC2"
24 | :title "Iris PCA"))
25 |
26 |
--------------------------------------------------------------------------------
/data/hair_eye_color.csv:
--------------------------------------------------------------------------------
1 | hair,eye,gender,count
2 | black,brown,male,32
3 | black,blue,male,11
4 | black,hazel,male,10
5 | black,green,male,3
6 | brown,brown,male,53
7 | brown,blue,male,50
8 | brown,hazel,male,25
9 | brown,green,male,15
10 | red,brown,male,10
11 | red,blue,male,10
12 | red,hazel,male,7
13 | red,green,male,7
14 | blond,brown,male,3
15 | blond,blue,male,30
16 | blond,hazel,male,5
17 | blond,green,male,8
18 | black,brown,female,36
19 | black,blue,female,9
20 | black,hazel,female,5
21 | black,green,female,2
22 | brown,brown,female,66
23 | brown,blue,female,34
24 | brown,hazel,female,29
25 | brown,green,female,14
26 | red,brown,female,16
27 | red,blue,female,7
28 | red,hazel,female,7
29 | red,green,female,7
30 | blond,brown,female,4
31 | blond,blue,female,64
32 | blond,hazel,female,5
33 | blond,green,female,8
34 |
--------------------------------------------------------------------------------
/modules/incanter-charts/project.clj:
--------------------------------------------------------------------------------
1 | (defproject incanter/incanter-charts "1.3.0"
2 | :description "Incanter-charts is the JFreeChart module of the Incanter project."
3 | :dependencies [[org.clojure/clojure "1.3.0"]
4 | [incanter/incanter-core "1.3.0"
5 | :exclusions [org.clojure/clojure
6 | org.clojure/clojure-contrib]]
7 | [incanter/incanter-io "1.3.0"
8 | :exclusions [org.clojure/clojure
9 | org.clojure/clojure-contrib]]
10 | [incanter/jfreechart "1.0.13-no-gnujaxp"]
11 | [clj-time "0.3.7"]]
12 | :dev-dependencies [[lein-clojars "0.7.0"
13 | :exclusions [org.clojure/clojure
14 | org.clojure/clojure-contrib]]])
15 |
--------------------------------------------------------------------------------
/data/cars.tdd:
--------------------------------------------------------------------------------
1 | speed dist
2 | 4 2
3 | 4 10
4 | 7 4
5 | 7 22
6 | 8 16
7 | 9 10
8 | 10 18
9 | 10 26
10 | 10 34
11 | 11 17
12 | 11 28
13 | 12 14
14 | 12 20
15 | 12 24
16 | 12 28
17 | 13 26
18 | 13 34
19 | 13 34
20 | 13 46
21 | 14 26
22 | 14 36
23 | 14 60
24 | 14 80
25 | 15 20
26 | 15 26
27 | 15 54
28 | 16 32
29 | 16 40
30 | 17 32
31 | 17 40
32 | 17 50
33 | 18 42
34 | 18 56
35 | 18 76
36 | 18 84
37 | 19 36
38 | 19 46
39 | 19 68
40 | 20 32
41 | 20 48
42 | 20 52
43 | 20 56
44 | 20 64
45 | 22 66
46 | 23 54
47 | 24 70
48 | 24 92
49 | 24 93
50 | 24 120
51 | 25 85
52 |
53 |
--------------------------------------------------------------------------------
/examples/run_prob_plots.clj:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bin/clj
2 |
3 |
4 | ;;; run_prob_plots.clj -- Creates plots of probability distributions
5 |
6 | ;; by David Edgar Liebke http://incanter.org
7 | ;; April 19, 2009
8 |
9 | ;; Copyright (c) David Edgar Liebke, 2009. All rights reserved. The use
10 | ;; and distribution terms for this software are covered by the Eclipse
11 | ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
12 | ;; which can be found in the file epl-v10.html at the root of this
13 | ;; distribution. By using this software in any fashion, you are
14 | ;; agreeing to be bound by the terms of this license. You must not
15 | ;; remove this notice, or any other, from this software.
16 |
17 | ;; CHANGE LOG
18 | ;; April 19, 2009: First version
19 |
20 |
21 | (ns examples.run_prob_plots
22 | (:use (examples probability-plots)))
23 |
24 |
25 |
--------------------------------------------------------------------------------
/modules/incanter-zoo/project.clj:
--------------------------------------------------------------------------------
1 | (defproject incanter-zoo "1.3.0"
2 | :description "Incanter Zoo is a port of Zoo from R."
3 | :dependencies [[org.clojure/clojure "1.3.0"]
4 | [incanter/incanter-core "1.3.0"
5 | :exclusions [org.clojure/clojure
6 | org.clojure/clojure-contrib]]
7 | [incanter/incanter-io "1.3.0"
8 | :exclusions [org.clojure/clojure
9 | org.clojure/clojure-contrib]]
10 | [clj-time "0.3.5"]]
11 | :dev-dependencies [[lein-clojars "0.7.0"
12 | :exclusions [org.clojure/clojure
13 | org.clojure/clojure-contrib]]]
14 | :repositories {"snapshots" "http://tyrol:8081/nexus/content/repositories/snapshots"
15 | "releases" "http://tyrol:8081/nexus/content/repositories/releases"}
16 | )
17 |
--------------------------------------------------------------------------------
/modules/incanter-core/project.clj:
--------------------------------------------------------------------------------
1 | (defproject incanter/incanter-core "1.3.0"
2 | :description "Incanter-core is the core module of the Incanter project."
3 | :dependencies [[org.clojure/clojure "1.3.0"]
4 | [org.clojure/math.combinatorics "0.0.1"
5 | :exclusions [org.clojure/clojure
6 | org.clojure/clojure-contrib]]
7 | [incanter/parallelcolt "0.9.4"
8 | :exclusions [org.clojure/clojure
9 | org.clojure/clojure-contrib]]]
10 | :dev-dependencies [[lein-clojars "0.7.0"
11 | :exclusions [org.clojure/clojure
12 | org.clojure/clojure-contrib]]]
13 | :java-source-path "java"
14 | ;;; Set a custom repository because math.combinatorics isn't into
15 | ;;; Clojars or Maven central yet.
16 | :repositories {"snapshots" {:url "http://oss.sonatype.org/content/repositories/snapshots"}}
17 | )
18 |
--------------------------------------------------------------------------------
/data/longley.dat:
--------------------------------------------------------------------------------
1 | y x1 x2 x3 x4 x5 x6
2 | 60323 83.0 234289 2356 1590 107608 1947
3 | 61122 88.5 259426 2325 1456 108632 1948
4 | 60171 88.2 258054 3682 1616 109773 1949
5 | 61187 89.5 284599 3351 1650 110929 1950
6 | 63221 96.2 328975 2099 3099 112075 1951
7 | 63639 98.1 346999 1932 3594 113270 1952
8 | 64989 99.0 365385 1870 3547 115094 1953
9 | 63761 100.0 363112 3578 3350 116219 1954
10 | 66019 101.2 397469 2904 3048 117388 1955
11 | 67857 104.6 419180 2822 2857 118734 1956
12 | 68169 108.4 442769 2936 2798 120445 1957
13 | 66513 110.8 444546 4681 2637 121950 1958
14 | 68655 112.6 482704 3813 2552 123366 1959
15 | 69564 114.2 502601 3931 2514 125368 1960
16 | 69331 115.7 518173 4806 2572 127852 1961
17 | 70551 116.9 554894 4007 2827 130081 1962
18 |
19 |
--------------------------------------------------------------------------------
/examples/blog/t_test.clj:
--------------------------------------------------------------------------------
1 | ;; from the following blog post:
2 | ;; http://incanter-blog.org/2009/06/03/students-t-test/
3 |
4 | ;; compare the means of different treatments in the plant-growth data set
5 |
6 | (use '(incanter core stats charts datasets))
7 |
8 | ;; load the plant-growth data
9 | (def plant-growth (to-matrix (get-dataset :plant-growth)))
10 |
11 | ;; create box-plots of the three treatment groups
12 | (view (box-plot (sel plant-growth :cols 0)
13 | :group-by (sel plant-growth :cols 1)))
14 |
15 |
16 | (def groups (group-by plant-growth 1 :cols 0))
17 | (map mean groups) ;; returns (5.032 4.661 5.526)
18 |
19 | ;; run three different t-tests comparing the treatments
20 | (def t-tests [(t-test (second groups) :y (first groups))
21 | (t-test (last groups) :y (first groups))
22 | (t-test (second groups) :y (last groups))])
23 |
24 | ;; view the p-values of the three-tests
25 | (map :p-value t-tests) ;; returns (0.250 0.048 0.009)
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/data/pontius.dat:
--------------------------------------------------------------------------------
1 | y x
2 | .11019 150000
3 | .21956 300000
4 | .32949 450000
5 | .43899 600000
6 | .54803 750000
7 | .65694 900000
8 | .76562 1050000
9 | .87487 1200000
10 | .98292 1350000
11 | 1.09146 1500000
12 | 1.20001 1650000
13 | 1.30822 1800000
14 | 1.41599 1950000
15 | 1.52399 2100000
16 | 1.63194 2250000
17 | 1.73947 2400000
18 | 1.84646 2550000
19 | 1.95392 2700000
20 | 2.06128 2850000
21 | 2.16844 3000000
22 | .11052 150000
23 | .22018 300000
24 | .32939 450000
25 | .43886 600000
26 | .54798 750000
27 | .65739 900000
28 | .76596 1050000
29 | .87474 1200000
30 | .98300 1350000
31 | 1.09150 1500000
32 | 1.20004 1650000
33 | 1.30818 1800000
34 | 1.41613 1950000
35 | 1.52408 2100000
36 | 1.63159 2250000
37 | 1.73965 2400000
38 | 1.84696 2550000
39 | 1.95445 2700000
40 | 2.06177 2850000
41 | 2.16829 3000000
42 |
43 |
--------------------------------------------------------------------------------
/examples/blog/corr_perm_test.clj:
--------------------------------------------------------------------------------
1 | ;; from the following blog post
2 | ;; http://incanter-blog.org/2009/06/02/correlation-and-permutation-tests/
3 |
4 | ;; example of testing the significance of a correlation
5 | ;; value with a permutation test
6 |
7 | (use '(incanter core stats charts datasets))
8 |
9 | ;; load the data
10 | (def data (to-matrix (get-dataset :us-arrests)))
11 | (def assault (sel data :cols 2))
12 | (def urban-pop (sel data :cols 3))
13 |
14 | ;; get the sample correlation
15 | (correlation assault urban-pop)
16 |
17 | ;; permute the data
18 | (def permuted-assault (sample-permutations 5000 assault))
19 | (def permuted-urban-pop (sample-permutations 5000 urban-pop))
20 |
21 | ;; generate a distribution of the permuted correlation values
22 | (def permuted-corrs (map correlation
23 | permuted-assault
24 | permuted-urban-pop))
25 |
26 | (view (histogram permuted-corrs))
27 | (mean permuted-corrs)
28 | (sd permuted-corrs)
29 |
30 | ;; get the 95% interval for the null hypothesis
31 | (quantile permuted-corrs :probs [0.025 0.975])
32 |
33 |
--------------------------------------------------------------------------------
/data/math_prog.csv:
--------------------------------------------------------------------------------
1 | "student number","math exam","programming course"
2 | 01,pass,pass
3 | 02,fail,pass
4 | 03,pass,pass
5 | 04,pass,pass
6 | 05,pass,fail
7 | 06,pass,pass
8 | 07,pass,fail
9 | 08,fail,pass
10 | 09,fail,pass
11 | 10,pass,fail
12 | 11,pass,pass
13 | 12,pass,pass
14 | 13,pass,pass
15 | 14,fail,fail
16 | 15,fail,fail
17 | 16,pass,pass
18 | 17,pass,pass
19 | 18,pass,pass
20 | 19,pass,fail
21 | 20,pass,pass
22 | 21,pass,fail
23 | 22,pass,pass
24 | 23,pass,fail
25 | 24,pass,pass
26 | 25,pass,pass
27 | 26,pass,fail
28 | 27,fail,fail
29 | 28,pass,pass
30 | 29,pass,pass
31 | 30,pass,pass
32 | 31,pass,fail
33 | 32,pass,pass
34 | 33,fail,fail
35 | 34,fail,pass
36 | 35,pass,pass
37 | 36,pass,fail
38 | 37,pass,pass
39 | 38,pass,pass
40 | 39,fail,fail
41 | 40,pass,pass
42 | 41,pass,pass
43 | 42,fail,pass
44 | 43,pass,fail
45 | 44,pass,pass
46 | 45,pass,pass
47 | 46,pass,pass
48 | 47,pass,fail
49 | 48,fail,fail
50 | 49,pass,pass
51 | 50,fail,pass
52 | 51,fail,pass
53 | 52,pass,pass
54 | 53,pass,pass
55 | 54,fail,pass
56 | 55,fail,fail
57 | 56,pass,fail
58 | 57,pass,pass
59 | 58,fail,pass
60 | 59,fail,fail
61 | 60,pass,pass
62 |
63 |
--------------------------------------------------------------------------------
/examples/blog/non_linear_models.clj:
--------------------------------------------------------------------------------
1 | ;; from the following blog post:
2 | ;; http://incanter-blog.org/2009/06/06/fitting-non-linear-models/
3 |
4 | (use '(incanter core optimize datasets charts))
5 |
6 | ;; Chwirut data set from NIST
7 | ;; http://www.itl.nist.gov/div898/strd/nls/data/LINKS/DATA/Chwirut1.dat
8 | (def data (to-matrix (get-dataset :chwirut)))
9 | (def x (sel data :cols 1))
10 | (def y (sel data :cols 0))
11 |
12 | ;; define model function: y = exp(-b1*x)/(b2+b3*x) + e
13 | (defn f [theta x]
14 | (let [[b1 b2 b3] theta]
15 | (div (exp (mult (minus b1) x)) (plus b2 (mult b3 x)))))
16 |
17 | (def plot (scatter-plot x y :legend true))
18 | (view plot)
19 |
20 | ;; the newton-raphson algorithm fails to converge to the correct solution
21 | ;; using first set of start values from NIST, but the default gauss-newton
22 | ;; algorith converges to the correct solution.
23 |
24 | (def start1 [0.1 0.01 0.02])
25 | (add-lines plot x (f start1 x))
26 | (def nlm1 (non-linear-model f y x start1))
27 | (add-lines plot x (:fitted nlm1))
28 |
29 | ;; both algorithms converges with second set of start values from NIST
30 | (def start2 [0.15 0.008 0.010])
31 | (add-lines plot x (f start2 x))
32 | (def nlm2 (non-linear-model f y x start2))
33 | (add-lines plot x (:fitted nlm2))
34 |
35 |
--------------------------------------------------------------------------------
/examples/processing/text_tests.clj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ;; example 13-01
5 | (use '(incanter core processing))
6 |
7 | (view
8 | (sketch
9 | (setup []
10 | (let [font (load-font this "examples/processing/data/Ziggurat-HTF-Black-32.vlw")]
11 | (doto this
12 | (text-font font)
13 | smooth
14 | (fill 0))))
15 |
16 | (draw []
17 | (doto this
18 | ;(text 9 0 40)
19 | ;(text 8 0 70)
20 | ;(text 7 0 100))))
21 | ;(text \L 0 40)
22 | ;(text \a 0 70)
23 | ;(text \F 0 100))))
24 | (text "LAX" 0 40)
25 | (text "AMS" 0 70)
26 | (text "FRA" 0 100))))
27 | :size [120 120])
28 |
29 |
30 |
31 | (use '(incanter core processing))
32 | ;; use list-fonts to see the fonts availabe on the current system
33 | ;; (list-fonts)
34 |
35 | (view
36 | (sketch
37 | (setup []
38 | (let [font (create-font this "Chalkboard" 48 true)]
39 | (doto this
40 | (text-font font)
41 | ;smooth
42 | (fill 0))))
43 |
44 | (draw []
45 | (doto this
46 | ;(text 9 0 40)
47 | ;(text 8 0 70)
48 | ;(text 7 0 100))))
49 | ;(text \L 0 40)
50 | ;(text \a 0 70)
51 | ;(text \F 0 100))))
52 | (text "LAX" 0 40)
53 | (text "AMS" 0 70)
54 | (text "FRA" 0 100))))
55 | :size [120 120])
56 |
57 |
58 |
--------------------------------------------------------------------------------
/examples/blog/bayesian_multinomial.clj:
--------------------------------------------------------------------------------
1 | ;; from the following blog post:
2 | ;; http://incanter-blog.org/2009/07/01/bayes-multinomial/
3 |
4 | ;; Bayesian inference of multinomial distribution parameters
5 |
6 | (use '(incanter core stats bayes charts))
7 |
8 |
9 | (def y [727 583 137])
10 |
11 | (div y 1447.) ;; (0.502 0.403 0.095)
12 |
13 |
14 | (def theta (sample-multinomial-params 1000 y))
15 | (def theta1 (sel theta :cols 0))
16 | (def theta2 (sel theta :cols 1))
17 | (def theta3 (sel theta :cols 2))
18 |
19 | ;; view means, 95% CI, and histograms of the proportion parameters
20 | (mean theta1) ;; 0.502
21 | (sd theta1) ;; 0.0129
22 | (quantile theta1 :probs [0.025 0.975]) ;; (0.476 0.528)
23 | (view (histogram theta1
24 | :title "Bush, Sr. Support"))
25 |
26 | (mean theta2) ;; 0.403
27 | (sd theta2) ;; 0.013
28 | (quantile theta2 :probs [0.025 0.975]) ;; (0.376 0.427)
29 | (view (histogram theta2
30 | :title "Dukakis Support"))
31 |
32 | (mean theta3) ;; 0.095
33 | (sd theta3) ;; 0.008
34 | (quantile theta3 :probs [0.025 0.975]) ;; (0.082 0.111)
35 | (view (histogram theta3
36 | :title "Other/Undecided"))
37 |
38 | ;; view a histogram of the difference in proportions between the first
39 | ;; two candidates
40 | (view (histogram (minus theta1 theta2)
41 | :title "Bush, Sr. and Dukakis Diff"))
42 |
43 |
44 |
45 | ;; sample the proportions directly from a Dirichlet distribution
46 | (def alpha [1 1 1])
47 | (def props (sample-dirichlet 1000 (plus y alpha)))
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/examples/blog/mahalanobis.clj:
--------------------------------------------------------------------------------
1 |
2 | ;; See: http://matlabdatamining.blogspot.com/2006/11/mahalanobis-distance.html
3 | ;; http://en.wikipedia.org/wiki/Mahalanobis_distance
4 |
5 | (use '(incanter core stats charts))
6 |
7 | ;; EXAMPLE
8 | ;; generate some multivariate normal data with a single outlier.
9 | (def data (bind-rows
10 | (bind-columns
11 | (sample-mvn 100
12 | :sigma (matrix [[1 0.9]
13 | [0.9 1]])))
14 | [-1.75 1.75]))
15 |
16 | ;; view a scatter plot of the data
17 | (let [[x y] (trans data)]
18 | (doto (scatter-plot x y)
19 | (add-points [(mean x)] [(mean y)])
20 | (add-pointer -1.75 1.75 :text "Outlier")
21 | (add-pointer (mean x) (mean y) :text "Centroid")
22 | view))
23 |
24 | ;; calculate the mahalanobis distances of each point from the centroid.
25 | (def dists (mahalanobis-distance data))
26 | ;; view a bar-chart of the distances
27 | (view (bar-chart (range 102) dists))
28 |
29 | ;; calculate the euclidean distances of each point from the centroid.
30 | (def dists (mahalanobis-distance data :W (matrix [[1 0] [0 1]])))
31 | ;; view a bar-chart of the distances
32 | (view (bar-chart (range 102) dists))
33 |
34 |
35 | (mahalanobis-distance [-1.75 1.75] :y data)
36 | (mahalanobis-distance [-1.75 1.75]
37 | :y data
38 | :W (matrix [[1 0]
39 | [0 1]]))
40 |
41 | (mahalanobis-distance [2.5 2.5] :y data)
42 | (mahalanobis-distance [3.5 3.5] :y data)
43 |
44 |
45 |
--------------------------------------------------------------------------------
/examples/blog/regression_high_order.clj:
--------------------------------------------------------------------------------
1 | ;; from the following blog post:
2 | ;; http://incanter-blog.org/2009/06/04/linear-regression-with-higher-order-terms/
3 |
4 | ;; load the necessary libraries
5 | (use '(incanter core stats charts datasets))
6 |
7 | ;; load the NIST filip data set
8 | ;; see information on this data set at http://www.itl.nist.gov/div898/strd/lls/data/Filip.shtml
9 | (def data (to-matrix (get-dataset :filip)))
10 |
11 | (def y (sel data :cols 0))
12 |
13 | ;; use the sweep function to center the x values to reduce collinearity of the polynomial terms
14 | (def x (sweep (sel data :cols 1)))
15 |
16 | (def plot (scatter-plot x y))
17 | (view plot)
18 |
19 | ;; use the following model for the data y = x + x^2 + x^3 + ... + x^10
20 |
21 | ;; the following line of code creates a matrix of the polynomial terms x, x^2, x^3, ..., x^10,
22 | (def X (reduce bind-columns (for [i (range 1 11)] (pow x i))))
23 |
24 |
25 | ;; run the regression
26 | (def lm (linear-model y X))
27 |
28 | ;; view the results
29 |
30 | ;; view the overall model fit
31 | (:f-stat lm) ;; 2162.439
32 | (:f-prob lm) ;; 1.1E-16
33 |
34 | ;; view the R^2 of the model
35 | (:r-square lm) ;; 0.997
36 |
37 | ;; view the estimates of the coefficients
38 | (:coefs lm)
39 | ;; (0.878 0.065 -0.066 -0.016 0.037 0.003 -0.009 -2.8273E-4 9.895E-4 1.050E-5 -4.029E-5)
40 |
41 | ;; view the p-values for each of the coefficients
42 | (:t-probs lm)
43 | ;; (0 0 0 1.28E-5 0.0 0.083 1.35E-12 0.379 3.74E-8 0.614 2.651E-5)
44 |
45 | ;; overlay the fitted values on the original scatter-plot
46 | (add-points plot x (:fitted lm))
47 |
48 |
--------------------------------------------------------------------------------
/data/austres.csv:
--------------------------------------------------------------------------------
1 | year,population,quarter
2 | 1972,13254.2,Q1
3 | 1973,13459.2,Q1
4 | 1974,13669.5,Q1
5 | 1975,13862.6,Q1
6 | 1976,14004.7,Q1
7 | 1977,14155.6,Q1
8 | 1978,14330.3,Q1
9 | 1979,14478.4,Q1
10 | 1980,14646.4,Q1
11 | 1981,14874.4,Q1
12 | 1982,15121.7,Q1
13 | 1983,15346.2,Q1
14 | 1984,15531.5,Q1
15 | 1985,15736.7,Q1
16 | 1986,15961.5,Q1
17 | 1987,16203.0,Q1
18 | 1988,16478.3,Q1
19 | 1989,16777.2,Q1
20 | 1990,17026.3,Q1
21 | 1991,17239.4,Q1
22 | 1992,17447.3,Q1
23 | 1972,13303.7,Q2
24 | 1973,13504.5,Q2
25 | 1974,13722.6,Q2
26 | 1975,13893.0,Q2
27 | 1976,14033.1,Q2
28 | 1977,14192.2,Q2
29 | 1978,14359.3,Q2
30 | 1979,14515.7,Q2
31 | 1980,14695.4,Q2
32 | 1981,14923.3,Q2
33 | 1982,15184.2,Q2
34 | 1983,15393.5,Q2
35 | 1984,15579.4,Q2
36 | 1985,15788.3,Q2
37 | 1986,16018.3,Q2
38 | 1987,16263.3,Q2
39 | 1988,16538.2,Q2
40 | 1989,16833.1,Q2
41 | 1990,17085.4,Q2
42 | 1991,17292.0,Q2
43 | 1992,17482.6,Q2
44 | 1972,13353.9,Q3
45 | 1973,13552.6,Q3
46 | 1974,13772.1,Q3
47 | 1975,13926.8,Q3
48 | 1976,14066.0,Q3
49 | 1977,14231.7,Q3
50 | 1978,14396.6,Q3
51 | 1979,14554.9,Q3
52 | 1980,14746.6,Q3
53 | 1981,14988.7,Q3
54 | 1982,15239.3,Q3
55 | 1983,15439.0,Q3
56 | 1984,15628.5,Q3
57 | 1985,15839.7,Q3
58 | 1986,16076.9,Q3
59 | 1987,16327.9,Q3
60 | 1988,16621.6,Q3
61 | 1989,16891.6,Q3
62 | 1990,17106.9,Q3
63 | 1991,17354.2,Q3
64 | 1992,17526.0,Q3
65 | 1972,13409.3,Q4
66 | 1973,13614.3,Q4
67 | 1974,13832.0,Q4
68 | 1975,13968.9,Q4
69 | 1976,14110.1,Q4
70 | 1977,14281.5,Q4
71 | 1978,14430.8,Q4
72 | 1979,14602.5,Q4
73 | 1980,14807.4,Q4
74 | 1981,15054.1,Q4
75 | 1982,15288.9,Q4
76 | 1983,15483.5,Q4
77 | 1984,15677.3,Q4
78 | 1985,15900.6,Q4
79 | 1986,16139.0,Q4
80 | 1987,16398.9,Q4
81 | 1988,16697.0,Q4
82 | 1989,16956.8,Q4
83 | 1990,17169.4,Q4
84 | 1991,17414.2,Q4
85 | 1992,17568.7,Q4
86 |
--------------------------------------------------------------------------------
/modules/incanter-core/test/incanter/symbolic_tests.clj:
--------------------------------------------------------------------------------
1 | ;;; symbolic-test-cases.clj -- Unit tests of Incanter functions
2 |
3 | ;; by David Edgar Liebke http://incanter.org
4 | ;; May 3 2010
5 |
6 | ;; Copyright (c) David Edgar Liebke, 2009. All rights reserved. The use
7 | ;; and distribution terms for this software are covered by the Eclipse
8 | ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
9 | ;; which can be found in the file epl-v10.html at the root of this
10 | ;; distribution. By using this software in any fashion, you are
11 | ;; agreeing to be bound by the terms of this license. You must not
12 | ;; remove this notice, or any other, from this software.
13 |
14 |
15 |
16 | (ns incanter.symbolic-tests
17 | (:use clojure.test
18 | (incanter core symbolic)))
19 |
20 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21 | ;; UNIT TESTS FOR incanter.symbolics.clj
22 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
23 |
24 | (deftest deriv-test
25 | (is (= (deriv (+ x 3) x) 1))
26 | (is (= (deriv (* x y) x) 'y))
27 | (is (= (deriv (* (* x y) (+ x 3)) x) '(+ (* (+ x 3) y) (* x y))))
28 | (is (= (deriv (* (* x y) (+ x 3)) y) '(* (+ x 3) x)))
29 | (is (= (deriv (* x y (+ x 3)) x) '(+ (* y (+ x 3)) (* y x))))
30 | (is (= (deriv (* x y (+ x 3)) y) '(* (+ x 3) x)))
31 | (is (= (deriv (sin x) x) '(cos x)))
32 | (is (= (deriv (cos x) x) '(* -1 (sin x))))
33 | (is (= (deriv (sin (* x y)) y) '(* x (cos (* x y)))))
34 | (is (= (deriv (pow x 3) x) '(* 3 (pow x 2))))
35 | (is (= (deriv (** x 3) x) '(* 3 (pow x 2))))
36 | (is (= (deriv (pow x 3) x 2) '(* 3 (* 2 x))))
37 | (is (= (deriv (* x y (+ x 3)) x 2) '(+ y y)))
38 | (is (= (deriv (* x y (+ x 3)) x 3) 0))
39 | (is (= (deriv (+ (* x y) (* 3 y)) y) '(+ 3 x)))
40 | (is (= (deriv (log (pow x 2)) x) '(* (pow (pow x 2) -1) (* 2 x))))
41 | (is (= (deriv (+ (* 3 x) (* 8 x)) x) 11)))
--------------------------------------------------------------------------------
/project.clj:
--------------------------------------------------------------------------------
1 | (defproject incanter "1.3.0"
2 | :description "Incanter is a Clojure-based, R-like statistical programming and data visualization environment."
3 | :dependencies [[org.clojure/clojure "1.3.0"]
4 | [incanter/incanter-core "1.3.0"
5 | :exclusions [org.clojure/clojure
6 | org.clojure/clojure-contrib]]
7 | [incanter/incanter-io "1.3.0"
8 | :exclusions [org.clojure/clojure
9 | org.clojure/clojure-contrib]]
10 | [incanter/incanter-charts "1.3.0"
11 | :exclusions [org.clojure/clojure
12 | org.clojure/clojure-contrib]]
13 | [incanter/incanter-processing "1.3.0"
14 | :exclusions [org.clojure/clojure
15 | org.clojure/clojure-contrib]]
16 | [incanter/incanter-mongodb "1.3.0"
17 | :exclusions [org.clojure/clojure
18 | org.clojure/clojure-contrib]]
19 | [incanter/incanter-pdf "1.3.0"
20 | :exclusions [org.clojure/clojure
21 | org.clojure/clojure-contrib]]
22 | [incanter/incanter-latex "1.3.0"
23 | :exclusions [org.clojure/clojure
24 | org.clojure/clojure-contrib]]
25 | [incanter/incanter-excel "1.3.0"
26 | :exclusions [org.clojure/clojure
27 | org.clojure/clojure-contrib]]
28 | [swingrepl "1.3.0"
29 | :exclusions [org.clojure/clojure
30 | org.clojure/clojure-contrib]]
31 | [jline "0.9.94"]]
32 | :dev-dependencies [[lein-clojars "0.7.0"
33 | :exclusions [org.clojure/clojure
34 | org.clojure/clojure-contrib]]]
35 | :main incanter.main)
36 |
--------------------------------------------------------------------------------
/modules/incanter-pdf/src/incanter/pdf.clj:
--------------------------------------------------------------------------------
1 | (ns
2 | ^{:doc "This library currently has only a single function, save-pdf, which saves
3 | charts as a PDF file. To build this namespace make sure the you have the iText
4 | library (http://itextpdf.com/) as a declared dependency in your pom.xml or
5 | project.clj file:
6 | [com.lowagie/itext \"1.4\"] "}
7 |
8 | incanter.pdf
9 | (:use (incanter charts))
10 | (:import (com.lowagie.text Document DocumentException Rectangle)
11 | (com.lowagie.text.pdf DefaultFontMapper FontMapper PdfContentByte
12 | PdfTemplate PdfWriter)
13 | (java.awt Graphics2D)
14 | (java.awt.geom Rectangle2D)
15 | (java.io BufferedOutputStream File FileOutputStream OutputStream)
16 | (java.text SimpleDateFormat)))
17 |
18 |
19 | (defn save-pdf
20 | " Save a chart object as a pdf document.
21 |
22 | Arguments:
23 | chart
24 | filename
25 |
26 | Options:
27 | :width (default 500)
28 | :height (defualt 400)
29 |
30 | Examples:
31 |
32 | (use '(incanter core charts pdf))
33 | (save-pdf (function-plot sin -4 4) \"./pdf-chart.pdf\")
34 |
35 |
36 | "
37 | ([chart filename & {:keys [width height ] :or {width 500 height 400}}]
38 | (let [pagesize (Rectangle. width height)
39 | document (Document. pagesize 50 50 50 50)
40 | out (BufferedOutputStream. (FileOutputStream. filename))
41 | writer (PdfWriter/getInstance document out)
42 | _ (.open document)
43 | cb (.getDirectContent writer)
44 | tp (.createTemplate cb width height)
45 | mapper (DefaultFontMapper.)
46 | g2 (.createGraphics tp width height mapper)
47 | r2D (new java.awt.geom.Rectangle2D$Double 0 0 width height)]
48 | (do
49 | (.draw chart g2 r2D)
50 | (.dispose g2)
51 | (.addTemplate cb tp 0 0)
52 | (.close document)
53 | (.close out)))))
54 |
55 |
56 |
--------------------------------------------------------------------------------
/examples/blog/projects/newtheme/src/newtheme/core.clj:
--------------------------------------------------------------------------------
1 |
2 | (ns newlook.core
3 | (:use [incanter core stats charts datasets]))
4 |
5 |
6 | (view (histogram (sample-normal 1000)))
7 |
8 |
9 |
10 | (view (function-plot sin -10 10))
11 |
12 | (doto (function-plot sin -10 10)
13 | (add-function cos -10 10)
14 | view)
15 |
16 | (view (scatter-plot :Sepal.Length :Sepal.Width :data (get-dataset :iris)))
17 |
18 | (doto (scatter-plot :Sepal.Length :Sepal.Width :data (get-dataset :iris))
19 | (set-stroke-color java.awt.Color/gray)
20 | view)
21 |
22 |
23 | (view (scatter-plot :Sepal.Length :Sepal.Width :group-by :Species :data (get-dataset :iris)))
24 |
25 |
26 |
27 |
28 | (with-data (->> (get-dataset :hair-eye-color)
29 | ($rollup :sum :count [:hair :eye]))
30 | (view (bar-chart :hair :count :group-by :eye :legend true)))
31 |
32 |
33 |
34 | (doto (box-plot (sample-gamma 1000 :shape 1 :rate 2)
35 | :legend true :y-label "")
36 | view
37 | (add-box-plot (sample-gamma 1000 :shape 2 :rate 2))
38 | (add-box-plot (sample-gamma 1000 :shape 3 :rate 2)))
39 |
40 |
41 |
42 | (use '(incanter core charts))
43 | (defn log32 [x] (/ (log x) (log 32)))
44 | (defn f1 [n] (plus (log2 n) (mult (log32 n) 5000)))
45 | (defn f2 [n] n)
46 |
47 |
48 |
49 | (def min-val 10)
50 | (def max-val 40000)
51 | (def chart (doto (function-plot f1 min-val max-val
52 | :legend true
53 | :series-label "O(log2 n) + O(log32 n) * 5000"
54 | :x-label ""
55 | :y-label "")
56 | (add-function f2 min-val max-val :step-size 5000 :series-label "O(n)")
57 | (set-stroke :width 2)
58 | (set-stroke :width 2 :series 1 :dash 5)))
59 |
60 | (view chart)
61 |
62 | ;; PLOT (A)
63 | (doto chart
64 | (set-title "(A)")
65 | (set-x-range 100 5000)
66 | (set-y-range 30 12000))
67 |
68 | ;; PLOT (B)
69 | (doto chart
70 | (set-title "(B)")
71 | (set-y-range 10000 16000)
72 | (set-x-range 10000 16000))
73 |
74 |
75 | ;; PLOT (C)
76 | (doto chart
77 | (set-title "(C)")
78 | (set-y-range 0 30000)
79 | (set-x-range 0 30000))
80 |
81 |
82 |
83 |
--------------------------------------------------------------------------------
/examples/blog/projects/string_template/src/hello.clj:
--------------------------------------------------------------------------------
1 | (ns hello
2 | (:gen-class)
3 | (:use (incanter core stats charts datasets))
4 | (:import [org.antlr.stringtemplate StringTemplate]))
5 |
6 |
7 | (defn apply-template [#^String txt #^java.util.Map context]
8 | (let [t (StringTemplate. txt)]
9 | (.setAttributes t context)
10 | (.toString t)))
11 |
12 |
13 | (defn to-html
14 | ""
15 | ([data]
16 | (let [html "
17 | $colnames:{col | | $col$ | }$
18 | $rows:{cols | $cols:{col || $col$ | }$
\n}$
19 |
\n"]
20 | (apply-template html {"colnames" (col-names data), "rows" (to-list data)}))))
21 |
22 |
23 |
24 | (defn -main [& args]
25 | (println (str "\n"
26 | (to-html (get-dataset :iris))
27 | "\n")))
28 |
29 |
30 |
31 | (def template-str
32 | "
33 |
34 |
35 |
36 | $names:{n | - $n$
\n}$
37 |
38 |
39 | $namekeys,namevalues:{k,v| - $k$, $v$
\n}$
40 |
41 |
42 | $colnames:{col | | $col$ | }$
43 | $rows:{cols | $cols:{col || $col$ | }$
\n}$
44 |
45 |
46 |
47 | ")
48 |
49 |
50 | (defn -main-orig [& args]
51 | ;(view (function-plot sin -4 4)))
52 | ;(println (template "Hello $user$. Today is $date$" {"user" "Joe" "date" (java.util.Date.)})))
53 | ;(println (template "Hello $user; separator=\",\"$. Today is $date$"
54 | ;{"user" ["Joe" "David"] "date" (java.util.Date.)})))
55 | (let [name-map {"Mike" "value 1", "David" "value 2", "Victor" "value 3", "Todd" "value 4"}
56 | row #^java.util.Map {"name" "David" "value" "value 1"}
57 | data (get-dataset :iris)]
58 | (println (apply-template template-str
59 | {"names" ["Mike" "David" "Victor" "Todd" "Cesar"] "date" (java.util.Date.)
60 | "rows" (to-list data)
61 | "colnames" (col-names data)
62 | "namekeys" (keys name-map) "namevalues" (vals name-map)}))))
63 |
64 |
65 |
--------------------------------------------------------------------------------
/examples/blog/categorical_plots.clj:
--------------------------------------------------------------------------------
1 | ;; from the following blog post
2 | ;; http://incanter-blog.org/2009/06/13/plotting-with-non-numeric-data/
3 | ;; plotting categorical data
4 |
5 | (use '(incanter core charts datasets))
6 |
7 | ;; bar-charts
8 | (view (bar-chart ["a" "b" "c" "d" "e"] [10 20 30 25 20]))
9 |
10 | (view (bar-chart ["a" "a" "b" "b" "c" "c" ] [10 20 30 10 40 20]
11 | :legend true
12 | :group-by ["I" "II" "I" "II" "I" "II"]))
13 |
14 | (view (line-chart ["a" "b" "c" "d" "e"] [20 10 30 25 40]))
15 |
16 |
17 | (def data (get-dataset :airline-passengers))
18 | (def by-year (group-by data 0))
19 | (view (bar-chart (sel (last by-year) :cols 2)
20 | (sel (last by-year) :cols 1)
21 | :title "Airline Travel in 1960"
22 | :y-label "Passengers"
23 | :x-label "Month"))
24 |
25 |
26 | ;; line-charts
27 | (view (line-chart (sel (last by-year) :cols 2)
28 | (sel (last by-year) :cols 1)
29 | :title "Airline Travel in 1960"
30 | :y-label "Passengers"
31 | :x-label "Month"))
32 |
33 | (view (line-chart (sel data :cols 2)
34 | (sel data :cols 1)
35 | :group-by (sel data :cols 0)
36 | :title "Airline Travel in 1949-1960"
37 | :legend true
38 | :y-label "Passengers"
39 | :x-label "Month"))
40 |
41 |
42 | ;; more bar-charts
43 | (view (bar-chart (sel data :cols 2)
44 | (sel data :cols 1)
45 | :group-by (sel data :cols 0)
46 | :title "Airline Travel in 1949-1960"
47 | :legend true
48 | :y-label "Passengers"
49 | :x-label "Year"))
50 |
51 | (view (bar-chart (sel data :cols 0)
52 | (sel data :cols 1)
53 | :group-by (sel data :cols 2)
54 | :title "Airline Travel in 1949-1960"
55 | :legend true
56 | :y-label "Passengers"
57 | :x-label "Year")
58 | :width 525)
59 |
60 |
--------------------------------------------------------------------------------
/data/filip.dat:
--------------------------------------------------------------------------------
1 | y x
2 | 0.8116 -6.860120914
3 | 0.9072 -4.324130045
4 | 0.9052 -4.358625055
5 | 0.9039 -4.358426747
6 | 0.8053 -6.955852379
7 | 0.8377 -6.661145254
8 | 0.8667 -6.355462942
9 | 0.8809 -6.118102026
10 | 0.7975 -7.115148017
11 | 0.8162 -6.815308569
12 | 0.8515 -6.519993057
13 | 0.8766 -6.204119983
14 | 0.8885 -5.853871964
15 | 0.8859 -6.109523091
16 | 0.8959 -5.79832982
17 | 0.8913 -5.482672118
18 | 0.8959 -5.171791386
19 | 0.8971 -4.851705903
20 | 0.9021 -4.517126416
21 | 0.909 -4.143573228
22 | 0.9139 -3.709075441
23 | 0.9199 -3.499489089
24 | 0.8692 -6.300769497
25 | 0.8872 -5.953504836
26 | 0.89 -5.642065153
27 | 0.891 -5.031376979
28 | 0.8977 -4.680685696
29 | 0.9035 -4.329846955
30 | 0.9078 -3.928486195
31 | 0.7675 -8.56735134
32 | 0.7705 -8.363211311
33 | 0.7713 -8.107682739
34 | 0.7736 -7.823908741
35 | 0.7775 -7.522878745
36 | 0.7841 -7.218819279
37 | 0.7971 -6.920818754
38 | 0.8329 -6.628932138
39 | 0.8641 -6.323946875
40 | 0.8804 -5.991399828
41 | 0.7668 -8.781464495
42 | 0.7633 -8.663140179
43 | 0.7678 -8.473531488
44 | 0.7697 -8.247337057
45 | 0.77 -7.971428747
46 | 0.7749 -7.676129393
47 | 0.7796 -7.352812702
48 | 0.7897 -7.072065318
49 | 0.8131 -6.774174009
50 | 0.8498 -6.478861916
51 | 0.8741 -6.159517513
52 | 0.8061 -6.835647144
53 | 0.846 -6.53165267
54 | 0.8751 -6.224098421
55 | 0.8856 -5.910094889
56 | 0.8919 -5.598599459
57 | 0.8934 -5.290645224
58 | 0.894 -4.974284616
59 | 0.8957 -4.64454848
60 | 0.9047 -4.290560426
61 | 0.9129 -3.885055584
62 | 0.9209 -3.408378962
63 | 0.9219 -3.13200249
64 | 0.7739 -8.726767166
65 | 0.7681 -8.66695597
66 | 0.7665 -8.511026475
67 | 0.7703 -8.165388579
68 | 0.7702 -7.886056648
69 | 0.7761 -7.588043762
70 | 0.7809 -7.283412422
71 | 0.7961 -6.995678626
72 | 0.8253 -6.691862621
73 | 0.8602 -6.392544977
74 | 0.8809 -6.067374056
75 | 0.8301 -6.684029655
76 | 0.8664 -6.378719832
77 | 0.8834 -6.065855188
78 | 0.8898 -5.752272167
79 | 0.8964 -5.132414673
80 | 0.8963 -4.811352704
81 | 0.9074 -4.098269308
82 | 0.9119 -3.66174277
83 | 0.9228 -3.2644011
84 |
85 |
--------------------------------------------------------------------------------
/modules/incanter-core/test/incanter/dataset_tests.clj:
--------------------------------------------------------------------------------
1 | (ns incanter.dataset-tests
2 | (:use clojure.test
3 | (incanter core)))
4 |
5 | (def dataset1 (dataset [:a :b :c] [[1 2 3] [4 5 6]]))
6 | (def dataset2 (dataset [" a" "b" "c"] [[1 2 3] [4 5 6]]))
7 | (def dataset3 (dataset [:a :b :c] [{:a 1 :b 2 :c 3} {:a 4 :b 5 :c 6}]))
8 | (def dataset4 (dataset ["a" "b" "c"] [{"a" 1 "b" 2 "c" 3} {"a" 4 "b" 5 "c" 6}]))
9 | (def dataset5 (dataset ["a" "b" "c"] [{"a" 1 "b" 2 "c" 3} {"b" 5 "c" 6}]))
10 |
11 | (deftest dataset-tests
12 | (is (= (sel dataset1 :cols :a) [1 4]))
13 | (is (= (sel dataset2 :cols :b) [2 5]))
14 | (is (= (sel dataset2 :cols "c") [3 6]))
15 | (is (= (sel dataset3 :cols :a) [1 4]))
16 | (is (= (sel dataset4 :cols :b) [2 5]))
17 | (is (= (sel dataset4 :cols "c") [3 6]))
18 | (is (= (sel dataset5 :rows 1 :cols :a) nil)))
19 |
20 | (def car0 [60, 6000, :green])
21 | (def car1 [70, 7000, :silver])
22 |
23 | (def cars (dataset [:speed :weight :colour] [car0 car1]))
24 | (def cars-without-weight (dataset [:speed :colour] [[60, :green] [70, :silver]]))
25 |
26 | (deftest select-col-from-dataset
27 | (is (= ($ :speed cars) [60 70]))
28 | (is (= ($ :weight cars) [6000 7000]))
29 | (is (= ($ :colour cars) [:green :silver])))
30 |
31 | (deftest unselect-column-from-dataset
32 | (is (= ($ [:not :weight] cars) (dataset [:speed :colour] [[60, :green] [70, :silver]]))))
33 |
34 | (deftest select-row-from-dataset
35 | (is (= ($ 0 :all cars) car0))
36 | (is (= ($ 1 :all cars) car1)))
37 |
38 | (deftest unselect-row-from-dataset
39 | (is (= ($ [:not 0] :all cars) car1))
40 | (is (= ($ [:not 1] :all cars) car0)))
41 |
42 | (deftest select-all-returns-input
43 | (is (= ($ :all cars) cars)))
44 |
45 | (testing "picks up data from scope"
46 | (with-data cars
47 | (is (= ($ :speed) [60 70]))
48 | (is (= ($ :speed nil) [60 70]))))
49 |
50 | (deftest test-head
51 | (is (= (head 2 dataset1) ($ (range 2) :all dataset1)))
52 | (is (= (head dataset1) ($ (range 2) :all dataset1))))
53 |
54 | ;; (deftest selects-on-badly-named-atoms
55 | ;; (let [with-nots (dataset [:first :second] [[:not :all] [:all :not]])]
56 | ;; (is (= ($ :first
57 | ;; )))))
58 |
--------------------------------------------------------------------------------
/data/us_arrests.dat:
--------------------------------------------------------------------------------
1 | State,Murder,Assault,UrbanPop,Rape
2 | Alabama,13.2,236,58,21.2
3 | Alaska,10.0,263,48,44.5
4 | Arizona,8.1, 294 , 80,31.0
5 | Arkansas,8.8, 190 , 50,19.5
6 | California,9.0, 276 , 91,40.6
7 | Colorado,7.9, 204 , 78,38.7
8 | Connecticut,3.3 , 110 , 77, 11.1
9 | Delaware, 5.9 , 238 , 72, 15.8
10 | Florida, 15.4 , 335 , 80, 31.9
11 | Georgia, 17.4 , 211 , 60, 25.8
12 | Hawaii, 5.3 , 46 , 83, 20.2
13 | Idaho, 2.6 , 120 , 54, 14.2
14 | Illinois, 10.4 , 249 , 83, 24.0
15 | Indiana, 7.2 , 113 , 65, 21.0
16 | Iowa, 2.2 , 56 , 57, 11.3
17 | Kansas, 6.0 , 115 , 66, 18.0
18 | Kentucky, 9.7 , 109 , 52, 16.3
19 | Louisiana, 15.4 , 249 , 66, 22.2
20 | Maine, 2.1 , 83 , 51 , 7.8
21 | Maryland, 11.3 , 300 , 67, 27.8
22 | Massachusetts, 4.4 , 149 , 85, 16.3
23 | Michigan, 12.1 , 255 , 74, 35.1
24 | Minnesota, 2.7 , 72 , 66, 14.9
25 | Mississippi, 16.1 , 259 , 44, 17.1
26 | Missouri, 9.0 , 178 , 70, 28.2
27 | Montana, 6.0 , 109 , 53, 16.4
28 | Nebraska, 4.3 , 102 , 62, 16.5
29 | Nevada, 12.2 , 252 , 81, 46.0
30 | New Hampshire, 2.1 , 57 , 56, 9.5
31 | New Jersey, 7.4 , 159 , 89, 18.8
32 | New Mexico, 11.4 , 285 , 70, 32.1
33 | New York, 11.1 , 254 , 86, 26.1
34 | North Carolina, 13.0 , 337 , 45, 16.1
35 | North Dakota, 0.8 , 45 , 44, 7.3
36 | Ohio, 7.3 , 120 , 75, 21.4
37 | Oklahoma, 6.6 , 151 , 68, 20.0
38 | Oregon, 4.9 , 159 , 67, 29.3
39 | Pennsylvania, 6.3 , 106 , 72, 14.9
40 | Rhode Island, 3.4 , 174 , 87, 8.3
41 | South Carolina, 14.4 , 279 , 48, 22.5
42 | South Dakota, 3.8 , 86 , 45, 12.8
43 | Tennessee, 13.2 , 188 , 59, 26.9
44 | Texas, 12.7 , 201 , 80, 25.5
45 | Utah, 3.2 , 120 , 80, 22.9
46 | Vermont, 2.2 , 48 , 32, 11.2
47 | Virginia, 8.5 , 156 , 63, 20.7
48 | Washington, 4.0 , 145 , 73, 26.2
49 | West Virginia, 5.7 , 81 , 39, 9.3
50 | Wisconsin, 2.6 , 53 , 66, 10.8
51 | Wyoming, 6.8 , 161 , 60, 15.6
52 |
53 |
--------------------------------------------------------------------------------
/examples/blog/annotations.clj:
--------------------------------------------------------------------------------
1 | ;; from the following blog post
2 | ;; http://incanter-blog.org/2009/06/07/annotating-incanter-plots/
3 |
4 | (use '(incanter core charts))
5 |
6 | (def x (range (* -2 Math/PI) (* 2 Math/PI) 0.01))
7 | (def plot (xy-plot x (sin x)))
8 | (view plot)
9 |
10 | ;; annotate the plot
11 | (doto plot
12 | (add-pointer (- Math/PI) (sin (- Math/PI))
13 | :text "(-pi, (sin -pi))")
14 | (add-pointer Math/PI (sin Math/PI)
15 | :text "(pi, (sin pi))" :angle :ne)
16 | (add-pointer (* 1/2 Math/PI) (sin (* 1/2 Math/PI))
17 | :text "(pi/2, (sin pi/2))" :angle :south))
18 |
19 |
20 | ;; try the different angle options
21 | (doto plot
22 | (add-pointer 0 0 :text "north" :angle :north)
23 | (add-pointer 0 0 :text "nw" :angle :nw)
24 | (add-pointer 0 0 :text "ne" :angle :ne)
25 | (add-pointer 0 0 :text "west" :angle :west)
26 | (add-pointer 0 0 :text "east" :angle :east)
27 | (add-pointer 0 0 :text "south" :angle :south)
28 | (add-pointer 0 0 :text "sw" :angle :sw)
29 | (add-pointer 0 0 :text "se" :angle :se))
30 |
31 |
32 |
33 | ;; PCA chart example
34 | (use '(incanter core stats charts datasets))
35 | ;; load the iris dataset
36 | (def iris (to-matrix (get-dataset :iris)))
37 | ;; run the pca
38 | (def pca (principal-components (sel iris :cols (range 4))))
39 | ;; extract the first two principal components
40 | (def pc1 (sel (:rotation pca) :cols 0))
41 | (def pc2 (sel (:rotation pca) :cols 1))
42 |
43 | ;; project the first four dimension of the iris data onto the first
44 | ;; two principal components
45 | (def x1 (mmult (sel iris :cols (range 4)) pc1))
46 | (def x2 (mmult (sel iris :cols (range 4)) pc2))
47 |
48 | ;; now plot the transformed data, coloring each species a different color
49 | (def plot (scatter-plot x1 x2
50 | :group-by (sel iris :cols 4)
51 | :x-label "PC1" :y-label "PC2" :title "Iris PCA"))
52 |
53 | (view plot)
54 |
55 |
56 | ;; add some text annotations
57 | (doto plot
58 | (add-text -2.5 -6.5 "Setosa")
59 | (add-text -5 -5.5 "Versicolor")
60 | (add-text -8 -5.5 "Virginica"))
61 |
62 | ;; put box around the first group
63 | (add-polygon plot [[-3.2 -6.3] [-2 -6.3] [-2 -3.78] [-3.2 -3.78]])
64 |
65 |
--------------------------------------------------------------------------------
/modules/incanter-excel/src/incanter/excel/workbook.clj:
--------------------------------------------------------------------------------
1 | (ns incanter.excel.workbook
2 | (:import [org.apache.poi.ss.usermodel Font]
3 | [org.apache.poi.ss.usermodel Workbook]
4 | [org.apache.poi.hssf.usermodel HSSFWorkbook]
5 | [org.apache.poi.xssf.usermodel XSSFWorkbook]
6 | [java.io FileOutputStream]))
7 |
8 | (defmulti ^ {:doc "Retrieve the Excel workbook based on either the index or the sheet name."}
9 | get-workbook-sheet
10 | (fn [wbk index-or-name] (if (integer? index-or-name) :indexed :named)))
11 |
12 | (defmethod get-workbook-sheet :indexed [wbk index-or-name]
13 | (. wbk getSheetAt index-or-name))
14 |
15 | (defmethod get-workbook-sheet :named [wbk index-or-name]
16 | (. wbk getSheet (str index-or-name)))
17 |
18 | (defn make-font [normal? ^Workbook w]
19 | (let [f (. w createFont)
20 | c (. w createCellStyle)]
21 | (. f setBoldweight (if normal? Font/BOLDWEIGHT_NORMAL Font/BOLDWEIGHT_BOLD))
22 | (. c setFont f)
23 | c))
24 |
25 | (defn create-sheet [blob ^String sheet]
26 | (assoc blob :sheet (. (:workbook blob) createSheet sheet)))
27 |
28 | (defn make-workbook-map
29 | ([^Workbook w]
30 | {:workbook w
31 | :normal (make-font true w)
32 | :bold (make-font false w)})
33 | ([^Workbook w ^String sheet]
34 | (create-sheet (make-workbook-map w) sheet)))
35 |
36 | (defn write-workbook
37 | [^Workbook workbook ^String filename]
38 | (with-open [f (FileOutputStream. filename)]
39 | (. workbook write f)))
40 |
41 | (defn create-workbook-object
42 | ([filename override?]
43 | (cond
44 | (= override? :xlsx) (XSSFWorkbook.)
45 | (= override? :xls) (HSSFWorkbook.)
46 | :else (if (. filename endsWith "xlsx")
47 | (XSSFWorkbook.)
48 | (HSSFWorkbook.))))
49 | ([filename override? filestream]
50 | (cond
51 | (= override? :xlsx) (XSSFWorkbook. filestream)
52 | (= override? :xls) (HSSFWorkbook. filestream)
53 | :else (if (. filename endsWith "xlsx")
54 | (XSSFWorkbook. filestream)
55 | (HSSFWorkbook. filestream)))))
56 |
57 | (defn get-all-sheets [^Workbook w]
58 | (for [i (range 0 (. w getNumberOfSheets))]
59 | (. w getSheetAt i)))
60 |
--------------------------------------------------------------------------------
/examples/blog/monte_hall.clj:
--------------------------------------------------------------------------------
1 | ;; from the following blog post
2 | ;; http://incanter-blog.org/2009/06/23/monty-hall-in-monte-carlo/
3 |
4 | ;; Monty Hall problem (Let's Make a Deal gameshow)
5 | ;; http://www.marilynvossavant.com/articles/gameshow.html
6 | (use '(incanter core stats charts))
7 |
8 | ;; set a simulation sample size
9 | (def n 10000)
10 |
11 | ;; generate samples of initial-guesses, prize-doors, and switch decisions
12 | (def initial-guesses (sample [1 2 3] :size n))
13 | (def prize-doors (sample [1 2 3] :size n))
14 | (def switches (sample [true false] :size n))
15 |
16 |
17 | ;; define a function that returns 1 if a switch decision results in winning
18 | (defn switch-win? [initial-guess switch prize-door]
19 | (if (and switch (not= initial-guess prize-door)) 1 0))
20 |
21 | ;; define a function that returns 1 if a stay decision results in winning
22 | (defn stay-win? [initial-guess switch prize-door]
23 | (if (and (not switch) (= initial-guess prize-door)) 1 0))
24 |
25 |
26 |
27 | ;; calculate the joint probability of winning and switching
28 | (def prob-switch-win (/ (sum (map switch-win?
29 | initial-guesses
30 | switches
31 | prize-doors))
32 | n))
33 |
34 | ;; calculate the probability of switching doors
35 | (def prob-switch (/ (sum (indicator true? switches)) n))
36 |
37 | ;; calculate the conditional probability of winning given a switch
38 | (def prob-win-given-switch (/ prob-switch-win prob-switch))
39 |
40 |
41 | ;; calculate the joint probability of winning and NOT switching
42 | (def prob-stay-win (/ (sum (map stay-win?
43 | initial-guesses
44 | switches
45 | prize-doors))
46 | n))
47 |
48 | ;; calculate the probability of NOT switching doors
49 | (def prob-stay (/ (sum (indicator false? switches)) n))
50 |
51 | ;; calculate the conditional probability of winning given NOT switching
52 | (def prob-win-given-stay (/ prob-stay-win prob-stay))
53 |
54 | (view (bar-chart ["Switch" "Stay"]
55 | [prob-win-given-switch prob-win-given-stay]
56 | :title "Monty Hall Problem"
57 | :x-label "Decision"
58 | :y-label "Win Probability"))
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/examples/blog/processing_intro.clj:
--------------------------------------------------------------------------------
1 | ;; from the following blog post
2 | ;; http://incanter-blog.org/2009/08/30/processing-intro/
3 |
4 | (use '(incanter core processing))
5 |
6 | ;; simple interactive Processing example taken from processingjs.org website:
7 | ;; http://processingjs.org/source/basic-example/processingjs_basic-example.html
8 |
9 | ;; set up variable references to use in the sketch object
10 | (let [radius (ref 50.0)
11 | X (ref nil)
12 | Y (ref nil)
13 | nX (ref nil)
14 | nY (ref nil)
15 | delay 16
16 |
17 | ;; define a sketch object (i.e. PApplet)
18 | sktch (sketch
19 |
20 | ;; define the setup function
21 | (setup []
22 | (doto this
23 | ;no-loop
24 | (size 200 200)
25 | (stroke-weight 10)
26 | (framerate 15)
27 | smooth)
28 | (dosync
29 | (ref-set X (/ (width this) 2))
30 | (ref-set Y (/ (width this) 2))
31 | (ref-set nX @X)
32 | (ref-set nY @Y)))
33 |
34 | ;; define the draw function
35 | (draw []
36 | (dosync
37 | (ref-set radius (+ @radius (sin (/ (frame-count this) 4))))
38 | (ref-set X (+ @X (/ (- @nX @X) delay)))
39 | (ref-set Y (+ @Y (/ (- @nY @Y) delay))))
40 | (doto this
41 | (background 125) ;; gray
42 | (fill 0 121 184)
43 | (stroke 255)
44 | (ellipse @X @Y @radius @radius)
45 | ;(save "proc_example1.png")
46 | ))
47 |
48 | ;; define mouseMoved function (mouseMoved and mouseDraw
49 | ;; require a 'mouse-event' argument unlike the standard Processing
50 | ;; methods)
51 | (mouseMoved [mouse-event]
52 | (dosync
53 | ;; mouse-x and mouse-y take the mouse-event as an argument
54 | (ref-set nX (mouse-x mouse-event))
55 | (ref-set nY (mouse-y mouse-event)))))]
56 |
57 | ;; use the view function to display the sketch
58 | (view sktch :size [200 200]))
59 |
60 |
--------------------------------------------------------------------------------
/examples/processing/simple_interation.clj:
--------------------------------------------------------------------------------
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 |
3 |
4 |
5 | (use '(incanter core processing))
6 |
7 | ;; simple interactive Processing example taken from processingjs.org website:
8 | ;; http://processingjs.org/source/basic-example/processingjs_basic-example.html
9 |
10 | ;; set up variable references to use in the sketch object
11 | (let [radius (ref 50.0)
12 | X (ref nil)
13 | Y (ref nil)
14 | nX (ref nil)
15 | nY (ref nil)
16 | delay 16
17 |
18 | ;; define a sketch object (i.e. PApplet)
19 | sktch (sketch
20 |
21 | ;; define the setup function
22 | (setup []
23 | (doto this
24 | ;no-loop
25 | (size 200 200)
26 | (stroke-weight 10)
27 | (framerate 15)
28 | smooth)
29 | (dosync
30 | (ref-set X (/ (width this) 2))
31 | (ref-set Y (/ (width this) 2))
32 | (ref-set nX @X)
33 | (ref-set nY @Y)))
34 |
35 | ;; define the draw function
36 | (draw []
37 | (dosync
38 | (ref-set radius (+ @radius (sin (/ (frame-count this) 4))))
39 | (ref-set X (+ @X (/ (- @nX @X) delay)))
40 | (ref-set Y (+ @Y (/ (- @nY @Y) delay))))
41 | (doto this
42 | (background 125) ;; gray
43 | (fill 0 121 184)
44 | (stroke 255)
45 | (ellipse @X @Y @radius @radius)
46 | ;(save "proc_example1.png")
47 | ))
48 |
49 | ;; define mouseMoved function (mouseMoved and mouseDraw
50 | ;; require a 'mouse-event' argument unlike the standard Processing
51 | ;; methods)
52 | (mouseMoved [mouse-event]
53 | (dosync
54 | ;; mouse-x and mouse-y take the mouse-event as an argument
55 | (ref-set nX (mouse-x mouse-event))
56 | (ref-set nY (mouse-y mouse-event)))))]
57 |
58 | ;; use the view function to display the sketch
59 | (view sktch :size [200 200]))
60 |
61 |
62 |
--------------------------------------------------------------------------------
/examples/blog/chisq_test_indep.clj:
--------------------------------------------------------------------------------
1 | ;; from the following blog post:
2 | ;; http://incanter-blog.org/2009/06/14/chi-square-test/
3 |
4 | ;; Chi-square test of independence:
5 | ;; testing the independence of eye and hair color
6 | ;; for both males and females
7 |
8 | (use '(incanter core stats charts datasets))
9 |
10 | (def by-gender (group-by (get-dataset :hair-eye-color) 2))
11 |
12 | (def male-data (first by-gender))
13 | (view male-data)
14 |
15 | (def female-data (second by-gender))
16 | (view female-data)
17 |
18 | (def m-hair (sel male-data :cols 0))
19 | (def m-eye (sel male-data :cols 1))
20 | (def m-count (sel male-data :cols 3))
21 |
22 | (view (bar-chart m-hair m-count
23 | :group-by m-eye
24 | :legend true
25 | :title "Male Hair and Eye Color"
26 | :x-label "Hair Color"
27 | :y-label "Number of males"))
28 |
29 | (def f-hair (sel female-data :cols 0))
30 | (def f-eye (sel female-data :cols 1))
31 | (def f-count (sel female-data :cols 3))
32 |
33 | (view (bar-chart f-hair f-count
34 | :group-by f-eye
35 | :legend true
36 | :title "Female Hair and Eye Color"
37 | :x-label "Hair Color"
38 | :y-label "Number of females"))
39 |
40 | ;; make contigency tables
41 | (def m-table (matrix m-count 4))
42 | (def f-table (matrix f-count 4))
43 |
44 | (def m-test (chisq-test :table m-table))
45 | (def f-test (chisq-test :table f-table))
46 |
47 | (:X-sq m-test)
48 | (:p-value m-test)
49 | (:df m-test)
50 |
51 | (:X-sq f-test)
52 | (:p-value f-test)
53 | (:df f-test)
54 |
55 |
56 | ;; In addition to contingency tables, you can pass raw data to the chisq-test.
57 | ;; the math-prog data set includes three columns: student_id, high school math
58 | ;; proficiency test pass/fail results, and freshman college programming course
59 | ;; pass/fail results.
60 |
61 | ;; This example will test whether there is any correlation between
62 | ;; the pass/fail results of a high school mathematics proficiency test and
63 | ;; a freshman college programming course
64 |
65 | (def math-prog (to-matrix (get-dataset :math-prog)))
66 | (def math (sel math-prog :cols 1))
67 | (def prog (sel math-prog :cols 2))
68 | (def math-prog-test (chisq-test :x math :y prog))
69 |
70 | ;; X-sq = 1.24145, df=1, p-value = 0.26519
71 | ;; can't reject null hypothesis, the the
72 | ;; results of the math exam is independent of
73 | ;; the pass/fail results of the college programming
74 | ;; course.
75 |
76 | (:X-sq math-prog-test)
77 | (:df math-prog-test)
78 | (:p-value math-prog-test)
79 |
80 |
81 |
82 |
--------------------------------------------------------------------------------
/data/airline_passengers.csv:
--------------------------------------------------------------------------------
1 | year,passengers,month
2 | 1949,112,Jan
3 | 1950,115,Jan,
4 | 1951,145,Jan,
5 | 1952,171,Jan,
6 | 1953,196,Jan,
7 | 1954,204,Jan,
8 | 1955,242,Jan,
9 | 1956,284,Jan,
10 | 1957,315,Jan,
11 | 1958,340,Jan,
12 | 1959,360,Jan,
13 | 1960,417,Jan,
14 | 1949,118,Feb
15 | 1950,126,Feb
16 | 1951,150,Feb
17 | 1952,180,Feb
18 | 1953,196,Feb
19 | 1954,188,Feb
20 | 1955,233,Feb
21 | 1956,277,Feb
22 | 1957,301,Feb
23 | 1958,318,Feb
24 | 1959,342,Feb
25 | 1960,391,Feb
26 | 1949,132,Mar
27 | 1950,141,Mar
28 | 1951,178,Mar
29 | 1952,193,Mar
30 | 1953,236,Mar
31 | 1954,235,Mar
32 | 1955,267,Mar
33 | 1956,317,Mar
34 | 1957,356,Mar
35 | 1958,362,Mar
36 | 1959,406,Mar
37 | 1960,419,Mar
38 | 1949,129,Apr
39 | 1950,135,Apr
40 | 1951,163,Apr
41 | 1952,181,Apr
42 | 1953,235,Apr
43 | 1954,227,Apr
44 | 1955,269,Apr
45 | 1956,313,Apr
46 | 1957,348,Apr
47 | 1958,348,Apr
48 | 1959,396,Apr
49 | 1960,461,Apr
50 | 1949,121,May
51 | 1950,125,May
52 | 1951,172,May
53 | 1952,183,May
54 | 1953,229,May
55 | 1954,234,May
56 | 1955,270,May
57 | 1956,318,May
58 | 1957,355,May
59 | 1958,363,May
60 | 1959,420,May
61 | 1960,472,May
62 | 1949,135,Jun
63 | 1950,149,Jun
64 | 1951,178,Jun
65 | 1952,218,Jun
66 | 1953,243,Jun
67 | 1954,264,Jun
68 | 1955,315,Jun
69 | 1956,374,Jun
70 | 1957,422,Jun
71 | 1958,435,Jun
72 | 1959,472,Jun
73 | 1960,535,Jun
74 | 1949,148,Jul
75 | 1950,170,Jul
76 | 1951,199,Jul
77 | 1952,230,Jul
78 | 1953,264,Jul
79 | 1954,302,Jul
80 | 1955,364,Jul
81 | 1956,413,Jul
82 | 1957,465,Jul
83 | 1958,491,Jul
84 | 1959,548,Jul
85 | 1960,622,Jul
86 | 1949,148,Aug
87 | 1950,170,Aug
88 | 1951,199,Aug
89 | 1952,242,Aug
90 | 1953,272,Aug
91 | 1954,293,Aug
92 | 1955,347,Aug
93 | 1956,405,Aug
94 | 1957,467,Aug
95 | 1958,505,Aug
96 | 1959,559,Aug
97 | 1960,606,Aug
98 | 1949,136,Sep
99 | 1950,158,Sep
100 | 1951,184,Sep
101 | 1952,209,Sep
102 | 1953,237,Sep
103 | 1954,259,Sep
104 | 1955,312,Sep
105 | 1956,355,Sep
106 | 1957,404,Sep
107 | 1958,404,Sep
108 | 1959,463,Sep
109 | 1960,508,Sep
110 | 1949,119,Oct
111 | 1950,133,Oct
112 | 1951,162,Oct
113 | 1952,191,Oct
114 | 1953,211,Oct
115 | 1954,229,Oct
116 | 1955,274,Oct
117 | 1956,306,Oct
118 | 1957,347,Oct
119 | 1958,359,Oct
120 | 1959,407,Oct
121 | 1960,461,Oct
122 | 1949,104,Nov
123 | 1950,114,Nov
124 | 1951,146,Nov
125 | 1952,172,Nov
126 | 1953,180,Nov
127 | 1954,203,Nov
128 | 1955,237,Nov
129 | 1956,271,Nov
130 | 1957,305,Nov
131 | 1958,310,Nov
132 | 1959,362,Nov
133 | 1960,390,Nov
134 | 1949,118,Dec
135 | 1950,140,Dec
136 | 1951,166,Dec
137 | 1952,194,Dec
138 | 1953,201,Dec
139 | 1954,229,Dec
140 | 1955,278,Dec
141 | 1956,306,Dec
142 | 1957,336,Dec
143 | 1958,337,Dec
144 | 1959,405,Dec
145 | 1960,432,Dec
146 |
--------------------------------------------------------------------------------
/modules/incanter-processing/test/incanter/processing_tests.clj:
--------------------------------------------------------------------------------
1 |
2 | (ns incanter.processing-tests
3 | (:use clojure.test
4 | (incanter core processing)))
5 |
6 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7 | ;; TESTS FOR incanter.processing.clj
8 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9 |
10 | (def wait-timeout 3000)
11 |
12 |
13 |
14 | (deftest processing-tests
15 |
16 | ;; simple interactive Processing example taken from processingjs.org website:
17 | ;; http://processingjs.org/source/basic-example/processingjs_basic-example.html
18 |
19 | ;; set up variable references to use in the sketch object
20 | (let [radius (ref 50.0)
21 | X (ref nil)
22 | Y (ref nil)
23 | nX (ref nil)
24 | nY (ref nil)
25 | delay 16
26 |
27 | ;; define a sketch object (i.e. PApplet)
28 | sktch (sketch
29 |
30 | ;; define the setup function
31 | (setup []
32 | (doto this
33 | ;no-loop
34 | (size 200 200)
35 | (stroke-weight 10)
36 | (framerate 15)
37 | smooth)
38 | (dosync
39 | (ref-set X (/ (width this) 2))
40 | (ref-set Y (/ (width this) 2))
41 | (ref-set nX @X)
42 | (ref-set nY @Y)))
43 |
44 | ;; define the draw function
45 | (draw []
46 | (dosync
47 | (ref-set radius (+ @radius (sin (/ (frame-count this) 4))))
48 | (ref-set X (+ @X (/ (- @nX @X) delay)))
49 | (ref-set Y (+ @Y (/ (- @nY @Y) delay))))
50 | (doto this
51 | (background 125) ;; gray
52 | (fill 0 121 184)
53 | (stroke 255)
54 | (ellipse @X @Y @radius @radius)
55 | ;(save "proc_example1.png")
56 | ))
57 |
58 | ;; define mouseMoved function (mouseMoved and mouseDraw
59 | ;; require a 'mouse-event' argument unlike the standard Processing
60 | ;; methods)
61 | (mouseMoved [mouse-event]
62 | (dosync
63 | ;; mouse-x and mouse-y take the mouse-event as an argument
64 | (ref-set nX (mouse-x mouse-event))
65 | (ref-set nY (mouse-y mouse-event)))))]
66 |
67 | ;; use the view function to display the sketch
68 | (def frame1 (view sktch :size [200 200]))
69 | (Thread/sleep wait-timeout)
70 | (.dispose frame1))
71 | (System/exit 0))
72 |
73 |
74 |
75 |
--------------------------------------------------------------------------------
/examples/blog/datasets_mongodb.clj:
--------------------------------------------------------------------------------
1 |
2 | ;; Basic dataset usage
3 | (use '(incanter core stats charts io))
4 |
5 | ;; load a dataset from a URL pointing to the data.
6 | (def data
7 | (read-dataset
8 | "http://github.com/liebke/incanter/raw/master/data/cars.csv"
9 | :header true))
10 |
11 |
12 | ;; the dataset could have been loaded with the incanter.datasets/get-dataset function
13 | (use 'incanter.datasets)
14 | (incanter.datasets/get-dataset :cars)
15 |
16 | ;; view the dimensions of the dataset
17 | (dim data)
18 |
19 | ;; view the column names
20 | (col-names data)
21 |
22 | (with-data (get-dataset :cars)
23 | (view (conj-cols (range (nrow $data)) $data)))
24 |
25 | ;; plot a scatter plot of speed vs. distance and add a regression line
26 | (with-data data
27 | (def lm (linear-model ($ :dist) ($ :speed)))
28 | (doto (scatter-plot ($ :speed) ($ :dist))
29 | (add-lines ($ :speed) (:fitted lm))
30 | view))
31 |
32 |
33 | ;; create a new dataset that includes the orig data and the
34 | ;; fitted values from the liner-model function
35 | (def results (conj-cols data (:fitted lm)))
36 |
37 | ;; give the new dataset meaningful column names
38 | (def results (col-names data [:speed :dist :predicted-dist]))
39 |
40 | ;; do both steps at once with the -> macro, and also add the residuals to the new dataset
41 | (def results (-> (conj-cols data (:fitted lm) (:residuals lm))
42 | (col-names [:speed :dist :predicted :residuals])))
43 |
44 |
45 | ;; now use the $where function
46 |
47 | ($where {:speed 10} results)
48 | ($where {:speed {:$gt 10 :$lt 20}} results)
49 | ($where {:speed {:$in #{4 7 24 25}}} results)
50 | ($where {:speed {:$nin #{4 7 24 25}}} results)
51 |
52 | (with-data results
53 | (mean ($ :speed ($where {:residuals {:$gt -10 :$lt 10}}))))
54 |
55 |
56 | (with-data results
57 | (conj-rows ($where {:speed {:$lt 10}})
58 | ($where {:speed {:$gt 20}})))
59 |
60 | (with-data results
61 | ($where (fn [row] (or (< (:speed row) 10)
62 | (> (:speed row) 20)))))
63 |
64 |
65 | ;; Now let's use MongoDB
66 | (use 'somnium.congomongo)
67 | (use 'incanter.mongodb)
68 |
69 | ;; connect to a MongoDB server running on the localhost on the default port.
70 | (mongo! :db "mydb")
71 |
72 | (insert-dataset :breaking-dists results)
73 |
74 | (def breaking-dists (fetch-dataset :breaking-dists))
75 | (col-names breaking-dists)
76 | (view breaking-dists)
77 |
78 | (insert-dataset :breaking-dists breaking-dists)
79 | (view (fetch-dataset :breaking-dists))
80 |
81 | ;; use fetch-dataset's :where option to retrieve only the
82 | ;; rows where the speed is between 10 and 20 mph, and
83 | ;; then calculate the mean breaking distance.
84 |
85 | (with-data (fetch-dataset :breaking-dists
86 | :where {:speed {:$gt 10 :$lt 20}})
87 | (mean ($ :dist)))
88 |
89 |
90 |
91 | (doc incanter.mongodb)
92 |
93 |
94 |
--------------------------------------------------------------------------------
/modules/incanter-excel/test/incanter/excel_tests.clj:
--------------------------------------------------------------------------------
1 | (ns incanter.excel-tests
2 | (:use [clojure.test :only[deftest is]]
3 | [incanter.core :only [dataset dataset? $]]
4 | [incanter.excel :only [save-xls read-xls]])
5 | (:import java.lang.Math
6 | java.util.Date
7 | java.io.File))
8 |
9 | (def sample-cols
10 | ["Ints" "Doubles" "Strings" "Dates"])
11 | (def sample-dataset
12 | (dataset
13 | sample-cols
14 | [[1 (Math/sqrt 2) "One" (Date.)]
15 | [2 Math/E "Two" (Date.)]
16 | [3 Math/PI "Three" (Date.)]]))
17 |
18 | (deftest xls-roundtrip
19 | (let [ffile (File/createTempFile "excel-test" ".xls")
20 | fname (. ffile getAbsolutePath)]
21 | (try
22 | (let [cols sample-cols
23 | dset sample-dataset
24 | result (do (save-xls dset fname) (read-xls fname))]
25 | (do
26 | (is (dataset? result))
27 | (is (= cols (:column-names result)))))
28 | (finally (. ffile delete)))))
29 |
30 | (deftest headers
31 | (let [ffile (File/createTempFile "excel-test" ".xls")
32 | fname (. ffile getAbsolutePath)]
33 | (try
34 | (let [dset (dataset [:a :b :c]
35 | [[1 2 3]])
36 | result (do (save-xls dset fname) (read-xls fname :header-keywords true))]
37 | (is (dataset? result))
38 | (is (= (:column-names dset) (:column-names result))))
39 | (finally (. ffile delete)))))
40 |
41 | (deftest xlsx
42 | (let [ffile (File/createTempFile "excel-test" ".xlsx")
43 | fname (. ffile getAbsolutePath)]
44 | (try
45 | (let [cols ["Ints" "Doubles" "Strings" "Dates"]
46 | dset (dataset
47 | cols
48 | [
49 | [1 (Math/sqrt 2) "One" (Date.)]
50 | [2 Math/E "Two" (Date.)]
51 | [3 Math/PI "Three" (Date.)]])
52 | result (do (save-xls dset fname) (read-xls fname))]
53 | (do
54 | (is (dataset? result))
55 | (is (= cols (:column-names result)))))
56 | (finally (. ffile delete)))))
57 |
58 | (deftest multi-sheets
59 | (let [ffile (File/createTempFile "excel-test" ".xls")
60 | fname (. ffile getAbsolutePath)]
61 | (try
62 | (let [data sample-dataset
63 | data2 (dataset [:a :b :c :d] [[1 2 3 4] [5 6 7 8] [9 10 11 12]])]
64 | (do
65 | (save-xls ["first" data "second" data2] fname)
66 | (let [res (read-xls fname :all-sheets? true :header-keywords true)
67 | one (first res)
68 | two (nth res 1)]
69 | (is (dataset? one))
70 | (is (= [1 2 3] (map int ($ :Ints one))))
71 | (is (dataset? two))
72 | (is (= [1 5 9] (map int ($ :a two))))
73 | (is (= [:a :b :c :d] (:column-names two)))
74 | )))
75 | (finally (. ffile delete)))))
76 |
--------------------------------------------------------------------------------
/examples/blog/projects/simple_web_app/src/simple_web_app.clj:
--------------------------------------------------------------------------------
1 | ;; This code is from the following blog post:
2 | ;; Building a simple Clojure web application with Incanter, Compojure, and Leiningen
3 | ;; http://incanter-blog.org/2009/11/29/incanter-webapp/
4 |
5 |
6 | (ns simple_web_app
7 | (:gen-class)
8 | (:use [compojure]
9 | [compojure.http response]
10 | [incanter core stats charts])
11 | (:import (java.io ByteArrayOutputStream
12 | ByteArrayInputStream)))
13 |
14 |
15 | ;; Pass a map as the first argument to be
16 | ;; set as attributes of the element
17 | (defn html-doc
18 | [title & body]
19 | (html
20 | (doctype :html4)
21 | [:html
22 | [:head
23 | [:title title]]
24 | [:body
25 | [:div
26 | [:h2
27 | [:a {:href "/"}
28 | "Generate a normal sample"]]]
29 | body]]))
30 |
31 |
32 | (def sample-form
33 | (html-doc "sample-normal histogram"
34 | (form-to [:get "/sample-normal"]
35 | "sample size: " (text-field {:size 4} :size)
36 | "mean: " (text-field {:size 4} :mean)
37 | "sd: " (text-field {:size 4} :sd)
38 | (submit-button "view"))))
39 |
40 |
41 |
42 | (defn gen-samp-hist-png
43 | [request size-str mean-str sd-str]
44 | (let [size (if (nil? size-str)
45 | 1000
46 | (Integer/parseInt size-str))
47 | m (if (nil? mean-str)
48 | 0
49 | (Double/parseDouble mean-str))
50 | s (if (nil? sd-str)
51 | 1
52 | (Double/parseDouble sd-str))
53 | samp (sample-normal size
54 | :mean m
55 | :sd s)
56 | chart (histogram
57 | samp
58 | :title "Normal Sample"
59 | :x-label (str "sample-size = " size
60 | ", mean = " m
61 | ", sd = " s))
62 | out-stream (ByteArrayOutputStream.)
63 | in-stream (do
64 | (save chart out-stream)
65 | (ByteArrayInputStream.
66 | (.toByteArray out-stream)))
67 | header {:status 200
68 | :headers {"Content-Type" "image/png"}}]
69 | (update-response request
70 | header
71 | in-stream)))
72 |
73 |
74 |
75 | ;; define routes
76 | (defroutes webservice
77 | (GET "/"
78 | sample-form)
79 | (GET "/sample-normal"
80 | (gen-samp-hist-png request
81 | (params :size)
82 | (params :mean)
83 | (params :sd))))
84 |
85 | ;; define main function that starts webserver
86 | (defn -main [& args]
87 | (run-server {:port 8080}
88 | "/*" (servlet webservice)))
89 |
90 |
91 |
--------------------------------------------------------------------------------
/examples/blog/bootstrapping.clj:
--------------------------------------------------------------------------------
1 | ;; from the following blog post
2 | ;; http://incanter-blog.org/2009/07/04/bootstrapping/
3 |
4 | ;; example with smoothing
5 | ;; Newcomb's speed of light data
6 |
7 | (use '(incanter core stats charts))
8 |
9 |
10 | ;; A numeric vector giving the Third Series of measurements of the
11 | ;; passage time of light recorded by Newcomb in 1882. The given
12 | ;; values divided by 1000 plus 24 give the time in millionths of a
13 | ;; second for light to traverse a known distance. The 'true' value is
14 | ;; now considered to be 33.02.
15 |
16 | (def x [28 -44 29 30 24 28 37 32 36
17 | 27 26 28 29 26 27 22 23 20
18 | 25 25 36 23 31 32 24 27 33
19 | 16 24 29 36 21 28 26 27 27
20 | 32 25 28 24 40 21 31 32 28
21 | 26 30 27 26 24 32 29 34 -2
22 | 25 19 36 29 30 22 28 33 39
23 | 25 16 23])
24 |
25 | ;; view histogram of data to see outlier observations
26 | (view (histogram x
27 | :nbins 20
28 | :title "Newcomb's speed of light data"))
29 |
30 |
31 |
32 | ;; calculate the median of the data
33 | (median x) ;; 27
34 |
35 | ;; define a function that converts Newcomb's data into speeds
36 | (defn to-speed
37 | "Converts Newcomb's data into speed (meters/sec)"
38 | ([x] (div 7400 (div (plus 24800 x) 1E9))))
39 |
40 |
41 | ;; convert the data to speeds and calculate the median
42 | (median (to-speed x)) ;; 2.981E8
43 |
44 |
45 |
46 | ;; Draw 10000 bootstrap samples of the median
47 | (def t* (bootstrap x median :size 10000))
48 |
49 | ;; view a histogram of the bootstrap medians
50 | (view (histogram t*
51 | :density true
52 | :nbins 20
53 | :title "Bootstrap sample of medians"
54 | :x-label "median"))
55 |
56 | ;; Calculate the estimate of the median: 27.301
57 | (mean t*)
58 |
59 | ;; Convert bootstrap median estimate to speed: 2.981E8
60 | (to-speed (mean t*))
61 |
62 | ;; Calculate a 95% CI for the median: (26.0 28.5)
63 | (quantile t* :probs [0.025 0.975])
64 |
65 | ;; Convert to speed and calculate 95% CI: (2.9804E8 2.9807E8)
66 | (quantile (to-speed t*) :probs [0.025 0.975])
67 |
68 | ;; estimate the standard error of the median: 0.681
69 | (sd t*)
70 |
71 | ;; estimate the bias of the sample median: -0.3
72 | (- (mean t*) (median x))
73 |
74 |
75 | ;; draw 10000 smoothed bootstrap samples of the median
76 | (def smooth-t* (bootstrap x median :size 10000 :smooth true))
77 |
78 | (view (histogram smooth-t*
79 | :density true
80 | :nbins 20
81 | :title "Smoothed bootstrap sample of medians"
82 | :x-label "median"))(mean smooth-samp)
83 |
84 | ;; Calculate the estimate of the median
85 | (mean smooth-t*)
86 |
87 | ;; Calculate a 95% CI: (25.905 28.446)
88 | (quantile smooth-t* :probs [0.025 0.975])
89 |
90 |
--------------------------------------------------------------------------------
/modules/incanter-core/java/Weibull.java:
--------------------------------------------------------------------------------
1 | package incanter;
2 |
3 | import cern.jet.random.tdouble.*;
4 | import cern.jet.random.tdouble.engine.*;
5 |
6 | /**
7 | * Weibull Distribution. See the
8 | * math definition.
9 | *
10 | * p(x) = shape/scale*(x/scale)^(shape-1)*exp{-(x/scale)^shape} for x >= scale, shape > 0.
11 | *
12 | * Note that mean is infinite if shape <= 1 and variance is infinite if
13 | * shape <= 2.
14 | *
15 | * Instance methods operate on a user supplied uniform random number generator; they are unsynchronized.
16 | *
17 | * Static methods operate on a default uniform random number generator; they are synchronized.
18 | *
19 | *
20 | * @author Hongbo Liu hongbol@winlab.rutgers.edu
21 | * @version 0.1
22 | */
23 | public class Weibull extends AbstractContinousDoubleDistribution {
24 |
25 | protected double scale;
26 | protected double shape;
27 |
28 | // The uniform random number generated shared by all static methods.
29 | protected static Weibull shared = new Weibull(1.0, 1.5, makeDefaultGenerator());
30 |
31 | /**
32 | * Constructs a Pareto distribution.
33 | */
34 | public Weibull(double a, double b, DoubleRandomEngine randomGenerator) {
35 | setRandomGenerator(randomGenerator);
36 | this.scale = a;
37 | this.shape = b;
38 | }
39 |
40 | /**
41 | * Returns the cumulative distribution function.
42 | */
43 | public double cdf(double x) {
44 | if (x < 0)
45 | return 0.0;
46 | return 1.0 - Math.exp(-Math.pow(x/scale,shape));
47 | }
48 |
49 | /**
50 | * Returns a random number from the distribution.
51 | */
52 | public double nextDouble() {
53 | return scale*Math.pow(-Math.log(1-randomGenerator.raw()), 1/shape);
54 | }
55 |
56 | /**
57 | * Returns a random number from the distribution; bypasses the internal
58 | state.*/
59 | public double nextDouble(double a, double b) {
60 | return a*Math.pow(-Math.log(1-randomGenerator.raw()), 1/b);
61 | }
62 |
63 | /**
64 | * Returns the probability distribution function.
65 | */
66 | public double pdf(double x) {
67 | if (x < 0)
68 | return 0.0;
69 | return shape/scale*Math.pow(x/scale, shape-1)
70 | *Math.exp(-Math.pow(x/scale,shape));
71 | }
72 |
73 | /**
74 | * Sets the parameters.
75 | */
76 | public void setState(double a, double b) {
77 | this.scale = a;
78 | this.shape = b;
79 | }
80 |
81 | /**
82 | * Returns a random number from the distribution with the given
83 | * scale = k, and shape = alpha.
84 | */
85 | public static double staticNextDouble(double a, double b) {
86 | synchronized (shared) {
87 | return shared.nextDouble(a,b);
88 | }
89 | }
90 |
91 | /**
92 | * Returns a String representation of the receiver.
93 | */
94 | public String toString() {
95 | return this.getClass().getName() + "(" + scale + ", " + shape + ")";
96 | }
97 |
98 | }
99 |
100 |
--------------------------------------------------------------------------------
/data/co2.csv:
--------------------------------------------------------------------------------
1 | Plant,Type,Treatment,conc,uptake
2 | Qn1,Quebec,nonchilled, 95, 16.0
3 | Qn1,Quebec,nonchilled, 175, 30.4
4 | Qn1,Quebec,nonchilled, 250, 34.8
5 | Qn1,Quebec,nonchilled, 350, 37.2
6 | Qn1,Quebec,nonchilled, 500, 35.3
7 | Qn1,Quebec,nonchilled, 675, 39.2
8 | Qn1,Quebec,nonchilled,1000, 39.7
9 | Qn2,Quebec,nonchilled, 95, 13.6
10 | Qn2,Quebec,nonchilled, 175, 27.3
11 | Qn2,Quebec,nonchilled, 250, 37.1
12 | Qn2,Quebec,nonchilled, 350, 41.8
13 | Qn2,Quebec,nonchilled, 500, 40.6
14 | Qn2,Quebec,nonchilled, 675, 41.4
15 | Qn2,Quebec,nonchilled,1000, 44.3
16 | Qn3,Quebec,nonchilled, 95, 16.2
17 | Qn3,Quebec,nonchilled, 175, 32.4
18 | Qn3,Quebec,nonchilled, 250, 40.3
19 | Qn3,Quebec,nonchilled, 350, 42.1
20 | Qn3,Quebec,nonchilled, 500, 42.9
21 | Qn3,Quebec,nonchilled, 675, 43.9
22 | Qn3,Quebec,nonchilled,1000, 45.5
23 | Qc1,Quebec, chilled, 95, 14.2
24 | Qc1,Quebec, chilled, 175, 24.1
25 | Qc1,Quebec, chilled, 250, 30.3
26 | Qc1,Quebec, chilled, 350, 34.6
27 | Qc1,Quebec, chilled, 500, 32.5
28 | Qc1,Quebec, chilled, 675, 35.4
29 | Qc1,Quebec, chilled,1000, 38.7
30 | Qc2,Quebec, chilled, 95, 9.3
31 | Qc2,Quebec, chilled, 175, 27.3
32 | Qc2,Quebec, chilled, 250, 35.0
33 | Qc2,Quebec, chilled, 350, 38.8
34 | Qc2,Quebec, chilled, 500, 38.6
35 | Qc2,Quebec, chilled, 675, 37.5
36 | Qc2,Quebec, chilled,1000, 42.4
37 | Qc3,Quebec, chilled, 95, 15.1
38 | Qc3,Quebec, chilled, 175, 21.0
39 | Qc3,Quebec, chilled, 250, 38.1
40 | Qc3,Quebec, chilled, 350, 34.0
41 | Qc3,Quebec, chilled, 500, 38.9
42 | Qc3,Quebec, chilled, 675, 39.6
43 | Qc3,Quebec, chilled,1000, 41.4
44 | Mn1,Mississippi,nonchilled, 95, 10.6
45 | Mn1,Mississippi,nonchilled, 175, 19.2
46 | Mn1,Mississippi,nonchilled, 250, 26.2
47 | Mn1,Mississippi,nonchilled, 350, 30.0
48 | Mn1,Mississippi,nonchilled, 500, 30.9
49 | Mn1,Mississippi,nonchilled, 675, 32.4
50 | Mn1,Mississippi,nonchilled,1000, 35.5
51 | Mn2,Mississippi,nonchilled, 95, 12.0
52 | Mn2,Mississippi,nonchilled, 175, 22.0
53 | Mn2,Mississippi,nonchilled, 250, 30.6
54 | Mn2,Mississippi,nonchilled, 350, 31.8
55 | Mn2,Mississippi,nonchilled, 500, 32.4
56 | Mn2,Mississippi,nonchilled, 675, 31.1
57 | Mn2,Mississippi,nonchilled,1000, 31.5
58 | Mn3,Mississippi,nonchilled, 95, 11.3
59 | Mn3,Mississippi,nonchilled, 175, 19.4
60 | Mn3,Mississippi,nonchilled, 250, 25.8
61 | Mn3,Mississippi,nonchilled, 350, 27.9
62 | Mn3,Mississippi,nonchilled, 500, 28.5
63 | Mn3,Mississippi,nonchilled, 675, 28.1
64 | Mn3,Mississippi,nonchilled,1000, 27.8
65 | Mc1,Mississippi, chilled, 95, 10.5
66 | Mc1,Mississippi, chilled, 175, 14.9
67 | Mc1,Mississippi, chilled, 250, 18.1
68 | Mc1,Mississippi, chilled, 350, 18.9
69 | Mc1,Mississippi, chilled, 500, 19.5
70 | Mc1,Mississippi, chilled, 675, 22.2
71 | Mc1,Mississippi, chilled,1000, 21.9
72 | Mc2,Mississippi, chilled, 95, 7.7
73 | Mc2,Mississippi, chilled, 175, 11.4
74 | Mc2,Mississippi, chilled, 250, 12.3
75 | Mc2,Mississippi, chilled, 350, 13.0
76 | Mc2,Mississippi, chilled, 500, 12.5
77 | Mc2,Mississippi, chilled, 675, 13.7
78 | Mc2,Mississippi, chilled,1000, 14.4
79 | Mc3,Mississippi, chilled, 95, 10.6
80 | Mc3,Mississippi, chilled, 175, 18.0
81 | Mc3,Mississippi, chilled, 250, 17.9
82 | Mc3,Mississippi, chilled, 350, 17.9
83 | Mc3,Mississippi, chilled, 500, 17.9
84 | Mc3,Mississippi, chilled, 675, 18.9
85 | Mc3,Mississippi, chilled,1000, 19.9
86 |
87 |
--------------------------------------------------------------------------------
/modules/incanter-core/test/incanter/infix_tests.clj:
--------------------------------------------------------------------------------
1 | ;;; infix-tests.clj -- Unit tests of Incanter infix expression functions
2 |
3 | ;; by Michael Nygard http://incanter.org
4 | ;; Sept 16 2011
5 |
6 | ;; Copyright (c) David Edgar Liebke, 2009. All rights reserved. The use
7 | ;; and distribution terms for this software are covered by the Eclipse
8 | ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
9 | ;; which can be found in the file epl-v10.html at the root of this
10 | ;; distribution. By using this software in any fashion, you are
11 | ;; agreeing to be bound by the terms of this license. You must not
12 | ;; remove this notice, or any other, from this software.
13 |
14 |
15 |
16 | (ns incanter.infix-tests
17 | (:use clojure.test
18 | (incanter core infix)))
19 |
20 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21 | ;; UNIT TESTS FOR incanter.infix.clj
22 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
23 |
24 | (deftest basic-arithmetic
25 | (is (= ($= 7 + 8 - 2 * 6 / 2) 9))
26 | (is (= ($= [1 2 3] + 5) '(6 7 8)))
27 | (is (= ($= [1 2 3] + [4 5 6]) '(5 7 9)))
28 | (is (= ($= [1 2 3] * [1 2 3]) '(1 4 9)))
29 | (is (= ($= [1 2 3] / [1 2 3]) '(1 1 1)))
30 | (is (= ($= (matrix [[1 2] [4 5]]) + 6) (matrix [[7 8] [10 11]])))
31 | (is (= ($= (trans [[1 2] [4 5]]) + 6) (matrix [[7 10] [8 11]])))
32 | (is (= ($= 8 ** 3) 512.0))
33 | (is (= ($= 8 ** 1/2) 2.8284271247461903))
34 | (is (= ($= 2 ** -2) 0.25))
35 | (is (= ($= [1 2 3] ** 2) '(1.0 4.0 9.0)))
36 | (is (= ($= 10 + 20 * (4 - 5) / 6) 20/3))
37 | (is (= ($= (10 + 20) * 4 - 5 / 6) 715/6))
38 | (is (= ($= 10 + 20 * (4 - 5 / 6)) 220/3))
39 | (is (= ($= ((((5 + 4) * 5)))) 45)))
40 |
41 | (deftest functions-in-infix-expressions
42 | (is (= (let [x 10 y -5] ($= x + y / -10)) 21/2))
43 | (is (= ($= (sqrt 5) * 5 + 3 * 3) 20.18033988749895))
44 | (is (= ($= sq [1 2 3] + [1 2 3]) [2 6 12]))
45 | (is (= ($= sin 2 * Math/PI * 2) 5.713284232087328))
46 | (is (= ($= (cos 0) * 10) 10.0))
47 | (is (= ($= (tan 2) * Math/PI * 10) -68.64505182223235))
48 | (is (= ($= (asin 1/2) * 10) 5.23598775598299))
49 | (is (= ($= (acos 1/2) * 10) 10.47197551196598))
50 | (is (= ($= (atan 1/2) * 10) 4.636476090008061))
51 | (is (= ($= [1 2 3] / (sq [1 2 3]) + [5 6 7]) '(6 13/2 22/3)))
52 | (is (= ($= [1 2 3] + (sin [4 5 6])) '(0.2431975046920718 1.0410757253368614 2.720584501801074)))
53 | (is (= ($= 3 > (5 * 2/7)) true))
54 | (is (= ($= 3 <= (5 * 2/7)) false))
55 | (is (= ($= 3 != (5 * 2/7)) true))
56 | (is (= ($= 3 == (5 * 2/7)) false))
57 | (is (= ($= 3 != 8 || 6 > 12) true)))
58 |
59 | (deftest matrix-products-in-infix
60 | (is (= ($= [1 2 3] <*> (trans [1 2 3])) (matrix [[1 2 3] [2 4 6] [3 6 9]])))
61 | (is (= ($= (trans [[1 2] [4 5]]) <*> (matrix [[1 2] [4 5]])) (matrix [[17 22] [22 29]])))
62 | (is (= ($= (trans [1 2 3 4]) <*> [1 2 3 4]) 30.0))
63 | (is (= ($= [1 2 3 4] <*> (trans [1 2 3 4])) (matrix [[1 2 3 4] [2 4 6 8] [3 6 9 12] [4 8 12 16]]))))
64 |
65 | (deftest kronecker-product-in-infix
66 | (is (= ($= [1 2 3] [1 2 3]) (matrix [1 2 3 2 4 6 3 6 9])))
67 | (is (= ($= (matrix [[1 2] [3 4] [5 6]]) 4) (matrix [[4 8] [12 16] [20 24]])))
68 | (is (= ($= (matrix [[1 2] [3 4] [5 6]]) (matrix [[1 2] [3 4]])) (matrix [[1 2 2 4] [3 4 6 8] [3 6 4 8] [9 12 12 16] [5 10 6 12] [15 20 18 24]])))
69 | (is (= ($= [1 2 3 4] 4) (matrix [4 8 12 16])))
70 | (is (= ($= [1 2 3 4] (trans [1 2 3 4])) (matrix [[1 2 3 4] [2 4 6 8] [3 6 9 12] [4 8 12 16]]))))
71 |
72 |
--------------------------------------------------------------------------------
/examples/processing/simple_rgb_cube.clj:
--------------------------------------------------------------------------------
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 | ; Simple 3d Clojure code example for Incanter and Processing
3 | ; Adapted by James Swift (james at 3dengineer com)
4 | ; from the simple_interation.clj example that comes with incanter
5 | ; and the RGB cube example here http://processing.org/learning/3d/rgbcube.html
6 |
7 | (ns example
8 | (:use [incanter core processing]))
9 |
10 | ;; set up variable references to use in the sketch object
11 | (let [X (ref nil)
12 | Y (ref nil)
13 | nX (ref nil)
14 | nY (ref nil)
15 | delay 16
16 |
17 | ;; define a sketch object (i.e. PApplet)
18 | sktch (sketch
19 |
20 | ;; define the setup function
21 | (setup []
22 | (doto this
23 | (no-stroke)
24 | (size 790 590 P3D)
25 | (color-mode RGB 1))
26 | (dosync
27 | (ref-set X (/ (width this) 2))
28 | (ref-set Y (/ (width this) 2))
29 | (ref-set nX @X)
30 | (ref-set nY @Y)))
31 |
32 | ;; define the draw function
33 | (draw []
34 | (doto this
35 | (background (color 0xFFFFFF))
36 | (push-matrix)
37 | (translate (/ (width this) 2) (/ (height this) 2) -30)
38 | (rotate-x (- @nX))
39 | (rotate-y (- @nY))
40 | (scale 90)
41 | (begin-shape QUADS)
42 | (fill 0 1 1) (vertex -1 1 1)
43 | (fill 1 1 1) (vertex 1 1 1)
44 | (fill 1 0 1) (vertex 1 -1 1)
45 | (fill 0 0 1) (vertex -1 -1 1)
46 |
47 | (fill 1 1 1) (vertex 1 1 1)
48 | (fill 1 1 0) (vertex 1 1 -1)
49 | (fill 1 0 0) (vertex 1 -1 -1)
50 | (fill 1 0 1) (vertex 1 -1 1)
51 |
52 | (fill 1 1 0) (vertex 1 1 -1)
53 | (fill 0 1 0) (vertex -1 1 -1)
54 | (fill 0 0 0) (vertex -1 -1 -1)
55 | (fill 1 0 0) (vertex 1 -1 -1)
56 |
57 | (fill 0 1 0) (vertex -1 1 -1)
58 | (fill 0 1 1) (vertex -1 1 1)
59 | (fill 0 0 1) (vertex -1 -1 1)
60 | (fill 0 0 0) (vertex -1 -1 -1)
61 |
62 | (fill 0 1 0) (vertex -1 1 -1)
63 | (fill 1 1 0) (vertex 1 1 -1)
64 | (fill 1 1 1) (vertex 1 1 1)
65 | (fill 0 1 1) (vertex -1 1 1)
66 |
67 | (fill 0 0 0) (vertex -1 -1 -1)
68 | (fill 1 0 0) (vertex 1 -1 -1)
69 | (fill 1 0 1) (vertex 1 -1 1)
70 | (fill 0 0 1) (vertex -1 -1 1)
71 | (end-shape)
72 | (pop-matrix)
73 | ))
74 |
75 | ;; define mouseMoved function (mouseMoved and mouseDraw
76 | ;; require a 'mouse-event' argument unlike the standard Processing
77 | ;; methods)
78 | (mouseMoved [mouse-event]
79 | (dosync
80 | ;; mouse-x and mouse-y take the mouse-event as an argument
81 | (ref-set nX (* (/ (mouse-y mouse-event) (width this)) TWO_PI))
82 | (ref-set nY (* (/ (mouse-x mouse-event) (height this)) TWO_PI)))))]
83 |
84 | ;; use the view function to display the sketch
85 | (view sktch :size [800 600]))
86 |
87 |
--------------------------------------------------------------------------------
/modules/incanter-io/test/incanter/io_tests.clj:
--------------------------------------------------------------------------------
1 |
2 | ;;; test-cases.clj -- Unit tests of Incanter functions
3 |
4 | ;; by David Edgar Liebke http://incanter.org
5 | ;; March 12, 2009
6 |
7 | ;; Copyright (c) David Edgar Liebke, 2009. All rights reserved. The use
8 | ;; and distribution terms for this software are covered by the Eclipse
9 | ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
10 | ;; which can be found in the file epl-v10.html at the root of this
11 | ;; distribution. By using this software in any fashion, you are
12 | ;; agreeing to be bound by the terms of this license. You must not
13 | ;; remove this notice, or any other, from this software.
14 |
15 | ;; CHANGE LOG
16 | ;; March 12, 2009: First version
17 |
18 | ;; to run these tests:
19 | ;; (use 'tests test-cases)
20 | ;; need to load this file to define data variables
21 | ;; (use 'clojure.contrib.test-is)
22 | ;; then run tests
23 | ;; (run-tests 'incanter.tests.test-cases)
24 |
25 | (ns incanter.io-tests
26 | (:use clojure.test
27 | (incanter core io)))
28 |
29 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 | ;; UNIT TESTS FOR incanter.io.clj
31 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 |
33 | ;(def incanter-home (System/getProperty "incanter.home"))
34 | (def incanter-home "../../")
35 |
36 | ;; read in a dataset from a space delimited file
37 | (def test-data (read-dataset
38 | (str incanter-home "data/cars.dat")
39 | :delim \space
40 | :header true)) ; default delimiter: \,
41 | ;; read in a dataset from a comma delimited file
42 | (def test-csv-data (read-dataset
43 | (str incanter-home "data/cars.csv")
44 | :header true))
45 | ;; read in a dataset from a tab delimited file
46 | (def test-tdd-data (read-dataset
47 | (str incanter-home "data/cars.tdd")
48 | :header true
49 | :delim \tab))
50 | ;; read in the iris dataset from a space delimited file
51 | (def iris-data (read-dataset
52 | (str incanter-home "data/iris.dat")
53 | :delim \space
54 | :header true))
55 | ;; read in the social science survey dataset from a space delimited file
56 | (def ols-data (to-matrix (read-dataset
57 | (str incanter-home "data/olsexamp.dat")
58 | :delim \space
59 | :header true)))
60 |
61 | ;; convert the space-delimited dataset into a matrix
62 | (def test-mat (to-matrix test-data))
63 | ;; convert the csv dataset into a matrix
64 | (def test-csv-mat (to-matrix test-csv-data))
65 | ;; convert the tab-delimited dataset into a matrix
66 | (def test-tdd-mat (to-matrix test-tdd-data))
67 | ;; convert the iris-data into a matrix, encoding strings into multiple dummy variables
68 | (def iris-mat (to-matrix iris-data))
69 | (def iris-mat-dummies (to-matrix iris-data :dummies true))
70 |
71 |
72 | (deftest io-validation
73 | ;; validate matrices read from files
74 | (is (= (reduce plus test-mat) (matrix [770 2149] 2)))
75 | (is (= (reduce plus test-csv-mat) (matrix [770 2149] 2)))
76 | (is (= (reduce plus test-tdd-mat) (matrix [770 2149] 2)))
77 | ;; confirm that iris species factor was converted to two dummy variables
78 | (is (= (first iris-mat) (matrix [5.10 3.50 1.40 0.20 0] 5)))
79 | (is (= (first iris-mat-dummies) (matrix [5.10 3.50 1.40 0.20 0 0] 6)))) ;; end of io-validation tests
80 |
81 | (deftest read-dataset-validation
82 | (doseq [[name cars-dataset]
83 | [["dat" test-data]
84 | ["csv" test-csv-data]
85 | ["tdd" test-tdd-data]]]
86 | (is (= [:speed :dist] (:column-names cars-dataset)) (str "Reading column names for " name " failed"))
87 | (is (= 50 (count (:rows cars-dataset)))) (str "Reading rows for " name " failed"))) ;; end of read-dataset-validation tests
88 |
89 |
90 |
--------------------------------------------------------------------------------
/modules/incanter-excel/src/incanter/excel/cells.clj:
--------------------------------------------------------------------------------
1 | (ns
2 | ^{:doc "Functions for reading and writing to cells."}
3 | incanter.excel.cells
4 | (:import [org.apache.poi.ss.usermodel Cell CellStyle DateUtil]
5 | [org.apache.poi.ss.usermodel Row Sheet]))
6 |
7 | (defmulti
8 | write-cell
9 | #(cond
10 | (isa? (. % getClass) Number) :numeric
11 | (keyword? %) :keyword
12 | :else :other))
13 |
14 | (defmulti
15 | get-cell-formula-value
16 | "Get the value after the evaluating the formula. See http://poi.apache.org/spreadsheet/eval.html#Evaluate"
17 | (fn [evaled-cell evaled-type]
18 | evaled-type))
19 |
20 | (defmulti
21 | get-cell-value
22 | "Get the cell value depending on the cell type."
23 | (fn [cell]
24 | (let [ct (. cell getCellType)]
25 | (if (not (= Cell/CELL_TYPE_NUMERIC ct))
26 | ct
27 | (if (DateUtil/isCellDateFormatted cell)
28 | :date
29 | ct)))))
30 |
31 | (defn write-line [^Sheet sheet row-num line ^CellStyle style]
32 | (let [^Row xl-line (. sheet createRow row-num)]
33 | (dorun
34 | (map
35 | #(if (not (nil? %2))
36 | (doto (. xl-line createCell %1) (.setCellValue (write-cell %2)) (.setCellStyle style)))
37 | (iterate inc 0)
38 | (seq line)))))
39 |
40 | (defn write-line-values [{:keys [sheet bold normal workbook]} bold? row-number coll]
41 | (write-line sheet row-number coll (if bold? bold normal)))
42 | (defn cell-iterator [^Row row]
43 | (if row
44 | (for [idx (range (.getFirstCellNum row) (.getLastCellNum row))]
45 | (if-let [cell (.getCell row idx)]
46 | cell
47 | (.createCell row idx Cell/CELL_TYPE_BLANK)))
48 | ()))
49 |
50 | (defn read-line-values [row-iterator-item]
51 | (doall (map get-cell-value (cell-iterator row-iterator-item))))
52 |
53 | ;; Implementations of the multi-methods:
54 |
55 | (defmethod write-cell :keyword [k] (name k))
56 | (defmethod write-cell :other [o] (str o))
57 | (defmethod write-cell :numeric [n] (. n doubleValue))
58 |
59 | (defmethod get-cell-formula-value
60 | Cell/CELL_TYPE_BOOLEAN [evaled-cell evaled-type]
61 | (. evaled-cell getBooleanValue))
62 |
63 | (defmethod get-cell-formula-value
64 | Cell/CELL_TYPE_STRING [evaled-cell evaled-type]
65 | (. evaled-cell getStringValue))
66 |
67 | (defmethod get-cell-formula-value
68 | :number [evaled-cell evaled-type]
69 | (. evaled-cell getNumberValue))
70 |
71 | (defmethod get-cell-formula-value
72 | :date [evaled-cell evaled-type]
73 | (DateUtil/getJavaDate (. evaled-cell getNumberValue)))
74 |
75 | (defmethod get-cell-formula-value
76 | :default [evaled-cell evaled-type]
77 | (str "Unknown cell type " (. evaled-cell getCellType)))
78 |
79 | (defmethod get-cell-value Cell/CELL_TYPE_BLANK [cell])
80 | (defmethod get-cell-value Cell/CELL_TYPE_FORMULA [cell]
81 | (let [val (.
82 | (.. cell
83 | getSheet
84 | getWorkbook
85 | getCreationHelper
86 | createFormulaEvaluator)
87 | evaluate cell)
88 | evaluated-type (. val getCellType)]
89 | (get-cell-formula-value
90 | val
91 | (if (= Cell/CELL_TYPE_NUMERIC evaluated-type)
92 | (if (DateUtil/isCellInternalDateFormatted cell)
93 | ;; Check the original for date formatting hints
94 | :date
95 | :number)
96 | evaluated-type))))
97 |
98 | (defmethod get-cell-value Cell/CELL_TYPE_BOOLEAN [cell]
99 | (. cell getBooleanCellValue))
100 | (defmethod get-cell-value Cell/CELL_TYPE_STRING [cell]
101 | (. cell getStringCellValue))
102 | (defmethod get-cell-value Cell/CELL_TYPE_NUMERIC [cell]
103 | (. cell getNumericCellValue))
104 | (defmethod get-cell-value :date [cell]
105 | (. cell getDateCellValue))
106 | (defmethod get-cell-value :default [cell]
107 | (str "Unknown cell type " (. cell getCellType)))
108 |
109 |
--------------------------------------------------------------------------------
/modules/incanter-core/src/incanter/internal.clj:
--------------------------------------------------------------------------------
1 | ;;; internal.clj -- Internal functions
2 |
3 | ;; by David Edgar Liebke http://incanter.org
4 | ;; April 19, 2009
5 |
6 | ;; Copyright (c) David Edgar Liebke, 2009. All rights reserved. The use
7 | ;; and distribution terms for this software are covered by the Eclipse
8 | ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
9 | ;; which can be found in the file epl-v10.htincanter.at the root of this
10 | ;; distribution. By using this software in any fashion, you are
11 | ;; agreeing to be bound by the terms of this license. You must not
12 | ;; remove this notice, or any other, from this software.
13 |
14 | ;; CHANGE LOG
15 | ;; April 19, 2009: First version
16 |
17 |
18 |
19 | (ns incanter.internal
20 | (:import (incanter Matrix)
21 | (cern.colt.matrix.tdouble.algo DoubleFormatter)
22 | (cern.jet.math.tdouble DoubleFunctions DoubleArithmetic)
23 | (cern.colt.function.tdouble DoubleDoubleFunction DoubleFunction)))
24 |
25 |
26 |
27 | (derive Matrix ::matrix)
28 |
29 | (defn is-matrix
30 | " Test if obj is 'derived' from ::matrix (e.g. class incanter.Matrix)."
31 | ([obj] (isa? (class obj) ::matrix)))
32 |
33 |
34 | (defn make-matrix
35 | ([data]
36 | (cond
37 | (coll? (first data))
38 | (Matrix. (into-array (map double-array data)))
39 | (number? (first data))
40 | (Matrix. (double-array data))))
41 | ([data ncol]
42 | (cond
43 | (or (coll? data) (.isArray (class data)))
44 | (Matrix. (double-array data) ncol)
45 | (number? data)
46 | (Matrix. data ncol))) ; data is the number of rows in this case
47 | ([init-val rows cols]
48 | (Matrix. rows cols init-val)))
49 |
50 |
51 |
52 |
53 | (defmacro ^Matrix transform-with [A op fun]
54 | `(cond
55 | (is-matrix ~A)
56 | (.assign (.copy ~A) (. DoubleFunctions ~fun))
57 | (and (coll? ~A) (coll? (first ~A)))
58 | (.assign ^Matrix (make-matrix ~A) (. DoubleFunctions ~fun))
59 | (coll? ~A)
60 | (map ~op ~A)
61 | (number? ~A)
62 | (~op ~A)))
63 |
64 |
65 | (defmacro combine-with [A B op fun]
66 | `(cond
67 | (and (number? ~A) (number? ~B))
68 | (~op ~A ~B)
69 | (and (is-matrix ~A) (is-matrix ~B))
70 | (.assign ^Matrix (.copy ^Matrix ~A)
71 | ^Matrix ~B
72 | ^DoubleDoubleFunction (. DoubleFunctions ~fun))
73 | (and (is-matrix ~A) (number? ~B))
74 | (.assign ^Matrix (.copy ^Matrix ~A)
75 | (make-matrix ~B (.rows ~A) (.columns ~A))
76 | ^DoubleDoubleFunction (. DoubleFunctions ~fun))
77 | (and (number? ~A) (is-matrix ~B))
78 | (.assign ^Matrix (make-matrix ~A (.rows ~B) (.columns ~B))
79 | ^Matrix ~B
80 | ^DoubleDoubleFunction (. DoubleFunctions ~fun))
81 | (and (coll? ~A) (is-matrix ~B))
82 | (.assign ^Matrix (make-matrix ~A (.columns ~B))
83 | ^Matrix (make-matrix ~B)
84 | ^DoubleDoubleFunction (. DoubleFunctions ~fun))
85 | (and (is-matrix ~A) (coll? ~B))
86 | (.assign ^Matrix (.copy ~A)
87 | ^Matrix (make-matrix ~B)
88 | ^DoubleDoubleFunction (. DoubleFunctions ~fun))
89 | (and (coll? ~A) (coll? ~B) (coll? (first ~A)))
90 | (.assign (make-matrix ~A)
91 | (make-matrix ~B)
92 | (. DoubleFunctions ~fun))
93 | (and (coll? ~A) (number? ~B) (coll? (first ~A)))
94 | (.assign (make-matrix ~A)
95 | (make-matrix ~B)
96 | (. DoubleFunctions ~fun))
97 | ;;(. DoubleFunctions (~fun ~B)))
98 | (and (number? ~A) (coll? ~B) (coll? (first ~B)))
99 | (.assign (make-matrix ~A (.rows ~B) (.columns ~B))
100 | (make-matrix ~B)
101 | (. DoubleFunctions ~fun))
102 | (and (coll? ~A) (coll? ~B))
103 | (map ~op ~A ~B)
104 | (and (number? ~A) (coll? ~B))
105 | (map ~op (replicate (count ~B) ~A) ~B)
106 | (and (coll? ~A) (number? ~B))
107 | (map ~op ~A (replicate (count ~A) ~B))
108 | ))
109 |
110 |
111 | ;; PRINT METHOD FOR COLT MATRICES
112 | (defmethod print-method Matrix [o, ^java.io.Writer w]
113 | (let [formatter (DoubleFormatter. "%1.4f")]
114 | (do
115 | (.setPrintShape formatter false)
116 | (.write w "[")
117 | (.write w (.toString formatter o))
118 | (.write w "]\n"))))
119 |
120 |
121 |
122 |
123 |
124 |
--------------------------------------------------------------------------------
/data/iran_election_2009.csv:
--------------------------------------------------------------------------------
1 | Region,Ahmadinejad,%,Rezai,%,Karrubi,%,Mousavi,%,Total votes,Invalid votes,Valid votes,Eligible voters,"Turnout, %"
2 | East Azerbaijan,1131111,56.7503455611386,16920,0.84891389695129,7246,0.36354787809155,837858,42.0371926638186,2010340,17205,1993135,2461553,80.9706311422098
3 | West Azerbaijan,623946,47.475008788202,12199,0.92820153059283,21609,1.64419271043369,656508,49.9525969707714,1334356,20094,1314262,1883144,69.7908391498473
4 | Ardabil,325911,51.1126306198079,6578,1.0316279113534,2319,0.36368883040871,302825,47.4920526384299,642005,4372,637633,804881,79.2207792207792
5 | Isfahan,1799255,68.8757766566794,51788,1.98245313838011,14579,0.55808651240526,746697,28.5836836925353,2637482,25163,2612319,2987946,87.4285880668526
6 | Ilam,199654,64.5769992107953,5221,1.68870402235649,7471,2.4164542714088,96826,31.3178424954394,312667,3495,309172,357687,86.4364654013146
7 | Bushehr,299357,61.369301921295,7608,1.55966838596462,3563,0.73042829379495,177268,36.3406013989455,493989,6193,487796,580822,83.9837333985283
8 | Tehran,3819495,51.5741025426019,147487,1.99149616944144,67334,0.90920150978167,3371523,45.525199778175,7521540,115701,7405839,8796466,84.1910717326708
9 | Chahar Mahaal and Bakhtiari,359578,73.0117991524753,22689,4.6069690330624,4127,0.8379814535435,106099,21.5432503609188,495446,2953,492493,562238,87.5951109672416
10 | South Khorasan,285984,75.0147546014684,3962,1.03924855142602,928,0.24341813622497,90363,23.7025787108806,383157,1920,381237
11 | Khorasan Razavi,2214801,70.1387795895864,44809,1.41902074932681,13561,0.42945257384947,884570,28.0127470872374,3181990,24249,3157741
12 | North Khorasan,341104,74.0035884051557,4129,0.89579956999885,2478,0.53760991389129,113218,24.5630021109542,464001,3072,460929
13 | Khuzestan,1303129,64.805753664047,139124,6.91875913494127,15934,0.79241186320228,552636,27.4830753378094,2038845,28022,2010823,2801644,71.7729661584413
14 | Zanjan,444480,76.5631997795156,7276,1.25331587832018,2223,0.38291935094912,126561,21.8005649912151,585721,5181,580540,632160,91.8343457352569
15 | Semnan,295177,77.7768116400276,4440,1.16990498474381,2147,0.56571756807319,77754,20.4875658071554,383308,3790,379518,436492,86.9472980031707
16 | Sistan and Baluchestan,450269,46.0711015158569,6616,0.67694291107962,12504,1.27939754536571,507946,51.9725580276978,982920,5585,977335,1306624,74.7984883179859
17 | Fars,1758026,70.1824156925241,23871,0.95295771791557,16277,0.64979652190992,706764,28.2148300676504,2523300,18362,2504938,2842209,88.1334905350029
18 | Qazvin,498061,72.5749740262957,7978,1.16251451685996,2690,0.39197343323556,177542,25.8705380236087,692355,6084,686271,749205,91.599895889643
19 | Qom,422457,71.6593586470693,16297,2.76438209775501,2314,0.39251274309413,148467,25.1837465120816,599040,9505,589535,655988,89.8697842033696
20 | Kurdistan,315689,52.7499611504805,7140,1.19305621233059,13862,2.31626683688048,261772,43.7407158003085,610756,12293,598463,943818,63.4087292253379
21 | Kerman,1160446,77.5860489714105,12016,0.80337556804924,4977,0.33275634172612,318250,21.2778191188141,1505814,10125,1495689,1738280,86.044193110431
22 | Kermanshah,573568,59.1421842583924,11258,1.16084354493448,10798,1.11341167154046,374188,38.5835605251327,983422,13610,969812,1231672,78.7394695990491
23 | Kohgiluyeh & Boyer-Ahmad,253962,69.4425987449243,8542,2.33569856308874,4274,1.1686695924422,98937,27.0530330995447,368707,2992,365715,415694,87.9769734468142
24 | Golestan,515211,60.1108854149044,5987,0.69851744426853,10097,1.17804086099538,325806,38.0125562798317,869453,12352,857101,1059769,80.8762098155353
25 | Gilan,998573,67.8570166568813,12022,0.81694283166982,7183,0.4881134885946,453806,30.8379270228543,1483258,11674,1471584,1576046,93.3718939675619
26 | Lorestan,677829,70.906991121837,14920,1.56076577947802,44036,4.60656044672213,219156,22.9256826519628,964270,8329,955941,1124940,84.9770654434903
27 | Mazandaran,1289257,67.7035835836046,19587,1.0285847520332,10050,0.52776212579433,585373,30.7400695385679,1919838,15571,1904267,1915240,99.4270691923728
28 | Markazi,572988,73.6423119286336,10057,1.29255888616562,4675,0.60084645449183,190349,24.464282730709,785961,7892,778069,885557,87.8621026088665
29 | Hormozgan,482990,65.5042917727347,7237,0.98149974028299,5126,0.69520072802136,241988,32.8190077589609,743024,5683,737341,919908,80.1537762471899
30 | Hamadan,765723,75.8627556464388,13117,1.29954535231975,12032,1.19205074934141,218481,21.6456482519,1019169,9816,1009353,1256250,80.3465074626866
31 | Yazd,337178,55.8289786537914,8406,1.39184168173419,2565,0.42470543821654,255799,42.3544742262579,609856,5908,603948,609341,99.114945490292
32 |
33 |
--------------------------------------------------------------------------------
/data/iris.dat:
--------------------------------------------------------------------------------
1 | Sepal.Length Sepal.Width Petal.Length Petal.Width Species
2 | 5.1 3.5 1.4 0.2 setosa
3 | 4.9 3.0 1.4 0.2 setosa
4 | 4.7 3.2 1.3 0.2 setosa
5 | 4.6 3.1 1.5 0.2 setosa
6 | 5.0 3.6 1.4 0.2 setosa
7 | 5.4 3.9 1.7 0.4 setosa
8 | 4.6 3.4 1.4 0.3 setosa
9 | 5.0 3.4 1.5 0.2 setosa
10 | 4.4 2.9 1.4 0.2 setosa
11 | 4.9 3.1 1.5 0.1 setosa
12 | 5.4 3.7 1.5 0.2 setosa
13 | 4.8 3.4 1.6 0.2 setosa
14 | 4.8 3.0 1.4 0.1 setosa
15 | 4.3 3.0 1.1 0.1 setosa
16 | 5.8 4.0 1.2 0.2 setosa
17 | 5.7 4.4 1.5 0.4 setosa
18 | 5.4 3.9 1.3 0.4 setosa
19 | 5.1 3.5 1.4 0.3 setosa
20 | 5.7 3.8 1.7 0.3 setosa
21 | 5.1 3.8 1.5 0.3 setosa
22 | 5.4 3.4 1.7 0.2 setosa
23 | 5.1 3.7 1.5 0.4 setosa
24 | 4.6 3.6 1.0 0.2 setosa
25 | 5.1 3.3 1.7 0.5 setosa
26 | 4.8 3.4 1.9 0.2 setosa
27 | 5.0 3.0 1.6 0.2 setosa
28 | 5.0 3.4 1.6 0.4 setosa
29 | 5.2 3.5 1.5 0.2 setosa
30 | 5.2 3.4 1.4 0.2 setosa
31 | 4.7 3.2 1.6 0.2 setosa
32 | 4.8 3.1 1.6 0.2 setosa
33 | 5.4 3.4 1.5 0.4 setosa
34 | 5.2 4.1 1.5 0.1 setosa
35 | 5.5 4.2 1.4 0.2 setosa
36 | 4.9 3.1 1.5 0.2 setosa
37 | 5.0 3.2 1.2 0.2 setosa
38 | 5.5 3.5 1.3 0.2 setosa
39 | 4.9 3.6 1.4 0.1 setosa
40 | 4.4 3.0 1.3 0.2 setosa
41 | 5.1 3.4 1.5 0.2 setosa
42 | 5.0 3.5 1.3 0.3 setosa
43 | 4.5 2.3 1.3 0.3 setosa
44 | 4.4 3.2 1.3 0.2 setosa
45 | 5.0 3.5 1.6 0.6 setosa
46 | 5.1 3.8 1.9 0.4 setosa
47 | 4.8 3.0 1.4 0.3 setosa
48 | 5.1 3.8 1.6 0.2 setosa
49 | 4.6 3.2 1.4 0.2 setosa
50 | 5.3 3.7 1.5 0.2 setosa
51 | 5.0 3.3 1.4 0.2 setosa
52 | 7.0 3.2 4.7 1.4 versicolor
53 | 6.4 3.2 4.5 1.5 versicolor
54 | 6.9 3.1 4.9 1.5 versicolor
55 | 5.5 2.3 4.0 1.3 versicolor
56 | 6.5 2.8 4.6 1.5 versicolor
57 | 5.7 2.8 4.5 1.3 versicolor
58 | 6.3 3.3 4.7 1.6 versicolor
59 | 4.9 2.4 3.3 1.0 versicolor
60 | 6.6 2.9 4.6 1.3 versicolor
61 | 5.2 2.7 3.9 1.4 versicolor
62 | 5.0 2.0 3.5 1.0 versicolor
63 | 5.9 3.0 4.2 1.5 versicolor
64 | 6.0 2.2 4.0 1.0 versicolor
65 | 6.1 2.9 4.7 1.4 versicolor
66 | 5.6 2.9 3.6 1.3 versicolor
67 | 6.7 3.1 4.4 1.4 versicolor
68 | 5.6 3.0 4.5 1.5 versicolor
69 | 5.8 2.7 4.1 1.0 versicolor
70 | 6.2 2.2 4.5 1.5 versicolor
71 | 5.6 2.5 3.9 1.1 versicolor
72 | 5.9 3.2 4.8 1.8 versicolor
73 | 6.1 2.8 4.0 1.3 versicolor
74 | 6.3 2.5 4.9 1.5 versicolor
75 | 6.1 2.8 4.7 1.2 versicolor
76 | 6.4 2.9 4.3 1.3 versicolor
77 | 6.6 3.0 4.4 1.4 versicolor
78 | 6.8 2.8 4.8 1.4 versicolor
79 | 6.7 3.0 5.0 1.7 versicolor
80 | 6.0 2.9 4.5 1.5 versicolor
81 | 5.7 2.6 3.5 1.0 versicolor
82 | 5.5 2.4 3.8 1.1 versicolor
83 | 5.5 2.4 3.7 1.0 versicolor
84 | 5.8 2.7 3.9 1.2 versicolor
85 | 6.0 2.7 5.1 1.6 versicolor
86 | 5.4 3.0 4.5 1.5 versicolor
87 | 6.0 3.4 4.5 1.6 versicolor
88 | 6.7 3.1 4.7 1.5 versicolor
89 | 6.3 2.3 4.4 1.3 versicolor
90 | 5.6 3.0 4.1 1.3 versicolor
91 | 5.5 2.5 4.0 1.3 versicolor
92 | 5.5 2.6 4.4 1.2 versicolor
93 | 6.1 3.0 4.6 1.4 versicolor
94 | 5.8 2.6 4.0 1.2 versicolor
95 | 5.0 2.3 3.3 1.0 versicolor
96 | 5.6 2.7 4.2 1.3 versicolor
97 | 5.7 3.0 4.2 1.2 versicolor
98 | 5.7 2.9 4.2 1.3 versicolor
99 | 6.2 2.9 4.3 1.3 versicolor
100 | 5.1 2.5 3.0 1.1 versicolor
101 | 5.7 2.8 4.1 1.3 versicolor
102 | 6.3 3.3 6.0 2.5 virginica
103 | 5.8 2.7 5.1 1.9 virginica
104 | 7.1 3.0 5.9 2.1 virginica
105 | 6.3 2.9 5.6 1.8 virginica
106 | 6.5 3.0 5.8 2.2 virginica
107 | 7.6 3.0 6.6 2.1 virginica
108 | 4.9 2.5 4.5 1.7 virginica
109 | 7.3 2.9 6.3 1.8 virginica
110 | 6.7 2.5 5.8 1.8 virginica
111 | 7.2 3.6 6.1 2.5 virginica
112 | 6.5 3.2 5.1 2.0 virginica
113 | 6.4 2.7 5.3 1.9 virginica
114 | 6.8 3.0 5.5 2.1 virginica
115 | 5.7 2.5 5.0 2.0 virginica
116 | 5.8 2.8 5.1 2.4 virginica
117 | 6.4 3.2 5.3 2.3 virginica
118 | 6.5 3.0 5.5 1.8 virginica
119 | 7.7 3.8 6.7 2.2 virginica
120 | 7.7 2.6 6.9 2.3 virginica
121 | 6.0 2.2 5.0 1.5 virginica
122 | 6.9 3.2 5.7 2.3 virginica
123 | 5.6 2.8 4.9 2.0 virginica
124 | 7.7 2.8 6.7 2.0 virginica
125 | 6.3 2.7 4.9 1.8 virginica
126 | 6.7 3.3 5.7 2.1 virginica
127 | 7.2 3.2 6.0 1.8 virginica
128 | 6.2 2.8 4.8 1.8 virginica
129 | 6.1 3.0 4.9 1.8 virginica
130 | 6.4 2.8 5.6 2.1 virginica
131 | 7.2 3.0 5.8 1.6 virginica
132 | 7.4 2.8 6.1 1.9 virginica
133 | 7.9 3.8 6.4 2.0 virginica
134 | 6.4 2.8 5.6 2.2 virginica
135 | 6.3 2.8 5.1 1.5 virginica
136 | 6.1 2.6 5.6 1.4 virginica
137 | 7.7 3.0 6.1 2.3 virginica
138 | 6.3 3.4 5.6 2.4 virginica
139 | 6.4 3.1 5.5 1.8 virginica
140 | 6.0 3.0 4.8 1.8 virginica
141 | 6.9 3.1 5.4 2.1 virginica
142 | 6.7 3.1 5.6 2.4 virginica
143 | 6.9 3.1 5.1 2.3 virginica
144 | 5.8 2.7 5.1 1.9 virginica
145 | 6.8 3.2 5.9 2.3 virginica
146 | 6.7 3.3 5.7 2.5 virginica
147 | 6.7 3.0 5.2 2.3 virginica
148 | 6.3 2.5 5.0 1.9 virginica
149 | 6.5 3.0 5.2 2.0 virginica
150 | 6.2 3.4 5.4 2.3 virginica
151 | 5.9 3.0 5.1 1.8 virginica
152 |
153 |
--------------------------------------------------------------------------------
/modules/incanter-mongodb/src/incanter/mongodb.clj:
--------------------------------------------------------------------------------
1 | (ns ^{:doc "A simple library that provides functions for persisting
2 | Incanter data structures using MongoDB.
3 |
4 | Use incanter.mongodb in combination with the somnium.congomongo library.
5 | For usage examples, see the Congomongo README at http://github.com/somnium/congomongo,
6 | and the examples/blog/mongodb_datasets.clj file in the Incanter distribution.
7 |
8 | Here are Somnium's descriptions of Congomongo's functions:
9 |
10 | (mongo! & args) : Creates a Mongo object and sets the default database.
11 | Keyword arguments include:
12 | :host -> defaults to localhost
13 | :port -> defaults to 27017
14 | :db -> defaults to nil (you'll have to set it anyway, might as well do it now.)
15 |
16 | (get-coll coll) : Returns a DBCollection object
17 |
18 | (fetch coll & options) : Fetches objects from a collection. Optional arguments include
19 | :where -> takes a query map
20 | :only -> takes an array of keys to retrieve
21 | :as -> what to return, defaults to :clojure, can also be :json or :mongo
22 | :from -> argument type, same options as above
23 | :one? -> defaults to false, use fetch-one as a shortcut
24 | :count? -> defaults to false, use fetch-count as a shortcut
25 |
26 | (fetch-one coll & options) : same as (fetch collection :one? true)
27 |
28 | (fetch-count coll & options) : same as (fetch collection :count? true)
29 |
30 | (insert! coll obj & options) : Inserts a map into collection. Will not overwrite existing maps.
31 | Takes optional from and to keyword arguments. To insert
32 | as a side-effect only specify :to as nil.
33 |
34 | (mass-insert! coll objs & options) : Inserts a sequence of maps.
35 |
36 | (update! coll old new & options) : Alters/inserts a map in a collection. Overwrites existing objects.
37 | The shortcut forms need a map with valid :_id and :_ns fields or
38 | a collection and a map with a valid :_id field.
39 |
40 | (destroy! coll query-map) : Removes map from collection. Takes a collection name and
41 | a query map
42 |
43 | (add-index! coll fields & options) : Adds an index on the collection for the specified fields if it does not exist.
44 | Options include:
45 | :unique -> defaults to false
46 | :force -> defaults to true
47 |
48 | (drop-index! coll fields) : Drops an index on the collection for the specified fields
49 |
50 | (drop-all-indexes! coll) : Drops all indexes from a collection
51 |
52 | (get-indexes coll & options) : Get index information on collection
53 |
54 | (drop-database title) : drops a database from the mongo server
55 |
56 | (set-database title) : atomically alters the current database
57 |
58 | (databases) : List databases on the mongo server
59 |
60 | (collections) : Returns the set of collections stored in the current database
61 |
62 | (drop-collection coll) : Permanently deletes a collection. Use with care.
63 |
64 | "
65 | :author "David Edgar Liebke"}
66 |
67 | incanter.mongodb
68 | (:use [incanter.core :only (dataset)]
69 | [somnium.congomongo :only (fetch mass-insert!)]))
70 |
71 |
72 | (defn fetch-dataset
73 | "Queries a MongoDB database, accepting the same arguments as
74 | somnium.congomongo/fetch, but returning an Incanter dataset instead
75 | of a sequence of maps.
76 |
77 | Examples:
78 |
79 | (use '(incanter core datasets mongodb))
80 | (use 'somnium.congomongo)
81 |
82 | ;; first load some sample data
83 | (def data (get-dataset :airline-passengers))
84 | (view data)
85 |
86 | ;; a MongoDB server must be running on the localhost on the default port
87 | ;; for the following steps.
88 |
89 | (mongo! :db \"mydb\")
90 | (mass-insert! :airline-data (:rows data))
91 |
92 | ;; and then retrieve it
93 | ;; notice that the retrieved data set has two additional columns, :_id :_ns
94 | (view (fetch-dataset :airline-data))
95 |
96 | "
97 | [& args]
98 | (let [results (apply fetch args)
99 | colnames (keys (first results))]
100 | (dataset colnames results)))
101 |
102 |
103 |
104 | (defn insert-dataset
105 | "Inserts the rows of the Incanter dataset into the given MongoDB collection.
106 |
107 | Examples:
108 |
109 | (use '(incanter core datasets mongodb))
110 | (use 'somnium.congomongo)
111 |
112 | (def data (get-dataset :airline-passengers))
113 | (view data)
114 |
115 | ;; a MongoDB server must be running on the localhost on the default port
116 | ;; for the following steps.
117 |
118 | (mongo! :db \"mydb\")
119 | (mass-insert! :airline-data (:rows data))
120 |
121 | ;; notice that the retrieved data set has two additional columns, :_id :_ns
122 | (view (fetch-dataset :airline-data))
123 |
124 |
125 | "
126 | [mongodb-coll dataset]
127 | (mass-insert! mongodb-coll (:rows dataset)))
128 |
--------------------------------------------------------------------------------
/modules/incanter-core/src/incanter/infix.clj:
--------------------------------------------------------------------------------
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 | ;; File : infix.clj
3 | ;; Function : Infix Math library
4 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 | ;; Copyright (c) 2008, J. Bester
6 | ;; All rights reserved.
7 | ;;
8 | ;; Redistribution and use in source and binary forms, with or without
9 | ;; modification, are permitted provided that the following conditions are met:
10 | ;; * Redistributions of source code must retain the above copyright
11 | ;; notice, this list of conditions and the following disclaimer.
12 | ;; * Redistributions in binary form must reproduce the above copyright
13 | ;; notice, this list of conditions and the following disclaimer in the
14 | ;; documentation and/or other materials provided with the distribution.
15 | ;;
16 | ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY
17 | ;; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 | ;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 | ;; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
20 | ;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 | ;; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 | ;; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 | ;; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 | ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 | ;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 |
28 | (ns ^{:doc "Library for converting infix mathematical formula to prefix expressions"
29 | :author "J. Bester"}
30 | incanter.infix)
31 |
32 |
33 | ;; operator precedence for formula macro
34 | (def +precedence-table+ (ref {}))
35 |
36 | ;; symbol translation for symbols in formula
37 | ;; (only supports binary operators)
38 | (def +translation-table+ (ref {}))
39 |
40 | (def +highest-precedence+ (ref 0))
41 |
42 | (defn defop
43 | "Define operators for formula macro"
44 | ([op prec & [trans]]
45 | (dosync (ref-set +precedence-table+ (assoc @+precedence-table+ op prec)))
46 | (when-not (nil? trans)
47 | (dosync (ref-set +translation-table+ (assoc @+translation-table+ op trans))))
48 | (dosync (ref-set +highest-precedence+ (apply max (map val @+precedence-table+))))))
49 |
50 |
51 | ;; == operators ==
52 | (defop '|| 10 'or)
53 | (defop '&& 20 'and)
54 | (defop '== 30 '=)
55 | (defop '!= 30 'not=)
56 | (defop '< 40)
57 | (defop '> 40)
58 | (defop '<= 40)
59 | (defop '>= 40)
60 | (defop 'mod 90 'rem)
61 |
62 |
63 |
64 | (defn- operator?
65 | "Check if is valid operator"
66 | ([sym]
67 | (not (nil? (get @+precedence-table+ sym)))))
68 |
69 | (defn- find-lowest-precedence
70 | "find the operator with lowest precedence; search from left to right"
71 | ([col]
72 | ;; loop through terms in the coluence
73 | (loop [idx 0
74 | col col
75 | lowest-idx nil
76 | lowest-prec @+highest-precedence+]
77 | ;; nothing left to process
78 | (if (empty? col)
79 | ;; return lowest found
80 | lowest-idx
81 | ;; otherwise check if current term is lower
82 | (let [prec (get @+precedence-table+ (first col))]
83 | ;; is of lower or equal precedence
84 | (if (and prec (<= prec lowest-prec))
85 | (recur (inc idx) (rest col)
86 | idx prec)
87 | ;; is of high precedence therefore skip for now
88 | (recur (inc idx) (rest col)
89 | lowest-idx lowest-prec)))))))
90 |
91 | (defn- translate-op
92 | "Translation of symbol => symbol for binary op allows for
93 | user defined operators"
94 | ([op]
95 | (if (contains? @+translation-table+ op)
96 | (get @+translation-table+ op)
97 | op)))
98 |
99 | (defn infix-to-prefix
100 | "Convert from infix notation to prefix notation"
101 | ([col]
102 | (cond
103 | ;; handle term only
104 | (not (seq? col)) col
105 | ;; handle sequence containing one term (i.e. handle parens)
106 | (= (count col) 1) (infix-to-prefix (first col))
107 | ;; handle all other cases
108 | true (let [lowest (find-lowest-precedence col)]
109 | (if (nil? lowest) ;; nothing to split
110 | col
111 | ;; (a b c) bind a to hd, c to tl, and b to op
112 | (let [[hd [op & tl]] (split-at lowest col)]
113 | ;; recurse
114 | (list (translate-op op)
115 | (infix-to-prefix hd)
116 | (infix-to-prefix tl))))))))
117 |
118 | (defmacro formula
119 | "Convert from infix notation to prefix notation"
120 | ([& equation]
121 | (infix-to-prefix equation)))
122 |
123 |
124 |
125 |
126 |
127 |
128 |
--------------------------------------------------------------------------------
/modules/incanter-latex/src/incanter/latex.clj:
--------------------------------------------------------------------------------
1 | (ns ^{:doc "This library is used to render LaTex Math equations, based
2 | on the jLateXMath library, and applying them incanter.charts as annotations
3 | and subtitles.
4 | "
5 | :author "David Edgar Liebke"}
6 |
7 | incanter.latex
8 | (:import [org.scilab.forge.jlatexmath TeXConstants TeXIcon TeXFormula])
9 | (:use [incanter.core :only [dim]]))
10 |
11 |
12 |
13 | (defn latex
14 | " Returns the given LaTeX equation rendered as an java.awt.Image.
15 |
16 | Options:
17 | :color (default java.awt.Color/black) -- the text color
18 | :background (default java.awt.Clolor/white) -- the background color
19 | :border (default [5 5 5 5]) -- image border
20 |
21 | Examples:
22 | (use '(incanter core charts stats latex))
23 |
24 | (def latex-img (latex \"\\\\frac{(a+b)^2} {(a-b)^2}\"))
25 | (save latex-img \"/tmp/latex-example1.png\")
26 | (view \"file:///tmp/latex-example1.png\")
27 |
28 | (view (latex \"f(x)=\\\\frac {1} {\\\\sqrt {2\\\\pi \\\\sigma ^2}} e^{\\\\frac {-(x - \\\\mu)^2}{2 \\\\sigma ^2}}\"))
29 |
30 | (view (latex \"\\\\begin{pmatrix}
31 | a & b & c \\\\\\\\
32 | d & e & f \\\\\\\\
33 | g & h & i
34 | \\\\end{pmatrix}\"))
35 |
36 |
37 | "
38 | ([latex-txt & {:keys [color background border] :or {color java.awt.Color/black
39 | background java.awt.Color/white
40 | border [5 5 5 5]}}]
41 | (let [formula (org.scilab.forge.jlatexmath.TeXFormula. latex-txt)
42 | icon (doto (.createTeXIcon formula TeXConstants/STYLE_DISPLAY 20)
43 | (.setInsets (apply #(java.awt.Insets. %1 %2 %3 %4) border)))
44 | image (java.awt.image.BufferedImage. (.getIconWidth icon)
45 | (.getIconHeight icon)
46 | java.awt.image.BufferedImage/TYPE_INT_ARGB)
47 | g2 (doto (.createGraphics image)
48 | (.setColor background)
49 | (.fillRect 0 0 (.getIconWidth icon) (.getIconHeight icon)))
50 | label (doto (javax.swing.JLabel.)
51 | (.setForeground color))]
52 | (do
53 | (.paintIcon icon label g2 0 0)
54 | image))))
55 |
56 |
57 |
58 |
59 |
60 |
61 | (defn add-latex-subtitle
62 | " Adds the given LaTeX equation as a subtitle to the chart.
63 |
64 |
65 | Options:
66 | :color (default java.awt.Color/darkGray) -- the text color
67 |
68 |
69 | Examples:
70 | (use '(incanter core charts stats latex))
71 |
72 | (doto (function-plot pdf-normal -3 3)
73 | (add-latex-subtitle \"f(x)=\\\\frac{1}{\\\\sqrt{2\\\\pi \\\\sigma^2}} e^{\\\\frac{-(x - \\\\mu)^2}{2 \\\\sigma^2}}\")
74 | view)
75 |
76 | "
77 | ([chart latex-str & {:keys [color] :or {color java.awt.Color/darkGray}}]
78 | (.addSubtitle chart (org.jfree.chart.title.ImageTitle. (latex latex-str :color color)))
79 | chart))
80 |
81 |
82 |
83 |
84 | (defn add-latex
85 | " Adds an LaTeX equation annotation to the chart at the given x,y coordinates.
86 |
87 | Arguments:
88 | chart -- the chart to add the polygon to.
89 | x, y -- the coordinates to place the image
90 | latex-str -- a string of latex code
91 |
92 |
93 | Options:
94 | :color (default java.awt.Color/darkGray) -- the text color
95 |
96 |
97 | Examples:
98 | (use '(incanter core charts stats latex))
99 |
100 | (doto (function-plot pdf-normal -3 3)
101 | (add-latex 0 0.1 \"f(x)=\\\\frac{1}{\\\\sqrt{2\\\\pi \\\\sigma^2}} e^{\\\\frac{-(x - \\\\mu)^2}{2 \\\\sigma^2}}\")
102 | view)
103 |
104 | "
105 | ([chart x y latex-str & {:keys [color] :or {color java.awt.Color/darkGray}}]
106 | (let [img (latex latex-str :color color)
107 | anno (org.jfree.chart.annotations.XYImageAnnotation. x y img)]
108 | (.addAnnotation (.getPlot chart) anno)
109 | chart)))
110 |
111 | (defn to-latex
112 | "Convert an Incanter Matrix into a string of LaTeX commands to render it.
113 |
114 | Options:
115 | :mxtype (default pmatrix) -- the type of matrix to output, see LaTeX documentation for other options.
116 | Example:
117 | (use '(incanter core latex))
118 | (view (latex (to-latex (matrix [[1 0][0 1]]))))
119 | "
120 | [mx &
121 | {:keys [mxtype]
122 | :or {mxtype "pmatrix"}}]
123 | (let [dimensions (zipmap [:height :width] (dim mx))
124 | write-row (fn [coll] (apply str (interpose " & " coll)))]
125 | (str
126 | "\\begin{" mxtype "}"
127 | (apply
128 | str
129 | (interpose
130 | "\\\\"
131 | (map
132 | write-row
133 | (map
134 | (partial nth mx)
135 | (range (:height dimensions))))))
136 | "\\end{" mxtype "}")))
137 |
--------------------------------------------------------------------------------
/examples/blog/chisq_goodness_of_fit.clj:
--------------------------------------------------------------------------------
1 | ;; from the following blog post:
2 | ;; http://incanter-blog.org/2009/06/21/chi-square-goodness-of-fit/
3 |
4 | ;; Chi-square goodness-of-fit analysis of 2009 Iranian election and Benford's law
5 |
6 | (use '(incanter core stats charts io))
7 | (def votes (read-dataset "data/iran_election_2009.csv"
8 | :header true))
9 | (view votes)
10 |
11 | (def regions (sel votes :cols :Region))
12 |
13 |
14 | (def ahmadinejad-votes (sel votes :cols :Ahmadinejad))
15 | (def mousavi-votes (sel votes :cols :Mousavi))
16 | (def rezai-votes (sel votes :cols :Rezai))
17 | (def karrubi-votes (sel votes :cols :Karrubi))
18 |
19 | (defn first-digit [x]
20 | (Character/digit (first (str x)) 10))
21 |
22 | (def ahmadinejad (map first-digit ahmadinejad-votes))
23 | (def mousavi (map first-digit mousavi-votes))
24 | (def rezai (map first-digit rezai-votes))
25 | (def karrubi (map first-digit karrubi-votes))
26 |
27 |
28 | ;; define function for Benford's law
29 | (defn benford-law [d] (log10 (plus 1 (div d))))
30 | ;; calculate the probabilities for digits 1-9
31 | (def benford-probs (benford-law (range 1 11)))
32 | ;; calculate the expected frequencies for the 30 regions
33 | (def benford-freq (mult benford-probs (count regions)))
34 |
35 | (defn get-counts [digits]
36 | (map #(get (:counts (tabulate digits)) % 0)
37 | (range 1.0 10.0 1.0)))
38 |
39 |
40 | (doto (xy-plot (range 1 10) (get-counts ahmadinejad)
41 | :legend true :series-label "Ahmadinejad"
42 | :y-label "First digit frequency"
43 | :x-label "First digit"
44 | :title "First digit frequency by candidate")
45 | (add-lines (range 1 10) (get-counts mousavi)
46 | :series-label "Mousavi")
47 | (add-lines (range 1 10) benford-freq
48 | :series-label "Predicted")
49 | ;(add-lines (range 1 10) (get-counts rezai) :series-label "Rezai")
50 | ;(add-lines (range 1 10) (get-counts karrubi) :series-label "Karrubi")
51 | clear-background
52 | view)
53 |
54 |
55 | (def ahmadinejad-test
56 | (chisq-test :table (get-counts ahmadinejad)
57 | :probs benford-probs))
58 | (:X-sq ahmadinejad-test) ;; 5.439
59 | (:p-value ahmadinejad-test) ;; 0.7098
60 |
61 | (def mousavi-test
62 | (chisq-test :table (get-counts mousavi)
63 | :probs benford-probs))
64 | (:X-sq mousavi-test) ;; 5.775
65 | (:p-value mousavi-test) ;; 0.672
66 |
67 | (def rezai-test
68 | (chisq-test :table (get-counts rezai)
69 | :probs benford-probs))
70 | (:X-sq rezai-test) ;; 12.834
71 | (:p-value rezai-test) ;; 0.118
72 |
73 | (def karrubi-test
74 | (chisq-test :table (get-counts karrubi)
75 | :probs benford-probs))
76 | (:X-sq karrubi-test) ;; 8.8696
77 | (:p-value karrubi-test) ;; 0.353
78 |
79 |
80 |
81 | ;; now compare the distribution of the last digit, it should be uniform
82 | ;; based on Washington Post article:
83 | ;; http://www.washingtonpost.com/wp-dyn/content/article/2009/06/20/AR2009062000004.html?referrer=reddit
84 |
85 | (defn last-digit [x]
86 | (Character/digit (last (str x)) 10))
87 |
88 | (def ahmadinejad-last (map last-digit
89 | ahmadinejad-votes))
90 | (def mousavi-last (map last-digit
91 | mousavi-votes))
92 | (def rezai-last (map last-digit
93 | rezai-votes))
94 | (def karrubi-last (map last-digit
95 | karrubi-votes))
96 |
97 |
98 | (defn get-counts [digits]
99 | (map #(get (:counts (tabulate digits)) % 0)
100 | (range 0.0 10.0 1.0)))
101 |
102 |
103 |
104 | (doto (xy-plot (range 10)
105 | (get-counts ahmadinejad-last)
106 | :legend true
107 | :series-label "Ahmadinejad"
108 | :y-label "First digit frequency"
109 | :x-label "First digit"
110 | :title "Last digit frequency by candidate")
111 | (add-lines (range 10) (get-counts mousavi-last)
112 | :series-label "Mousavi")
113 | (add-lines (range 10) (get-counts rezai-last)
114 | :series-label "Rezai")
115 | (add-lines (range 10) (get-counts karrubi-last)
116 | :series-label "Karrubi")
117 | clear-background
118 | view)
119 |
120 |
121 | (def ahmadinejad-test
122 | (chisq-test :table (get-counts ahmadinejad-last)))
123 |
124 | (:X-sq ahmadinejad-test) ;; 4.667
125 | (:p-value ahmadinejad-test) ;; 0.862
126 |
127 | (def mousavi-test
128 | (chisq-test :table (get-counts mousavi-last)))
129 | (:X-sq mousavi-test) ;; 8.667
130 | (:p-value mousavi-test) ;; 0.469
131 |
132 | (def rezai-test
133 | (chisq-test :table (get-counts rezai-last)))
134 | (:X-sq rezai-test) ;; 15.333
135 | (:p-value rezai-test) ;; 0.0822
136 |
137 | (def karrubi-test
138 | (chisq-test :table (get-counts karrubi-last)))
139 | (:X-sq karrubi-test) ;; 4.0
140 | (:p-value karrubi-test) ;; 0.911
141 |
142 |
143 |
144 |
--------------------------------------------------------------------------------
/data/Chwirut1.dat:
--------------------------------------------------------------------------------
1 | y x
2 | 92.9000E0 0.5000E0
3 | 78.7000E0 0.6250E0
4 | 64.2000E0 0.7500E0
5 | 64.9000E0 0.8750E0
6 | 57.1000E0 1.0000E0
7 | 43.3000E0 1.2500E0
8 | 31.1000E0 1.7500E0
9 | 23.6000E0 2.2500E0
10 | 31.0500E0 1.7500E0
11 | 23.7750E0 2.2500E0
12 | 17.7375E0 2.7500E0
13 | 13.8000E0 3.2500E0
14 | 11.5875E0 3.7500E0
15 | 9.4125E0 4.2500E0
16 | 7.7250E0 4.7500E0
17 | 7.3500E0 5.2500E0
18 | 8.0250E0 5.7500E0
19 | 90.6000E0 0.5000E0
20 | 76.9000E0 0.6250E0
21 | 71.6000E0 0.7500E0
22 | 63.6000E0 0.8750E0
23 | 54.0000E0 1.0000E0
24 | 39.2000E0 1.2500E0
25 | 29.3000E0 1.7500E0
26 | 21.4000E0 2.2500E0
27 | 29.1750E0 1.7500E0
28 | 22.1250E0 2.2500E0
29 | 17.5125E0 2.7500E0
30 | 14.2500E0 3.2500E0
31 | 9.4500E0 3.7500E0
32 | 9.1500E0 4.2500E0
33 | 7.9125E0 4.7500E0
34 | 8.4750E0 5.2500E0
35 | 6.1125E0 5.7500E0
36 | 80.0000E0 0.5000E0
37 | 79.0000E0 0.6250E0
38 | 63.8000E0 0.7500E0
39 | 57.2000E0 0.8750E0
40 | 53.2000E0 1.0000E0
41 | 42.5000E0 1.2500E0
42 | 26.8000E0 1.7500E0
43 | 20.4000E0 2.2500E0
44 | 26.8500E0 1.7500E0
45 | 21.0000E0 2.2500E0
46 | 16.4625E0 2.7500E0
47 | 12.5250E0 3.2500E0
48 | 10.5375E0 3.7500E0
49 | 8.5875E0 4.2500E0
50 | 7.1250E0 4.7500E0
51 | 6.1125E0 5.2500E0
52 | 5.9625E0 5.7500E0
53 | 74.1000E0 0.5000E0
54 | 67.3000E0 0.6250E0
55 | 60.8000E0 0.7500E0
56 | 55.5000E0 0.8750E0
57 | 50.3000E0 1.0000E0
58 | 41.0000E0 1.2500E0
59 | 29.4000E0 1.7500E0
60 | 20.4000E0 2.2500E0
61 | 29.3625E0 1.7500E0
62 | 21.1500E0 2.2500E0
63 | 16.7625E0 2.7500E0
64 | 13.2000E0 3.2500E0
65 | 10.8750E0 3.7500E0
66 | 8.1750E0 4.2500E0
67 | 7.3500E0 4.7500E0
68 | 5.9625E0 5.2500E0
69 | 5.6250E0 5.7500E0
70 | 81.5000E0 .5000E0
71 | 62.4000E0 .7500E0
72 | 32.5000E0 1.5000E0
73 | 12.4100E0 3.0000E0
74 | 13.1200E0 3.0000E0
75 | 15.5600E0 3.0000E0
76 | 5.6300E0 6.0000E0
77 | 78.0000E0 .5000E0
78 | 59.9000E0 .7500E0
79 | 33.2000E0 1.5000E0
80 | 13.8400E0 3.0000E0
81 | 12.7500E0 3.0000E0
82 | 14.6200E0 3.0000E0
83 | 3.9400E0 6.0000E0
84 | 76.8000E0 .5000E0
85 | 61.0000E0 .7500E0
86 | 32.9000E0 1.5000E0
87 | 13.8700E0 3.0000E0
88 | 11.8100E0 3.0000E0
89 | 13.3100E0 3.0000E0
90 | 5.4400E0 6.0000E0
91 | 78.0000E0 .5000E0
92 | 63.5000E0 .7500E0
93 | 33.8000E0 1.5000E0
94 | 12.5600E0 3.0000E0
95 | 5.6300E0 6.0000E0
96 | 12.7500E0 3.0000E0
97 | 13.1200E0 3.0000E0
98 | 5.4400E0 6.0000E0
99 | 76.8000E0 .5000E0
100 | 60.0000E0 .7500E0
101 | 47.8000E0 1.0000E0
102 | 32.0000E0 1.5000E0
103 | 22.2000E0 2.0000E0
104 | 22.5700E0 2.0000E0
105 | 18.8200E0 2.5000E0
106 | 13.9500E0 3.0000E0
107 | 11.2500E0 4.0000E0
108 | 9.0000E0 5.0000E0
109 | 6.6700E0 6.0000E0
110 | 75.8000E0 .5000E0
111 | 62.0000E0 .7500E0
112 | 48.8000E0 1.0000E0
113 | 35.2000E0 1.5000E0
114 | 20.0000E0 2.0000E0
115 | 20.3200E0 2.0000E0
116 | 19.3100E0 2.5000E0
117 | 12.7500E0 3.0000E0
118 | 10.4200E0 4.0000E0
119 | 7.3100E0 5.0000E0
120 | 7.4200E0 6.0000E0
121 | 70.5000E0 .5000E0
122 | 59.5000E0 .7500E0
123 | 48.5000E0 1.0000E0
124 | 35.8000E0 1.5000E0
125 | 21.0000E0 2.0000E0
126 | 21.6700E0 2.0000E0
127 | 21.0000E0 2.5000E0
128 | 15.6400E0 3.0000E0
129 | 8.1700E0 4.0000E0
130 | 8.5500E0 5.0000E0
131 | 10.1200E0 6.0000E0
132 | 78.0000E0 .5000E0
133 | 66.0000E0 .6250E0
134 | 62.0000E0 .7500E0
135 | 58.0000E0 .8750E0
136 | 47.7000E0 1.0000E0
137 | 37.8000E0 1.2500E0
138 | 20.2000E0 2.2500E0
139 | 21.0700E0 2.2500E0
140 | 13.8700E0 2.7500E0
141 | 9.6700E0 3.2500E0
142 | 7.7600E0 3.7500E0
143 | 5.4400E0 4.2500E0
144 | 4.8700E0 4.7500E0
145 | 4.0100E0 5.2500E0
146 | 3.7500E0 5.7500E0
147 | 24.1900E0 3.0000E0
148 | 25.7600E0 3.0000E0
149 | 18.0700E0 3.0000E0
150 | 11.8100E0 3.0000E0
151 | 12.0700E0 3.0000E0
152 | 16.1200E0 3.0000E0
153 | 70.8000E0 .5000E0
154 | 54.7000E0 .7500E0
155 | 48.0000E0 1.0000E0
156 | 39.8000E0 1.5000E0
157 | 29.8000E0 2.0000E0
158 | 23.7000E0 2.5000E0
159 | 29.6200E0 2.0000E0
160 | 23.8100E0 2.5000E0
161 | 17.7000E0 3.0000E0
162 | 11.5500E0 4.0000E0
163 | 12.0700E0 5.0000E0
164 | 8.7400E0 6.0000E0
165 | 80.7000E0 .5000E0
166 | 61.3000E0 .7500E0
167 | 47.5000E0 1.0000E0
168 | 29.0000E0 1.5000E0
169 | 24.0000E0 2.0000E0
170 | 17.7000E0 2.5000E0
171 | 24.5600E0 2.0000E0
172 | 18.6700E0 2.5000E0
173 | 16.2400E0 3.0000E0
174 | 8.7400E0 4.0000E0
175 | 7.8700E0 5.0000E0
176 | 8.5100E0 6.0000E0
177 | 66.7000E0 .5000E0
178 | 59.2000E0 .7500E0
179 | 40.8000E0 1.0000E0
180 | 30.7000E0 1.5000E0
181 | 25.7000E0 2.0000E0
182 | 16.3000E0 2.5000E0
183 | 25.9900E0 2.0000E0
184 | 16.9500E0 2.5000E0
185 | 13.3500E0 3.0000E0
186 | 8.6200E0 4.0000E0
187 | 7.2000E0 5.0000E0
188 | 6.6400E0 6.0000E0
189 | 13.6900E0 3.0000E0
190 | 81.0000E0 .5000E0
191 | 64.5000E0 .7500E0
192 | 35.5000E0 1.5000E0
193 | 13.3100E0 3.0000E0
194 | 4.8700E0 6.0000E0
195 | 12.9400E0 3.0000E0
196 | 5.0600E0 6.0000E0
197 | 15.1900E0 3.0000E0
198 | 14.6200E0 3.0000E0
199 | 15.6400E0 3.0000E0
200 | 25.5000E0 1.7500E0
201 | 25.9500E0 1.7500E0
202 | 81.7000E0 .5000E0
203 | 61.6000E0 .7500E0
204 | 29.8000E0 1.7500E0
205 | 29.8100E0 1.7500E0
206 | 17.1700E0 2.7500E0
207 | 10.3900E0 3.7500E0
208 | 28.4000E0 1.7500E0
209 | 28.6900E0 1.7500E0
210 | 81.3000E0 .5000E0
211 | 60.9000E0 .7500E0
212 | 16.6500E0 2.7500E0
213 | 10.0500E0 3.7500E0
214 | 28.9000E0 1.7500E0
215 | 28.9500E0 1.7500E0
216 |
217 |
--------------------------------------------------------------------------------
/modules/incanter-io/src/incanter/io.clj:
--------------------------------------------------------------------------------
1 | ;;; io.clj -- Data I/O library for Clojure built on CSVReader
2 |
3 | ;; by David Edgar Liebke http://incanter.org
4 | ;; March 11, 2009
5 |
6 | ;; Copyright (c) David Edgar Liebke, 2009. All rights reserved. The use
7 | ;; and distribution terms for this software are covered by the Eclipse
8 | ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
9 | ;; which can be found in the file epl-v10.htincanter.at the root of this
10 | ;; distribution. By using this software in any fashion, you are
11 | ;; agreeing to be bound by the terms of this license. You must not
12 | ;; remove this notice, or any other, from this software.
13 |
14 | ;; CHANGE LOG
15 | ;; March 11, 2009: First version
16 |
17 |
18 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19 | ;; DATA IO FUNCTIONS
20 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21 |
22 | (ns
23 | ^{:doc
24 | "Library for reading and writing Incanter datasets and matrices."}
25 |
26 | incanter.io
27 | (:import (java.io FileReader FileWriter File)
28 | (au.com.bytecode.opencsv CSVReader))
29 | (:use [incanter.core :only (dataset save get-input-reader)]))
30 |
31 | (defn- parse-string [value]
32 | (if (re-matches #"\d+" value)
33 | (try (Integer/parseInt value)
34 | (catch NumberFormatException _ value))
35 | (try (Double/parseDouble value)
36 | (catch NumberFormatException _ value))))
37 |
38 |
39 | (defn read-dataset
40 | "
41 | Returns a dataset read from a file or a URL.
42 |
43 | Options:
44 | :delim (default \\,), other options (\\tab \\space \\| etc)
45 | :quote (default \\\") character used for quoting strings
46 | :skip (default 0) the number of lines to skip at the top of the file.
47 | :header (default false) indicates the file has a header line
48 | :compress-delim (default true if delim = \\space, false otherwise) means
49 | compress multiple adjacent delimiters into a single delimiter
50 | "
51 | ([filename & {:keys [delim keyword-headers quote skip header compress-delim]
52 | :or {delim \,
53 | quote \"
54 | skip 0
55 | header false
56 | keyword-headers true}}]
57 | (let [compress-delim? (or compress-delim (if (= delim \space) true false))]
58 | (with-open [reader ^CSVReader (CSVReader.
59 | (get-input-reader filename)
60 | delim
61 | quote
62 | skip)]
63 | (let [data-lines (map seq (seq (.readAll reader)))
64 | raw-data (filter #(> (count (filter (fn [field] (not= field "")) %)) 0)
65 | (if compress-delim?
66 | (map (fn [line] (filter #(not= % "") line)) data-lines)
67 | data-lines))
68 | parsed-data (into [] (map (fn [row] (into [] (map parse-string row)))
69 | raw-data))]
70 | (if header
71 | ; have header row
72 | (dataset (if keyword-headers
73 | (map keyword (first parsed-data))
74 | (first parsed-data))
75 | (rest parsed-data))
76 | ; no header row so build a default one
77 | (let [col-count (count (first parsed-data))
78 | col-names (apply vector (map str
79 | (repeat col-count "col")
80 | (iterate inc 0)))]
81 | (dataset (if keyword-headers
82 | (map keyword col-names)
83 | col-names)
84 | parsed-data))))))))
85 |
86 |
87 |
88 |
89 | (defmethod save incanter.Matrix [mat filename & {:keys [delim header append]
90 | :or {append false delim \,}}]
91 | (let [file-writer (java.io.FileWriter. filename append)]
92 | (do
93 | (when (and header (not append))
94 | (.write file-writer (str (first header)))
95 | (doseq [column-name (rest header)]
96 | (.write file-writer (str delim column-name)))
97 | (.write file-writer (str \newline)))
98 | (doseq [row mat]
99 | (if (number? row)
100 | (.write file-writer (str row \newline))
101 | (do
102 | (.write file-writer (str (first row)))
103 | (doseq [column (rest row)]
104 | (.write file-writer (str delim column)))
105 | (.write file-writer (str \newline)))))
106 | (.flush file-writer)
107 | (.close file-writer))))
108 |
109 |
110 |
111 |
112 | (defmethod save :incanter.core/dataset [dataset filename & {:keys [delim header append]
113 | :or {append false delim \,}}]
114 | (let [header (or header (map #(if (keyword? %) (name %) %) (:column-names dataset)))
115 | file-writer (java.io.FileWriter. filename append)
116 | rows (:rows dataset)
117 | columns (:column-names dataset)]
118 | (do
119 | (when (and header (not append))
120 | (.write file-writer (str (first header)))
121 | (doseq [column-name (rest header)]
122 | (.write file-writer (str delim column-name)))
123 | (.write file-writer (str \newline)))
124 | (doseq [row rows]
125 | (do
126 | (.write file-writer (str (row (first columns))))
127 | (doseq [column-name (rest columns)]
128 | (.write file-writer (str delim (row column-name))))
129 | (.write file-writer (str \newline))))
130 | (.flush file-writer)
131 | (.close file-writer))))
132 |
133 |
134 | (defmethod save java.awt.image.BufferedImage
135 | ([img filename & {:keys [format] :or {format "png"}}]
136 | (javax.imageio.ImageIO/write img
137 | format
138 | (.getAbsoluteFile (java.io.File. filename)))))
139 |
140 |
141 |
142 |
143 |
144 |
--------------------------------------------------------------------------------
/examples/bayes.clj:
--------------------------------------------------------------------------------
1 |
2 | ;;; examples/bayes.clj -- Bayesian estimation library for Clojure
3 |
4 | ;; by David Edgar Liebke http://incanter.org
5 | ;; March 11, 2009
6 |
7 | ;; Copyright (c) David Edgar Liebke, 2009. All rights reserved. The use
8 | ;; and distribution terms for this software are covered by the Eclipse
9 | ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
10 | ;; which can be found in the file epl-v10.html at the root of this
11 | ;; distribution. By using this software in any fashion, you are
12 | ;; agreeing to be bound by the terms of this license. You must not
13 | ;; remove this notice, or any other, from this software.
14 |
15 | ;; CHANGE LOG
16 | ;; March 11, 2009: First version
17 |
18 |
19 |
20 | (ns examples.bayes
21 | (:use (incanter core stats)))
22 |
23 |
24 |
25 | (defn bayes-regression-noref [N x y]
26 | "
27 | This function implments the Gibbs sampling example using full conditional in OLS
28 | from Scott Lynch book 'Introduction to Applied Bayesian Statistics in the Social
29 | Sciences (page 171). This version is purely functional with no immutability.
30 | "
31 | (let [lm (linear-model y x :intercept false)
32 | pars (trans (:coefs lm))
33 | xtxi (solve (mmult (trans x) x))
34 | nx (ncol x)
35 | shape (/ (- (nrow x) (ncol x)) 2)]
36 | (loop [coefs (transient [[0 0 0 0 0 0 0 0 0]])
37 | variances (transient [1])
38 | i 0]
39 | (if (= i N)
40 | {:coef (matrix (persistent! coefs)) :var (persistent! variances)}
41 | (let [b (to-list (plus pars (mmult (trans (sample-normal nx))
42 | (decomp-cholesky (mult xtxi (variances i))))))
43 | resid (minus y (mmult x b))
44 | s2 (/ 1 (sample-gamma 1 :shape shape :rate (mult (mmult (trans resid) resid) 0.5) ))]
45 | (recur (conj! coefs b) (conj! variances s2) (inc i)))))))
46 |
47 |
48 |
49 |
50 | (defn bayes-regression-full [N x y]
51 | "
52 | This function implments the Gibbs sampling example using full conditional in OLS
53 | from Scott Lynch book 'Introduction to Applied Bayesian Statistics in the Social
54 | Sciences (page 171). This version uses immutability (i.e. references)
55 | "
56 | (let [lm (linear-model y x :intercept false)
57 | pars (trans (:coefs lm))
58 | xtxi (solve (mmult (trans x) x))
59 | nx (ncol x)
60 | b (ref [[0 0 0 0 0 0 0 0 0]])
61 | s2 (ref [1])
62 | resid (ref 0)
63 | shape (/ (- (nrow x) (ncol x)) 2)]
64 | (do
65 | (dotimes [i N]
66 | (dosync
67 | (alter b conj
68 | (to-list (plus pars (mmult (trans (sample-normal nx)) (decomp-cholesky (mult xtxi (@s2 i)))))))
69 | (ref-set resid (minus y (mmult x (@b (inc i)))))
70 | (alter s2 conj (/ 1 (sample-gamma 1 :shape shape :rate (mult (mmult (trans @resid) @resid) 0.5) )))))
71 | ;; return a map with the estimated coefficients and variances
72 | {:coef (matrix @b) :var @s2})))
73 |
74 |
75 |
76 |
77 | (defn bayes-regression [N x y]
78 | "
79 | This function implments the Gibbs sampling example using the composition method
80 | in OLS from Scott Lynch book 'Introduction to Applied Bayesian Statistics in the
81 | Social Sciences (page 173)
82 | "
83 | (let [lm (linear-model y x :intercept false)
84 | pars (:coefs lm)
85 | xtxi (solve (mmult (trans x) x))
86 | resid (:residuals lm)
87 | shape (/ (- (nrow x) (ncol x)) 2)
88 | rate (mult 1/2 (mmult (trans resid) resid))
89 | s-sq (div 1 (sample-gamma N :shape shape :rate rate))]
90 | ;; return a map with the estimated coefficients and variances
91 | {:coef
92 | (matrix
93 | ;(pmap ;; run a parallel map over the values of s-sq
94 | (map
95 | (fn [s2]
96 | (to-list (plus (trans pars)
97 | (mmult (trans (sample-normal (ncol x)))
98 | (decomp-cholesky (mult s2 xtxi))))))
99 | (to-list (trans s-sq))))
100 | :var s-sq}))
101 |
102 |
103 |
104 |
105 | (defn bayes-regression-mh [N x y]
106 | "
107 | This function implments the Gibbs sampling example using Metropolis Hastings
108 | in OLS from Scott Lynch book 'Introduction to Applied Bayesian Statistics in the
109 | Social Sciences (page 168)
110 | "
111 | (let [ lm (linear-model y x :intercept false)
112 | b-scale (to-list (div (sqrt (:std-errors lm)) 2))
113 | s2-scale (/ (sd (mult (:residuals lm) (div (dec (nrow x)) (- (nrow x) (ncol x))))) 2)
114 | post-fn (fn [x y b s-sq]
115 | (let [resid (minus y (mmult x b))]
116 | (plus (mult -1157.5 (log s-sq))
117 | (mult (div -0.5 s-sq) (mmult (trans resid) resid)))))
118 | reject? (fn [x y cand-b cand-s2 old-b old-s2]
119 | (< (- (post-fn x y cand-b cand-s2)
120 | (post-fn x y old-b old-s2))
121 | (log (rand))))
122 | ncol-x (ncol x)]
123 | (loop [coefs (transient [[0 0 0 0 0 0 0 0 0]])
124 | variances (transient [1])
125 | i 0]
126 | (if (= i N)
127 | {:coef (matrix (persistent! coefs)) :var (persistent! variances)}
128 | (let [old-b (coefs i)
129 | old-s2 (variances i)
130 | cand-b (into [] (map #(+ (sample-normal 1 :mean 0 :sd %1) %2) b-scale old-b))
131 | new-b (loop [b old-b j 0]
132 | (if (= j ncol-x)
133 | b
134 | (recur
135 | (if (reject? x y (assoc b j (cand-b j)) old-s2 old-b old-s2)
136 | b
137 | (assoc b j (cand-b j)))
138 | (inc j))))
139 | cand-s2 (+ (sample-normal 1 :mean 0 :sd s2-scale) old-s2)
140 | new-s2 (if (or (< cand-s2 0) (reject? x y new-b cand-s2 new-b old-s2))
141 | old-s2
142 | cand-s2)]
143 | (recur (conj! coefs new-b) (conj! variances new-s2) (inc i)))))))
144 |
145 |
146 |
--------------------------------------------------------------------------------
/modules/incanter-zoo/test/incanter/zoo_test.clj:
--------------------------------------------------------------------------------
1 | (ns incanter.zoo-test
2 | (:use clojure.test
3 | (incanter zoo core stats))
4 | (:require [clj-time.coerce :as c]))
5 |
6 |
7 | ;; --= Tests
8 | (def integers (iterate inc 0))
9 |
10 | (deftest roll-apply-test
11 | (is (= [3 6 9 12 15]
12 | (take 5 (roll-apply #(apply + %) 3 integers)))))
13 |
14 | (deftest roll-mean-test
15 | (is (= [2 3 4 5 6 7 8 9 10 11]
16 | (take 10 (roll-mean 5 integers)))))
17 |
18 | (deftest roll-median-test
19 | (is (= [2.0 3.0 4.0 5.0 6.0]
20 | (take 5 (roll-median 5 integers)))))
21 |
22 | (deftest roll-max-test
23 | (is (= [2 3 4 5 6]
24 | (take 5 (roll-max 3 integers)))))
25 |
26 | (deftest roll-min-test
27 | (is (= [0 1 2 3 4]
28 | (take 5 (roll-min 3 integers)))))
29 |
30 | ;; --------------------
31 | ;; Standard dataset, with :index column
32 | (def ds1 (to-dataset [{:index "2012-01-01" :temp 32 :press 100}
33 | {:index "2012-01-02" :temp 35 :press 98}
34 | {:index "2012-01-03" :temp 30 :press 102}
35 | {:index "2012-01-04" :temp 31 :press 103}
36 | {:index "2012-01-05" :temp 32 :press 104}
37 | {:index "2012-01-06" :temp 33 :press 105}]))
38 |
39 | ;; Standard dataset with :date column
40 | (def ds2 (to-dataset [{:date "2012-01-01" :temp 32 :press 100}
41 | {:date "2012-01-02" :temp 35 :press 98}
42 | {:date "2012-01-03" :temp 30 :press 102}
43 | {:date "2012-01-04" :temp 31 :press 103}
44 | {:date "2012-01-05" :temp 32 :press 104}
45 | {:date "2012-01-06" :temp 33 :press 105}]))
46 |
47 | ;; Data with missing values
48 | (def ds3 (to-dataset [{:date "2012-01-01" :temp 32}
49 | {:date "2012-01-02" :press 98 :temp 32}
50 | {:date "2012-01-06" :temp 33}]))
51 |
52 | (deftest zoo-test
53 | (is (zoo ds1))
54 | (is (zoo ds2 :date))
55 | (is (zoo ds3 :date))
56 | (testing "Check ordering"
57 | (let [ts (zoo (to-dataset [{:index "2012-01-01" :a 1}
58 | {:index "2012-01-02" :a 2}
59 | {:index "2012-01-03" :a 3}]))]
60 | (is (= ts
61 | (zoo (to-dataset [{:index "2012-01-01" :a 1}
62 | {:index "2012-01-03" :a 3}
63 | {:index "2012-01-02" :a 2}]))))
64 | (is (= ts
65 | (zoo (to-dataset [{:index "2012-01-03" :a 3}
66 | {:index "2012-01-02" :a 2}
67 | {:index "2012-01-01" :a 1}])))))))
68 |
69 | (deftest $$-test
70 | ;; Time slicing
71 | (let [ts1 (zoo ds1)]
72 | (testing "Single date slice"
73 | (is (= ($$ "2012-01-01" :temp ts1) 32) "Single col")
74 | (is (= ($$ "2012-01-01" :all ts1)
75 | [100 32 (to-date-time "2012-01-01")]) "Whole row")
76 | (is (= ($$ "2012-01-01" :all ts1)
77 | ($$ "2012-01-01" ts1)) "Single arity check")
78 | (is (= (nrow ($$ "2012-01-10" :all ts1)) 0) "Date out of range"))
79 |
80 | (testing "Date range slice"
81 | (let [slice ($$ "2012-01-01" "2012-01-03" :all ts1)]
82 | (is (= 3 (nrow slice)))
83 | (is (= 3 (ncol slice))))
84 | (is (= ($$ "2012-01-01" "2012-01-03" :temp ts1) [32 35 30]) "Single col"))
85 |
86 | (testing "Native Joda as index"
87 | (is (= ($$ (c/from-string "2012-01-03") :temp ts1) 30)))
88 |
89 | (testing "End point overlaps"
90 | (is (= ($$ "2012-01-01" "2012-01-06" :all ts1)
91 | ($$ "2012-01-01" "2012-06-10" :all ts1)) "RHS")
92 | (is (= ($$ "2012-01-01" "2012-01-06" :all ts1)
93 | ($$ "2011-01-01" "2012-06-10" :all ts1)) "LHS&RHS"))))
94 |
95 | (deftest aligned?-test
96 | (let [ts1 (zoo ds1)
97 | ts2 (zoo ds2 :date)]
98 | (aligned? ts1 ts2)
99 | (is (not (aligned? ts1
100 | (zoo (to-dataset [{:index "2012-01-01" :a 1}
101 | {:index "2012-01-02" :a 2}]))))
102 | "Unequal length")
103 | (is (not (aligned? (zoo (to-dataset [{:index "2012-01-01" :a 1}
104 | {:index "2012-01-02" :a 2}]))
105 | (zoo (to-dataset [{:index "2012-01-02" :a 1}
106 | {:index "2012-01-03" :a 2}]))))
107 | "Different indices")))
108 |
109 | (deftest within-zoo?-test
110 | (let [ts1 (zoo ds1)]
111 | (is (within-zoo? "2012-01-01" ts1))
112 | (is (within-zoo? "2012-01-04" ts1))
113 | (is (not (within-zoo? "2012-01-11" ts1)))
114 | (is (within-zoo? (c/from-string "2012-01-02T01:03:03") ts1) "Needn't be same frequency")))
115 |
116 | (deftest lag-test
117 | (let [ts1 (zoo ds1)
118 | ls1 (lag ts1)]
119 | (is (aligned? ts1 ls1) "Indices equal")
120 | (is (aligned? ts1 (lag ls1)) "Indices equal")
121 | (is (aligned? ts1 (lag ts1 3)) "Indices equal")
122 |
123 | (is (= ($ 0 [:press :temp] ls1)
124 | [nil nil]) "Nil pad the front values")
125 | (is (= ($ 0 [:press :temp] ts1)
126 | ($ 1 [:press :temp] ls1)) "Second vals of lag = first of ts")
127 | (is (= ($ 0 [:press :temp] ts1)
128 | ($ 2 [:press :temp] (lag ts1 2))) "Multiperiod lag")
129 | (is (= ($ 2 [:press :temp] (-> ts1 lag lag))
130 | ($ 2 [:press :temp] (lag ts1 2))) "Multiperiod lag")))
131 |
132 | (deftest zoo-apply-test
133 | (let [ts (zoo ds1)
134 | zs (zoo-apply #(apply min %) 2 ts :temp)]
135 | (aligned? ts zs)
136 | (is (= (rest ($ :temp zs))
137 | (roll-min 2 ($ :temp ts))))))
138 |
139 | ;; Helper for test below
140 | (defn- map-diff
141 | "Return a map with the different values of v2 - v1"
142 | [m1 m2]
143 | (into {} (map (fn [[k1 v1] [k2 v2]]
144 | (if (and v1 v2)
145 | {k2 (- v1 v2)}
146 | {k2 nil}))
147 | m1 m2)))
148 |
149 | (deftest zoo-row-map-test
150 | (let [ts1 (zoo ds1)
151 | ms1 (zoo-row-map map-diff ts1 ts1)
152 | ms2 (zoo-row-map map-diff ts1 (lag ts1))]
153 | (is (every? (partial = 0) ($ :temp ms1)))
154 | ms2
155 | (is (= ($ :press ms2) [nil -2 4 1 1 1]))))
156 |
157 |
--------------------------------------------------------------------------------
/modules/incanter-excel/src/incanter/excel.clj:
--------------------------------------------------------------------------------
1 | (ns
2 | ^{
3 | :doc "Excel module for reading and writing Incanter datasets. Recognises both old and new
4 | Excel file formats (.xls and .xlsx)."
5 | :author "David James Humphreys"}
6 | incanter.excel
7 | (:import
8 | [java.io FileOutputStream FileInputStream])
9 | (:use
10 | [incanter.core :only [dataset get-input-stream dataset?]]
11 | [incanter.excel.cells :only [read-line-values write-line-values]]
12 | [incanter.excel.workbook :only [get-workbook-sheet make-workbook-map write-workbook create-workbook-object create-sheet get-all-sheets]]))
13 |
14 | (defn- commit-sheet!
15 | "Internally save the dataset into the :sheet object."
16 | [workbook-blob dataset use-bold]
17 | (let [align-row (fn [row cols] (map #(get row %1) cols))]
18 | (write-line-values workbook-blob use-bold 0 (:column-names dataset))
19 | (dorun
20 | (map
21 | (partial write-line-values workbook-blob false)
22 | (iterate inc 1)
23 | (seq (map #(align-row % (:column-names dataset)) (:rows dataset)))))))
24 |
25 | (defmulti
26 | ^{:doc "Save a dataset to an Excel file. Can save in both older and newer
27 | Excel formats, uses the filename suffix or :override-format option.
28 |
29 | By passing in a collection of datasets and names it is possible to write more than
30 | one sheet at a time: e.g.
31 | (save-xls [\"first sheet\" dataset1 \"second\" dataset2] my-file)
32 |
33 | Options are:
34 | :sheet defaults to \"dataset\" if not provided.
35 | :use-bold defaults to true. Set the header line in bold.
36 | :override-format If nil use the filename suffix to guess the Excel file format.
37 | If :xls or :xlsx override the suffix check.
38 |
39 | Examples:
40 | (use '(incanter core datasets excel))
41 | (save-xls (get-dataset :cars) \"/tmp/cars.xls\")
42 |
43 | "}
44 | save-xls
45 |
46 | (fn [dataset
47 | ^String filename
48 | & {:keys [use-bold sheet override-format]
49 | :or {use-bold true sheet "dataset" override-format nil}}]
50 | (if (dataset? dataset)
51 | :incanter.core/dataset
52 | :collection)))
53 |
54 | (defmethod
55 | save-xls
56 | :incanter.core/dataset
57 | [dataset
58 | ^String filename
59 | & {:keys [use-bold sheet override-format]
60 | :or {use-bold true sheet "dataset" override-format nil}}]
61 | (write-workbook
62 | (let [workbook-blob (make-workbook-map (create-workbook-object filename override-format) sheet)]
63 | (commit-sheet! workbook-blob dataset use-bold)
64 | (:workbook workbook-blob))
65 | filename))
66 |
67 | (defmethod
68 | save-xls
69 | :collection
70 | [datasets
71 | ^String filename
72 | & {:keys [use-bold override-format]
73 | :or {use-bold true override-format nil}}]
74 | (if (not (zero? (mod (count datasets) 2)))
75 | (throw (Exception. "dataset count must be even: a name then a dataset."))
76 | (write-workbook
77 | (loop [workbook-blob (make-workbook-map (create-workbook-object filename override-format))
78 | [sheet-name dataset & others] (doall datasets)]
79 | (if (and sheet-name dataset)
80 | (let [next-blob (create-sheet workbook-blob sheet-name)]
81 | (commit-sheet! next-blob dataset use-bold)
82 | (recur next-blob others))
83 | (:workbook workbook-blob)))
84 | filename)))
85 |
86 | (defn- read-sheet [rows-it header-keywords]
87 | (let [colnames (read-line-values (first rows-it))]
88 | (dataset
89 | (if header-keywords
90 | (map keyword colnames)
91 | colnames)
92 | (map
93 | read-line-values
94 | (rest rows-it)))))
95 |
96 | (defmulti
97 | ^{:doc "Read an Excel file into a dataset. Note: cells containing formulas will be
98 | empty upon import. Can read both older and newer Excel file formats, uses the filename suffix
99 | or :override-format option.
100 |
101 | Options are:
102 | :sheet either a String for the tab name or an int for the sheet index -- defaults to 0
103 | :header-keywords convert the incoming header line to keywords -- defaults to false (no conversion)
104 | :override-format If nil use the filename suffix to guess the Excel file format. If :xls
105 | or :xlsx override the suffix check.
106 | :all-sheets? true to try to read in all sheets of data (false by default).
107 |
108 | Examples:
109 | (use '(incanter core io excel))
110 | (view (read-xls \"http://incanter.org/data/aus-airline-passengers.xls\"))
111 |
112 | (use '(incanter core charts excel))
113 | ;; read .xls file of Australian airline passenger data from the 1950s.
114 | (with-data (read-xls \"http://incanter.org/data/aus-airline-passengers.xls\")
115 | (view $data)
116 | ;; time-series-plot needs time in millisecs
117 | ;; create a function, to-millis, to convert a sequence of Date objects
118 | ;; to a sequence of milliseconds
119 | (let [to-millis (fn [dates] (map #(.getTime %) dates))]
120 | (view (time-series-plot (to-millis ($ :date)) ($ :passengers)))))
121 |
122 | "}
123 |
124 | read-xls
125 | (fn [^String filename
126 | & {:keys [sheet header-keywords override-format all-sheets?]
127 | :or {sheet 0 header-keywords false override-format nil all-sheets? false}}]
128 | (if all-sheets?
129 | :collection
130 | :singleton)))
131 | (defmethod
132 | read-xls
133 | :singleton
134 | [^String filename
135 | & {:keys [sheet header-keywords override-format]
136 | :or {sheet 0 header-keywords false override-format nil}}]
137 | (with-open [in-fs (get-input-stream filename)]
138 | (let [rows-it (iterator-seq
139 | (. (get-workbook-sheet
140 | (create-workbook-object filename override-format in-fs)
141 | sheet)
142 | iterator))]
143 | (read-sheet rows-it header-keywords))))
144 |
145 | (defmethod
146 | read-xls
147 | :collection
148 | [^String filename
149 | & {:keys [sheet header-keywords override-format all-sheets?]
150 | :or {sheet 0 header-keywords false override-format nil all-sheets? false}}]
151 | (with-open [in-fs (get-input-stream filename)]
152 | (let [workbook (create-workbook-object filename override-format in-fs)]
153 | (if all-sheets?
154 | (for [current-sheet (get-all-sheets workbook)]
155 | (let [rows-it (iterator-seq (. current-sheet iterator))]
156 | (read-sheet rows-it header-keywords)))))))
157 |
--------------------------------------------------------------------------------
/examples/blog/kmeans.clj:
--------------------------------------------------------------------------------
1 |
2 | (use '(incanter core stats charts))
3 |
4 | ;(defn init-k-means
5 | ; ""
6 | ; ([k data]
7 | ; (let [dist-fn [a b] (sq (minus a b))
8 | ; ]
9 | ; (loop [
10 | ; dists []
11 | ; centroids [(sample data)]
12 | ; ]
13 | ; (if (= k (count centroids))
14 | ; centroids
15 | ; (recur
16 | ; (loop [])))))))
17 |
18 | (defn index-of [coll value]
19 | "
20 | Examples:
21 |
22 | (use '(incanter core stats))
23 |
24 | (def data [2 4 6 7 5 3 1])
25 | (index-of data (apply max data))
26 |
27 |
28 | (def data (diag (repeat 10 1)))
29 | (map #(index-of % (apply max %)) data)
30 |
31 | "
32 | (loop [i 0]
33 | (if (= value (nth coll i))
34 | i
35 | (recur (inc i)))))
36 |
37 |
38 | (defn indices-of [coll value]
39 | "
40 | Examples:
41 |
42 | (use '(incanter core stats))
43 |
44 | (def data [2 7 4 6 7 5 3 1])
45 | (indices-of data (apply max data))
46 |
47 |
48 | (def data (diag (repeat 10 1)))
49 | (map #(indices-of % (apply max %)) data)
50 |
51 | "
52 | (for [i (range (count coll)) :when (= value (nth coll i))] i))
53 |
54 |
55 | (defn k-means
56 | "
57 | Examples:
58 |
59 | (use 'incanter.datasets)
60 | (def iris (sel (to-matrix (get-dataset :iris)) :cols (range 4)))
61 |
62 | (def clusters (k-means iris 3))
63 | (dim (:dist-matrix clusters))
64 | (dim (:cluster-indices clusters))
65 |
66 | (count (first (:dist-matrix clusters)))
67 | (count (second (:dist-matrix clusters)))
68 | (count (nth (:dist-matrix clusters) 2))
69 |
70 | clusters
71 |
72 | "
73 | ([data k]
74 | (let [p (ncol data)
75 | W (diag (repeat p 1))
76 | dist (fn [a b]
77 | (mahalanobis-dist a
78 | :centroid (trans b)
79 | :W W))
80 | n (nrow data)]
81 | (loop [centroids (sample data :size k)
82 | last-members nil
83 | member-indices nil
84 | i 0]
85 | (let [last-members member-indices
86 | dist-mat (trans (map #(dist data %) centroids))
87 | ;dist-mat (trans
88 | ; (map #(mahalanobis-dist data %)
89 | ; (trans (sel data :rows %))
90 | member-indices (map #(index-of % (apply min %)) dist-mat)
91 | cluster-indices (map (fn [idx]
92 | (indices-of member-indices idx))
93 | (range k))
94 | centroids (matrix (map #(map mean (trans (sel data :rows %)) )
95 | cluster-indices))
96 | ]
97 | (if (= member-indices last-members)
98 | ;(if true
99 | { :dist-matrix dist-mat
100 | :cluster-indices cluster-indices
101 | :centroids centroids
102 | :member-indices member-indices
103 | :iterations i}
104 | (recur centroids last-members member-indices (inc i))))))))
105 |
106 |
107 |
108 | ;(trans (map #(mahalanobis-dist iris :centroid (trans %) :W (diag (repeat (ncol iris) 1))) (sample iris :size 3)))
109 |
110 |
111 |
112 |
113 | (defn k-means
114 | "
115 | Examples:
116 |
117 | (use 'incanter.datasets)
118 | (def iris (sel (to-matrix (get-dataset :iris)) :cols (range 4)))
119 |
120 | (def clusters (k-means iris 3))
121 | ;(:member-indices clusters)
122 | (partition 50 (:member-indices clusters))
123 | (:iterations clusters)
124 |
125 | ;; calculate average distance of all the observations are from
126 | ;; its cluster's centroid
127 | (:mean-sq-dist clusters)
128 |
129 |
130 | (def mahalanobis-clusters (k-means iris 3 :mahalanobis true))
131 | ;(:member-indices mahalanobis-clusters)
132 | (partition 50 (:member-indices mahalanobis-clusters))
133 | (:iterations mahalanobis-clusters)
134 |
135 | "
136 | ([data k & options]
137 | (let [opts (when options (apply assoc {} options))
138 | mahalanobis (:mahalanobis opts)
139 | p (ncol data)
140 | W (diag (repeat p 1))
141 | euclid-dist (fn [a b]
142 | (mahalanobis-dist
143 | a
144 | :centroid (trans b)
145 | :W W))
146 | n (nrow data)
147 | mean-sq-dist (fn [centroids membership]
148 | (mean (map (fn [obs clust-idx]
149 | (sum
150 | (sq
151 | (minus obs
152 | (sel centroids
153 | :rows clust-idx)))))
154 | data
155 | membership)))]
156 | (loop [centroids (sample data :size k)
157 | dist-mat (trans (map #(euclid-dist data %) centroids)) ;; euclidean to init
158 | last-members nil
159 | member-indices nil
160 | i 0]
161 | (let [last-members member-indices
162 | member-indices (map #(index-of % (apply min %)) dist-mat)
163 | cluster-indices (map (fn [idx]
164 | (indices-of member-indices idx))
165 | (range k))
166 | centroids (matrix (map #(map mean (trans (sel data :rows %)) )
167 | cluster-indices))
168 | dist-mat (if mahalanobis
169 | (trans
170 | (map (fn [row] (mahalanobis-dist data :y row))
171 | (map #(matrix (sel data :rows %))
172 | cluster-indices)))
173 | ;; else euclidean
174 | (trans (map #(euclid-dist data %) centroids)))
175 | ]
176 | (if (= member-indices last-members)
177 | ;(if true
178 | { :dist-matrix dist-mat
179 | :cluster-indices cluster-indices
180 | :centroids centroids
181 | :member-indices member-indices
182 | :iterations i
183 | :mean-sq-dist (mean-sq-dist centroids member-indices)}
184 | (recur centroids dist-mat last-members member-indices (inc i))))))))
185 |
186 |
187 |
188 |
--------------------------------------------------------------------------------
/modules/incanter-core/src/incanter/censored.clj:
--------------------------------------------------------------------------------
1 | (ns incanter.censored
2 | (:use [incanter.core :only (mult pow)]
3 | [incanter.stats :only (pdf-normal cdf-normal sd)]))
4 |
5 |
6 | (defn- lambda-two-sided
7 | ([a-std b-std]
8 | (/ (- (pdf-normal a-std) (pdf-normal b-std))
9 | (- (cdf-normal b-std) (cdf-normal a-std)))))
10 |
11 | (defn- lambda-lower
12 | ([a-std] (/ (pdf-normal a-std) (- 1 (cdf-normal a-std)))))
13 |
14 | (defn- lambda-upper
15 | ([b-std]
16 | (/ (- (pdf-normal b-std)) (cdf-normal b-std))))
17 |
18 | (defn- psi [y mu sigma] (/ (- y mu) sigma))
19 |
20 |
21 | (defn censored-mean-two-sided
22 | " Returns the mean of a normal distribution (with mean mu and standard
23 | deviation sigma) with the lower tail censored at 'a' and the upper
24 | tail censored at 'b'
25 | "
26 | ([a b mu sigma]
27 | (let [a-std (psi a mu sigma)
28 | b-std (psi b mu sigma)
29 | cdf-a (cdf-normal a-std)
30 | cdf-b (cdf-normal b-std)]
31 | (+ (* cdf-a a)
32 | (* (- cdf-b cdf-a) (+ mu (* sigma (lambda-two-sided a-std b-std))))
33 | (* (- 1 cdf-b) b)))))
34 |
35 |
36 | (defn censored-variance-two-sided
37 | " Returns the variance of a normal distribution (with mean mu and standard
38 | deviation sigma) with the lower tail censored at 'a' and the upper
39 | tail censored at 'b'
40 | "
41 | ([a b mu sigma]
42 | (let [a-std (psi a mu sigma)
43 | b-std (psi b mu sigma)
44 | sigma-sq (* sigma sigma)
45 | Ey (censored-mean-two-sided a b mu sigma)
46 | cdf-a (cdf-normal a-std)
47 | cdf-b (cdf-normal b-std)]
48 | (+ (* cdf-a (* a a))
49 | (* (- cdf-b cdf-a)
50 | (+ sigma-sq (* mu mu) (* 2 mu sigma (lambda-two-sided a-std b-std))))
51 | (* sigma-sq (- (* a-std (pdf-normal a-std)) (* b-std (pdf-normal b-std))))
52 | (- (* (- 1 cdf-b) (* b b)) (* Ey Ey))))))
53 |
54 |
55 |
56 | (defn censored-mean-lower
57 | " Returns the mean of a normal distribution (with mean mu and standard
58 | deviation sigma) with the lower tail censored at 'a'
59 | "
60 | ([a mu sigma]
61 | (let [a-std (psi a mu sigma)]
62 | (+ (* (cdf-normal a-std) a)
63 | (* (- 1 (cdf-normal a-std))
64 | (+ mu (* sigma (lambda-lower a-std))))))))
65 |
66 |
67 | (defn censored-variance-lower
68 | " Returns the variance of a normal distribution (with mean mu and standard
69 | deviation sigma) with the lower tail censored at 'a'
70 | "
71 | ([a mu sigma]
72 | (let [a-std (psi a mu sigma)
73 | cdf-a (cdf-normal a-std)
74 | sigma-sq (* sigma sigma)
75 | Ey (censored-mean-lower a mu sigma)]
76 | (+ (* cdf-a (* a a))
77 | (* (- 1 cdf-a)
78 | (+ sigma-sq (* mu mu) (* 2 mu sigma (lambda-lower a-std))))
79 | (* sigma-sq a-std (pdf-normal a-std))
80 | (- (* Ey Ey))))))
81 |
82 |
83 |
84 | (defn censored-mean-upper
85 | " Returns the mean of a normal distribution (with mean mu and standard
86 | deviation sigma) with the upper tail censored at 'b'
87 | "
88 | ([b mu sigma]
89 | (let [b-std (psi b mu sigma)]
90 | (+ (* (cdf-normal b-std) (+ mu (* sigma (lambda-upper b-std))))
91 | (* (- 1 (cdf-normal b-std)) b)))))
92 |
93 |
94 | (defn censored-variance-upper
95 | " Returns the variance of a normal distribution (with mean mu and standard
96 | deviation sigma) with the upper tail censored at 'b'
97 | "
98 | ([b mu sigma]
99 | (let [sigma-sq (* sigma sigma)
100 | b-std (psi b mu sigma)
101 | cdf-b (cdf-normal b-std)
102 | Ey (censored-mean-upper b mu sigma)]
103 | (+ (* cdf-b
104 | (+ sigma-sq (* mu mu) (* 2 mu sigma (lambda-upper b-std))))
105 | (- (* sigma-sq b-std (pdf-normal b-std)))
106 | (* (- 1 cdf-b) (* b b))
107 | (- (* Ey Ey))))))
108 |
109 |
110 |
111 | (defn truncated-variance
112 | " Returns the variance of a normal distribution truncated at a and b.
113 |
114 | Options:
115 | :mean (default 0) mean of untruncated normal distribution
116 | :sd (default 1) standard deviation of untruncated normal distribution
117 | :a (default -infinity) lower truncation point
118 | :b (default +infinity) upper truncation point
119 |
120 | Examples:
121 |
122 | (use '(incanter core stats))
123 | (truncated-variance :a -1.96 :b 1.96)
124 | (truncated-variance :a 0)
125 | (truncated-variance :b 0)
126 |
127 | (use 'incanter.charts)
128 | (def x (range -3 3 0.1))
129 | (def plot (xy-plot x (map #(truncated-variance :a %) x)))
130 | (view plot)
131 | (add-lines plot x (map #(truncated-variance :b %) x))
132 |
133 | (def samp (sample-normal 10000))
134 | (add-lines plot x (map #(variance (filter (fn [s] (> s %)) samp)) x))
135 | (add-lines plot x (map #(variance (mult samp (indicator (fn [s] (> s %)) samp))) x))
136 |
137 |
138 | References:
139 | DeMaris, A. (2004) Regression with social data: modeling continuous and limited response variables.
140 | Wiley-IEEE.
141 |
142 | http://en.wikipedia.org/wiki/Truncated_normal_distribution
143 | "
144 | ([& {:keys [mean sd a b]
145 | :or {mean 0
146 | sd 1
147 | a Double/NEGATIVE_INFINITY
148 | b Double/POSITIVE_INFINITY}}]
149 | (let [sigma-sq (* sd sd)
150 | lambda (fn [alpha] (/ (pdf-normal alpha) (- 1 (cdf-normal alpha))))
151 | delta (fn [alpha] (* (lambda alpha) (- (lambda alpha) alpha)))
152 | ;one-tail-var (fn [alpha s-sq]
153 | ; (* s-sq
154 | ; (- 1 (cdf-normal alpha))
155 | ; (+ (- 1 (delta alpha))
156 | ; (* (pow (- alpha (lambda alpha)) 2)
157 | ; (cdf-normal alpha)))))
158 | a-std (if (= a Double/NEGATIVE_INFINITY) Double/NEGATIVE_INFINITY (/ (- a mean) sd))
159 | b-std (if (= b Double/POSITIVE_INFINITY) Double/POSITIVE_INFINITY (/ (- b mean) sd))
160 | pdf-a (if (= a Double/NEGATIVE_INFINITY) 0 (pdf-normal a-std))
161 | pdf-b (if (= b Double/POSITIVE_INFINITY) 0 (pdf-normal b-std))
162 | cdf-a (if (= a Double/NEGATIVE_INFINITY) 0 (cdf-normal a-std))
163 | cdf-b (if (= b Double/POSITIVE_INFINITY) 1 (cdf-normal b-std))]
164 | (cond
165 | (and (= b Double/POSITIVE_INFINITY) (= a Double/NEGATIVE_INFINITY))
166 | sigma-sq
167 | (and (= b Double/POSITIVE_INFINITY) (> a Double/NEGATIVE_INFINITY))
168 | (* sigma-sq (- 1 (delta a-std)))
169 | ;(one-tail-var a-std sigma-sq)
170 | (and (= a Double/NEGATIVE_INFINITY) (< b Double/POSITIVE_INFINITY))
171 | (* sigma-sq (- 1 (delta (- 1 b-std))))
172 | ;(- sigma-sq (one-tail-var b-std sigma-sq))
173 | :else
174 | (* sigma-sq
175 | (+ 1 (/ (- (* a-std pdf-a) (* b-std pdf-b))
176 | (- cdf-b cdf-a))
177 | (- (pow (/ (- pdf-a pdf-b) (- cdf-b cdf-a)) 2))))))))
178 |
--------------------------------------------------------------------------------
/examples/mixture_em.clj:
--------------------------------------------------------------------------------
1 |
2 |
3 | (use '(incanter core stats charts))
4 |
5 | (def data [-0.39 0.12 0.94 1.67 1.76 2.44 3.72 4.28 4.92 5.53
6 | 0.06 0.48 1.01 1.68 1.80 3.25 4.12 4.60 5.28 6.22])
7 |
8 |
9 | (def mu-hats (sample data :size 2 :replacement false))
10 | ;(def mu-hats [4.5 1.0])
11 | ;(def mu-hats [5 1])
12 | (def sigma-hats [(variance data) (variance data)])
13 | ;(def sigma-hats [0.9 0.8])
14 | (def pi-hat 0.6)
15 |
16 |
17 | (defn next-gamma-hats [data mu-hats sigma-hats pi-hat]
18 | (map (fn [y_i]
19 | (/ (* pi-hat (pdf-normal y_i :mean (second mu-hats) :sd (sqrt (second sigma-hats))))
20 | (+ (* (- 1 pi-hat) (pdf-normal y_i :mean (first mu-hats) :sd (sqrt (first sigma-hats))))
21 | (* pi-hat (pdf-normal y_i :mean (second mu-hats) :sd (sqrt (second sigma-hats)))))))
22 | data))
23 |
24 | (defn next-mu-hats [data gamma-hats]
25 | [(/ (sum (mult (minus 1 gamma-hats) data))
26 | (sum (minus 1 gamma-hats)))
27 | (/ (sum (mult gamma-hats data))
28 | (sum gamma-hats))])
29 |
30 | (defn next-sigma-hats [data gamma-hats]
31 | [(/ (sum (mult (minus 1 gamma-hats) (sq (minus data (first mu-hats)))))
32 | (sum (minus 1 gamma-hats)))
33 | (/ (sum (mult gamma-hats (sq (minus data (second mu-hats)))))
34 | (sum gamma-hats))])
35 |
36 | (defn next-pi-hat [gamma-hats]
37 | (div (sum gamma-hats) (count gamma-hats)))
38 |
39 |
40 | (defn log-likelihood [data mu-hats sigma-hats pi-hat gamma-hats]
41 | (+ (sum (map (fn [y g]
42 | (plus (mult (minus 1 g) (log (pdf-normal y :mean (first mu-hats) :sd (sqrt (first sigma-hats)))))
43 | (mult g (log (pdf-normal y :mean (second mu-hats) :sd (sqrt (second sigma-hats)))))))
44 | data gamma-hats))
45 | (sum (map (fn [y g]
46 | (plus (mult (minus 1 g) (log (- 1 pi-hat))) (mult g (log pi-hat))))
47 | data gamma-hats))))
48 |
49 |
50 | (def results
51 | (pmap (fn [j]
52 | (let [mu-hats (sample data :size 2 :replacement false)]
53 | ;; main loop
54 | (loop [i 0
55 | m mu-hats
56 | s sigma-hats
57 | p pi-hat]
58 | (let [g (next-gamma-hats data m s p)
59 | m-tmp (next-mu-hats data g)
60 | s-tmp (next-sigma-hats data g)
61 | p-tmp (next-pi-hat g)
62 | diff [(minus m m-tmp) (minus s s-tmp) (minus p p-tmp)]]
63 | ;(if (or (= [0.1 0.1 0.1] diff) (= i 20))
64 | (if (= i 50)
65 | ;[i m s p diff]
66 | [i m s p (log-likelihood data m s p g)]
67 | (do
68 | ;(println [i m s p (log-likelihood data m s p g)])
69 | ;(println diff)
70 | (recur (inc i)
71 | (next-mu-hats data g)
72 | (next-sigma-hats data g)
73 | (next-pi-hat g))))))))
74 | (range 500)))
75 |
76 | (defn max-index
77 | "Returns the index of the maximum value in the given sequence."
78 | ([x]
79 | (let [max-x (reduce max x)
80 | n (length x)]
81 | (loop [i 0]
82 | (if (= (nth x i) max-x)
83 | i
84 | (recur (inc i)))))))
85 |
86 |
87 |
88 | (nth results (max-index (map #(nth % 4) results)))
89 |
90 |
91 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
92 | ;; Mixture model for heart disease data
93 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
94 |
95 |
96 | (use '(incanter core stats charts io))
97 |
98 | ;; info available at: http://www-stat.stanford.edu/~tibs/ElemStatLearn/datasets/SAheart.info
99 |
100 | (def heart-data (to-matrix
101 | (read-dataset "http://www-stat.stanford.edu/~tibs/ElemStatLearn/datasets/SAheart.data" :header true)))
102 |
103 | (def groups (group-by heart-data 10 :cols [9 10]))
104 | (view (histogram (sel (first groups) :cols 0)))
105 | (view (histogram (sel (second groups) :cols 0)))
106 | (view (histogram (sel heart-data :cols 9)))
107 |
108 | (def data (sel heart-data :cols 9))
109 |
110 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
111 | (def sigma-hats [(variance data) (variance data)])
112 | ;(def sigma-hats [0.9 0.8])
113 | (def pi-hat 0.5)
114 |
115 |
116 | (defn next-gamma-hats [data mu-hats sigma-hats pi-hat]
117 | (map (fn [y_i]
118 | (/ (* pi-hat (pdf-normal y_i :mean (second mu-hats) :sd (sqrt (second sigma-hats))))
119 | (+ (* (- 1 pi-hat) (pdf-normal y_i :mean (first mu-hats) :sd (sqrt (first sigma-hats))))
120 | (* pi-hat (pdf-normal y_i :mean (second mu-hats) :sd (sqrt (second sigma-hats)))))))
121 | data))
122 |
123 | (defn next-mu-hats [data gamma-hats]
124 | [(/ (sum (mult (minus 1 gamma-hats) data))
125 | (sum (minus 1 gamma-hats)))
126 | (/ (sum (mult gamma-hats data))
127 | (sum gamma-hats))])
128 |
129 | (defn next-sigma-hats [data mu-hats gamma-hats]
130 | [(/ (sum (mult (minus 1 gamma-hats) (sq (minus data (first mu-hats)))))
131 | (sum (minus 1 gamma-hats)))
132 | (/ (sum (mult gamma-hats (sq (minus data (second mu-hats)))))
133 | (sum gamma-hats))])
134 |
135 | (defn next-pi-hat [gamma-hats]
136 | (div (sum gamma-hats) (count gamma-hats)))
137 |
138 |
139 | (defn log-likelihood [data mu-hats sigma-hats pi-hat gamma-hats]
140 | (+ (sum (map (fn [y g]
141 | (plus (mult (minus 1 g) (log (pdf-normal y :mean (first mu-hats) :sd (sqrt (first sigma-hats)))))
142 | (mult g (log (pdf-normal y :mean (second mu-hats) :sd (sqrt (second sigma-hats)))))))
143 | data gamma-hats))
144 | (sum (map (fn [y g]
145 | (plus (mult (minus 1 g) (log (- 1 pi-hat))) (mult g (log pi-hat))))
146 | data gamma-hats))))
147 |
148 |
149 | (def results
150 | (pmap (fn [j]
151 | (let [mu-hats (sample data :size 2 :replacement false)]
152 | ;; main loop
153 | (loop [i 0
154 | m mu-hats
155 | s sigma-hats
156 | p pi-hat]
157 | (let [g (next-gamma-hats data m s p)
158 | m-tmp (next-mu-hats data g)
159 | s-tmp (next-sigma-hats data m g)
160 | p-tmp (next-pi-hat g)
161 | diff [(minus m m-tmp) (minus s s-tmp) (minus p p-tmp)]]
162 | ;(if (or (= [0.1 0.1 0.1] diff) (= i 20))
163 | (if (= i 200)
164 | ;[i m s p diff]
165 | [i m s p (log-likelihood data m s p g)]
166 | (do
167 | ;(println [i m s p (log-likelihood data m s p g)])
168 | ;(println diff)
169 | (recur (inc i)
170 | (next-mu-hats data g)
171 | (next-sigma-hats data m g)
172 | (next-pi-hat g)))))) ))
173 | (range 50)))
174 |
175 |
176 | (defn max-index
177 | "Returns the index of the maximum value in the given sequence."
178 | ([x]
179 | (let [max-x (reduce max x)
180 | n (length x)]
181 | (loop [i 0]
182 | (if (= (nth x i) max-x)
183 | i
184 | (recur (inc i)))))))
185 |
186 |
187 |
188 | (println (nth results (max-index (map #(nth % 4) results))))
189 |
190 |
191 |
--------------------------------------------------------------------------------
/examples/blog/signif_testing.clj:
--------------------------------------------------------------------------------
1 | ;; from the following blog post:
2 | ;; http://incanter-blog.org/2009/06/17/randomization-significance/
3 |
4 | ;; significance testing with randomization
5 |
6 | (use '(incanter core stats datasets charts))
7 | (def data (to-matrix (get-dataset :plant-growth)))
8 |
9 | ;; Break the first column of the data into groups based on
10 | ;; treatment type (second column) using the group-by function.
11 | (def groups (group-by data 1 :cols 0))
12 |
13 | (t-test (first groups) :y (second groups))
14 | ;; returns t-stat :t-stat 1.1912603818487033
15 |
16 | (t-test (first groups) :y (last groups))
17 | ;; returns t-stat -2.1340204531240676
18 |
19 |
20 |
21 | ;; calculate the means of the three groups
22 | (map mean groups)
23 |
24 |
25 | ;; perform the t-test comparing the first two groups
26 | (def t1 (t-test (first groups) :y (second groups)))
27 | (def t2 (t-test (first groups) :y (last groups)))
28 |
29 | ;; view the test statistic and its p-value
30 | (:t-stat t1) ;; returns 1.1912603818487033
31 | (:p-value t1) ;; returns 0.25038250858754796
32 |
33 | (:t-stat t2) ;; returns -2.134
34 | (:p-value t2) ;; returns 0.0479
35 |
36 |
37 | ;; The above p-value is based on the assumption that the
38 | ;; distribution of t-test statistics when the null hypothesis,
39 | ;; that the two sample means are identical, is true. Let's see
40 | ;; if that assumption appears correct for these samples.
41 |
42 | ;; Now create 1000 permuted versions of the original two groups using
43 | ;; the sample-permutations function,
44 | (def perm-groups1 (sample-permutations 1000
45 | (first groups)
46 | (second groups)))
47 |
48 | (def perm-groups2 (sample-permutations 1000
49 | (first groups)
50 | (last groups)))
51 |
52 |
53 | ;; create a function, based on t-test, that takes two sequence and returns
54 | ;; just the :t-stat value
55 | (defn t-stat [x y] (:t-stat (t-test x :y y)))
56 |
57 | ;; calculate the t-test statistics for each of the permuted versions of
58 | ;; the two samples
59 | (def t-stats1 (map t-stat
60 | (first perm-groups1)
61 | (second perm-groups1)))
62 |
63 | (def t-stats2 (map t-stat
64 | (first perm-groups2)
65 | (second perm-groups2)))
66 |
67 | ;; plot the t-test statistics, and overlay the pdf of a t-distribution
68 | (doto (histogram t-stats1 :density true :nbins 20)
69 | (add-function #(pdf-t % :df (:df t1)) -3 3)
70 | view)
71 |
72 | ;; and view their mean, sd, and 95% confidence interval
73 | (mean t-stats1)
74 | ;; returns 0.02308030751953895
75 | (sd t-stats1)
76 | ;; returns 1.0640114204888618
77 | (quantile t-stats1 :probs [0.025 0.975])
78 | ;; returns (-2.1164160713197497 2.002005620604495)
79 |
80 |
81 | (doto (histogram t-stats2 :density true :nbins 20)
82 | (add-function #(pdf-t % :df (:df t2)) -3 3)
83 | view)
84 |
85 |
86 | ;; and view their mean, sd, and 95% confidence interval
87 | (mean t-stats2)
88 | ;; returns -0.014
89 | (sd t-stats2)
90 | ;; returns 1.054
91 | (quantile t-stats2 :probs [0.025 0.975])
92 | ;; returns (-2.075 2.122)
93 |
94 |
95 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
96 | ;; do it with raw means-differences instead of the t-test statistic
97 |
98 | ;; Define a function to calculate the difference in means between two sequences.
99 | (defn means-diff [x y]
100 | (minus (mean x) (mean y)))
101 |
102 | ;; Calculate the difference in sample means between the two groups.
103 | (def samp-mean-diff (means-diff (first groups)
104 | (second groups)))
105 |
106 | ;; and calculate the difference of means of the 1000 samples by mapping
107 | ;; the means-diff function, defined earlier, over the rows returned by
108 | ;; sample-permutations.
109 | (def perm-means-diffs1 (map means-diff
110 | (first perm-groups)
111 | (second perm-groups)))
112 |
113 |
114 | ;; Then take the mean of this sequence, which gives the proportion of times
115 | ;; that a value from the permuted sequences are more extreme than the original
116 | ;; sample mean (i.e. the p-value).
117 | (mean (indicator #(> % samp-mean-diff)
118 | perm-means-diffs1))
119 |
120 | ;; Calculate a 95% interval of the permuted differences. If the original
121 | ;; sample means-difference is outside of this range, there is evidence
122 | ;; that the two means are statistically significantly different.
123 | (quantile perm-means-diffs1 :probs [0.025 0.975])
124 |
125 | ;; Plot a histogram of the perm-means-diffs using the density option,
126 | ;; instead of the default frequency, and then add a normal pdf curve
127 | ;; with the mean and sd of perm-means-diffs data for a visual comparison.
128 | (doto (histogram perm-means-diffs1 :density true)
129 | (add-lines (range -1 1 0.01)
130 | (pdf-normal (range -1 1 0.01)
131 | :mean (mean perm-means-diffs1)
132 | :sd (sd perm-means-diffs1)))
133 | view)
134 |
135 | ;; now standardize the means and overlay the pdf of a t-distribution
136 | (doto (histogram (sweep (sweep perm-means-diffs1) :stat sd :fun div) :density true)
137 | (add-lines (range -3 3 0.01)
138 | (pdf-t (range -3 3 0.01)
139 | :df 18))
140 | view)
141 |
142 |
143 | ;; The permuted data looks normal. Now, calculate the p-values for
144 | ;; the difference in means between the control and treatment two.
145 |
146 | (def perm-groups (sample-permutations 1000
147 | (first groups)
148 | (last groups)))
149 |
150 | (def perm-means-diffs2 (map means-diff
151 | (first perm-groups)
152 | (second perm-groups)))
153 |
154 | (def samp-mean-diff (means-diff (first groups)
155 | (last groups)))
156 |
157 | (mean (indicator #(< % samp-mean-diff)
158 | perm-means-diffs2))
159 |
160 | (quantile perm-means-diffs2
161 | :probs [0.025 0.975])
162 |
163 |
164 | ;; Finally, calculate the p-values for the difference in means
165 | ;; between the treatment one and treatment two.
166 |
167 | (def perm-groups (sample-permutations 1000
168 | (second groups)
169 | (last groups)))
170 |
171 | (def perm-means-diffs3 (map means-diff (first perm-groups)
172 | (second perm-groups)))
173 |
174 | (def samp-mean-diff (means-diff (second groups)
175 | (last groups)))
176 |
177 | (mean (indicator #(< % samp-mean-diff)
178 | perm-means-diffs3))
179 |
180 | (quantile perm-means-diffs3
181 | :probs [0.025 0.975])
182 |
183 |
184 | ;; Plot box-plots of the three perm-means-diffs sequences
185 | (doto (box-plot perm-means-diffs1)
186 | (add-box-plot perm-means-diffs2)
187 | (add-box-plot perm-means-diffs3)
188 | view)
189 |
190 |
--------------------------------------------------------------------------------
/modules/incanter-core/src/incanter/som.clj:
--------------------------------------------------------------------------------
1 | ;;; som.clj -- Self-Organizing-Map Neural Network Library
2 |
3 | ;; by David Edgar Liebke http://incanter.org
4 | ;; June 13, 2009
5 |
6 | ;; Copyright (c) David Edgar Liebke, 2009. All rights reserved. The use
7 | ;; and distribution terms for this software are covered by the Eclipse
8 | ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
9 | ;; which can be found in the file epl-v10.htincanter.at the root of this
10 | ;; distribution. By using this software in any fashion, you are
11 | ;; agreeing to be bound by the terms of this license. You must not
12 | ;; remove this notice, or any other, from this software.
13 |
14 |
15 |
16 | (ns incanter.som
17 | (:use [incanter.core :only (sel ncol nrow mult div plus minus trans to-vect sqrt sum pow)]
18 | [incanter.stats :only (mean principal-components covariance)]))
19 |
20 |
21 |
22 | (defn- som-dimensions
23 | ([pc1-sd pc2-sd]
24 | (let [dim-1 (mult 5 pc1-sd)
25 | dim-2 (mult (div pc2-sd pc1-sd) dim-1)]
26 | [dim-1 dim-2])))
27 |
28 |
29 | (defn- som-initialize-linear
30 | ([data]
31 | (let [pc (principal-components (covariance data))
32 | pc1-sd (nth (:std-dev pc) 0)
33 | pc2-sd (nth (:std-dev pc) 1)
34 | pc1 (sel (:rotation pc) :cols 0)
35 | pc2 (sel (:rotation pc) :cols 1)
36 | [dim-1 dim-2] (map #(Math/ceil %) (som-dimensions pc1-sd pc2-sd))
37 | data-mean (map mean (trans data))
38 | weight-fn (fn [i j data-mean pc1-sd dim-1 dim-2 pc1 pc2]
39 | (to-vect
40 | (plus data-mean
41 | (mult (mult 5 pc1-sd)
42 | (plus (mult pc1 (minus i (div dim-1 2)))
43 | (mult pc2 (minus j (div dim-2 2))))))))
44 | weights (reduce conj {}
45 | (for [i (range dim-1) j (range dim-2)]
46 | {[i j] (weight-fn i j data-mean pc1-sd dim-1 dim-2 pc1 pc2)} ))
47 | sets (reduce conj {}
48 | (for [i (range dim-1) j (range dim-2)]
49 | {[i j] #{}} ))]
50 | {:dims [dim-1 dim-2]
51 | :weights weights
52 | :sets sets
53 | :data-means data-mean})))
54 |
55 |
56 | (defn- dist-euclidean [x y] (sqrt (sum (pow (minus x y) 2))))
57 |
58 |
59 | (defn- get-distances
60 | ([x som]
61 | (reduce conj {}
62 | (pmap #(hash-map % (dist-euclidean x ((:weights som) %)))
63 | (keys (:weights som))))))
64 |
65 |
66 | (defn- get-min-dist
67 | ([x som]
68 | (let [distances (get-distances x som)
69 | min-dist-key (reduce #(if (<= (distances %1) (distances %2)) %1 %2)
70 | (keys distances))]
71 | {:index min-dist-key :dist (distances min-dist-key)} )))
72 |
73 |
74 | (defn- som-update-cells
75 | ([data som]
76 | (let [sets (loop [i 0 sets {}]
77 | (if (= i (nrow data))
78 | sets
79 | (let [{idx :index min-dist :dist} (get-min-dist (trans (nth data i)) som)]
80 | (recur (inc i) (assoc sets idx (conj (sets idx) i))))))]
81 | (assoc som :sets sets))))
82 |
83 |
84 | (defn- alpha-fn
85 | ([r total-cycles alpha-init]
86 | (max 0.01 (* alpha-init (- 1 (/ r total-cycles))))))
87 |
88 |
89 | (defn- beta-fn
90 | ([r beta-init]
91 | (max 0 (- beta-init r))))
92 |
93 |
94 | (defn- som-neighborhoods
95 | ([r dim-1 dim-2 total-cycles beta0]
96 | (reduce conj {}
97 | (for [i (range dim-1) j (range dim-2)]
98 | [[i j]
99 | (for [s1 (range (if (pos? (- i (beta-fn r beta0))) (- i (beta-fn r beta0)) 0)
100 | (if (<= (+ i (beta-fn r beta0)) dim-1) (+ i (beta-fn r beta0) 1) dim-1))
101 | s2 (range (if (pos? (- j (beta-fn r beta0))) (- j (beta-fn r beta0)) 0)
102 | (if (<= (+ j (beta-fn r beta0)) dim-2) (+ j (beta-fn r beta0) 1) dim-2))]
103 | [s1 s2])]))))
104 |
105 |
106 |
107 | (defn- som-update-weights [r total-cycles som alpha-init beta-init]
108 | (let [
109 | sets (:sets som)
110 | weights (:weights som)
111 | indices (keys weights)
112 | dims (:dims som)
113 | neighborhoods (som-neighborhoods r (first dims) (second dims) total-cycles beta-init)
114 | ]
115 | (assoc som :weights
116 | (reduce conj {}
117 | (pmap (fn [indx]
118 | {indx
119 | (plus (weights indx)
120 | (mult (alpha-fn r total-cycles alpha-init)
121 | (minus (if (pos? (count (sets indx)))
122 | (div (sum (sets indx))
123 | (count (sets indx)))
124 | 0)
125 | (weights indx))))} ) indices)))))
126 |
127 |
128 | (defn- som-fitness
129 | ([data som]
130 | (/ (sum (for [indx (keys (:weights som))]
131 | (sum (map #(dist-euclidean ((:weights som) indx) (trans (nth data %)))
132 | ((:sets som) indx)))))
133 | (nrow data))))
134 |
135 |
136 | (defn som-batch-train
137 | " Performs BL-SOM (batch-learning self organizing map) learning on
138 | the given data, returning a hashmap containing resulting BL-SOM
139 | values.
140 |
141 |
142 | Arguments:
143 | data -- data matrix
144 |
145 | Options:
146 | :cycles -- number of cycles of learning
147 | :alpha -- initial value of alpha learning parameter
148 | :beta -- initial value of beta learning parameter
149 |
150 |
151 | Returns: A hashmap containing the following fields:
152 |
153 | :fit -- array of fitness values for each cycle of SOM learning
154 | :weights -- hashmap of weight vectors, keyed by lattice indices
155 | :sets -- hashmap mapping data elements to lattice nodes
156 | (key lattice index) (value list of row indices from data)
157 | :dims -- dimensions of SOM lattice
158 | :data-means -- column means of input data matrix
159 |
160 |
161 | Examples:
162 |
163 | (use '(incanter core som stats charts datasets))
164 | (def data (to-matrix (sel (get-dataset :iris)
165 | :cols [\"Sepal.Length\" \"Sepal.Width\" \"Petal.Length\" \"Petal.Width\"])))
166 |
167 | (def som (som-batch-train data :cycles 10 :alpha 0.5 :beta 3))
168 |
169 | ;; plot the fitness for each cycle of training
170 | (view (xy-plot (range (count (:fit som))) (:fit som)))
171 | ;; view indices of data items in each cell
172 | (:sets som)
173 | ;; view the species in each cell
174 | (doseq [rws (vals (:sets som))]
175 | (println (sel (get-dataset :iris) :cols \"Species\" :rows rws) \\newline))
176 |
177 | ;; plot the means of the data vectors in each cell/cluster
178 | (def cell-means (map #(map mean (trans (sel data :rows ((:sets som) %)))) (keys (:sets som))))
179 | (def x (range (ncol data)))
180 | (doto (xy-plot x (first cell-means))
181 | view
182 | (add-lines x (nth cell-means 1))
183 | (add-lines x (nth cell-means 2)))
184 |
185 |
186 | References:
187 |
188 | http://en.wikipedia.org/wiki/Self-organizing_map
189 |
190 | "
191 | ([data & {:keys [alpha beta cycles]
192 | :or {alpha 0.5
193 | beta 3
194 | cycles 10}}]
195 | (loop [r 1 som (som-initialize-linear data) fit []]
196 | (if (= r cycles)
197 | (assoc som :fit fit)
198 | (let [new-som (som-update-weights r cycles (som-update-cells data som)
199 | alpha beta)]
200 | (recur (inc r) new-som (conj fit (som-fitness data new-som))))))))
201 |
--------------------------------------------------------------------------------