# How to fit data to only half of a model

pIDmm = {{1, 4.}, {2, 4.}, {3, 0.}, {4, 5.}, {5, 4.}, {6, 8.}, {7,
12.}, {8, 11.}, {9, 27.}, {10, 28.}, {11, 41.}, {12, 49.}, {13,
36.}, {14, 133.}, {15, 97.}, {16, 168.}, {17, 196.}, {18,
189.}, {19, 250.}, {20, 175.}, {21, 368.}, {22, 349.}, {23,
345.}, {24, 475.}, {25, 427.}};

sic = 11.6186 + 1.72833 x - 0.105655 x^2 + 0.0588155 x^3 -
0.00346728 x^4 + 0.0000699641 x^5 - 4.71206*10^-7 x^6;


sic is the model for the data pCD (blue dots). I would like to adapt or shape the model to fit the data in red dots (pIDmm) by phase-shifting the model and increasing its amplitude and range. But the red dots represent only the first half of the model. Is it possible to use FindFit using the red dots data on only the first half of the model so as to predict the likely subsequence of data points?

• What coefficients in sic do you think represent phase, amplitude, and range?
– JimB
Commented Mar 21, 2020 at 0:04
• @JimB - good question -- answer probably none of them individually, if a trig function like a Sin[ b x + c ] + d, then -- a-amplitude, c/b - phase shift, d - vertical shift if my memory serves me correctly. I was hoping for a genius, guess I need a new model , but could I use just half a model to syn with the new data to predict the rest of the data??
– ray
Commented Mar 21, 2020 at 2:05
• It sounds like you need a theoretically-based model that has all the features you want/need. Otherwise you're just describing the existing data and extrapolation is at best very risky. Whatever kind of curve you fit, you should use NonlinearModelFit rather than FindFit as NonlinearModelFit will give you estimates of precision.
– JimB
Commented Mar 21, 2020 at 2:15

It's what Jim said- what parameters you can change. If, say, all the coefficients are related to phase you might have to put all of them as parameters in Findfit and your model will be like:

    model= a + b x + c x^2 + c x^3 + e x^4 + f x^5 + g x^6;


For the first set of data you provided (pIDmm) it will fit as:

    fit = FindFit[pIDmm,  a + b x + c x^2 + c x^3 + e x^4 + f x^5 + g
x^6, {a, b, c, d , e, f, g}, x];
Show[ListPlot[pIDmm],  Plot[model /. fit, {x, 0, 25}, PlotStyle -> Red]]


However, If you want to fit combined data points of pIDmm and sic, first combine them in a list and obtain a fit which will depend on relative strength and number of points. For ex:

      data = Table[{x, sic + RandomVariate[NormalDistribution[0, 3]]}, {x, 0, 56.5, 56.5/25}];
fit2 = FindFit[Sort[Join[pIDmm, data]],
a + b x + c x^2 + c x^3 + e x^4 + f x^5 + g x^6, {a, b, c,
d , e, f, g}, x];
Show[ListPlot[Sort[Join[pIDmm, data]]], Plot[model /. fit2, {x, 0, 55}, PlotStyle -> Red], PlotRange -> All]


• thanks Maeinss, okay I will try again some of your ideas
– ray
Commented Mar 21, 2020 at 2:21

China model adapted to Italy, black dots are new data, the model was developed from the first 60 days and then not changed.

It seems that the best fit to pCD data is a gaussian bell as can be depicted. Hopelessly we don't have pCD data...

pIDmm = {{1, 4.}, {2, 4.}, {3, 0.}, {4, 5.}, {5, 4.}, {6, 8.}, {7, 12.}, {8, 11.}, {9, 27.}, {10, 28.}, {11, 41.}, {12, 49.}, {13, 36.}, {14, 133.}, {15, 97.}, {16, 168.}, {17, 196.}, {18, 189.}, {19, 250.}, {20, 175.}, {21, 368.}, {22, 349.}, {23, 345.}, {24, 475.}, {25, 427.}}

f[x_] := 11.6186 + 1.72833 x - 0.105655 x^2 + 0.0588155 x^3 - 0.00346728 x^4 + 0.0000699641 x^5 - 4.71206*10^-7 x^6
fa[x_, a_, b_, c_] := a Exp[-(x - b)^2/c]
data = Sum[(f[x] - fa[x, a, b, c])^2, {x, 0, 60}];
sol = NMinimize[data, {a, b, c}, Method -> "DifferentialEvolution"]
fa0 = fa[x, a, b, c] /. sol[[2]]
gr2 = Plot[{fa0, f[x]}, {x, 0, 60}, PlotStyle -> {{Thick, Blue}, {Dashed, Red}}]


data2 = Sum[(fa[pIDmm[[k, 1]], a, b, c] - pIDmm[[k, 2]])^2, {k, 1, Length[pIDmm]}];
sol2 = NMinimize[data2, {a, b, c}, Method -> "DifferentialEvolution"]
fa02 = fa[x, a, b, c] /. sol2[[2]]
gr0 = ListPlot[pIDmm];
gr1 = Plot[{fa02, fa0}, {x, 0, 60}, PlotStyle -> {{Thick, Black}, {Thick, Blue}}];
Show[gr1, gr0]


a new model based on trig -( a Sin[b x + c] + d ) on the completed trend, results

   In[1246]:= trigfitCD =  a Sin[b x + c] + d /. {a -> -56.11407429814903,
b -> 0.12234789054358615, c -> 8.054346767593303,
d -> 61.16172326460761}

Out[1246]= 61.1617 - 56.1141 Sin[8.05435 + 0.122348 x]


I multiplied trigfitCD * Max[ItalyDeaths]/Max[ChinaDeaths] and plotted the results

It is close but the predictive line is just the trendline amplified. I would like to fit the old model to the new data. The new data is only half of the story, so I need to fit half the old model to the new data - to get a predictive trend, is that possible with Fit or FindFit or NonlinearModelFit or something else???

• You need to consult with a statistician or an epidemiologist. What you're proposing sounds misleading at best.
– JimB
Commented Mar 21, 2020 at 2:52
• @JimB as far as I understand he is talking about forecasting based on the previous trend. That's the hot topic these days in media. Commented Mar 21, 2020 at 2:53
• I don't mean to be rude but it's a hot topic for folks who know what they're doing. There's enough misinformation already coming from all directions.
– JimB
Commented Mar 21, 2020 at 2:56
• @JimB I understand Commented Mar 21, 2020 at 2:59
• Curve fitting is not just about learning what a piece of software has to offer. It's also about know what kind of data you have and how it was obtained. (Yes, I understand that this is for a classroom example but the particular data you're using -COVID-19 - has too many misleading so-called analyses already which is why I'm taking a hard-nosed approach with your question.) In addition, your data is about counts (integers). FindFit is not appropriate for that. Also, with any polynomial fit, extrapolating too far will get you predictions of + or - $\infty$.
– JimB
Commented Mar 21, 2020 at 17:57