How to extract the Std. Error of a linear regression model by GLM?

How to extract the Std. Error of a linear regression model by GLM.jl?

julia> using GLM

julia> x = rand(10); y = rand(10);

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

y ~ 1 + x

Coefficients:
─────────────────────────────────────────────────────────────────────────
                  Coef.  Std. Error     t  Pr(>|t|)  Lower 95%  Upper 95%
─────────────────────────────────────────────────────────────────────────
(Intercept)  0.539296      0.158513  3.40    0.0093   0.173764   0.904827
x            0.00747856    0.255377  0.03    0.9774  -0.581422   0.59638
─────────────────────────────────────────────────────────────────────────

I want to extract the 0.255377 value. The lr object has many fields and sub-fields that are time-consuming to probe. I don’t know how to extract the t value of 0.03 too.

You can use stderror:

julia> using GLM

julia> x = rand(10); y = rand(10);

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

y ~ 1 + x

Coefficients:
─────────────────────────────────────────────────────────────────────────
                 Coef.  Std. Error      t  Pr(>|t|)  Lower 95%  Upper 95%
─────────────────────────────────────────────────────────────────────────
(Intercept)   0.768012   0.0978733   7.85    <1e-04   0.542315  0.993708
x            -0.455953   0.218396   -2.09    0.0703  -0.959576  0.0476696
─────────────────────────────────────────────────────────────────────────

julia> stderror(res)
2-element Vector{Float64}:
 0.09787333346669594
 0.21839629429830254

Edit: see here for more access methods: Examples · GLM

1 Like

Thank you so much. What about the values of t?

t-values are the coefficients / std error, so:

coef(res) ./ stderror(res)
1 Like