0

I am trying to get the array mean and covariance matrix from a data structure that is 1000x2 using incanter.

My test case looks as such

(ns test.mean-cov
  (:require clojure.string
            [incanter.core :as in-core]
            [incanter.stats :as in-stats])
  (:use clojure.java.io))

(def test-mat [[1 2] [2 2]])

(in-stats/mean test-mat)

which I am running in LighTable.

The error I get is this:

java.lang.ClassCastException: clojure.lang.PersistentList cannot be cast to java.lang.Number
        Numbers.java:1104 clojure.lang.Numbers.double_array
           stats.clj:1492 incanter.stats/mean

I cannot quite say that I understand this, and unfortunately my Googling is not helping. Could someone offer a simple explanation and solution?

Naturally one awful way of doing it would be

(def c1 (first (transpose test-mat)))
(def c2 (second (transpose test-mat)))

(def data-cov-mat [[(in-stats/covariance c1 c1) (in-stats/covariance c1 c2)] [(in-stats/covariance c2 c1) (in-stats/covariance c2 c2)]])

Full disclosure: I am new to clojure.

Thanks

Astrid
  • 1,846
  • 4
  • 26
  • 48

1 Answers1

2

For matrix covariance you need to use the following:

(in-stats/covariance (in-core/matrix test-mat))

and for the matrix mean vector:

(mapv in-stats/mean (in-core/trans test-mat))
Symfrog
  • 3,398
  • 1
  • 17
  • 13
  • Hmmm... `(in-stats/covariance (in-core/matrix test-mat))` returns `java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.Number` – Astrid Jul 24 '15 at 14:48
  • @Astrid You most likely have a boolean value in your matrix. This example matrix would cause the same exception: `(def test-mat [[1 2] [2 true]])` – Symfrog Jul 24 '15 at 14:49
  • How very odd, I am doing simply: `(def test-mat [[1 2] [2 2]])` and then `(in-stats/covariance (in-core/matrix test-mat))` which gives `java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.Number` – Astrid Jul 24 '15 at 14:54