Skip to content
This repository has been archived by the owner on May 4, 2019. It is now read-only.

Fixed xtab and added docstrings #148

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions src/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,43 @@ end

gl(n::Integer, k::Integer) = gl(n, k, n*k)

# A cross-tabulation type. Currently just a one-way table
type xtab{T}
#' @description
#'
#' A cross-tabulation type. Currently, this is just a one-way table.
#'
#' @field vals::Array{T} The values of the original data
#' @field counts::Vector{Int} The counts corresponding to the values
type Xtab{T}
vals::Array{T}
counts::Vector{Int}
end

#' @description
#'
#' Create a new cross-tabulation from the array `x`
#' This is currently just for one-way tables.
#'
#' @param x::AbstractDataArray The AbstractDataArray with the data
#'
#' @returns out::Xtab The `Xtab` corresponding to `x`
function xtab{T}(x::AbstractArray{T})
d = Dict{T, Int}()
for el in x
d[el] = get(d, el, 0) + 1
end
kk = sort(keys(d))
d = xtabs(x)
kk = sort(collect(keys(d)))
cc = Array(Int, length(kk))
for i in 1:length(kk)
cc[i] = d[kk[i]]
end
return xtab(kk, cc)
return Xtab(kk, cc)
end

# Another cross-tabulation function, this one leaves the result as a Dict
# Again, this is currently just for one-way tables.
#' @description
#'
#' A cross-tabulation function that returns the results as a Dict
#' This is currently just for one-way tables.
#'
#' @param x::AbstractDataArray The AbstractDataArray with the data
#'
#' @returns out::Dict The values and their counts
function xtabs{T}(x::AbstractArray{T})
d = Dict{T, Int}()
for el in x
Expand Down
9 changes: 8 additions & 1 deletion test/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@ module TestStats

autocor(DataArray([1, 2, 3, 4, 5]))

@assert isequal(xtabs([1, 2, 2, 2, 3]), Dict([(2, 3), (3, 1), (1, 1)]))
xtab_data = [1, 2, 2, 2, 3]
@assert isequal(xtabs(xtab_data), Dict([(2, 3), (3, 1), (1, 1)]))

xt = xtab(xtab_data)
# Testing that the keys are correct and sorted
@assert xt.vals == [1, 2, 3]
# Testing that the counts are correct
@assert xt.counts == [1, 3, 1]
end