# Adding logarithmic or polynomial regression to series

**URL:** <https://discourse.julialang.org/t/adding-logarithmic-or-polynomial-regression-to-series/73170>\
**Category:** New to Julia\
**Tags:** regression, glm\
**Created:** [December 15, 2021, 10:50pm UTC](https://discourse.julialang.org/t/adding-logarithmic-or-polynomial-regression-to-series/73170 "2021-12-15T22:50:12Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [December 15, 2021, 10:50pm UTC](https://discourse.julialang.org/t/adding-logarithmic-or-polynomial-regression-to-series/73170/1 "2021-12-15T22:50:12Z")

</div>

Good Day:

I have been attempting to fit a logarithmic or  
polynomial trendline to the following series:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/b/0bab5b121083d0990e27c63f1b2c0923c65b136b.png)

I am currently using GLM.jl and plotting with StatsPlots.jl, but do  
not believe there are parameters I can adjust to plot!(kwarg…) to  
the existing series.

I will need to adjust the y and x scale to :log10? Do I need to  
define p0? Any suggestions?

Thank you,

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [December 16, 2021, 4:02am UTC](https://discourse.julialang.org/t/adding-logarithmic-or-polynomial-regression-to-series/73170/2 "2021-12-16T04:02:00Z")

</div>

Please post code 😀

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 16, 2021, 4:49am UTC](https://discourse.julialang.org/t/adding-logarithmic-or-polynomial-regression-to-series/73170/3 "2021-12-16T04:49:12Z")

</div>

I don’t understand your question - are you asking how to hit a polynomial regression in GLM or have you already done that and are struggling to plot a linear and polynomial model on the same graph?

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [December 16, 2021, 10:35pm UTC](https://discourse.julialang.org/t/adding-logarithmic-or-polynomial-regression-to-series/73170/4 "2021-12-16T22:35:11Z")

</div>

@nilshg Thanks for your questions, Nils.

I am struggling to overlay a polynomial | logarithmic  
regression over the series. The linear model here  
does not quite fit the data and was simply the built  
in param within the scatter( … smooth=:true) method.

@ptoche – The code I am using to generate the graph  
was:

```julia
using StatsPlots, PlotlyJS

stp.scatter(DF[!, :Sales], DF[!, :Δ], 
	        label="Intersection", xlab="Sales",
            ylab="Cases", 
	        xlim=(0.6, 1.01),
	        smooth=:true,
	        lw = 2,
		framestyle=:axes)

```

---

<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:** [December 16, 2021, 11:55pm UTC](https://discourse.julialang.org/t/adding-logarithmic-or-polynomial-regression-to-series/73170/5 "2021-12-16T23:55:41Z")

</div>

Not sure what the problem is, but just in case it may help:

```julia
using GLM, DataFrames, StatsPlots; gr(dpi=600)

# automatically digitized with Chrome's WebPlotDigitizer app:
m = [ 0.62 2.25e-9;
      0.64 2.43e-9;
      0.66 2.65e-9;
      0.74 2.87e-9;
      0.77 2.08e-9;
      0.78 3.08e-9;
      0.81 3.25e-9;
      0.88 3.38e-9;
      0.96 3.45e-9;
      0.99 3.49e-9
]

DF = DataFrame(Sales = m[:,1], Δ = m[:,2])

Plots.scatter(DF[!, :Sales], DF[!, :Δ], 
	label="Input data", xlab="Sales",
    ylab="Cases", 
	xlim=(0.6, 1.01),
	smooth=:true,
	lw = 2, lc = :red,
    framestyle=:axes,
    legend=:outertop
)

pfit = lm(@formula(Δ ~ 1 + Sales), DF)
a, b = round.(coef(pfit), sigdigits=3)

plot!(DF.Sales, predict(pfit), lc=:cyan, lw=3, ls=:dot, label="Cases Δ = $a + $b * Sales")

```

 ![GLM_linear_regression](https://global.discourse-cdn.com/julialang/original/3X/8/2/82c30b0a114ce0bbbb6d1878777100cf7075ca13.png)

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [December 17, 2021, 3:20am UTC](https://discourse.julialang.org/t/adding-logarithmic-or-polynomial-regression-to-series/73170/6 "2021-12-17T03:20:34Z")

</div>

@rafael.guerra Thank you for this Rafael – it  
inadvertently answered a question that has  
been lingering on my mind.

I think the reason why the curve/regression is  
straight is because of the scale. I wanted to  
produce a logistic or polynomial curve to better  
fit the series.

---

<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:** [December 17, 2021, 7:55am UTC](https://discourse.julialang.org/t/adding-logarithmic-or-polynomial-regression-to-series/73170/7 "2021-12-17T07:55:32Z")

</div>

@YummyPampers2, you have a significant outlier in your data set, which raises the question of the accuracy of the other points. If their error bars are large and there is no a priori model for the data, could the straight line be an honest fit? If you have the data uncertainties, a weighted regression could be performed.

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [December 17, 2021, 10:28am UTC](https://discourse.julialang.org/t/adding-logarithmic-or-polynomial-regression-to-series/73170/8 "2021-12-17T10:28:28Z")

</div>

> [@rafael.guerra](#):
>
> `# automatically digitized with Chrome's WebPlotDigitizer app:`

Wow! Nice!

@YummyPampers2 , your dataset is very small: I would not recommend non-linear regression for such a small sample, unless you have very good reasons. You may want to [check the docs](https://juliastats.org/GLM.jl/stable/) for something along the lines of `logit = glm(@formula(Δ ~ 1 + Sales), DF, Binomial(), LogitLink())`, but do read up on this carefully beforehand to make sure you want to do that, e.g. [Stock and Watson](https://www.pearson.com/us/higher-education/program/Stock-Introduction-to-Econometrics-Plus-My-Lab-Economics-with-Pearson-e-Text-Access-Card-Package-4th-Edition/PGM2416966.html) pages \sim 328.

---

<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:** [December 17, 2021, 10:54am UTC](https://discourse.julialang.org/t/adding-logarithmic-or-polynomial-regression-to-series/73170/9 "2021-12-17T10:54:17Z")

</div>

You may want to play with the excelent Polynomials.jl package.

By removing the outlier you may get a decent data fit, but polynomials can be in general dangerous for data extrapolation, specially the high-order ones:

```julia
using Polynomials
(; Sales, Δ) = DF; # Julia 1.7 destructuring
deleteat!(Sales, 5) # this and next line will delete DF row!
deleteat!(Δ, 5)
p = Polynomials.fit(Sales, Δ, 2)
scatter(Sales, Δ, label="Input without oultier", legend=:topleft)
plot!(Sales, p.(Sales), lc=:green, lw=2, ls=:dash, label="2nd order Polynomial")

```

 ![Polynomials_2nd_order_fit](https://global.discourse-cdn.com/julialang/original/3X/2/d/2d20072e515c59d59a5e90c695bd6ceb52cae53a.png)

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [December 17, 2021, 4:06pm UTC](https://discourse.julialang.org/t/adding-logarithmic-or-polynomial-regression-to-series/73170/10 "2021-12-17T16:06:27Z")

</div>

@rafael.guerra – thank you for this. Would you recommend  
using OutlierDetection.jl or something similar (i.e.,  
LinRegOutliers.jl) to remove the outlier.

When I apply your instructions, I am generating

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/f/af87e37f5cf575cbca0ae680d4ce7d4416080ac3.png)

I think the original outlier is at index 10.

Testing now, any suggestions short of  
calculating Cook’s Distance and performing  
some quartile exclusion?

Thanks,

---

<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:** [December 17, 2021, 7:01pm UTC](https://discourse.julialang.org/t/adding-logarithmic-or-polynomial-regression-to-series/73170/11 "2021-12-17T19:01:28Z")

</div>

Take a look at the [RAFF.jl](https://fsobral.github.io/RAFF.jl/stable/) package. which looks interesting for this problem, as it allows to fit a nonlinear model in a robust way in the presence of outliers.

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [December 20, 2021, 4:49pm UTC](https://discourse.julialang.org/t/adding-logarithmic-or-polynomial-regression-to-series/73170/12 "2021-12-20T16:49:42Z")

</div>

@rafael.guerra Excellent!

Thank you for sharing the  
knowledge – will test out  
some use cases and  
report.

Best regards,
