Skip to content

Commit bcdc001

Browse files
committed
missings
adjust esample check for missings fix fix work with esample2 clean up
1 parent bed6bea commit bcdc001

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

src/fit.jl

+43-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,15 @@ function reg(
164164

165165
# Obtain X
166166
_Xexo_ = modelmatrix(formula_schema, subdf)
167-
167+
168+
esample2 = .!ismissing.(_y_)
169+
170+
# PR #109, to be removed when fixed in StatsModels
171+
if size(_Xexo_, 2) > 0
172+
for c in eachcol(_Xexo_)
173+
esample2 .&= .!ismissing.(c)
174+
end
175+
end
168176

169177
response_name, coef_names = coefnames(formula_schema)
170178
if !(coef_names isa Vector)
@@ -183,6 +191,24 @@ function reg(
183191
formula_iv_schema = apply_schema(formula_iv, schema(formula_iv, subdf, contrasts), StatisticalModel)
184192
_Z_ = modelmatrix(formula_iv_schema, subdf)
185193

194+
# PR #109, to be removed when fixed in StatsModels
195+
if size(_Xendo_, 2) > 0
196+
for c in eachcol(_Xendo_)
197+
esample2 .&= .!ismissing.(c)
198+
end
199+
end
200+
201+
# PR #109, to be removed when fixed in StatsModels
202+
for c in eachcol(_Z_)
203+
esample2 .&= .!ismissing.(c)
204+
end
205+
206+
# PR #109, to be removed when fixed in StatsModels
207+
if !all(esample2)
208+
_Xendo_ = _Xendo_[esample2,:]
209+
_Z_ = _Z_[esample2,:]
210+
end
211+
186212
# for a Vector{Float64}, convert(Vector{Float64}, y) aliases y
187213
Xendo = convert(Matrix{Float64}, _Xendo_)
188214
all(isfinite, Xendo) || throw("Some observations for the endogenous variables are infinite")
@@ -194,6 +220,22 @@ function reg(
194220
formula_schema = FormulaTerm(formula_schema.lhs, (tuple(eachterm(formula_schema.rhs)..., (term for term in eachterm(formula_endo_schema.rhs) if term != ConstantTerm(0))...)))
195221
end
196222

223+
# PR #109, to be removed when fixed in StatsModels
224+
if !all(esample2)
225+
if esample != Colon() && !all(esample)
226+
throw(ArgumentError("You passed a dataset missing with observations and used formula terms that introduce missings. This is not yet supported."))
227+
end
228+
_y_ = _y_[esample2]
229+
_Xexo_ = _Xexo_[esample2,:]
230+
231+
if esample == Colon()
232+
esample = esample2
233+
else
234+
esample .&= esample2
235+
end
236+
nobs = sum(esample)
237+
end
238+
197239
# for a Vector{Float64}, convert(Vector{Float64}, y) aliases y
198240
y = convert(Vector{Float64}, _y_)
199241
all(isfinite, y) || throw("Some observations for the dependent variable are infinite")

0 commit comments

Comments
 (0)