From a622c5241eade70ce2b8f288226c710551e6c457 Mon Sep 17 00:00:00 2001 From: Pearl Li Date: Sun, 8 Dec 2024 23:24:39 -0500 Subject: [PATCH] Fix nonpositive weights bug (#87) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix nonpositive weights bug --------- Co-authored-by: José Bayoán Santiago Calderón --- src/formula.jl | 4 ++-- test/issues.jl | 11 +++++++++++ test/runtests.jl | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 test/issues.jl diff --git a/src/formula.jl b/src/formula.jl index 7afc486..d8b0b36 100644 --- a/src/formula.jl +++ b/src/formula.jl @@ -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) |> ( @@ -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, diff --git a/test/issues.jl b/test/issues.jl new file mode 100644 index 0000000..7c724bf --- /dev/null +++ b/test/issues.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 999cc2e..18f3545 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -12,6 +12,7 @@ for file in [ "ologit", "formula_display", "mlj", + "issues", "docs" ] include("$file.jl")