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

Show R^2 and F-statistic for regression models #225

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

OndrejSlamecka
Copy link

@OndrejSlamecka OndrejSlamecka commented Apr 25, 2021

Adds R² and F-statistic for regression models, bringing the output closer to what users might expect when coming from R.

An example implementation of fstatistic is in JuliaStats/GLM.jl#424 -- I am unsure about import/export/using/... and welcome advice, thanks!

Example output:

julia> ols_lin = lm(@formula(y ~ x), data)
StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Vector{Float64}}, GLM.DensePredChol{Float64, LinearAlgebra.CholeskyPivoted{Float64, Matrix{Float64}}}}, Matrix{Float64}}

y ~ 1 + x

Coefficients:
──────────────────────────────────────────────────────────────────────
              Coef.  Std. Error      t  Pr(>|t|)  Lower 95%  Upper 95%
──────────────────────────────────────────────────────────────────────
(Intercept)  -442.0     54.5539  -8.10    <1e-09  -551.688   -332.312
x              51.0      1.8619  27.39    <1e-30    47.2564    54.7436
──────────────────────────────────────────────────────────────────────

R²: 0.9399	Adjusted R²: 0.9386
F-statistic: 750.3 on 50.0 and 1.0 degrees of freedom, p-value: 0.0290

@OndrejSlamecka OndrejSlamecka force-pushed the show-r2-fstat branch 4 times, most recently from c41a7e8 to 950ba92 Compare April 25, 2021 13:01
@OndrejSlamecka OndrejSlamecka changed the title Show R^2 and F-statistic for regression models Show R^2 for regression models Apr 25, 2021
@OndrejSlamecka OndrejSlamecka changed the title Show R^2 for regression models Show R^2 and F-statistic for regression models Apr 25, 2021
@OndrejSlamecka OndrejSlamecka force-pushed the show-r2-fstat branch 2 times, most recently from b953553 to 8060e86 Compare April 25, 2021 13:38
src/statsmodel.jl Outdated Show resolved Hide resolved
src/statsmodel.jl Outdated Show resolved Hide resolved
src/statsmodel.jl Outdated Show resolved Hide resolved
@OndrejSlamecka
Copy link
Author

Thanks @palday for the good suggestions, I implemented them.

src/statsmodel.jl Outdated Show resolved Hide resolved
@nalimilan
Copy link
Member

How about printing also the number of observations and degrees of freedom?

Can you also add tests?

Comment on lines +200 to +201
println("R²: ", round(r2(model), sigdigits=4),
"\t Adjusted R²: ", round(adjr2(model), sigdigits=4))
Copy link
Member

Choose a reason for hiding this comment

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

This should ensure that 4 digits are always used, and that "Ajusted" is aligned with the value of the F-statistic on the next line.

Suggested change
println("R²: ", round(r2(model), sigdigits=4),
"\t Adjusted R²: ", round(adjr2(model), sigdigits=4))
@printf(io, "R²: %.4f Adjusted R²: %.4f", r2(model), adjr2(model))

Comment on lines +203 to +204
fstat = fstatistic(model)
println(io, fstat)
Copy link
Member

Choose a reason for hiding this comment

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

fstatistic doesn't exist anywhere currently, and only GLM provides the needed functionality via ftest (JuliaStats/GLM.jl#424). An empty ftest definition should be added to StatsAPI first. Probably the API can just require that the returned object has the dof, fstat and pval properties so that you can extract them and print them.

Suggested change
fstat = fstatistic(model)
println(io, fstat)
ftr = ftest(model)
@printf(io, "F-statistic: %.4f on %s degrees of freedom, p-value %s",
ftr.fstat, ftr.dof, PValue(ftr.pvalue))

Copy link

Choose a reason for hiding this comment

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

@nalimilan what if adding a print API to StatsAPI so packages can implement their own printing?

Copy link
Member

Choose a reason for hiding this comment

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

@xgdgsc every package is free to implement Base.show methods for their own types

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants