# Plot fitted curves

**URL:** <https://discourse.julialang.org/t/plot-fitted-curves/78181>\
**Category:** New to Julia\
**Tags:** question, curve-fitting\
**Created:** [March 20, 2022, 10:37pm UTC](https://discourse.julialang.org/t/plot-fitted-curves/78181 "2022-03-20T22:37:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mahmoud](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahmoud](https://discourse.julialang.org/u/Mahmoud)\
**Post date:** [March 20, 2022, 10:37pm UTC](https://discourse.julialang.org/t/plot-fitted-curves/78181/1 "2022-03-20T22:37:16Z")

</div>

Is there anyway in Julia to fit these data represented in the attached figure?  
I want to make them smooth curves (may be exponential fit).  
 ![datageneration](https://global.discourse-cdn.com/julialang/original/3X/d/4/d483ae0a0acc8cf7a3f59a0544fbcca6f5f0ecb1.png)

```julia
p1=plot!(dayss,numberofhealthyy,legend=false,color = :blue)
p2=plot!(dayss,numberofinfectedd,legend=false,color =:green)
p3=plot!(dayss,numberofrecoveredd,legend=false,color = :gold )
p4=plot!(dayss,numberofsickk,legend=false,color = :red)

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 21, 2022, 6:30am UTC](https://discourse.julialang.org/t/plot-fitted-curves/78181/2 "2022-03-21T06:30:06Z")

</div>

A general principle for fitting functions: you need to decide what _model_ to fit to - the best thing is if you have some inkling of what the underlying truth looks like. For instance I imagine these will be sums of logistic functions?

Once you know what model to fit, you can use something like the package `LsqFit` to fit it.

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [March 21, 2022, 6:41am UTC](https://discourse.julialang.org/t/plot-fitted-curves/78181/3 "2022-03-21T06:41:00Z")

</div>

Kernel regression smoothing is a possibility. An unregistered function that will do it is [https://github.com/mcreel/Econometrics/blob/main/src/NP/npreg.jl](https://github.com/mcreel/Econometrics/blob/main/src/NP/npreg.jl)

---

<div class="post-metadata">

**Author:** ![Mahmoud](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahmoud](https://discourse.julialang.org/u/Mahmoud)\
**Post date:** [March 21, 2022, 4:23pm UTC](https://discourse.julialang.org/t/plot-fitted-curves/78181/4 "2022-03-21T16:23:04Z")

</div>

Thanks for reply.  
it is exponential fit  
do you know how to write that using Lsqfit library?  
I can not find clear example for exponential fit.!

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 21, 2022, 5:59pm UTC](https://discourse.julialang.org/t/plot-fitted-curves/78181/5 "2022-03-21T17:59:17Z")

</div>

Seems unlikely from the look of it, but for demonstration purposes:

```julia
model(x, p) = @. exp(p[1]*x + p[2]) # define a model function that takes an x-vector and a vector of fitting parameters
fit = curve_fit(model, xdata, ydata, [1.0, 1.0]) # the fourth argument is your best initial guess for `p`

@show fit.param # this is the found least-squares `p`
plot!(xdata, model(xdata, fit.param))

```

---

<div class="post-metadata">

**Author:** ![Mahmoud](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahmoud](https://discourse.julialang.org/u/Mahmoud)\
**Post date:** [March 21, 2022, 6:24pm UTC](https://discourse.julialang.org/t/plot-fitted-curves/78181/6 "2022-03-21T18:24:37Z")

</div>

Many thanks again for reply.  
Is there any link or something to read from how to use different fitting functions?  
Like how I know what is the best fit for my plots and what are the equations used ?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 21, 2022, 7:20pm UTC](https://discourse.julialang.org/t/plot-fitted-curves/78181/7 "2022-03-21T19:20:38Z")

</div>

I’m a physicist, so this will be from that perspective, it might be different in other fields but:

It depends a bit on what you want to know about your data. The best way to choose is to know the answer a priori: If I throw a ball I know from Newtonian mechanics its trajectory should be a parabola,

```julia
model(x, p) = @. p[1] - p[2] * (x - p[3])^2

```

Performing this fit on a set of data points, I would be able to extract things like how hard the ball was thrown and what the local gravity acceleration is. It would not (unless I have some new physical understanding of the situation) be meaningful to fit these points to some kind of gaussian or lorentzian function. For one thing, the fit is likely to be poor, but with enough noise in the measurement I might not know that. For another, it becomes a lot less clear what it would mean to extract those parameters: in my parabolic model, `g` follows from `p[2]`. In a lorentzian model the relationship is a lot less obvious.

If you don’t know in advance, you can follow something called the smallest viable degree method. Here you’re cheating a bit by fitting a function, comparing it to your data, and increasing the complexity of your function until the fit is _good_ without being _overfitted_. Works very well for polynomial model functions.

You can also look at the shape and, with some experience, make educated guesses as to what functions might fit: If it has three fix points, try a 3d degree polynomial. If it … looks gaussian: try a gaussian. Sigmoids (S-shaped curves like your blue and yellow ones) might be arctan functions, logistic functions or a myriad of others with discretely different properties.

In this case, my initial guess that you would want to try the logistic function was a combination of the methods above: Two of your curves look sigmoid, and a sigmoid in epidemiology is often a logistic map (from my very limited experience). If you can find one that fits, its parameters will map well to interesting characteristics like “growth rate”, “saturation level” and “inflection point”.

Try

```julia
model(x, p) = @. p[1]/(1 + exp(-p[2]*(x-p[3]))

```

`p[3]` here will be the x point halfway up the sigmoid, `p[2]` will be a measure of the growth rate, and `p[1]` will be the maximum. You can fit this to both the blue and yellow curve, where one will have a negative `p[2]`. The red curve should then be reasonably fitted by `100 - ` those two other curves.
