Skip to content

Commit

Permalink
Fix nonpositive weights bug (#87)
Browse files Browse the repository at this point in the history
* Fix nonpositive weights bug

---------

Co-authored-by: José Bayoán Santiago Calderón <naoyabpr@gmail.com>
  • Loading branch information
pearlzli and Nosferican authored Dec 9, 2024
1 parent 032c0f2 commit a622c52
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/formula.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function decompose(
convert(Vector{Symbol}, filter!(!isnothing, union(termvars(f), [panel, time, wts])))
data = select(data, pns...) |> columntable
if isa(wts, Symbol)
wts = ifelse.(getproperty(data, wts) .≤ 0, missing, getproperty(data, wts))
data = (data..., wts = ifelse.(getproperty(data, wts) .≤ 0, missing, getproperty(data, wts)))
end
categorical_variables =
Tables.schema(data) |> (
Expand Down Expand Up @@ -158,7 +158,7 @@ function decompose(
Z = zeros(0, 0)
end
wts =
isnothing(wts) ? FrequencyWeights(ones(length(y))) : FrequencyWeights(collect(wts))
isnothing(wts) ? FrequencyWeights(ones(length(y))) : FrequencyWeights(collect(getproperty(data, wts)))
if isa(estimator, Type{<:RandomEffectsEstimator})
panel = (
panel,
Expand Down
11 changes: 11 additions & 0 deletions test/issues.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@testset "Issues" begin
@testset "Nonpositive Weights" begin
x = randn(100)
y = 2*x + randn(100)
w = randn(100)
data = DataFrame(x = x, y = y, w = w)
# Test that no error is thrown
# https://github.com/JuliaLang/julia/issues/18780#issuecomment-251534863
@test fit(EconometricModel, @formula(y ~ x), data; wts = :w) isa Any
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ for file in [
"ologit",
"formula_display",
"mlj",
"issues",
"docs"
]
include("$file.jl")
Expand Down

2 comments on commit a622c52

@Nosferican
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register

Release notes:

Fixed bug with nonpositive weights.
Updated compat.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/121054

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.11 -m "<description of version>" a622c5241eade70ce2b8f288226c710551e6c457
git push origin v0.2.11

Please sign in to comment.