# Very basic GLM help

**URL:** <https://discourse.julialang.org/t/very-basic-glm-help/49832>\
**Category:** New to Julia\
**Tags:** regression, fit, glm\
**Created:** [November 9, 2020, 11:30am UTC](https://discourse.julialang.org/t/very-basic-glm-help/49832 "2020-11-09T11:30:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [November 9, 2020, 11:30am UTC](https://discourse.julialang.org/t/very-basic-glm-help/49832/1 "2020-11-09T11:30:37Z")

</div>

Hello, I have been trying to learn how to use [GLM.jl](https://juliastats.org/GLM.jl/stable/manual/) but I really don’t understand it. My goal is to show a linear model on a plot. I know how to create the base plot, but not the linear model or how to plot that onto the plot. Please help me - I really need baby step instructions with this it seems. (or with [Algebra of Graphics](http://juliaplots.org/AlgebraOfGraphics.jl/stable/generated/tutorial/), I’m not picky, I just need to learn how to do it with _something_)

```julia
df = DataFrame(A = 1:2:1000, B = repeat(1:10, inner=50), C = 1:500)
lm(@formula(:A ~ :C),df) #doesn't work

```

---

<div class="post-metadata">

**Author:** ![Rudi79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rudi79/32/3884_2.png) [@Rudi79](https://discourse.julialang.org/u/Rudi79)\
**Post date:** [November 9, 2020, 12:58pm UTC](https://discourse.julialang.org/t/very-basic-glm-help/49832/2 "2020-11-09T12:58:04Z")

</div>

I don’t know too much about linear models, but following the manual of GLM you should try:

```julia
df = DataFrame(A = 1:2:1000, B = repeat(1:10, inner=50), C = 1:500)
lm(@formula(A ~ C),df)

```

note the missing : in front of the variables in formula.  
But if you are only interested in linear regression most of the plotting options can do that for you e.g.,  
[http://gadflyjl.org/stable/gallery/statistics/#[Stat.smooth](@ref)-1](http://gadflyjl.org/stable/gallery/statistics/#%5BStat.smooth%5D(@ref)-1)  
or  
[https://www.queryverse.org/VegaLite.jl/stable/examples/examples\_advancedcalculations/#Linear-Regression-1](https://www.queryverse.org/VegaLite.jl/stable/examples/examples_advancedcalculations/#Linear-Regression-1)

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [November 9, 2020, 2:35pm UTC](https://discourse.julialang.org/t/very-basic-glm-help/49832/3 "2020-11-09T14:35:20Z")

</div>

As @Rudi79 points out, the error with your linear model is with the syntax and if all you want is to add a regression line, you can use the `smooth` keyword argument in `Plots.jl`, for example. Have a look at this example:

```julia
using DataFrames
using GLM
using StatsPlots

x = rand(10:0.1:30, 30)
f(x) = x + rand(-5:0.1:5)
df = DataFrame(y = f.(x), x = x)

scatter(df.x, df.y, legend=false, smooth=true)

```

That being said, you stated you are trying to learn GLM.jl, so you can also do something like this:

```julia
ols = lm(@formula(y ~ x), df)

```

You can then get the coefficients by calling `coef(ols)` which you can use to plot in a couple of different ways:

```julia
# create your own function from your coefficients:
model(x) = coef(ols)[1] + coef(ols)[2] * x
scatter(df.x, df.y)
plot!(x, model.(x), legend=false)

# make use of linear algebra to compute all the y values:
scatter(df.x, df.y)
plot!(x, (coef(ols)' * hcat(ones(size(df,1)), x)')', legend=false)

```

Or, as @nilshg points out:

```julia
scatter(df.x, df.y)
plot!(x, predict(ols, DataFrame(x = x)), legend=false)

```

I’ve personally always found it very annoying that you have to pass in some `Table` type to the `predict` function but it is of course still more convenient than the two alternatives I showed.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 9, 2020, 2:46pm UTC](https://discourse.julialang.org/t/very-basic-glm-help/49832/4 "2020-11-09T14:46:33Z")

</div>

Note that this basically re-invents the `predict` function in GLM, which can also give you confidence or prediction intervals. Cf this thread here: [Plot the confidence interval for a model fit - #4 by nilshg](https://discourse.julialang.org/t/plot-the-confidence-interval-for-a-model-fit/37767/4)

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [November 9, 2020, 2:47pm UTC](https://discourse.julialang.org/t/very-basic-glm-help/49832/5 "2020-11-09T14:47:45Z")

</div>

Thank you for your reply, removing `:` let it calculate the formula. I thought `y` and `x` could be replaced by column headings.

Now I am trying to plot it but have difficulties. Basically I want to do is what is done in R with the commands`geom_smooth(method = "lm")` and `geom_smooth(method = "loess")`.

Following the example [here](https://discourse.julialang.org/t/plot-the-confidence-interval-for-a-model-fit/37767)

```julia
df = DataFrame(A = rand(500), B = repeat(1:10, inner=50), C = 1:500)
linear_model_df = lm(@formula(A ~ C),df)
pred = DataFrame(df(C)); #this does not work
pred.y = predict(model, pred)

```

Edit: I guess I am confused when to use `x` and `y` and when to use column headings.

Does one always need to state the intervals for the x axis?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 9, 2020, 2:54pm UTC](https://discourse.julialang.org/t/very-basic-glm-help/49832/6 "2020-11-09T14:54:24Z")

</div>

It seems to me that your problems come from a more fundamental misunderstanding of Julia’s syntax rather than the specifics of GLM or Plots.jl (or any other plotting library).

In the above, you are calling `DataFrame(df(C))`, which probably should be `DataFrame(C = df.C)`. In Julia, round brackets `()` denote function calls, so `df(C)` means “call function `df` with argument `C`”, which doesn’t really make sense as `df` isn’t a function.

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [November 9, 2020, 5:13pm UTC](https://discourse.julialang.org/t/very-basic-glm-help/49832/7 "2020-11-09T17:13:25Z")

</div>

Thank you for your replies. I acknowledge that I have only basic understanding of programing syntax and often make mistakes. I will continue to try and learn.

```julia
df = DataFrame(A = rand(500), B = repeat(1:10, inner=50), C = 1:500)
linear_model_df = lm(@formula(A ~ C),df)
pred = DataFrame(C = 1:500);
pred.y = predict(linear_model_df, pred)

plot(df.C,df.A)
plot!(pred.C,pred.y)

```
