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

Parametrize by array type instead of eltype #56

Merged
merged 1 commit into from
Feb 24, 2014
Merged
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
1 change: 1 addition & 0 deletions src/GLM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module GLM
wrkresp # working response

typealias FP FloatingPoint
typealias FPStoredVector{T<:FloatingPoint} StoredArray{T,1}

abstract ModResp # model response

Expand Down
40 changes: 20 additions & 20 deletions src/glmfit.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
type GlmResp{T<:FP} <: ModResp # response in a glm model
y::AbstractVector{T} # response
type GlmResp{V<:FPStoredVector} <: ModResp # response in a glm model
y::V # response
d::UnivariateDistribution
l::Link
devresid::AbstractVector{T} # (squared) deviance residuals
eta::AbstractVector{T} # linear predictor
mu::AbstractVector{T} # mean response
mueta::AbstractVector{T} # derivative of mu w.r.t. eta
offset::AbstractVector{T} # offset added to linear predictor (usually 0)
var::AbstractVector{T} # (unweighted) variance at current mu
wts::AbstractVector{T} # prior weights
wrkresid::AbstractVector{T} # working residuals
function GlmResp(y::AbstractVector{T}, d::UnivariateDistribution, l::Link,
eta::AbstractVector{T}, mu::AbstractVector{T},
off::AbstractVector{T}, wts::AbstractVector{T})
devresid::V # (squared) deviance residuals
eta::V # linear predictor
mu::V # mean response
mueta::V # derivative of mu w.r.t. eta
offset::V # offset added to linear predictor (usually 0)
var::V # (unweighted) variance at current mu
wts::V # prior weights
wrkresid::V # working residuals
function GlmResp(y::V, d::UnivariateDistribution, l::Link,
eta::V, mu::V,
off::V, wts::V)
if isa(d, Binomial)
for yy in y; 0. <= yy <= 1. || error("$yy in y is not in [0,1]"); end
else
Expand Down Expand Up @@ -42,14 +42,14 @@ linkinv!(r::GlmResp) = linkinv!(r.l, r.mu, r.eta)
# evaluate the mueta vector (derivative of mu w.r.t. eta) from the linear predictor (eta)
mueta!(r::GlmResp) = mueta!(r.l, r.mueta, r.eta)

function updatemu!{T<:FP}(r::GlmResp{T}, linPr::Vector{T})
function updatemu!{T<:FPStoredVector}(r::GlmResp{T}, linPr::T)
n = length(linPr)
length(r.offset) == n ? map!(Add(), r.eta, linPr, r.offset) : copy!(r.eta, linPr)
linkinv!(r); mueta!(r); var!(r); wrkresid!(r); devresid!(r)
sum(r.devresid)
end

updatemu!{T<:FP}(r::GlmResp{T}, linPr) = updatemu!(r, convert(typeof(r.y),vec(linPr)))
updatemu!{T<:FPStoredVector}(r::GlmResp{T}, linPr) = updatemu!(r, convert(T,vec(linPr)))

var!(r::GlmResp) = var!(r.d, r.var, r.mu)

Expand All @@ -60,9 +60,9 @@ function wrkresp(r::GlmResp)
map(Add(), r.eta, r.wrkresid)
end

function wrkwt{T<:FP}(r::GlmResp{T})
length(r.wts) == 0 && return [(r.mueta[i] * r.mueta[i]/r.var[i])::T for i in 1:length(r.var)]
[(r.wts[i] * r.mueta[i] * r.mueta[i]/r.var[i])::T for i in 1:length(r.var)]
function wrkwt(r::GlmResp)
length(r.wts) == 0 && return [r.mueta[i] * r.mueta[i]/r.var[i] for i in 1:length(r.var)]
[r.wts[i] * r.mueta[i] * r.mueta[i]/r.var[i] for i in 1:length(r.var)]
end

type GlmMod <: LinPredModel
Expand Down Expand Up @@ -126,10 +126,10 @@ function glm(f::Formula, df::AbstractDataFrame, d::UnivariateDistribution, l::Li
end
n = length(y); lw = length(wts)
lw == 0 || lw == n || error("length(wts) = $lw should be 0 or $n")
w = lw == 0 ? ones(T,n) : (T <: Float64 ? copy(wts) : convert(Vector{T}, wts))
w = lw == 0 ? ones(T,n) : (T <: Float64 ? copy(wts) : convert(typeof(y), wts))
mu = mustart(d, y, w)
off = T <: Float64 ? copy(offset) : convert(Vector{T}, offset)
rr = GlmResp{T}(y, d, l, linkfun!(l, similar(mu), mu), mu, off, w)
rr = GlmResp{typeof(y)}(y, d, l, linkfun!(l, similar(mu), mu), mu, off, w)
res = GlmMod(mf, rr, DensePredChol(mm), f, false)
dofit ? fit(res) : res
end
Expand Down
18 changes: 9 additions & 9 deletions src/lm.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
type LmResp{T<:FP} <: ModResp # response in a linear model
mu::Vector{T} # mean response
offset::Vector{T} # offset added to linear predictor (may have length 0)
wts::Vector{T} # prior weights (may have length 0)
y::Vector{T} # response
function LmResp(mu::Vector{T}, off::Vector{T}, wts::Vector{T}, y::Vector{T})
type LmResp{V<:FPStoredVector} <: ModResp # response in a linear model
mu::V # mean response
offset::V # offset added to linear predictor (may have length 0)
wts::V # prior weights (may have length 0)
y::V # response
function LmResp(mu::V, off::V, wts::V, y::V)
n = length(y); length(mu) == n || error("mismatched lengths of mu and y")
ll = length(off); ll == 0 || ll == n || error("length of offset is $ll, must be $n or 0")
ll = length(wts); ll == 0 || ll == n || error("length of wts is $ll, must be $n or 0")
new(mu,off,wts,y)
end
end
LmResp{T<:FP}(y::Vector{T}) = LmResp{T}(zeros(T,length(y)), T[], T[], y)
LmResp{V<:FPStoredVector}(y::V) = LmResp{V}(fill!(similar(y), zero(eltype(V))), similar(y, 0), similar(y, 0), y)

function updatemu!{T<:FP}(r::LmResp{T}, linPr::Vector{T})
function updatemu!{V<:FPStoredVector}(r::LmResp{V}, linPr::V)
n = length(linPr); length(r.y) == n || error("length(linPr) is $n, should be $(length(r.y))")
length(r.offset) == 0 ? copy!(r.mu, linPr) : map!(Add(), r.mu, linPr, r.offset)
deviance(r)
end
updatemu!{T<:FP}(r::LmResp{T}, linPr) = updatemu!(r, convert(Vector{T},vec(linPr)))
updatemu!{V<:FPStoredVector}(r::LmResp{V}, linPr) = updatemu!(r, convert(V,vec(linPr)))

type WtResid <: Functor{3} end
evaluate{T<:FP}(::WtResid,wt::T,y::T,mu::T) = (y - mu)*sqrt(wt)
Expand Down