# Linear Fit

**URL:** https://discourse.julialang.org/t/linear-fit/84847
**Category:** New to Julia
**Tags:** curve-fitting
**Created:** [July 26, 2022, 9:39pm UTC](https://discourse.julialang.org/t/linear-fit/84847 "2022-07-26T21:39:28Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Yangheng\_Jizhe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yangheng_jizhe/32/36007_2.png) [@Yangheng\_Jizhe](https://discourse.julialang.org/u/Yangheng_Jizhe)
#### Post date: [July 26, 2022, 9:39pm UTC](https://discourse.julialang.org/t/linear-fit/84847/1 "2022-07-26T21:39:28Z")

</div>

Hi!  
I’m a newbie to Julia and I’ve been trying to fit some scatter points ( coordinates in arrays ) to a line that goes through the origin. Anyone has any recommended functions and packages? I tried LinearLeastSquares package and LinearRegression.lingress, but they did not seem to work. Many thanks!

---

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [July 26, 2022, 9:45pm UTC](https://discourse.julialang.org/t/linear-fit/84847/2 "2022-07-26T21:45:44Z")

</div>

If you want a full linear model, with statistics and probability oils associated, check GLM.jl

Otherwise, juste doing `b = y \ X` should give you the linear coefficients of the regression `y = b'X + error`.

---

<div class="post-metadata">

### Author: ![lijas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lijas/32/20023_2.png) [@lijas](https://discourse.julialang.org/u/lijas)
#### Post date: [July 27, 2022, 8:14am UTC](https://discourse.julialang.org/t/linear-fit/84847/3 "2022-07-27T08:14:17Z")

</div>

Have you tried CurveFit.jl?

> **[GitHub - pjabardo/CurveFit.jl: Simple least squares and curve fitting functions](https://github.com/pjabardo/CurveFit.jl)**
>
> Simple least squares and curve fitting functions . Contribute to pjabardo/CurveFit.jl development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)
#### Post date: [July 27, 2022, 2:18pm UTC](https://discourse.julialang.org/t/linear-fit/84847/4 "2022-07-27T14:18:39Z")

</div>

> I tried LinearLeastSquares package and LinearRegression.lingress, but they did not seem to work.

Can you describe how they didn’t work? Submitting an issue over that corresponding github repos for those packages might benefit them.

However, for the problem you are describing those are overkill. I’d recommend @lrnv 's solution.

---

<div class="post-metadata">

### Author: ![Yangheng\_Jizhe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yangheng_jizhe/32/36007_2.png) [@Yangheng\_Jizhe](https://discourse.julialang.org/u/Yangheng_Jizhe)
#### Post date: [July 27, 2022, 6:32pm UTC](https://discourse.julialang.org/t/linear-fit/84847/5 "2022-07-27T18:32:31Z")

</div>

Thanks! That helps!

---

<div class="post-metadata">

### Author: ![Yangheng\_Jizhe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yangheng_jizhe/32/36007_2.png) [@Yangheng\_Jizhe](https://discourse.julialang.org/u/Yangheng_Jizhe)
#### Post date: [August 3, 2022, 6:07am UTC](https://discourse.julialang.org/t/linear-fit/84847/6 "2022-08-03T06:07:36Z")

</div>

I tried using this to get the linear fit, but it does not seem to work well.  
The data that I’m trying to do the fit on looks like:

 ![Screen Shot 2022-08-03 at 02.02.10](https://global.discourse-cdn.com/julialang/original/3X/d/5/d591ab10f0f8ac713c3e1a12ba065311ab389c52.png)  
in scatter points, and the regression line looks like:  
 ![Screen Shot 2022-08-03 at 02.02.24](https://global.discourse-cdn.com/julialang/original/3X/6/b/6be8b3bc30f72eb1ac7d64a55114f8ca5a5f28d3.png)

The code for the scatter points and the fitting process is as follows:  
scatter( time\_list\_r\_05[1:end]\*tf\_small/length(sol\_r\_05) , P\_r\_05[1:end] )

b\_05 = ( time\_list\_r\_05[1:end]_tf\_small/length(sol\_r\_05)) \ P\_r\_05[1:end]  
f\_05(x) = 1-b\_05_x  
plot!(f\_05 , 0 , time\_list\_r\_05[end]\*tf\_small/length(sol\_r\_05), label=“regular linear fit using ''”)

I’m not sure if I’ve used this fitting method correctly. Thanks!

---

<div class="post-metadata">

### Author: ![Yangheng\_Jizhe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yangheng_jizhe/32/36007_2.png) [@Yangheng\_Jizhe](https://discourse.julialang.org/u/Yangheng_Jizhe)
#### Post date: [August 3, 2022, 6:22am UTC](https://discourse.julialang.org/t/linear-fit/84847/7 "2022-08-03T06:22:16Z")

</div>

Yea I tried CurveFit. It does return a line that fits well. But linear\_fit.CurveFit will return a line with y intersection that is not zero. And I would like to have a linear fit that goes though a particular value on y axis. Thanks for bringing CurveFit up tho.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 3, 2022, 9:12am UTC](https://discourse.julialang.org/t/linear-fit/84847/9 "2022-08-03T09:12:01Z")

</div>

In the solution above just shift the origin (_to pass through b=1.0 at t=0 in example below_):

```julia
using Plots
t = 0:0.2:40
y = @. 1 - 6e-3 * (t/40 - 1/(1+0.5t)*sin(2π*t/6))
b = 1.0
a = t \ (y .- b)
scatter(t, y, ms=2)
plot!(t, a*t .+ b, c =:red)

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 3, 2022, 9:12am UTC](https://discourse.julialang.org/t/linear-fit/84847/10 "2022-08-03T09:12:42Z")

</div>

> [@Yangheng\_Jizhe](#):
>
> And I would like to have a linear fit that goes though a particular value on y axis.

You can try EasyFit:

> **[GitHub - m3g/EasyFit.jl: Easy interface for obtaining fits for 2D data](https://github.com/m3g/EasyFit.jl#bounds)**
>
> Easy interface for obtaining fits for 2D data. Contribute to m3g/EasyFit.jl development by creating an account on GitHub.
