@@ -236,8 +236,8 @@ function StatsAPI.fit(::Type{FixedEffectModel},
236236
237237 # compute tss now before potentially demeaning y
238238 tss_total = tss (y, has_intercept | has_fe_intercept, weights)
239- # create unitilaized
240- iterations, converged, r2_within = nothing , nothing , nothing
239+ # initalize fields
240+ iterations, converged, r2_within = 0 , true , nothing
241241 F_kp, p_kp = nothing , nothing
242242 collinear_fe = falses (length (var_names_all))
243243
@@ -441,22 +441,20 @@ function StatsAPI.fit(::Type{FixedEffectModel},
441441 end
442442 end
443443 end
444- dof_residual_ = max (1 , nobs - size (X, 2 ) - dof_fes - dof_add)
445- dof_ = max (1 , size (X, 2 ) - (has_intercept | has_fe_intercept))
446-
447444
448445 nclusters = nothing
449446 if vcov isa Vcov. ClusterCovariance
450447 nclusters = Vcov. nclusters (vcov_method)
451448 end
452449
453-
454450 # Compute standard error
455- vcov_data = Vcov. VcovData (Xhat, crossx, residuals, dof_residual_ )
451+ vcov_data = Vcov. VcovData (Xhat, crossx, residuals, nobs - size (X, 2 ) - dof_fes - dof_add )
456452 matrix_vcov = StatsAPI. vcov (vcov_data, vcov_method)
457453
458454 # Compute Fstat
459455 F = Fstat (coef, matrix_vcov, has_intercept)
456+ # dof_ is the number of estimated coefficients beyond the intercept.
457+ dof_ = size (X, 2 ) - has_intercept
460458 dof_tstat_ = max (1 , Vcov. dof_residual (vcov_data, vcov_method) - has_intercept | has_fe_intercept)
461459 p = fdistccdf (dof_, dof_tstat_, F)
462460 # Compute Fstat of First Stage
@@ -477,7 +475,7 @@ function StatsAPI.fit(::Type{FixedEffectModel},
477475 rss = sum (abs2, residuals)
478476 mss = tss_total - rss
479477 r2 = 1 - rss / tss_total
480- adjr2 = 1 - rss / tss_total * (nobs - (has_intercept | has_fe_intercept)) / dof_residual_
478+ adjr2 = 1 - rss / tss_total * (nobs - (has_intercept | has_fe_intercept)) / max (nobs - size (X, 2 ) - dof_fes - dof_add, 1 )
481479 if has_fes
482480 r2_within = 1 - rss / tss_partial
483481 end
0 commit comments