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

**URL:** <https://discourse.julialang.org/t/how-to-extract-the-std-error-of-a-linear-regression-model-by-glm/113793>\
**Category:** Statistics\
**Tags:** glm\
**Created:** [May 3, 2024, 1:15pm UTC](https://discourse.julialang.org/t/how-to-extract-the-std-error-of-a-linear-regression-model-by-glm/113793 "2024-05-03T13:15:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [May 3, 2024, 1:15pm UTC](https://discourse.julialang.org/t/how-to-extract-the-std-error-of-a-linear-regression-model-by-glm/113793/1 "2024-05-03T13:15:38Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![BatyLeo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/batyleo/32/35017_2.png) [@BatyLeo](https://discourse.julialang.org/u/BatyLeo)\
**Post date:** [May 3, 2024, 1:21pm UTC](https://discourse.julialang.org/t/how-to-extract-the-std-error-of-a-linear-regression-model-by-glm/113793/2 "2024-05-03T13:21:16Z")

</div>

You can use `stderror`:

```julia
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](https://juliastats.org/GLM.jl/stable/examples/)

---

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [May 3, 2024, 1:21pm UTC](https://discourse.julialang.org/t/how-to-extract-the-std-error-of-a-linear-regression-model-by-glm/113793/3 "2024-05-03T13:21:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![junder873](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@junder873](https://discourse.julialang.org/u/junder873)\
**Post date:** [May 3, 2024, 3:10pm UTC](https://discourse.julialang.org/t/how-to-extract-the-std-error-of-a-linear-regression-model-by-glm/113793/4 "2024-05-03T15:10:39Z")

</div>

> [@BatyLeo](#):
>
> `res = lm(@formula(y~x), (;x, y))`

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

```julia
coef(res) ./ stderror(res)

```
