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

Fix nonpositive weights bug #87

Merged
merged 4 commits into from
Dec 9, 2024
Merged

Conversation

pearlzli
Copy link
Contributor

This pull request fixes a bug in which passing in a weight vector with nonpositive elements would throw the following error:

julia> using DataFrames, Econometrics, Random;

julia> Random.seed!(123);

julia> x = randn(100); y = 2*x + randn(100); w = randn(100);

julia> data = DataFrame(x = x, y = y, w = w);

julia> fit(EconometricModel, @formula(y ~ x), data; wts = :w)
ERROR: MethodError: no method matching StatsBase.FrequencyWeights(::Vector{Union{Missing, Float64}})

Closest candidates are:
  StatsBase.FrequencyWeights(::AbstractVector{<:Real})
   @ StatsBase ~/.julia/packages/StatsBase/XgjIN/src/weights.jl:16
  StatsBase.FrequencyWeights(::var"#24#V", ::var"#22#S") where {var"#22#S"<:Real, var"#23#T"<:Real, var"#24#V"<:AbstractVector{var"#23#T"}}
   @ StatsBase ~/.julia/packages/StatsBase/XgjIN/src/weights.jl:13

Stacktrace:
 [1] decompose(f::StatsModels.FormulaTerm{StatsModels.Term, StatsModels.Term}, data::DataFrame, contrasts::Dict{Symbol, Union{StatsModels.AbstractContrasts, StatsModels.AbstractTerm}}, wts::Symbol, panel::Nothing, time::Nothing, estimator::Type{EconometricModel}, vce::Econometrics.VCE)
   @ Econometrics ~/.julia/dev/Econometrics/src/formula.jl:160
 [2] EconometricModel(estimator::Type{EconometricModel}, f::StatsModels.FormulaTerm{StatsModels.Term, StatsModels.Term}, data::DataFrame; contrasts::Dict{Symbol, Union{StatsModels.AbstractContrasts, StatsModels.AbstractTerm}}, wts::Symbol, panel::Nothing, time::Nothing, vce::Econometrics.VCE)
   @ Econometrics ~/.julia/dev/Econometrics/src/main.jl:64
 [3] fit(estimator::Type{EconometricModel}, f::StatsModels.FormulaTerm{StatsModels.Term, StatsModels.Term}, data::DataFrame; fit::Bool, kw::Base.Pairs{Symbol, Symbol, Tuple{Symbol}, NamedTuple{(:wts,), Tuple{Symbol}}})
   @ Econometrics ~/.julia/dev/Econometrics/src/main.jl:216
 [4] top-level scope
   @ REPL[13]:1

Before this change, while the following lines of decompose did filter out the rows of data with nonpositive weights (as well as missing values of the other variables in the formula)...

data = materializer(data)(
row
for
row rows(data) if all(pn -> !ismissing(getproperty(row, pn)), propertynames(row))
)

... this wasn't being done for the wts vector itself, which was defined earlier using all rows of the dataset:
wts = ifelse.(getproperty(data, wts) .≤ 0, missing, getproperty(data, wts))

This same unfiltered weight vector was later passed into FrequencyWeights(...) with missing values, resulting in the error:
wts =
isnothing(wts) ? FrequencyWeights(ones(length(y))) : FrequencyWeights(collect(wts))

@Nosferican
Copy link
Owner

Thanks! Could you rebase (it should pass the tests then) and then add a test? You can add it to a new test file issues.jl and use the example above.

@pearlzli
Copy link
Contributor Author

pearlzli commented Dec 8, 2024

Sorry for the delay! I've now rebased and added the test.

Copy link

codecov bot commented Dec 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.92%. Comparing base (f8751d1) to head (446b217).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #87      +/-   ##
==========================================
- Coverage   91.32%   90.92%   -0.41%     
==========================================
  Files           9        9              
  Lines         715      716       +1     
==========================================
- Hits          653      651       -2     
- Misses         62       65       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Nosferican Nosferican merged commit a622c52 into Nosferican:master Dec 9, 2024
2 of 3 checks passed
@Nosferican
Copy link
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants