Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add proptable #19

Merged
merged 7 commits into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ julia> x = repeat(["a", "b", "c", "d"], outer=[100]);

julia> y = repeat(["A", "B", "C", "D"], inner=[10], outer=[10]);

julia> freqtable(x)
julia> tbl = freqtable(x)
4-element Named Array{Int64,1}
Dim1 │
──────┼────
Expand All @@ -26,6 +26,15 @@ b │ 100
c │ 100
d │ 100

julia> prop(tbl)
4-element Named Array{Float64,1}
Dim1 │
──────┼─────
a │ 0.25
b │ 0.25
c │ 0.25
d │ 0.25

julia> freqtable(x, y)
4×4 Named Array{Int64,2}
Dim1 ╲ Dim2 │ A B C D
Expand All @@ -35,7 +44,7 @@ b │ 30 20 30 20
c │ 20 30 20 30
d │ 20 30 20 30

julia> freqtable(x, y, subset=1:20)
julia> tbl2 = freqtable(x, y, subset=1:20)
4×2 Named Array{Int64,2}
Dim1 ╲ Dim2 │ A B
────────────┼─────
Expand All @@ -44,6 +53,15 @@ b │ 3 2
c │ 2 3
d │ 2 3

julia> prop(tbl2, 1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior (inherited from sum) is the opposite of R's: the passed dimensions are those to collapse, while in R they are ones to retain. I wonder whether it's appropriate for computing proportions: it's probably more natural to think "I want to compute proportion by rows" than "I want to divide each row by the column sums", isn't it? After all, we say "row profiles/percents" and "column profiles/percents".

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about it, initially I wanted to be consistent with sum.
But let us call the argument margin then and make it work like in R.

4×2 Named Array{Float64,2}
Dim1 ╲ Dim2 │ A B
────────────┼─────────
a │ 0.3 0.2
b │ 0.3 0.2
c │ 0.2 0.3
d │ 0.2 0.3

julia> freqtable(x, y, subset=1:20, weights=repeat([1, .5], outer=[10]))
4×2 Named Array{Float64,2}
Dim1 ╲ Dim2 │ A B
Expand Down
2 changes: 1 addition & 1 deletion src/FreqTables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ module FreqTables

include("freqtable.jl")

export freqtable
export freqtable, prop
end # module
3 changes: 3 additions & 0 deletions src/freqtable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,6 @@ function freqtable(d::AbstractDataFrame, x::Symbol...; args...)
setdimnames!(a, x)
a
end

prop(tbl::AbstractArray{<:Number}) = tbl / sum(tbl)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a docstring?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to add a docstring for freqtable and prop in a separate PR when we have settled the implementation.

prop(tbl::AbstractArray{<:Number}, dims) = tbl ./ sum(tbl, dims)
34 changes: 33 additions & 1 deletion test/freqtable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ y = repeat(["D", "C", "A", "B"], inner=[10], outer=[10]);
tab = @inferred freqtable(x)
@test tab == [100, 100, 100, 100]
@test names(tab) == [["a", "b", "c", "d"]]
@test prop(tab) == [0.25, 0.25, 0.25, 0.25]
tab = @inferred freqtable(y)
@test tab == [100, 100, 100, 100]
@test names(tab) == [["A", "B", "C", "D"]]
Expand All @@ -17,6 +18,31 @@ tab = @inferred freqtable(x, y)
20 30 30 20;
20 30 30 20]
@test names(tab) == [["a", "b", "c", "d"], ["A", "B", "C", "D"]]
@test prop(tab) == [0.075 0.05 0.05 0.075;
0.075 0.05 0.05 0.075;
0.05 0.075 0.075 0.05;
0.05 0.075 0.075 0.05]
@test prop(tab, (1,2)) == [0.075 0.05 0.05 0.075;
0.075 0.05 0.05 0.075;
0.05 0.075 0.075 0.05;
0.05 0.075 0.075 0.05]
@test prop(tab, 1) == [0.3 0.2 0.2 0.3;
0.3 0.2 0.2 0.3;
0.2 0.3 0.3 0.2;
0.2 0.3 0.3 0.2]
@test prop(tab, 2) == [0.3 0.2 0.2 0.3;
0.3 0.2 0.2 0.3;
0.2 0.3 0.3 0.2;
0.2 0.3 0.3 0.2]
@test prop(tab, ()) == [1.0 1.0 1.0 1.0;
1.0 1.0 1.0 1.0;
1.0 1.0 1.0 1.0;
1.0 1.0 1.0 1.0]

@test_throws MethodError prop()
@test_throws MethodError prop([1,2,3], ("a","b"))
@test_throws MethodError prop(("a","b"))
@test_throws MethodError prop((1, 2))

tab =freqtable(x, y,
subset=1:20,
Expand All @@ -26,7 +52,13 @@ tab =freqtable(x, y,
3.0 2.0
1.5 1.0]
@test names(tab) == [["a", "b", "c", "d"], ["C", "D"]]

@test prop(tab) == [4 6; 2 3; 6 4; 3 2] / 30.0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't hurt to have @inferred here too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@test prop(tab, 1) == [8 12; 4 6; 12 8; 6 4] / 30.0
@test prop(tab, (1,)) == [8 12; 4 6; 12 8; 6 4] / 30.0
@test prop(tab, 2) == [6 9; 6 9; 9 6; 9 6] / 15.0
@test prop(tab, (2,)) == [6 9; 6 9; 9 6; 9 6] / 15.0
@test prop(tab, ()) == [1.0 1.0; 1.0 1.0; 1.0 1.0; 1.0 1.0]
@test prop(tab, (1, 2)) == [4 6; 2 3; 6 4; 3 2] / 30.0

using CategoricalArrays
cx = CategoricalArray(x)
Expand Down