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!
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.
Have you tried CurveFit.jl?
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.
Thanks! That helps!
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:
in scatter points, and the regression line looks like:
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_05x
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!
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.
In the solution above just shift the origin (to pass through b=1.0 at t=0 in example below):
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)
You can try EasyFit: