plot - 添加回归方程和 R2 以在 Julia 中绘图

标签 plot julia linear-regression coefficients

我有以下带有平滑回归线的散点图:

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")

输出:

enter image description here

回归模型:

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))")
)

enter image description here

关于plot - 添加回归方程和 R2 以在 Julia 中绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74929577/

相关文章:

r - 如何使用自由刻度但在ggplot中保留固定引用点?

python - 安德鲁斯曲线中的颜色

dataframe - 如何改变 DataFrame?

python - 在polyfit-python中包含sigma

pandas - sklearn : Found input variables with inconsistent numbers of samples: [1, 99]

r - element_text 中 "face"的多个参数

julia - 如何从我的模块中的子模块导出相同功能的多个方法?

julia - 我如何在 julia 的 AM 和 PM 中读取带有时间的 csv 文件

r - 如何将复杂的方程放入R公式中?

windows - 使用 PyPlot 时出现 Julia 运行时错误