# How can I plot more regression trend line in the same graph?

I would like to do something like the example I drawed manually:

data = MapAt[DateString[{#, {"Day", "/", "Month", "/", "Year"}}] &,
Import["D:\\Analytics www.superinformati.com Panoramica del pubblico 20141201-20150303 - Sheet 1.tsv"][[3 ;;, {1, 2}]]
, {All, 1}]

tsm = TimeSeriesModelFit[data]

DateListPlot[{tsm["TemporalData"]}]


The series of data is here

• It's probably best to merge this question with your other one since they are very related. – bobthechemist May 8 '15 at 18:08
• @bobthechemist: the set of data is the same, but you have already answered this pretty well. As you prefer.. – Revious May 8 '15 at 21:52

This may be useful..

 sampledata =
{#,
Piecewise[ {{ 1 + # , # < 1}  , { 6 (# - 1) + 2 , # > 1} }]
(1 + RandomVariate[NormalDistribution[0, .2]])} &
/@ RandomReal[{0, 2}, 50];

Manipulate[
fit = NonlinearModelFit[ sampledata ,
Piecewise[ {{ a + b  x , x <= m }  ,
{ c (x - m) + a + b m , x > m} }] , {a, b, c} , x];
Show[{ListPlot[sampledata, Epilog -> Inset[Grid[{{
"R^2=" <> ToString[fit["RSquared"]], "m=" <> ToString[m]},
fit["BestFitParameters"]}], Scaled[{.4, .75}]]],
Plot[fit@x, {x, 0, m}, PlotStyle -> Red],
Plot[fit@x, {x, m, 2}, PlotStyle -> Blue]}], {{m, 1}, 0, 2}]


For this simple case it works well if you also make the transition point a fit parameter:

 Clear[m];
fit = NonlinearModelFit[ sampledata ,
Piecewise[ {{ a + b  x , x <= m }  ,
{ c (x - m) + a + b m , x > m}
}] ,  {a, b, c, m} , x];
x0 = m /. fit["BestFitParameters"];
Show[{ListPlot[sampledata],
Plot[fit@x, {x, 0, x0}, PlotStyle -> Red],
Plot[fit@x, {x, x0, 2}, PlotStyle -> Blue]}]


In general you are likely better off doing that by hand though