Skip to content

Commit

Permalink
Added code to provide DataFrames input when user has DataFrames loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
BioTurboNick committed Jan 20, 2019
1 parent 943345f commit ed4c0d2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
10 changes: 8 additions & 2 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[Missings]]
deps = ["Dates", "InteractiveUtils", "SparseArrays", "Test"]
git-tree-sha1 = "adc26d2ee85a49c413464110d922cf21efc9d233"
git-tree-sha1 = "d1d2585677f2bd93a97cfeb8faa7a0de0f982042"
uuid = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
version = "0.3.1"
version = "0.4.0"

[[Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"
Expand Down Expand Up @@ -113,6 +113,12 @@ uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
deps = ["Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[Requires]]
deps = ["Test"]
git-tree-sha1 = "f6fbf4ba64d295e146e49e021207993b6b48c7d1"
uuid = "ae029012-a4dd-5104-9daa-d747884805df"
version = "0.5.2"

[[Rmath]]
deps = ["BinaryProvider", "Libdl", "Random", "Statistics", "Test"]
git-tree-sha1 = "9a6c758cdf73036c3239b0afbea790def1dabff9"
Expand Down
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.2.0"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
julia 0.7
Distributions
Requires
15 changes: 10 additions & 5 deletions src/SimpleANOVA.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module SimpleANOVA

using Distributions
using Distributions, Requires
include("InvertedIndices.jl")
include("AnovaEffect.jl")
include("AnovaValue.jl")
Expand All @@ -14,9 +14,14 @@ const cellsname = "Cells"
const errorname = "Error"
const remaindername = "Remainder"

function __init__()
@require DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" include("anova_dataframes.jl")
end

"""
anova(observations::Union{Array{Number}, Array{Vector{Number}}}, factortypes = FactorType[]; factornames = String[], hasreplicates = true)
anova(observations::Vector{Number}, factorassignments::Vector{Vector{Int}}, factortypes = FactorType[]; factornames = String[], hasreplicates = true)
anova(observations::Vector{Number}, factorassignments::Vector{Vector{Any}}, factortypes = FactorType[]; factornames = String[], hasreplicates = true)
anova(df::DataFrame, observationscolumn::Symbol, factorcolumns::Vector{Symbol}, factortypes = FactorType[]; factornames = String[])
Performs an Analysis of Variance (ANOVA) computation.
Expand Down Expand Up @@ -107,7 +112,7 @@ function anova(observations::AbstractArray{T}, factortypes::Vector{FactorType} =
anovakernel(observations, nreplicates, ncells, nnestedfactors, ncrossedfactors, nfactorlevels, crossedfactortypes, crossedfactornames, nestedfactornames)
end

function anova(observations::AbstractVector{T}, factorassignments::AbstractVector{<:AbstractVector{<:Int}}, factortypes::Vector{FactorType} = FactorType[], factornames::Vector{<:AbstractString} = String[]) where {T <: Number}
function anova(observations::AbstractVector{T}, factorassignments::AbstractVector{<:AbstractVector}, factortypes::Vector{FactorType} = FactorType[], factornames::Vector{<:AbstractString} = String[]) where {T <: Number}
length(observations) > 0 || return
nfactors = length(factorassignments)
N = length(observations)
Expand All @@ -122,7 +127,7 @@ function anova(observations::AbstractVector{T}, factorassignments::AbstractVecto
all(nperfactorlevel .|> length .== 1) || error("Design is unbalanced.")
nperfactorlevel = nperfactorlevel .|> first

if any(maximum.(factorlevels) .> nfactorlevels)
if !(factorassignements <: Number) || any(maximum.(factorlevels) .> nfactorlevels)
compressedfactorlevels = [1:i for i nfactorlevels]
factorlevelremapping = [factorlevels[i] .=> compressedfactorlevels[i] for i 1:nfactors]
factorassignments = [replace(factorassignments[i], factorlevelremapping[i]...) for i 1:nfactors]
Expand All @@ -134,7 +139,7 @@ function anova(observations::AbstractVector{T}, factorassignments::AbstractVecto
sortorder = sortperm(repeat(1:nreplicates, Int(N / nreplicates)) .+
sum([factorassignments[i] .* prod(nlevels[1:i]) for i 1:nfactors]))
observationsmatrix = reshape(observations[sortorder], nlevels...)
anova(observationsmatrix, factortypes, factornames = factornames)
anova(observationsmatrix, factortypes, factornames = factornames, hasreplicates = nreplicates > 1)
end

function validate(factortypes::Vector{FactorType}, factornames::Vector{<:AbstractString}, nfactors)
Expand Down
7 changes: 7 additions & 0 deletions src/anova_dataframe.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function anova(df::DataFrame, observationscolumn::Symbol, factorcolumns::Vector{Symbol}, factortypes::Vector{FactorType} = FactorType[]; factornames::Vector{<:AbstractString} = String[])
observations = df[observationscolumn]
length(observations) > 0 || return
eltype(observations) <: Number || error("Obervations must be numeric")
isempty(factornames) && (factornames = [String(col) for col factorcolumns])
anova(df[observationscolumn], df[factorcolumns], factortypes, factornames = factornames)
end

0 comments on commit ed4c0d2

Please sign in to comment.