Skip to content
Open
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
22 changes: 17 additions & 5 deletions src/glmfit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -846,15 +846,27 @@ function _initialeta!(eta::AbstractVector,
end

# Helper function to check that the values of y are in the allowed domain
function checky(y, d::Distribution)
if any(x -> !insupport(d, x), y)
throw(ArgumentError("y must be in the support of D"))
function checky(y::AbstractVector, d::Distribution)
for yy in y
if !insupport(d, yy)
minsupport, maxsupport = extrema(d)
left = insupport(d, minsupport) ? '[' : '('
right = insupport(d, maxsupport) ? ']' : ')'
discrete = d isa DiscreteUnivariateDistribution && eltype(d) !== Bool ?
"discrete over " : ""
throw(ArgumentError("value $yy in response is not in the support of the " *
"$(nameof(typeof(d))) distribution, which is $(discrete)" *
"$left$minsupport, $maxsupport$right"))
end
end
return nothing
end
function checky(y, d::Binomial)
# we allow proportions but Distributions.jl takes only the number of successes
function checky(y::AbstractVector, d::Binomial)
for yy in y
0 ≤ yy ≤ 1 || throw(ArgumentError("$yy in y is not in [0,1]"))
0 ≤ yy ≤ 1 ||
throw(ArgumentError("value $yy in response is not in the support of " *
"the Binomial distribution, which is [0, 1]"))
end
return nothing
end
Expand Down
27 changes: 25 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1708,8 +1708,6 @@ end
[coef(lm1), stderror(lm1), coef(lm1) ./ stderror(lm1)]
@test t.cols[4] ≈ [0.5515952883836446, 3.409192065429258e-7]
@test hcat(t.cols[5:6]...) == confint(lm1)
# TODO: call coeftable(gm1, ...) directly once DataFrameRegressionModel
# supports keyword arguments
t = coeftable(lm1; level=0.99)
@test hcat(t.cols[5:6]...) == confint(lm1; level=0.99)

Expand Down Expand Up @@ -2659,3 +2657,28 @@ end
x -0.502451 0.675377 -0.74 0.4982 -2.3776 1.3727
─────────────────────────────────────────────────────────────────────────"""
end

@testset "response in support" begin
@test GLM.checky([0.0, -0.0, 1.0, -1.0, Inf, -Inf], Normal()) === nothing
@test_throws(ArgumentError("value NaN in response is not in the support of " *
"the Normal distribution, which is [-Inf, Inf]"),
GLM.checky([NaN], Normal()))

@test GLM.checky([0.0, -0.0, 1.0], Poisson()) === nothing
@test_throws(ArgumentError("value 0.5 in response is not in the support of " *
"the Poisson distribution, which is discrete over [0, Inf)"),
GLM.checky([0.5], Poisson()))
@test_throws(ArgumentError("value Inf in response is not in the support of " *
Comment thread
nalimilan marked this conversation as resolved.
"the Poisson distribution, which is discrete over [0, Inf)"),
GLM.checky([Inf], Poisson()))

@test GLM.checky([0.0, -0.0, 1.0, 0.5], Binomial()) === nothing
@test_throws(ArgumentError("value 2.0 in response is not in the support of " *
Comment thread
nalimilan marked this conversation as resolved.
"the Binomial distribution, which is [0, 1]"),
GLM.checky([2.0], Binomial()))

Comment thread
nalimilan marked this conversation as resolved.
@test GLM.checky([0.0, -0.0, 1.0], Bernoulli()) === nothing
@test_throws(ArgumentError("value 0.5 in response is not in the support of " *
"the Bernoulli distribution, which is [false, true]"),
GLM.checky([0.5], Bernoulli()))
end
Loading