Skip to content

Commit

Permalink
Add invcrossmodelmatrix as field of VcovData
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieugomez committed Nov 30, 2023
1 parent 269f1d0 commit 902e005
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Vcov"
uuid = "ec2bfdc2-55df-4fc9-b9ae-4958c2cf2486"
version = "0.7.1"
version = "0.8.0"

[deps]
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
Expand Down
6 changes: 4 additions & 2 deletions src/Vcov.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ using Base: @propagate_inbounds
## Mimimum RegressionModel used in Vcov
##
##############################################################################
struct VcovData{T, N} <: RegressionModel
struct VcovData{T, Tu, N} <: RegressionModel
modelmatrix::Matrix{Float64} # X
crossmodelmatrix::T # X'X in the simplest case. Can be Matrix but preferably Factorization
crossmodelmatrix::T # X'X
invcrossmodelmatrix::Tu # inv(X'X)
residuals::Array{Float64, N} # vector or matrix of residuals (matrix in the case of IV, residuals of Xendo on (Z, Xexo))
dof_residual::Int
end
StatsAPI.modelmatrix(x::VcovData) = x.modelmatrix
StatsAPI.crossmodelmatrix(x::VcovData) = x.crossmodelmatrix
invcrossmodelmatrix(x::VcovData) = x.invcrossmodelmatrix
StatsAPI.residuals(x::VcovData) = x.residuals
StatsAPI.dof_residual(x::VcovData) = x.dof_residual

Expand Down
3 changes: 2 additions & 1 deletion src/covarianceestimators/vcovcluster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function S_hat(x::RegressionModel, v::ClusterCovariance)
# I choose the first option following reghdfe
G = minimum(nclusters(v))
rmul!(S, (size(modelmatrix(x), 1) - 1) / dof_residual(x) * G / (G - 1))
return Symmetric(S)
end

# res is a Vector in OLS, Matrix in IV
Expand All @@ -111,7 +112,7 @@ function helper_cluster(X::Matrix, res::Union{Vector, Matrix}, g::GroupedArray)
end

function StatsAPI.vcov(x::RegressionModel, v::ClusterCovariance)
xtx = inv(crossmodelmatrix(x))
xtx = invcrossmodelmatrix(x)
pinvertible(Symmetric(xtx * S_hat(x, v) * xtx))
end

2 changes: 1 addition & 1 deletion src/covarianceestimators/vcovrobust.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function S_hat(x::RegressionModel, ::RobustCovariance)
end

function StatsAPI.vcov(x::RegressionModel, v::RobustCovariance)
xtx = inv(crossmodelmatrix(x))
xtx = invcrossmodelmatrix(x)
pinvertible(Symmetric(xtx * S_hat(x, v) * xtx))
end

7 changes: 3 additions & 4 deletions src/covarianceestimators/vcovsimple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ Base.show(io::IO, ::SimpleCovariance) =
print(io, "Simple covariance estimator")

function S_hat(x::RegressionModel, ::SimpleCovariance)
rmul!(crossmodelmatrix(x), sum(abs2, residuals(x)))
Symmetric(crossmodelmatrix(x) .* sum(abs2, residuals(x)))
end

function StatsAPI.vcov(x::RegressionModel, ::SimpleCovariance)
invcrossmodelmatrix = Matrix(inv(crossmodelmatrix(x)))
rmul!(invcrossmodelmatrix, sum(abs2, residuals(x)) / dof_residual(x))
Symmetric(invcrossmodelmatrix)
xtx = invcrossmodelmatrix(x)
Symmetric(xtx .* (sum(abs2, residuals(x)) / dof_residual(x)))
end

2 changes: 1 addition & 1 deletion src/ranktest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function ranktest!(X::Matrix{Float64},
vlab = cholesky!(Hermitian((kronv * kronv') ./ size(X, 1)), check = false)
else
K = kron(Gmatrix, Fmatrix)'
vcovmodel = Vcov.VcovData(Z, K, X, size(Z, 1) - dof_small - dof_fes)
vcovmodel = Vcov.VcovData(Z, K, X, nothing, size(Z, 1) - dof_small - dof_fes)
matrix_vcov2 = Vcov.S_hat(vcovmodel, vcov_method)
vhat = K \ (K \ matrix_vcov2)'
vlab = cholesky!(Hermitian(kronv * vhat * kronv'), check = false)
Expand Down

0 comments on commit 902e005

Please sign in to comment.