Julia:在拟合模型上绘制残差

时间:2021-04-23 22:56:10

标签: statistics julia

我想弄清楚如何制作像这样的残差图来显示与预测结果的偏差: enter image description here

我发现 this question 似乎可以用 Python 回答它,但我在 Plots.jl 文档中找不到,或者只是在 Julia 中搜索如何做到这一点。

注意:数据在这里不是问题,我可以处理,这只是我正在努力处理的文字绘图 API。

2 个答案:

答案 0 :(得分:3)

您在 API 中遇到了什么困难? 到目前为止你尝试了什么?

julia> points_y = [x for x in 1:10] .+ rand(10)
10-element Vector{Float64}:
  1.1165819028282722
  2.986599717814377
  3.5743557742882377
  4.835499304548991
  5.106506715905332
  6.483859149461656
  7.041461273394912
  8.22314808617758
  9.128892867863867
 10.891435553032899

julia> points_x = 1:10
1:10

julia> p1 = plot(points_x,points_x) #Plotting the fit

julia> scatter!(p1,points_x,points_y) #Plotting the points

julia> for i in 1:length(points_x)
           plot!(p1,[points_x[i], points_x[i]],[points_x[i],points_y[i]], color=:black, legend=false)
       end

julia> p1

产生这个:

Output

在 for 循环中,我认为这是你感兴趣的,我只是在拟合点 (x,f(x)) 和散点 (x, y) 之间画线,所以语法变成了 plot !(p,[x,x],[f(x),y])。有点难以深入,因为您没有具体说明您在流程的哪个部分挣扎。

答案 1 :(得分:2)

只是为了用 this Julia discourse answer 补充 aramirezreyes 的答案,如果您有很多点,您可能希望避免循环(因为调用 plot! 可能很昂贵,并且您可能希望避免许多系列)。在这种情况下,您可以在每个条的坐标之间插入 NaN。例如,

julia> x2 = repeat(points_x, inner=3) ;

julia> y2 = reduce(vcat, [y, yhat, NaN] for (y, yhat) in zip(points_x, points_y)) ;

julia> p1 = plot(points_x, points_x) #Plotting the fit

julia> plot!(x2, y2, lab="")

julia> scatter!(p1, points_x, points_y, c=2) #Plotting the points

会给你

enter image description here