Your figure with the "band" shows a 95% prediction band and not a 95% confidence band. In Mathematica lingo you need to decide on whether you want a SinglePredictionBand
or a MeanPredictionBand
, respectively. (And the one you show is not appropriate given the change in variance from low predictor values to high predictor values.)
Here is an example for obtaining both types of bands:
n = 100;
x = Table[(π/3) i/n, {i, n}];
y = Sin[2 x] + RandomVariate[NormalDistribution[0, 0.2], n];
data = Transpose[{x, y}];
nlm = NonlinearModelFit[data, a + Sin[b t], {{a, 0}, {b, 2}}, t];
Show[ListPlot[data],
Plot[{nlm[t], nlm["SinglePredictionBands"],
nlm["MeanPredictionBands"]}, {t, 0, π/3},
Filling -> {2 -> {1}, 3 -> {1}}], Frame -> True,
AxesOrigin -> {0, -0.5}, ImageSize -> Large]