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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pearlzli
Copy link

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.

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