# Plot the confidence interval for a model fit

**URL:** <https://discourse.julialang.org/t/plot-the-confidence-interval-for-a-model-fit/37767>\
**Category:** Statistics\
**Tags:** plotting, fit, glm\
**Created:** [April 17, 2020, 3:52pm UTC](https://discourse.julialang.org/t/plot-the-confidence-interval-for-a-model-fit/37767 "2020-04-17T15:52:05Z")\
**Posts on this page:** 1\
**Page:** 2

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [September 24, 2021, 12:43pm UTC](https://discourse.julialang.org/t/plot-the-confidence-interval-for-a-model-fit/37767/21 "2021-09-24T12:43:53Z")

</div>

indeed! that works. thanks. here for my future self:

```julia
using DataFrames, StatsPlots, GLM

data = DataFrame(x = rand(100));
data.y = 1 .+ 2*data.x .+ rand(100);
r = lm( @formula(y ~ x), data)
pred = predict(r, data, interval = :confidence, level = 0.95)

p = @df data scatter(:x, :y, leg = false)

# sort data on x
pred_s = pred[sortperm(data[!,:x]), : ]
x_s = sort(data[!, :x])

plot!(p, x_s, pred_s.prediction, linewidth = 2,
        ribbon = (pred_s.prediction .- pred_s.lower, pred_s.upper .- pred_s.prediction))

```

 ![Screenshot 2021-09-24 at 14.43.41](https://global.discourse-cdn.com/julialang/original/3X/3/7/37f372f1e078014b90dd0b249b0e8686d3bfa5a1.png)

[Previous page](https://discourse.julialang.org/t/plot-the-confidence-interval-for-a-model-fit/37767.md?page=1)
