Yet Another Blog in Statistical Computing

I can calculate the motion of heavenly bodies but not the madness of people. -Isaac Newton

Archive for the ‘incanter’ Category

Two Ways to Select Rows in Incanter

user=> (use '(incanter core io))
nil

user=> (def iris (read-dataset "../data/iris.dat" :header true :delim \space))
#'user/iris

user=> (sel iris :rows (range 3))
[:Sepal.Length :Sepal.Width :Petal.Length :Petal.Width :Species]
[5.1 3.5 1.4 0.2 "setosa"]
[4.9 3.0 1.4 0.2 "setosa"]
[4.7 3.2 1.3 0.2 "setosa"]

;; METHOD 1 - USING $WHERE 
user=> ($where {:Species {:in #{"virginica" "setosa"}} :Sepal.Length {:gt 5.5, :lt 6.0}} iris)
[:Sepal.Length :Sepal.Width :Petal.Length :Petal.Width :Species]
[5.8 4.0 1.2 0.2 "setosa"]
[5.7 4.4 1.5 0.4 "setosa"]
[5.7 3.8 1.7 0.3 "setosa"]
[5.8 2.7 5.1 1.9 "virginica"]
[5.7 2.5 5.0 2.0 "virginica"]
[5.8 2.8 5.1 2.4 "virginica"]
[5.6 2.8 4.9 2.0 "virginica"]
[5.8 2.7 5.1 1.9 "virginica"]
[5.9 3.0 5.1 1.8 "virginica"]

;; METHOD 2 - USING QUERY-DATASET
user=> (query-dataset iris {:Species {:in #{"virginica" "setosa"}} :Sepal.Length {:gt 5.5, :lt 6.0}})
[:Sepal.Length :Sepal.Width :Petal.Length :Petal.Width :Species]
[5.8 4.0 1.2 0.2 "setosa"]
[5.7 4.4 1.5 0.4 "setosa"]
[5.7 3.8 1.7 0.3 "setosa"]
[5.8 2.7 5.1 1.9 "virginica"]
[5.7 2.5 5.0 2.0 "virginica"]
[5.8 2.8 5.1 2.4 "virginica"]
[5.6 2.8 4.9 2.0 "virginica"]
[5.8 2.7 5.1 1.9 "virginica"]
[5.9 3.0 5.1 1.8 "virginica"]

Written by statcompute

March 15, 2015 at 1:35 am

Posted in clojure, incanter

Tagged with ,

Read CSV Data File with Clojure

;; LOAD PACKAGES
user=> (use '(incanter core io))
nil

;; READ CSV FILE
user=> (def ds1 (read-dataset "../data/credit_count.csv" :header true :delim \,))
#'user/ds1

;; EXAMINE THE DATA
user=> ($ (range 0 3) :all ds1)
[:CARDHLDR :DEFAULT :AGE :ACADMOS :ADEPCNT :MAJORDRG :MINORDRG :OWNRENT :INCOME :SELFEMPL :INCPER :EXP_INC :SPENDING :LOGSPEND ]
[0 0 27.25 4 0 0 0 0 1200 0 18000 6.667E-4 " " "  "]
[0 0 40.8333321 111 3 0 0 1 4000 0 13500 2.222E-4 " " "  "]
[1 0 37.6666679 54 3 0 0 1 3666.6666667 0 11300 0.0332699 121.9896773 4.8039364]

;; CALCULATE SUMMARY BY GROUP
user=> ($rollup :mean :INCOME [:CARDHLDR :DEFAULT] ds1)
[:DEFAULT :CARDHLDR :INCOME]
[1 1 2156.117553547691]
[0 1 2653.2908642884945]
[0 0 2165.1530843234673]

Written by statcompute

March 14, 2015 at 3:23 pm

Posted in clojure, incanter

Tagged with ,

Read Space-Delimited File with Clojure

;; load clojure libraries
user=> (use 'incanter.core 'incanter.io)
nil

;; import data file
user=> (def iris (read-dataset "../data/iris.dat" :header true :delim \space))
#'user/iris

;; show data header
user=> (col-names iris)
[:Sepal.Length :Sepal.Width :Petal.Length :Petal.Width :Species]

;; show first 3 data records with $
user=> ($ (range 0 3) :all iris)
[:Sepal.Length :Sepal.Width :Petal.Length :Petal.Width :Species]
[5.1 3.5 1.4 0.2 "setosa"]
[4.9 3.0 1.4 0.2 "setosa"]
[4.7 3.2 1.3 0.2 "setosa"]

Written by statcompute

March 9, 2015 at 2:13 am

Posted in clojure, incanter

Tagged with ,