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

Add invcrossmodelmatrix as field #20

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading