I need to calculate confidence and prediction bands for a linear regression that is in the general form of $y=ax$, where $a$ is the multiplier and $x$ the variable.
I found a code in http://demonstrations.wolfram.com/ConfidenceAndPredictionBands/ , though it gives the confidence and prediction bands for $y=ax+b$ which is out of my interest.
This is the code I wrote based on @shabbychef's answer. Is it correct?
bands[list_, x_, type_, gamma_] :=
Module[{a, nlm, z, xs, ys, alphahat, betahat, sigmahat2, n, xbar,
multiplier},
xs = list[[All, 1]];
ys = list[[All, 2]];
alphahat = Mean[ys];
nlm = NonlinearModelFit[list, a z, {a}, z,
ConfidenceLevel -> 1 - gamma];
betahat = a /. nlm["BestFitParameters"];
n = Length[list];
xbar = Mean[xs];
sigmahat2 = nlm["EstimatedVariance"];
multiplier =
If[type == 1,
Sqrt[n*sigmahat2/(n - 2)*(1/n + (x - xbar)^2/
Plus @@ ((xs - xbar)^2))],
Sqrt[n*sigmahat2/(n - 2)*(1 +
1/n + (x - xbar)^2/ Plus @@ ((xs - xbar)^2))]];
{betahat*(x),
betahat*(x) -
multiplier*Quantile[StudentTDistribution[n - 2], 1 - gamma/2],
betahat*(x) +
multiplier*Quantile[StudentTDistribution[n - 2], 1 - gamma/2]}
]
LinearModelFit
: see the help page, especially the introductory section titled "Properties of predicted values include:". $\endgroup$