我有以下带有平滑回归线的散点图:
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")
输出:
回归模型:
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
─────────────────────────────────────────────────────────────────────────
我想将回归方程 (y=ax+b) 和 R 平方系数添加到该图中。所以我想知道是否有人知道如何将回归拟合的方程和系数添加到 Julia 中的绘图中?
最佳答案
使用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))")
)
关于plot - 添加回归方程和 R2 以在 Julia 中绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74929577/