Skip to content

Commit

Permalink
display both types of dof (#231)
Browse files Browse the repository at this point in the history
* display both  types of df

* Update README.md

* Update Project.toml

* Update README.md

* Update README.md
  • Loading branch information
matthieugomez authored May 24, 2023
1 parent ba6228e commit 4e795e9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FixedEffectModels"
uuid = "9d5cd8c9-2029-5cab-9928-427838db53e3"
version = "1.9.1"
version = "1.9.2"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Expand Down
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ The objective of the package is similar to the Stata command [`reghdfe`](https:/
using DataFrames, RDatasets, FixedEffectModels
df = dataset("plm", "Cigar")
reg(df, @formula(Sales ~ NDI + fe(State) + fe(Year)), Vcov.cluster(:State), weights = :Pop)
# =====================================================================
# Number of obs: 1380 Degrees of freedom: 32
# R2: 0.803 R2 Adjusted: 0.798
# F Statistic: 13.3382 p-value: 0.001
# R2 within: 0.139 Iterations: 6
# Converged: true
# =====================================================================
# Estimate Std.Error t value Pr(>|t|) Lower 95% Upper 95%
# ---------------------------------------------------------------------
# NDI -0.00526264 0.00144097 -3.65216 0.000 -0.00808942 -0.00243586
# =====================================================================
FixedEffectModel
=========================================================================
Number of obs: 1380 Converged: true
dof (model): 1 dof (residuals): 45
R²: 0.803 R² adjusted: 0.798
F-statistic: 13.3382 P-value: 0.001
R² within: 0.139 Iterations: 5
=========================================================================
Estimate Std. Error t-stat Pr(>|t|) Lower 95% Upper 95%
─────────────────────────────────────────────────────────────────────────
NDI -0.00526264 0.00144097 -3.65216 0.0007 -0.0081649 -0.00236038
=========================================================================
```


Expand Down
13 changes: 7 additions & 6 deletions src/FixedEffectModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ struct FixedEffectModel <: RegressionModel
contrasts::Dict

nobs::Int64 # Number of observations
dof::Int64 # Number parameters estimated - has_intercept
dof_residual::Int64 # dof used for t-test and F-stat. nobs - degrees of freedoms with simple std

dof::Int64 # Number parameters estimated - has_intercept. Used for p-value of F-stat.
dof_residual::Int64 # dof used for t-test and p-value of F-stat. nobs - degrees of freedoms with simple std
rss::Float64 # Sum of squared residuals
tss::Float64 # Total sum of squares
r2::Float64 # R squared
Expand All @@ -36,8 +35,8 @@ struct FixedEffectModel <: RegressionModel
p::Float64 # p value for the F statistics

# for FE
iterations::Union{Int, Nothing} # Number of iterations
converged::Union{Bool, Nothing} # Has the demeaning algorithm converged?
iterations::Int # Number of iterations
converged::Bool # Has the demeaning algorithm converged?
r2_within::Union{Float64, Nothing} # within r2 (with fixed effect

# for IV
Expand Down Expand Up @@ -178,7 +177,9 @@ end
function top(m::FixedEffectModel)
out = [
"Number of obs" sprint(show, nobs(m), context = :compact => true);
"Degrees of freedom" sprint(show, dof(m), context = :compact => true);
"Converged" m.converged;
"dof (model)" sprint(show, dof(m), context = :compact => true);
"dof (residuals)" sprint(show, dof_residual(m), context = :compact => true);
"" @sprintf("%.3f",r2(m));
"R² adjusted" @sprintf("%.3f",adjr2(m));
"F-statistic" sprint(show, m.F, context = :compact => true);
Expand Down
14 changes: 6 additions & 8 deletions src/fit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ function StatsAPI.fit(::Type{FixedEffectModel},

# compute tss now before potentially demeaning y
tss_total = tss(y, has_intercept | has_fe_intercept, weights)
# create unitilaized
iterations, converged, r2_within = nothing, nothing, nothing
# initalize fields
iterations, converged, r2_within = 0, true, nothing
F_kp, p_kp = nothing, nothing
collinear_fe = falses(length(var_names_all))

Expand Down Expand Up @@ -441,22 +441,20 @@ function StatsAPI.fit(::Type{FixedEffectModel},
end
end
end
dof_residual_ = max(1, nobs - size(X, 2) - dof_fes - dof_add)
dof_ = max(1, size(X, 2) - (has_intercept | has_fe_intercept))


nclusters = nothing
if vcov isa Vcov.ClusterCovariance
nclusters = Vcov.nclusters(vcov_method)
end


# Compute standard error
vcov_data = Vcov.VcovData(Xhat, crossx, residuals, dof_residual_)
vcov_data = Vcov.VcovData(Xhat, crossx, residuals, nobs - size(X, 2) - dof_fes - dof_add)
matrix_vcov = StatsAPI.vcov(vcov_data, vcov_method)

# Compute Fstat
F = Fstat(coef, matrix_vcov, has_intercept)
# dof_ is the number of estimated coefficients beyond the intercept.
dof_ = size(X, 2) - has_intercept
dof_tstat_ = max(1, Vcov.dof_residual(vcov_data, vcov_method) - has_intercept | has_fe_intercept)
p = fdistccdf(dof_, dof_tstat_, F)
# Compute Fstat of First Stage
Expand All @@ -477,7 +475,7 @@ function StatsAPI.fit(::Type{FixedEffectModel},
rss = sum(abs2, residuals)
mss = tss_total - rss
r2 = 1 - rss / tss_total
adjr2 = 1 - rss / tss_total * (nobs - (has_intercept | has_fe_intercept)) / dof_residual_
adjr2 = 1 - rss / tss_total * (nobs - (has_intercept | has_fe_intercept)) / max(nobs - size(X, 2) - dof_fes - dof_add, 1)
if has_fes
r2_within = 1 - rss / tss_partial
end
Expand Down

2 comments on commit 4e795e9

@matthieugomez
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/84159

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.9.2 -m "<description of version>" 4e795e91457d7501aeb8a987c24d8faff56353a9
git push origin v1.9.2

Please sign in to comment.