I have the following scatter plot with smoothed regression line:
using Plots
using DataFrames
using GLM
df = DataFrame(x = collect(1:7), y = collect(1:7)+rand(7))
scatter(df.x, df.y, smooth = :true, label = "data")
xlabel!("x")
ylabel!("y")
Output:
And the regression model:
model = lm(@formula(y ~ x), df)
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.469577 0.21843 2.15 0.0843 -0.0919156 1.03107
x 1.03064 0.0488424 21.10 <1e-05 0.905083 1.15619
─────────────────────────────────────────────────────────────────────────
I would like to add the regression equation (y=ax+b) and the R squared coefficient to this plot. So I was wondering if anyone knows how to add the equation and coefficients of a regression fit to a plot in Julia
?
using the LaTeXStrings.jl
:
annotate!(
4,
7,
latexstring(
"y = $(round(coef(model)[2], digits = 2))x + $(round(coef(model)[1], digits = 2))"
)
)
annotate!(
4,
6.5,
latexstring("r^2 = $(round(r2(model), digits = 2))")
)