@@ -24,8 +24,9 @@ struct FixedEffectModel <: RegressionModel
2424 contrasts:: Dict
2525
2626 nobs:: Int64 # Number of observations
27- dof_residual:: Int64 # nobs - degrees of freedoms
28- df_FStat:: Int64
27+ dof:: Int64 # Number parameters estimated - has_intercept
28+ dof_residual:: Int64 # nobs - degrees of freedoms
29+ dof_tstat:: Int64 # dof used for t-test and F-stat
2930
3031 rss:: Float64 # Sum of squared residuals
3132 tss:: Float64 # Total sum of squares
@@ -49,30 +50,31 @@ has_iv(m::FixedEffectModel) = m.F_kp !== nothing
4950has_fe (m:: FixedEffectModel ) = has_fe (m. formula)
5051
5152
52- # Check API at https://github.com/JuliaStats/StatsBase.jl/blob/11a44398bdc16a00060bc6c2fb65522e4547f159/src/statmodels.jl
53- # fields
54- StatsBase. coef (m:: FixedEffectModel ) = m. coef
55- StatsBase. coefnames (m:: FixedEffectModel ) = m. coefnames
56- StatsBase. responsename (m:: FixedEffectModel ) = m. yname
57- StatsBase. vcov (m:: FixedEffectModel ) = m. vcov
58- StatsBase. nobs (m:: FixedEffectModel ) = m. nobs
59- StatsBase. dof_residual (m:: FixedEffectModel ) = m. dof_residual
60- StatsBase. r2 (m:: FixedEffectModel ) = m. r2
61- StatsBase. adjr2 (m:: FixedEffectModel ) = m. adjr2
62- StatsBase. islinear (m:: FixedEffectModel ) = true
63- StatsBase. deviance (m:: FixedEffectModel ) = m. tss
64- StatsBase. rss (m:: FixedEffectModel ) = m. rss
65- StatsBase. mss (m:: FixedEffectModel ) = deviance (m) - rss (m)
66-
67-
68- function StatsBase. confint (m:: FixedEffectModel ; level:: Real = 0.95 )
69- scale = tdistinvcdf (m. df_FStat, 1 - (1 - level) / 2 )
53+
54+ StatsAPI. coef (m:: FixedEffectModel ) = m. coef
55+ StatsAPI. coefnames (m:: FixedEffectModel ) = m. coefnames
56+ StatsAPI. responsename (m:: FixedEffectModel ) = m. yname
57+ StatsAPI. vcov (m:: FixedEffectModel ) = m. vcov
58+ StatsAPI. nobs (m:: FixedEffectModel ) = m. nobs
59+ StatsAPI. dof (m:: FixedEffectModel ) = m. dof
60+ StatsAPI. dof_residual (m:: FixedEffectModel ) = m. dof_residual
61+ Vcov. dof_tstat (m:: FixedEffectModel ) = m. dof_tstat
62+ StatsAPI. r2 (m:: FixedEffectModel ) = m. r2
63+ StatsAPI. adjr2 (m:: FixedEffectModel ) = m. adjr2
64+ StatsAPI. islinear (m:: FixedEffectModel ) = true
65+ StatsAPI. deviance (m:: FixedEffectModel ) = m. tss
66+ StatsAPI. rss (m:: FixedEffectModel ) = m. rss
67+ StatsAPI. mss (m:: FixedEffectModel ) = deviance (m) - rss (m)
68+
69+
70+ function StatsAPI. confint (m:: FixedEffectModel ; level:: Real = 0.95 )
71+ scale = tdistinvcdf (Vcov. dof_tstat (m), 1 - (1 - level) / 2 )
7072 se = stderror (m)
7173 hcat (m. coef - scale * se, m. coef + scale * se)
7274end
7375
7476# predict, residuals, modelresponse
75- function StatsBase . predict (m:: FixedEffectModel , t)
77+ function StatsAPI . predict (m:: FixedEffectModel , t)
7678 # Require DataFrame input as we are using leftjoin and select from DataFrames here
7779 # Make sure fes are saved
7880 if has_fe (m)
@@ -95,7 +97,7 @@ function StatsBase.predict(m::FixedEffectModel, t)
9597 return out
9698end
9799
98- function StatsBase . residuals (m:: FixedEffectModel , t)
100+ function StatsAPI . residuals (m:: FixedEffectModel , t)
99101 if has_fe (m)
100102 m. residuals != = nothing || throw (" To access residuals in a fixed effect regression, run `reg` with the option save = :residuals, and then access residuals with `residuals()`" )
101103 residuals (m)
@@ -115,7 +117,7 @@ function StatsBase.residuals(m::FixedEffectModel, t)
115117end
116118
117119
118- function StatsBase . residuals (m:: FixedEffectModel )
120+ function StatsAPI . residuals (m:: FixedEffectModel )
119121 has_fe (m) || throw (" To access residuals, use residuals(x, t) where t is a Table" )
120122 m. residuals
121123end
@@ -140,7 +142,7 @@ function fe(m::FixedEffectModel; keepkeys = false)
140142end
141143
142144
143- function StatsBase . coeftable (m:: FixedEffectModel ; level = 0.95 )
145+ function StatsAPI . coeftable (m:: FixedEffectModel ; level = 0.95 )
144146 cc = coef (m)
145147 se = stderror (m)
146148 coefnms = coefnames (m)
@@ -155,7 +157,7 @@ function StatsBase.coeftable(m::FixedEffectModel; level = 0.95)
155157 end
156158 tt = cc ./ se
157159 CoefTable (
158- hcat (cc, se, tt, fdistccdf .(Ref (1 ), Ref (m . df_FStat ), abs2 .(tt)), conf_int[:, 1 : 2 ]),
160+ hcat (cc, se, tt, fdistccdf .(Ref (1 ), Ref (Vcov . dof_tstat (m) ), abs2 .(tt)), conf_int[:, 1 : 2 ]),
159161 [" Estimate" ," Std.Error" ," t value" , " Pr(>|t|)" , " Lower 95%" , " Upper 95%" ],
160162 [" $(coefnms[i]) " for i = 1 : length (cc)], 4 )
161163end
@@ -186,7 +188,7 @@ format_scientific(x) = @sprintf("%.3f", x)
186188function top (m:: FixedEffectModel )
187189 out = [
188190 " Number of obs" sprint (show, nobs (m), context = :compact => true );
189- " Degrees of freedom" sprint (show, nobs (m) - dof_residual (m), context = :compact => true );
191+ " Degrees of freedom" sprint (show, dof (m), context = :compact => true );
190192 " R2" format_scientific (r2 (m));
191193 " R2 Adjusted" format_scientific (adjr2 (m));
192194 " F-Stat" sprint (show, m. F, context = :compact => true );
@@ -225,7 +227,7 @@ function Base.show(io::IO, m::FixedEffectModel)
225227 coefnms = coefnms[newindex]
226228 end
227229 tt = cc ./ se
228- mat = hcat (cc, se, tt, fdistccdf .(Ref (1 ), Ref (m . df_FStat ), abs2 .(tt)), conf_int[:, 1 : 2 ])
230+ mat = hcat (cc, se, tt, fdistccdf .(Ref (1 ), Ref (Vcov . dof_tstat (m) ), abs2 .(tt)), conf_int[:, 1 : 2 ])
229231 nr, nc = size (mat)
230232 colnms = [" Estimate" ," Std.Error" ," t value" , " Pr(>|t|)" , " Lower 95%" , " Upper 95%" ]
231233 rownms = [" $(coefnms[i]) " for i = 1 : length (cc)]
0 commit comments