44Exclude rows that are invalid for variance-covariance estimator.
55See also [`CheckVcov`](@ref).
66"""
7- checkvcov! (data, esample:: BitVector ,
7+ checkvcov! (data, esample:: BitVector , aux :: BitVector ,
88 vce:: Union{Vcov.SimpleCovariance, Vcov.RobustCovariance} ) = NamedTuple ()
99
10- function checkvcov! (data, esample:: BitVector , vce:: Vcov.ClusterCovariance )
11- esample .&= Vcov. completecases (data, vce)
10+ function checkvcov! (data, esample:: BitVector , aux:: BitVector , vce:: Vcov.ClusterCovariance )
11+ for name in Vcov. names (vce)
12+ col = getcolumn (data, name)
13+ if Missing <: eltype (col)
14+ aux .= .! ismissing .(col)
15+ esample .&= aux
16+ end
17+ end
1218 return (esample= esample,)
1319end
1420
@@ -20,7 +26,7 @@ exclude rows that are invalid for variance-covariance estimator.
2026"""
2127const CheckVcov = StatsStep{:CheckVcov , typeof (checkvcov!), true }
2228
23- required (:: CheckVcov ) = (:data , :esample )
29+ required (:: CheckVcov ) = (:data , :esample , :aux )
2430default (:: CheckVcov ) = (vce= Vcov. robust (),)
2531copyargs (:: CheckVcov ) = (2 ,)
2632
@@ -163,7 +169,7 @@ default(::MakeYXCols) = (contrasts=nothing, fetol=1e-8, femaxiter=10000)
163169
164170function combinedargs (:: MakeYXCols , allntargs)
165171 yx = TermSet ()
166- @inbounds for nt in allntargs
172+ for nt in allntargs
167173 push! (yx, nt. yterm)
168174 foreach (x-> push! (yx, x), nt. xterms)
169175 end
@@ -182,15 +188,15 @@ function maketreatcols(data, treatname::Symbol, treatintterms::TermSet,
182188 weights:: AbstractWeights , esample:: BitVector ,
183189 cohortinteracted:: Bool , fetol:: Real , femaxiter:: Int ,
184190 :: Type{DynamicTreatment{SharpDesign}} , time:: Symbol ,
185- exc:: IdDict {Int,Int} , notreat:: IdDict{TimeType ,Int} )
191+ exc:: Dict {Int,Int} , notreat:: IdDict{ValidTimeType ,Int} )
186192
187193 nobs = sum (esample)
188194 # Putting treatname before time avoids sorting twice if cohortinteracted
189195 cellnames = Symbol[treatname, time, sort! (termvars (treatintterms))... ]
190196 cols = subcolumns (data, cellnames, esample)
191197 cells, rows = cellrows (cols, findcell (cols))
192198
193- rel = cells[2 ] .- cells[1 ]
199+ rel = refarray ( cells[2 ]) .- refarray ( cells[1 ])
194200 kept = .! haskey .(Ref (exc), rel) .& .! haskey .(Ref (notreat), cells[1 ])
195201 treatrows = rows[kept]
196202 # Construct cells needed for treatment indicators
@@ -283,17 +289,17 @@ combinedargs(step::MakeTreatCols, allntargs) =
283289
284290# Obtain the relative time periods excluded by all tr in allntargs
285291function combinedargs (:: MakeTreatCols , allntargs, :: Type{DynamicTreatment{SharpDesign}} )
286- exc = IdDict {Int,Int} ()
287- notreat = IdDict {TimeType ,Int} ()
292+ exc = Dict {Int,Int} ()
293+ notreat = IdDict {ValidTimeType ,Int} ()
288294 @inbounds for nt in allntargs
289295 foreach (x-> _count! (exc, x), nt. tr. exc)
290296 foreach (x-> _count! (notreat, x), nt. pr. e)
291297 end
292298 nnt = length (allntargs)
293- @inbounds for (k, v) in exc
299+ for (k, v) in exc
294300 v == nnt || delete! (exc, k)
295301 end
296- @inbounds for (k, v) in notreat
302+ for (k, v) in notreat
297303 v == nnt || delete! (notreat, k)
298304 end
299305 return (exc, notreat)
@@ -339,14 +345,14 @@ function solveleastsquares!(tr::DynamicTreatment{SharpDesign}, pr::TrendParallel
339345 X = hcat (tcols... , (yxcols[x] for x in xs). .. )
340346
341347 ntcols = length (tcols)
342- basecols = trues (size (X,2 ))
348+ basiscols = trues (size (X,2 ))
343349 if size (X, 2 ) > ntcols
344- basecols = basecol (X)
350+ basiscols = diag ( invsym! (X ' X)) .> 0
345351 # Do not drop any treatment indicator
346- sum (basecols [1 : ntcols]) == ntcols ||
352+ sum (basiscols [1 : ntcols]) == ntcols ||
347353 error (" covariates are collinear with treatment indicators" )
348- sum (basecols ) < size (X, 2 ) &&
349- (X = X[:, basecols ])
354+ sum (basiscols ) < size (X, 2 ) &&
355+ (X = X[:, basiscols ])
350356 end
351357
352358 crossx = cholesky! (Symmetric (X' X))
@@ -356,7 +362,7 @@ function solveleastsquares!(tr::DynamicTreatment{SharpDesign}, pr::TrendParallel
356362 return (coef= coef:: Vector{Float64} , X= X:: Matrix{Float64} ,
357363 crossx= crossx:: Cholesky{Float64,Matrix{Float64}} ,
358364 residuals= residuals:: Vector{Float64} , treatcells= treatcells:: VecColumnTable ,
359- xterms= xs:: Vector{AbstractTerm} , basecols = basecols :: BitVector )
365+ xterms= xs:: Vector{AbstractTerm} , basiscols = basiscols :: BitVector )
360366end
361367
362368"""
0 commit comments