Reputation: 5427
I'd be interested in calculating confidence intervals of data. For example using R:
data = c(1,0,2,1,2,0,1,0)
confidence_interv = (sd(data)/sqrt(8))*qnorm(.95)
Where 8 is the length of the data array. I'd like to do the same with Mathematica, maybe using a quicker way. And, is it possible to plot the data and the confidence interval around the single data? Thanks
Upvotes: 1
Views: 4051
Reputation: 6989
A little different interpretation of the question..
Needs["HypothesisTesting`"]
data = Table[RandomVariate[NormalDistribution[1, 1]], {2000}];
ci = NormalCI[ Mean[data] , StandardDeviation[data],
ConfidenceLevel -> .99]
{-1.55307, 3.58071}
Histogram[data,
Epilog -> {Text[Style["99% confidence range", FontSize -> 20], {1, 100}],
Red,Line[{{#, 0}, {#, 100}}] & /@ ci ,
Arrowheads[{-.05, .05}], Arrow[{{ci[[1]], 70}, {ci[[2]], 70}}]}]
Incidentally you can get the interval like this as well: (w/o loading the HypothesisTesting
package)
InverseCDF[
NormalDistribution[Mean[data], StandardDeviation[data]],
(1 + # {-1, 1})/2 &@.99 ]
{-1.55307, 3.58071}
Upvotes: 2
Reputation: 8655
data = {1, 0, 2, 1, 2, 0, 1, 0};
lm = LinearModelFit[data, {1, x}, x];
lm["SinglePredictionConfidenceIntervalTable", ConfidenceLevel -> 0.95]
Show[ListPlot[data],
Plot[{lm[x],
lm["SinglePredictionBands", ConfidenceLevel -> 0.95]}, {x, 0, 9},
Filling -> {2 -> {1}}], PlotRange -> {Automatic, {-4, 6}},
Frame -> True, ImageSize -> 300]
See also Showing the correlation of two variables using a plot
Upvotes: 1