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:
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:
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:
# 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:
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.