Skip to content

Commit 8d81e39

Browse files
committed
add covarianceestimator
1 parent e745ace commit 8d81e39

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/fit.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function regife(df, m::FixedEffectModels.ModelTerm; kwargs...)
1212
regife(df, m.f; m.dict..., kwargs...)
1313
end
1414

15-
function regife(df, f::FormulaTerm, vcov::Vcov.AbstractVcov = Vcov.simple();
15+
function regife(df, f::FormulaTerm, vcov::CovarianceEstimator = Vcov.simple();
1616
weights::Union{Symbol, Nothing} = nothing,
1717
subset::Union{AbstractVector, Nothing} = nothing,
1818
method::Symbol = :dogleg,
@@ -70,10 +70,10 @@ function regife(df, f::FormulaTerm, vcov::Vcov.AbstractVcov = Vcov.simple();
7070
if feformula != nothing # remove after depreciation
7171
vars = vcat(vars, StatsModels.termvars(@eval(@formula(0 ~ $(feformula)))))
7272
end
73-
vcov_vars = StatsModels.termvars(vcov)
7473
factor_vars = [m.id, m.time]
75-
all_vars = unique(vcat(vars, factor_vars, vcov_vars))
74+
all_vars = unique(vcat(vars, factor_vars))
7675
esample = completecases(df[!, all_vars])
76+
esample .&= completecases(df, vcov)
7777
if has_weights
7878
esample .&= BitArray(!ismissing(x) & (x > 0) for x in df[!, weights])
7979
all_vars = unique(vcat(all_vars, weights))
@@ -88,7 +88,7 @@ function regife(df, f::FormulaTerm, vcov::Vcov.AbstractVcov = Vcov.simple();
8888

8989

9090
# Compute data needed for errors
91-
vcov_method_data = Vcov.VcovMethod(df[esample, unique(Symbol.(vcov_vars))], vcov)
91+
vcov_method_data = Vcov.materialize(vcov, view(df, esample,:))
9292

9393
# Compute weights
9494
sqrtw = Ones{Float64}(sum(esample))
@@ -260,8 +260,8 @@ function regife(df, f::FormulaTerm, vcov::Vcov.AbstractVcov = Vcov.simple();
260260
dof_residual = max(size(X, 1) - size(X, 2) - df_absorb_fe, 1)
261261

262262
## estimate vcov matrix
263-
vcov_data = Vcov.VcovData(Xm, crossxm, residualsm, dof_residual)
264-
matrix_vcov = Vcov.vcov!(vcov_method_data, vcov_data)
263+
vcov_data = FixedEffectModels.VcovData(Xm, crossxm, residualsm, dof_residual)
264+
matrix_vcov = StatsBase.vcov(vcov_data, vcov_method_data)
265265
# compute various r2
266266
nobs = sum(esample)
267267
rss = sum(abs2, residualsm)
@@ -318,7 +318,7 @@ function regife(df, f::FormulaTerm, vcov::Vcov.AbstractVcov = Vcov.simple();
318318
if !has_regressors
319319
return FactorResult(esample, augmentdf, rss, iterations, converged)
320320
else
321-
return InteractiveFixedEffectModel(fs.b, matrix_vcov, esample, augmentdf,
321+
return InteractiveFixedEffectModel(fs.b, matrix_vcov, vcov, esample, augmentdf,
322322
coef_names, yname, f, nobs, dof_residual, r2, r2_a, r2_within,
323323
rss, sum(iterations), all(converged))
324324
end

src/types.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ end
249249
struct InteractiveFixedEffectModel <: RegressionModel
250250
coef::Vector{Float64} # Vector of coefficients
251251
vcov::Matrix{Float64} # Covariance matrix
252+
vcov_type::CovarianceEstimator
252253

253254
esample::BitVector # Is the row of the original dataframe part of the estimation sample?
254255
augmentdf::DataFrame

0 commit comments

Comments
 (0)