Skip to content

Commit

Permalink
Fix deprecations (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
femtocleaner[bot] authored and andreasnoack committed Aug 16, 2017
1 parent 3d22e3b commit dbad91a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 75 deletions.
10 changes: 5 additions & 5 deletions src/GLM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ module GLM
ftest # compare models with an F test

const FP = AbstractFloat
@compat const FPVector{T<:FP} = AbstractArray{T,1}
const FPVector{T<:FP} = AbstractArray{T,1}

@compat abstract type ModResp end # model response
abstract type ModResp end # model response

@compat abstract type LinPred end # linear predictor in statistical models
@compat abstract type DensePred <: LinPred end # linear predictor with dense X
@compat abstract type LinPredModel <: RegressionModel end # model based on a linear predictor
abstract type LinPred end # linear predictor in statistical models
abstract type DensePred <: LinPred end # linear predictor with dense X
abstract type LinPredModel <: RegressionModel end # model based on a linear predictor

include("linpred.jl")
include("lm.jl")
Expand Down
10 changes: 5 additions & 5 deletions src/ftest.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type FTestResult{N}
mutable struct FTestResult{N}
ssr::NTuple{N, Float64}
dof::NTuple{N, Int}
dof_resid::NTuple{N, Int}
Expand Down Expand Up @@ -36,11 +36,11 @@ function issubmodel(mod1::LinPredModel, mod2::LinPredModel)
return true
end

_diffn{N, T}(t::NTuple{N, T}) = ntuple(i->t[i]-t[i+1], N-1)
_diffn(t::NTuple{N, T}) where {N, T} = ntuple(i->t[i]-t[i+1], N-1)

_diff{N, T}(t::NTuple{N, T}) = ntuple(i->t[i+1]-t[i], N-1)
_diff(t::NTuple{N, T}) where {N, T} = ntuple(i->t[i+1]-t[i], N-1)

dividetuples{N}(t1::NTuple{N}, t2::NTuple{N}) = ntuple(i->t1[i]/t2[i], N)
dividetuples(t1::NTuple{N}, t2::NTuple{N}) where {N} = ntuple(i->t1[i]/t2[i], N)

"""
ftest(mod::LinearModel...)
Expand Down Expand Up @@ -109,7 +109,7 @@ function ftest(mods::LinearModel...)
return FTestResult(SSR, nparams, df1, r2.(mods), fstat, pval)
end

function show{N}(io::IO, ftr::FTestResult{N})
function show(io::IO, ftr::FTestResult{N}) where N
Δdof = _diffn(ftr.dof_resid)
Δssr = _diffn(ftr.ssr)
ΔR² = _diffn(ftr.r2)
Expand Down
40 changes: 20 additions & 20 deletions src/glmfit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
The response vector and various derived vectors in a generalized linear model.
"""
immutable GlmResp{V<:FPVector,D<:UnivariateDistribution,L<:Link} <: ModResp
struct GlmResp{V<:FPVector,D<:UnivariateDistribution,L<:Link} <: ModResp
"`y`: response vector"
y::V
d::D
Expand All @@ -23,7 +23,7 @@ immutable GlmResp{V<:FPVector,D<:UnivariateDistribution,L<:Link} <: ModResp
wrkresid::V
end

function GlmResp{V<:FPVector, D, L}(y::V, d::D, l::L, η::V, μ::V, off::V, wts::V)
function GlmResp(y::V, d::D, l::L, η::V, μ::V, off::V, wts::V) where {V<:FPVector, D, L}
if d == Binomial()
for yy in y
0 yy 1 || throw(ArgumentError("$yy in y is not in [0,1]"))
Expand Down Expand Up @@ -55,17 +55,17 @@ of the variance function for `D`. If they are the same a numerator and denomina
the expression for the working weights will cancel.
"""
cancancel(::GlmResp) = false
cancancel{V,D<:Union{Bernoulli,Binomial}}(::GlmResp{V,D,LogitLink}) = true
cancancel{V,D<:Normal}(::GlmResp{V,D,IdentityLink}) = true
cancancel{V,D<:Poisson}(::GlmResp{V,D,LogLink}) = true
cancancel(::GlmResp{V,D,LogitLink}) where {V,D<:Union{Bernoulli,Binomial}} = true
cancancel(::GlmResp{V,D,IdentityLink}) where {V,D<:Normal} = true
cancancel(::GlmResp{V,D,LogLink}) where {V,D<:Poisson} = true

"""
updateμ!{T<:FPVector}(r::GlmResp{T}, linPr::T)
Update the mean, working weights and working residuals, in `r` given a value of
the linear predictor, `linPr`.
"""
function updateμ!{T<:FPVector}(r::GlmResp{T}, linPr::T)
function updateμ!(r::GlmResp{T}, linPr::T) where T<:FPVector
isempty(r.offset) ? copy!(r.eta, linPr) : broadcast!(+, r.eta, linPr, r.offset)
updateμ!(r)
if !isempty(r.wts)
Expand All @@ -75,7 +75,7 @@ function updateμ!{T<:FPVector}(r::GlmResp{T}, linPr::T)
r
end

function updateμ!{V<:FPVector,D,L}(r::GlmResp{V,D,L})
function updateμ!(r::GlmResp{V,D,L}) where {V<:FPVector,D,L}
y, η, μ, wrkres, wrkwt, dres = r.y, r.eta, r.mu, r.wrkresid, r.wrkwt, r.devresid

@inbounds for i in eachindex(y, η, μ, wrkres, wrkwt, dres)
Expand All @@ -88,7 +88,7 @@ function updateμ!{V<:FPVector,D,L}(r::GlmResp{V,D,L})
end
end

function updateμ!{V<:FPVector,D<:Union{Bernoulli,Binomial},L<:Link01}(r::GlmResp{V,D,L})
function updateμ!(r::GlmResp{V,D,L}) where {V<:FPVector,D<:Union{Bernoulli,Binomial},L<:Link01}
y, η, μ, wrkres, wrkwt, dres = r.y, r.eta, r.mu, r.wrkresid, r.wrkwt, r.devresid

@inbounds for i in eachindex(y, η, μ, wrkres, wrkwt, dres)
Expand All @@ -113,14 +113,14 @@ wrkresp(r::GlmResp) = wrkresp!(similar(r.eta), r)
Overwrite `v` with the working response of `r`
"""
function wrkresp!{T<:FPVector}(v::T, r::GlmResp{T})
function wrkresp!(v::T, r::GlmResp{T}) where T<:FPVector
broadcast!(+, v, r.eta, r.wrkresid)
isempty(r.offset) ? v : broadcast!(-, v, v, r.offset)
end

@compat abstract type AbstractGLM <: LinPredModel end
abstract type AbstractGLM <: LinPredModel end

type GeneralizedLinearModel{G<:GlmResp,L<:LinPred} <: AbstractGLM
mutable struct GeneralizedLinearModel{G<:GlmResp,L<:LinPred} <: AbstractGLM
rr::G
pp::L
fit::Bool
Expand Down Expand Up @@ -253,13 +253,13 @@ function StatsBase.fit!(m::AbstractGLM, y; wts=nothing, offset=nothing, dofit::B
end
end

function fit{M<:AbstractGLM,T<:FP,V<:FPVector}(::Type{M},
function fit(::Type{M},
X::Union{Matrix{T},SparseMatrixCSC{T}}, y::V,
d::UnivariateDistribution,
l::Link = canonicallink(d);
dofit::Bool = true,
wts::V = ones(y),
offset::V = similar(y, 0), fitargs...)
offset::V = similar(y, 0), fitargs...) where {M<:AbstractGLM,T<:FP,V<:FPVector}

size(X, 1) == size(y, 1) || throw(DimensionMismatch("number of rows in X and y must match"))
n = length(y)
Expand All @@ -276,20 +276,20 @@ function fit{M<:AbstractGLM,T<:FP,V<:FPVector}(::Type{M},
dofit ? fit!(res; fitargs...) : res
end

fit{M<:AbstractGLM}(::Type{M},
X::Union{Matrix,SparseMatrixCSC},
y::AbstractVector,
d::UnivariateDistribution,
l::Link=canonicallink(d); kwargs...) =
fit(::Type{M},
X::Union{Matrix,SparseMatrixCSC},
y::AbstractVector,
d::UnivariateDistribution,
l::Link=canonicallink(d); kwargs...) where {M<:AbstractGLM} =
fit(M, float(X), float(y), d, l; kwargs...)

glm(X, y, args...; kwargs...) = fit(GeneralizedLinearModel, X, y, args...; kwargs...)

GLM.Link(mm::AbstractGLM) = mm.l
GLM.Link{T,D,L}(r::GlmResp{T,D,L}) = L()
GLM.Link(r::GlmResp{T,D,L}) where {T,D,L} = L()
GLM.Link(m::GeneralizedLinearModel) = Link(m.rr)

Distributions.Distribution{T,D,L}(r::GlmResp{T,D,L}) = D
Distributions.Distribution(r::GlmResp{T,D,L}) where {T,D,L} = D
Distributions.Distribution(m::GeneralizedLinearModel) = Distribution(m.rr)

"""
Expand Down
26 changes: 13 additions & 13 deletions src/glmtools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,51 @@
An abstract type whose subtypes determine methods for [`linkfun`](@ref), [`linkinv`](@ref),
[`mueta`](@ref), and [`inverselink`](@ref).
"""
@compat abstract type Link end
abstract type Link end

"""
Link01
An abstract subtype of [`Link`](@ref) which are links defined on (0, 1)
"""
@compat abstract type Link01 <: Link end
abstract type Link01 <: Link end

"""
CauchitLink
A [`Link01`](@ref) corresponding to the standard Cauchy distribution,
[`Distributions.Cauchy`](@ref).
"""
type CauchitLink <: Link01 end
mutable struct CauchitLink <: Link01 end

"""
CloglogLink
A [`Link01`](@ref) corresponding to the extreme value (or log-Wiebull) distribution. The
link is the complementary log-log transformation, `log(1 - log(-μ))`.
"""
type CloglogLink <: Link01 end
mutable struct CloglogLink <: Link01 end

"""
IdentityLink
The canonical [`Link`](@ref) for the `Normal` distribution, defined as `η = μ`.
"""
type IdentityLink <: Link end
mutable struct IdentityLink <: Link end

"""
InverseLink
The canonical [`Link`](@ref) for [`Distributions.Gamma`](@ref) distribution, defined as `η = inv(μ)`.
"""
type InverseLink <: Link end
mutable struct InverseLink <: Link end

"""
InverseSquareLink
The canonical [`Link`](@ref) for [`Distributions.InverseGaussian`](@ref) distribution, defined as `η = inv(abs2(μ))`.
"""
type InverseSquareLink <: Link end
mutable struct InverseSquareLink <: Link end

"""
LogitLink
Expand All @@ -57,29 +57,29 @@ The canonical [`Link01`](@ref) for [`Distributions.Bernoulli`](@ref) and [`Distr
The inverse link, [`linkinv`](@ref), is the c.d.f. of the standard logistic distribution,
[`Distributions.Logistic`](@ref).
"""
type LogitLink <: Link01 end
mutable struct LogitLink <: Link01 end

"""
LogLink
The canonical [`Link`](@ref) for [`Distributions.Poisson`](@ref), defined as `η = log(μ)`.
"""
type LogLink <: Link end
mutable struct LogLink <: Link end

"""
ProbitLink
A [`Link01`](@ref) whose [`linkinv`](@ref) is the c.d.f. of the standard normal
distribution, ()`Distributions.Normal()`).
"""
type ProbitLink <: Link01 end
mutable struct ProbitLink <: Link01 end

"""
SqrtLink
A [`Link`](@ref) defined as `η = √μ`
"""
type SqrtLink <: Link end
mutable struct SqrtLink <: Link end

"""
linkfun(L::Link, μ)
Expand Down Expand Up @@ -174,10 +174,10 @@ function inverselink(::CauchitLink, η)
end

linkfun(::CloglogLink, μ) = log(-log1p(-μ))
function linkinv{T<:Real}(::CloglogLink, η::T)
function linkinv(::CloglogLink, η::T) where T<:Real
clamp(-expm1(-exp(η)), eps(T), one(T) - eps(T))
end
function mueta{T<:Real}(::CloglogLink, η::T)
function mueta(::CloglogLink, η::T) where T<:Real
max(eps(T), exp(η) * exp(-exp(η)))
end
function inverselink(::CloglogLink, η)
Expand Down
46 changes: 21 additions & 25 deletions src/linpred.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ function installbeta!(p::LinPred, f::Real=1.)
p.beta0
end

type DensePredQR{T<:BlasReal} <: DensePred
mutable struct DensePredQR{T<:BlasReal} <: DensePred
X::Matrix{T} # model matrix
beta0::Vector{T} # base coefficient vector
delbeta::Vector{T} # coefficient increment
scratchbeta::Vector{T}
qr::QRCompactWY{T}
function (::Type{DensePredQR{T}}){T}(X::Matrix{T}, beta0::Vector{T})
function DensePredQR{T}(X::Matrix{T}, beta0::Vector{T}) where T
n, p = size(X)
length(beta0) == p || throw(DimensionMismatch("length(β0) ≠ size(X,2)"))
new{T}(X, beta0, zeros(T,p), zeros(T,p), qrfact(X))
end
end
(::Type{DensePredQR})(X::Matrix, beta0::Vector) = DensePredQR{eltype(X)}(X, beta0)
convert{T}(::Type{DensePredQR{T}}, X::Matrix{T}) = DensePredQR{T}(X, zeros(T, size(X, 2)))
DensePredQR(X::Matrix, beta0::Vector) = DensePredQR{eltype(X)}(X, beta0)
convert(::Type{DensePredQR{T}}, X::Matrix{T}) where {T} = DensePredQR{T}(X, zeros(T, size(X, 2)))

function delbeta!{T<:BlasReal}(p::DensePredQR{T}, r::Vector{T})
function delbeta!(p::DensePredQR{T}, r::Vector{T}) where T<:BlasReal
p.delbeta = p.qr\r
return p
end

type DensePredChol{T<:BlasReal,C} <: DensePred
mutable struct DensePredChol{T<:BlasReal,C} <: DensePred
X::Matrix{T} # model matrix
beta0::Vector{T} # base vector for coefficients
delbeta::Vector{T} # coefficient increment
Expand All @@ -57,41 +57,37 @@ end
cholpred(X::StridedMatrix) = DensePredChol(X)

cholfactors(c::Cholesky) = c.factors
Base.LinAlg.cholfact!{T<:FP}(p::DensePredChol{T}) = p.chol
Base.LinAlg.cholfact!(p::DensePredChol{T}) where {T<:FP} = p.chol

if VERSION < v"0.7.0-DEV.393"
Base.LinAlg.cholfact{T<:FP}(p::DensePredQR{T}) = Cholesky{T,typeof(p.X)}(copy(p.qr[:R]), 'U')
function Base.LinAlg.cholfact{T<:FP}(p::DensePredChol{T})
Base.LinAlg.cholfact(p::DensePredQR{T}) where {T<:FP} = Cholesky{T,typeof(p.X)}(copy(p.qr[:R]), 'U')
function Base.LinAlg.cholfact(p::DensePredChol{T}) where T<:FP
c = p.chol
return Cholesky(copy(cholfactors(c)), c.uplo)
end
Base.LinAlg.cholfact!{T<:FP}(p::DensePredQR{T}) = Cholesky{T,typeof(p.X)}(p.qr[:R], 'U')
Base.LinAlg.cholfact!(p::DensePredQR{T}) where {T<:FP} = Cholesky{T,typeof(p.X)}(p.qr[:R], 'U')
else
Base.LinAlg.cholfact{T<:FP}(p::DensePredQR{T}) = Cholesky{T,typeof(p.X)}(copy(p.qr[:R]), 'U', 0)
function Base.LinAlg.cholfact{T<:FP}(p::DensePredChol{T})
Base.LinAlg.cholfact(p::DensePredQR{T}) where {T<:FP} = Cholesky{T,typeof(p.X)}(copy(p.qr[:R]), 'U', 0)
function Base.LinAlg.cholfact(p::DensePredChol{T}) where T<:FP
c = p.chol
return Cholesky(copy(cholfactors(c)), c.uplo, c.info)
end
Base.LinAlg.cholfact!{T<:FP}(p::DensePredQR{T}) = Cholesky{T,typeof(p.X)}(p.qr[:R], 'U', 0)
Base.LinAlg.cholfact!(p::DensePredQR{T}) where {T<:FP} = Cholesky{T,typeof(p.X)}(p.qr[:R], 'U', 0)
end

function delbeta!{T<:BlasReal}(p::DensePredChol{T}, r::Vector{T})
function delbeta!(p::DensePredChol{T}, r::Vector{T}) where T<:BlasReal
A_ldiv_B!(p.chol, At_mul_B!(p.delbeta, p.X, r))
p
end

function delbeta!{T<:BlasReal}(p::DensePredChol{T}, r::Vector{T}, wt::Vector{T})
function delbeta!(p::DensePredChol{T}, r::Vector{T}, wt::Vector{T}) where T<:BlasReal
scr = scale!(p.scratch, wt, p.X)
if VERSION < v"0.5.0-dev+4677"
cholfact!(At_mul_B!(cholfactors(p.chol), scr, p.X), :U)
else
cholfact!(Hermitian(At_mul_B!(cholfactors(p.chol), scr, p.X), :U))
end
cholfact!(Hermitian(At_mul_B!(cholfactors(p.chol), scr, p.X), :U))
A_ldiv_B!(p.chol, At_mul_B!(p.delbeta, scr, r))
p
end

type SparsePredChol{T,M<:SparseMatrixCSC,C} <: GLM.LinPred
mutable struct SparsePredChol{T,M<:SparseMatrixCSC,C} <: GLM.LinPred
X::M # model matrix
Xt::M # X'
beta0::Vector{T} # base vector for coefficients
Expand All @@ -100,7 +96,7 @@ type SparsePredChol{T,M<:SparseMatrixCSC,C} <: GLM.LinPred
chol::C
scratch::M
end
function SparsePredChol{T}(X::SparseMatrixCSC{T})
function SparsePredChol(X::SparseMatrixCSC{T}) where T
chol = cholfact(speye(size(X, 2)))
return SparsePredChol{eltype(X),typeof(X),typeof(chol)}(X,
X',
Expand All @@ -113,15 +109,15 @@ end

cholpred(X::SparseMatrixCSC) = SparsePredChol(X)

function delbeta!{T}(p::SparsePredChol{T}, r::Vector{T}, wt::Vector{T})
function delbeta!(p::SparsePredChol{T}, r::Vector{T}, wt::Vector{T}) where T
scr = scale!(p.scratch, wt, p.X)
XtWX = p.Xt*scr
c = p.chol = cholfact(Symmetric{eltype(XtWX),typeof(XtWX)}(XtWX, 'L'))
p.delbeta = c\Ac_mul_B!(p.delbeta, scr, r)
end

Base.cholfact{T}(p::SparsePredChol{T}) = copy(p.chol)
Base.cholfact!{T}(p::SparsePredChol{T}) = p.chol
Base.cholfact(p::SparsePredChol{T}) where {T} = copy(p.chol)
Base.cholfact!(p::SparsePredChol{T}) where {T} = p.chol

invchol(x::DensePred) = inv(cholfact!(x))
invchol(x::SparsePredChol) = cholfact!(x) \ eye(size(x.X, 2))
Expand Down
Loading

0 comments on commit dbad91a

Please sign in to comment.