1.2.3-SNAPSHOT permalink Arrow_down_16x16

tabulate

incanter.stats

  • (tabulate x & options)
Cross-tabulates the values of the given numeric matrix.

Returns a hash-map with the following fields:
:table -- the table of counts for each combination of values,
this table is only returned if x has two-columns
:levels -- a sequence of sequences, where each sequence list
the levels (possible values) of the corresponding
column of x.
:margins -- a sequence of sequences, where each sequence
represents the marginal total for each level
of the corresponding column of x.
:counts -- a hash-map, where vectors of unique combinations
of the cross-tabulated levels are the keys and the
values are the total count of each combination.
:N -- the grand-total for the contingency table


Examples:

(use '(incanter core stats))
(tabulate [1 2 3 2 3 2 4 3 5])
(tabulate (sample-poisson 100 :lambda 5))

(use '(incanter core stats datasets))
(def math-prog (to-matrix (get-dataset :math-prog)))
(tabulate (sel math-prog :cols [1 2]))


(def data (matrix [[1 0 1]
[1 1 1]
[1 1 1]
[1 0 1]
[0 0 0]
[1 1 1]
[1 1 1]
[1 0 1]
[1 1 0]]))
(tabulate data)


(def data (matrix [[1 0]
[1 1]
[1 1]
[1 0]
[0 0]
[1 1]
[1 1]
[1 0]
[1 1]]))
(tabulate data)

0 Examples top

Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source incanter/stats.clj:2227 top

(defn tabulate
" Cross-tabulates the values of the given numeric matrix.

  Returns a hash-map with the following fields:
    :table -- the table of counts for each combination of values,
              this table is only returned if x has two-columns
    :levels -- a sequence of sequences, where each sequence list
               the levels (possible values) of the corresponding
               column of x.
    :margins -- a sequence of sequences, where each sequence
                represents the marginal total for each level
                of the corresponding column of x.
    :counts -- a hash-map, where vectors of unique combinations
               of the cross-tabulated levels are the keys and the
               values are the total count of each combination.
    :N  -- the grand-total for the contingency table


  Examples:

    (use '(incanter core stats))
    (tabulate [1 2 3 2 3 2 4 3 5])
    (tabulate (sample-poisson 100 :lambda 5))

    (use '(incanter core stats datasets))
    (def math-prog (to-matrix (get-dataset :math-prog)))
    (tabulate (sel math-prog :cols [1 2]))


    (def data (matrix [[1 0 1]
                       [1 1 1]
                       [1 1 1]
                       [1 0 1]
                       [0 0 0]
                       [1 1 1]
                       [1 1 1]
                       [1 0 1]
                       [1 1 0]]))
    (tabulate data)


    (def data (matrix [[1 0]
                       [1 1]
                       [1 1]
                       [1 0]
                       [0 0]
                       [1 1]
                       [1 1]
                       [1 0]
                       [1 1]]))
    (tabulate data)

"
  ([x & options]
    (let [opts (when options (apply assoc {} options))
          _x (if (matrix? x) x (matrix x))
          p (ncol _x)
          n (nrow _x)
          levels (for [i (range p)] (sort (seq (into #{} (sel _x :cols i)))))
          margins (for [j (range p)]
                  (loop [marg {} i (int 0)]
                    (if (= i n)
                      marg
                      (let [lvl (sel _x :rows i :cols j)]
                        (recur (let [cnt (get marg lvl)]
                                (if cnt
                                  (assoc marg lvl (inc cnt))
                                  (assoc marg lvl 1)))
                              (inc i))))))
          counts (loop [tab {} i (int 0)]
                    (if (= i n)
                      tab
                      (recur (let [row (if (> p 1)
                                         (to-list (nth _x i))
                                         (nth _x i))
                                   cnt (get tab row)]
                              (if (nil? cnt)
                                (assoc tab row 1)
                                (assoc tab row (inc cnt))))
                            (inc i))))
          n-levels (map #(count (keys %)) margins)]

      {:counts counts
       :margins margins
       :table (when (= p 2)
                (matrix (for [r (first levels)
                              c (second levels)]
                          (let [c (get counts (to-list (trans [r c])))]
                            (if c c 0)))
                        (second n-levels)))
       :n-vars p
       :N (reduce + (vals (first margins)))
       :n-levels n-levels
       :levels levels})))
Vars in incanter.stats/tabulate: defn let
Used in 0 other vars

Comments top

No comments for tabulate. Log in to add a comment.