Skip to content

Commit

Permalink
Fix deprecations (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
femtocleaner[bot] authored and nalimilan committed Nov 24, 2017
1 parent f1e2675 commit 85e3abd
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 40 deletions.
22 changes: 11 additions & 11 deletions src/contrasts.jl
Expand Up @@ -70,7 +70,7 @@ from levels to `ModelMatrix` column values, and (optionally) a `termnames`
method:
```julia
type MyCoding <: AbstractContrasts
mutable struct MyCoding <: AbstractContrasts
...
end
Expand All @@ -79,10 +79,10 @@ termnames(C::MyCoding, levels, baseind) = ...
```
"""
abstract AbstractContrasts
abstract type AbstractContrasts end

# Contrasts + Levels (usually from data) = ContrastsMatrix
type ContrastsMatrix{C <: AbstractContrasts, T}
mutable struct ContrastsMatrix{C <: AbstractContrasts, T}
matrix::Matrix{Float64}
termnames::Vector{T}
levels::Vector{T}
Expand All @@ -92,12 +92,12 @@ end
# only check equality of matrix, termnames, and levels, and that the type is the
# same for the contrasts (values are irrelevant). This ensures that the two
# will behave identically in creating modelmatrix columns
Base.:(==){C<:AbstractContrasts,T}(a::ContrastsMatrix{C,T}, b::ContrastsMatrix{C,T}) =
Base.:(==)(a::ContrastsMatrix{C,T}, b::ContrastsMatrix{C,T}) where {C<:AbstractContrasts,T} =
a.matrix == b.matrix &&
a.termnames == b.termnames &&
a.levels == b.levels

Base.hash{C}(a::ContrastsMatrix{C}, h::UInt) =
Base.hash(a::ContrastsMatrix{C}, h::UInt) where {C} =
hash(C, hash(a.matrix, hash(a.termnames, hash(a.levels, h))))

"""
Expand Down Expand Up @@ -128,7 +128,7 @@ ContrastsMatrix(contrasts_matrix::ContrastsMatrix, levels::AbstractVector)
constructing a model matrix from a `ModelFrame` using different data.
"""
function ContrastsMatrix{C <: AbstractContrasts}(contrasts::C, levels::AbstractVector)
function ContrastsMatrix(contrasts::C, levels::AbstractVector) where C <: AbstractContrasts

# if levels are defined on contrasts, use those, validating that they line up.
# what does that mean? either:
Expand Down Expand Up @@ -176,7 +176,7 @@ function ContrastsMatrix{C <: AbstractContrasts}(contrasts::C, levels::AbstractV
ContrastsMatrix(mat, tnames, c_levels, contrasts)
end

ContrastsMatrix{C <: AbstractContrasts}(c::Type{C}, levels::AbstractVector) =
ContrastsMatrix(c::Type{C}, levels::AbstractVector) where {C <: AbstractContrasts} =
throw(ArgumentError("contrast types must be instantiated (use $c() instead of $c)"))

# given an existing ContrastsMatrix, check that all passed levels are present
Expand Down Expand Up @@ -208,7 +208,7 @@ nullify(x) = Nullable(x)
# The rest is boilerplate.
for contrastType in [:DummyCoding, :EffectsCoding, :HelmertCoding]
@eval begin
type $contrastType <: AbstractContrasts
mutable struct $contrastType <: AbstractContrasts
base::Nullable{Any}
levels::Nullable{Vector}
end
Expand Down Expand Up @@ -244,11 +244,11 @@ julia> StatsModels.ContrastsMatrix(StatsModels.FullDummyCoding(), ["a", "b", "c"
0.0 0.0 0.0 1.0
```
"""
type FullDummyCoding <: AbstractContrasts
mutable struct FullDummyCoding <: AbstractContrasts
# Dummy contrasts have no base level (since all levels produce a column)
end

ContrastsMatrix{T}(C::FullDummyCoding, levels::Vector{T}) =
ContrastsMatrix(C::FullDummyCoding, levels::Vector{T}) where {T} =
ContrastsMatrix(eye(Float64, length(levels)), levels, levels, C)

"Promote contrasts matrix to full rank version"
Expand Down Expand Up @@ -367,7 +367,7 @@ end
Coding by manual specification of contrasts matrix. For k levels, the contrasts
must be a k by k-1 Matrix.
"""
type ContrastsCoding <: AbstractContrasts
mutable struct ContrastsCoding <: AbstractContrasts
mat::Matrix
base::Nullable{Any}
levels::Nullable{Vector}
Expand Down
6 changes: 3 additions & 3 deletions src/formula.jl
Expand Up @@ -11,7 +11,7 @@
## The lhs of a one-sided formula is 'nothing'
## The rhs of a formula can be 1

type Formula
mutable struct Formula
lhs::Union{Symbol, Expr, Void}
rhs::Union{Symbol, Expr, Integer}
end
Expand All @@ -35,7 +35,7 @@ Representation of parsed `Formula`
This is an internal type whose implementation is likely to change in the near
future.
"""
type Terms
mutable struct Terms
terms::Vector
eterms::Vector # evaluation terms
factors::Matrix{Bool} # maps terms to evaluation terms
Expand Down Expand Up @@ -87,7 +87,7 @@ end
dospecials(a::Any) = a

## Distribution of & over +
const distributive = @compat Dict(:& => :+)
const distributive = Dict(:& => :+)

distribute(ex::Expr) = distribute!(copy(ex))
distribute(a::Any) = a
Expand Down
12 changes: 6 additions & 6 deletions src/modelframe.jl
Expand Up @@ -46,7 +46,7 @@ julia> mf = ModelFrame(y ~ 1 + x, df)
```
"""
type ModelFrame
mutable struct ModelFrame
df::AbstractDataFrame
terms::Terms
msng::BitArray
Expand Down Expand Up @@ -227,19 +227,19 @@ function coefnames(mf::ModelFrame)
terms = droprandomeffects(dropresponse!(mf.terms))

## strategy mirrors ModelMatrx constructor:
eterm_names = @compat Dict{Tuple{Symbol,Bool}, Vector{Compat.UTF8String}}()
term_names = Vector{Compat.UTF8String}[]
eterm_names = Dict{Tuple{Symbol,Bool}, Vector{String}}()
term_names = Vector{String}[]

if terms.intercept
push!(term_names, Compat.UTF8String["(Intercept)"])
push!(term_names, String["(Intercept)"])
end

factors = terms.factors

for (i_term, term) in enumerate(terms.terms)

## names for columns for eval terms
names = Vector{Compat.UTF8String}[]
names = Vector{String}[]

ff = Compat.view(factors, :, i_term)
eterms = Compat.view(terms.eterms, ff)
Expand All @@ -254,5 +254,5 @@ function coefnames(mf::ModelFrame)
push!(term_names, expandtermnames(names))
end

reduce(vcat, Vector{Compat.UTF8String}(), term_names)
reduce(vcat, Vector{String}(), term_names)
end
24 changes: 12 additions & 12 deletions src/modelmatrix.jl
@@ -1,5 +1,5 @@

typealias AbstractFloatMatrix{T<:AbstractFloat} AbstractMatrix{T}
AbstractFloatMatrix{T<:AbstractFloat} = AbstractMatrix{T}

"""
Convert a `ModelFrame` into a numeric matrix suitable for modeling
Expand All @@ -13,7 +13,7 @@ ModelMatrix{T <: AbstractFloatMatrix}(mf::ModelFrame)
```
"""
type ModelMatrix{T <: AbstractFloatMatrix}
mutable struct ModelMatrix{T <: AbstractFloatMatrix}
m::T
assign::Vector{Int}
end
Expand All @@ -24,7 +24,7 @@ Base.size(mm::ModelMatrix, dim...) = size(mm.m, dim...)


## construct model matrix columns from model frame + name (checks for contrasts)
function modelmat_cols{T<:AbstractFloatMatrix}(::Type{T}, name::Symbol, mf::ModelFrame; non_redundant::Bool = false)
function modelmat_cols(::Type{T}, name::Symbol, mf::ModelFrame; non_redundant::Bool = false) where T<:AbstractFloatMatrix
if haskey(mf.contrasts, name)
modelmat_cols(T, mf.df[name],
non_redundant ?
Expand All @@ -35,10 +35,10 @@ function modelmat_cols{T<:AbstractFloatMatrix}(::Type{T}, name::Symbol, mf::Mode
end
end

modelmat_cols{T<:AbstractFloatMatrix}(::Type{T}, v::AbstractVector{<:Union{Missing, Real}}) =
modelmat_cols(::Type{T}, v::AbstractVector{<:Union{Missing, Real}}) where {T<:AbstractFloatMatrix} =
convert(T, reshape(v, length(v), 1))
# Categorical column, does not make sense to convert to float
modelmat_cols{T<:AbstractFloatMatrix}(::Type{T}, v::AbstractVector) =
modelmat_cols(::Type{T}, v::AbstractVector) where {T<:AbstractFloatMatrix} =
modelmat_cols(T, reshape(v, length(v), 1))

# All non-real columns are considered as categorical
Expand All @@ -49,13 +49,13 @@ modelmat_cols{T<:AbstractFloatMatrix}(::Type{T}, v::AbstractVector) =
Construct `ModelMatrix` columns of type `T` based on specified contrasts, ensuring that
levels align properly.
"""
modelmat_cols{T<:AbstractFloatMatrix}(::Type{T}, v::AbstractVector, contrast::ContrastsMatrix) =
modelmat_cols(::Type{T}, v::AbstractVector, contrast::ContrastsMatrix) where {T<:AbstractFloatMatrix} =
modelmat_cols(T, categorical(v), contrast)


function modelmat_cols{T<:AbstractFloatMatrix}(::Type{T},
v::AbstractCategoricalVector,
contrast::ContrastsMatrix)
function modelmat_cols(::Type{T},
v::AbstractCategoricalVector,
contrast::ContrastsMatrix) where T<:AbstractFloatMatrix
## make sure the levels of the contrast matrix and the categorical data
## are the same by constructing a re-indexing vector. Indexing into
## reindex with v.refs will give the corresponding row number of the
Expand All @@ -72,7 +72,7 @@ indexrows(m::AbstractMatrix, ind::Vector{Int}) = m[ind, :]
expandcols{T<:AbstractFloatMatrix}(trm::Vector{T})
Create pairwise products of columns from a vector of matrices
"""
function expandcols{T<:AbstractFloatMatrix}(trm::Vector{T})
function expandcols(trm::Vector{T}) where T<:AbstractFloatMatrix
if length(trm) == 1
trm[1]
else
Expand Down Expand Up @@ -139,7 +139,7 @@ If there is an intercept in the model, that column occurs first and its
Mixed-effects models include "random-effects" terms which are ignored when
creating the model matrix.
"""
@compat function (::Type{ModelMatrix{T}}){T<:AbstractFloatMatrix}(mf::ModelFrame)
function ModelMatrix{T}(mf::ModelFrame) where T<:AbstractFloatMatrix
dfrm = mf.df
terms = droprandomeffects(dropresponse!(mf.terms))

Expand All @@ -153,7 +153,7 @@ creating the model matrix.
factors = terms.factors

## Map eval. term name + redundancy bool to cached model matrix columns
eterm_cols = @compat Dict{Tuple{Symbol,Bool}, T}()
eterm_cols = Dict{Tuple{Symbol,Bool}, T}()
## Accumulator for each term's vector of eval. term columns.

## TODO: this method makes multiple copies of the data in the ModelFrame:
Expand Down
10 changes: 5 additions & 5 deletions src/statsmodel.jl
Expand Up @@ -32,13 +32,13 @@ macro delegate(source, targets)
end

# Wrappers for DataFrameStatisticalModel and DataFrameRegressionModel
immutable DataFrameStatisticalModel{M,T} <: StatisticalModel
struct DataFrameStatisticalModel{M,T} <: StatisticalModel
model::M
mf::ModelFrame
mm::ModelMatrix{T}
end

immutable DataFrameRegressionModel{M,T} <: RegressionModel
struct DataFrameRegressionModel{M,T} <: RegressionModel
model::M
mf::ModelFrame
mm::ModelMatrix{T}
Expand All @@ -47,8 +47,8 @@ end
for (modeltype, dfmodeltype) in ((:StatisticalModel, DataFrameStatisticalModel),
(:RegressionModel, DataFrameRegressionModel))
@eval begin
function StatsBase.fit{T<:$modeltype}(::Type{T}, f::Formula, df::AbstractDataFrame,
args...; contrasts::Dict = Dict(), kwargs...)
function StatsBase.fit(::Type{T}, f::Formula, df::AbstractDataFrame,
args...; contrasts::Dict = Dict(), kwargs...) where T<:$modeltype
mf = ModelFrame(f, df, contrasts=contrasts)
mm = ModelMatrix(mf)
y = model_response(mf)
Expand All @@ -58,7 +58,7 @@ for (modeltype, dfmodeltype) in ((:StatisticalModel, DataFrameStatisticalModel),
end

# Delegate functions from StatsBase that use our new types
typealias DataFrameModels @compat(Union{DataFrameStatisticalModel, DataFrameRegressionModel})
const DataFrameModels = Union{DataFrameStatisticalModel, DataFrameRegressionModel}
@delegate DataFrameModels.model [StatsBase.coef, StatsBase.confint,
StatsBase.deviance, StatsBase.nulldeviance,
StatsBase.loglikelihood, StatsBase.nullloglikelihood,
Expand Down
6 changes: 3 additions & 3 deletions test/statsmodel.jl
Expand Up @@ -9,7 +9,7 @@ using Compat
# Tests for statsmodel.jl

# A dummy RegressionModel type
immutable DummyMod <: RegressionModel
struct DummyMod <: RegressionModel
beta::Vector{Float64}
x::Matrix
y::Vector
Expand Down Expand Up @@ -91,8 +91,8 @@ fit(DummyMod, f3, d, contrasts = Dict(:x1p => EffectsCoding(),


## Another dummy model type to test fall-through show method
immutable DummyModTwo <: RegressionModel
msg::Compat.UTF8String
struct DummyModTwo <: RegressionModel
msg::String
end

StatsBase.fit(::Type{DummyModTwo}, ::Matrix, ::Vector) = DummyModTwo("hello!")
Expand Down

0 comments on commit 85e3abd

Please sign in to comment.