|
201 | 201 | nt = (tr=tr, yterm=term(:oop_spend), xterms=(term(1),), yxcols=yxcols0, treatcols=tcols0) |
202 | 202 | ret, share = solveleastsquares(nt...) |
203 | 203 | # Compare estimates with Stata |
| 204 | + # gen col0 = wave_hosp==10 & wave==10 |
| 205 | + # gen col1 = wave_hosp==10 & wave==11 |
| 206 | + # reg oop_spend col0 col1 |
204 | 207 | @test ret.coef[1] ≈ 2862.4141 atol=1e-4 |
205 | 208 | @test ret.coef[2] ≈ 490.44869 atol=1e-4 |
206 | 209 | @test ret.coef[3] ≈ 3353.6565 atol=1e-4 |
|
224 | 227 | res = SolveLeastSquares()(nt) |
225 | 228 | @test res.coef == ret.coef |
226 | 229 | end |
| 230 | + |
| 231 | +@testset "EstVcov" begin |
| 232 | + hrs = exampledata("hrs") |
| 233 | + nobs = size(hrs, 1) |
| 234 | + col0 = convert(Vector{Float64}, (hrs.wave_hosp.==10).&(hrs.wave.==10)) |
| 235 | + col1 = convert(Vector{Float64}, (hrs.wave_hosp.==10).&(hrs.wave.==11)) |
| 236 | + y = convert(Vector{Float64}, hrs.oop_spend) |
| 237 | + X = hcat(col0, col1, ones(nobs, 1)) |
| 238 | + crossx = cholesky!(Symmetric(X'X)) |
| 239 | + coef = crossx \ (X'y) |
| 240 | + residuals = y - X * coef |
| 241 | + nt = (data=hrs, esample=trues(nobs), vcov=Vcov.simple(), X=X, crossx=crossx, |
| 242 | + residuals=residuals, fes=FixedEffect[]) |
| 243 | + ret, share = estvcov(nt...) |
| 244 | + # Compare estimates with Stata |
| 245 | + # reg oop_spend col0 col1 |
| 246 | + # mat list e(V) |
| 247 | + @test ret.vcov_mat[1,1] ≈ 388844.2 atol=0.1 |
| 248 | + @test ret.vcov_mat[2,2] ≈ 388844.2 atol=0.1 |
| 249 | + @test ret.vcov_mat[3,3] ≈ 20334.169 atol=1e-3 |
| 250 | + @test ret.vcov_mat[2,1] ≈ 20334.169 atol=1e-3 |
| 251 | + @test ret.vcov_mat[3,1] ≈ -20334.169 atol=1e-3 |
| 252 | + @test ret.dof_residuals == nobs - 3 |
| 253 | + |
| 254 | + nt = merge(nt, (vcov=Vcov.robust(), fes=FixedEffect[])) |
| 255 | + ret, share = estvcov(nt...) |
| 256 | + # Compare estimates with Stata |
| 257 | + # reg oop_spend col0 col1, r |
| 258 | + # mat list e(V) |
| 259 | + @test ret.vcov_mat[1,1] ≈ 815817.44 atol=0.1 |
| 260 | + @test ret.vcov_mat[2,2] ≈ 254993.93 atol=1e-2 |
| 261 | + @test ret.vcov_mat[3,3] ≈ 19436.209 atol=1e-3 |
| 262 | + @test ret.vcov_mat[2,1] ≈ 19436.209 atol=1e-3 |
| 263 | + @test ret.vcov_mat[3,1] ≈ -19436.209 atol=1e-3 |
| 264 | + @test ret.dof_residuals == nobs - 3 |
| 265 | + |
| 266 | + nt = merge(nt, (vcov=Vcov.cluster(:hhidpn),)) |
| 267 | + ret, share = estvcov(nt...) |
| 268 | + # Compare estimates with Stata |
| 269 | + # reghdfe oop_spend col0 col1, noa clu(hhidpn) |
| 270 | + # mat list e(V) |
| 271 | + @test ret.vcov_mat[1,1] ≈ 744005.01 atol=0.1 |
| 272 | + @test ret.vcov_mat[2,2] ≈ 242011.45 atol=1e-2 |
| 273 | + @test ret.vcov_mat[3,3] ≈ 28067.783 atol=1e-3 |
| 274 | + @test ret.vcov_mat[2,1] ≈ 94113.386 atol=1e-2 |
| 275 | + @test ret.vcov_mat[3,1] ≈ 12640.559 atol=1e-2 |
| 276 | + @test ret.dof_residuals == nobs - 3 |
| 277 | + |
| 278 | + fes = FixedEffect[FixedEffect(hrs.hhidpn)] |
| 279 | + wt = uweights(nobs) |
| 280 | + feM = AbstractFixedEffectSolver{Float64}(fes, wt, Val{:cpu}, Threads.nthreads()) |
| 281 | + X = hcat(col0, col1) |
| 282 | + _feresiduals!(Combination(y, X), feM, 1e-8, 10000) |
| 283 | + crossx = cholesky!(Symmetric(X'X)) |
| 284 | + coef = crossx \ (X'y) |
| 285 | + residuals = y - X * coef |
| 286 | + nt = merge(nt, (X=X, crossx=crossx, residuals=residuals, fes=fes, vcov=Vcov.robust())) |
| 287 | + ret, share = estvcov(nt...) |
| 288 | + # Compare estimates with Stata |
| 289 | + # reghdfe oop_spend col0 col1, a(hhidpn) vce(robust) |
| 290 | + # mat list e(V) |
| 291 | + @test ret.vcov_mat[1,1] ≈ 654959.97 atol=0.1 |
| 292 | + @test ret.vcov_mat[2,2] ≈ 503679.27 atol=0.1 |
| 293 | + @test ret.vcov_mat[2,1] ≈ 192866.2 atol=0.1 |
| 294 | + @test ret.dof_residuals == nobs - nunique(fes[1]) - 2 |
| 295 | + |
| 296 | + nt = merge(nt, (vcov=Vcov.cluster(:hhidpn),)) |
| 297 | + ret, share = estvcov(nt...) |
| 298 | + # Compare estimates with Stata |
| 299 | + # reghdfe oop_spend col0 col1, a(hhidpn) clu(hhidpn) |
| 300 | + # mat list e(V) |
| 301 | + @test ret.vcov_mat[1,1] ≈ 606384.66 atol=0.1 |
| 302 | + @test ret.vcov_mat[2,2] ≈ 404399.89 atol=0.1 |
| 303 | + @test ret.vcov_mat[2,1] ≈ 106497.43 atol=0.1 |
| 304 | + @test ret.dof_residuals == nobs - 3 |
| 305 | +end |
0 commit comments