PairedTTest - how to extract 95% confidence intervals for difference between paired means?
I don't think you can. While I like the options with LinearModelFit
and NonlinearModelFit
, whoever wrote PairedTTest
and TTest
(and a few others) seemed to think that all one needs are P-values.
Here's how to get the confidence intervals in a brute-force way:
data1 = {19.4, 21.2, 17.1, 21.1, 19.1, 18.2, 19.6};
data2 = {19.6, 21.1, 18.2, 21.8, 19.5, 18.3, 19.1};
confidenceLevel = 0.95;
diff = data1 - data2;
n = Length[diff];
meanDiff = Mean[diff]
(* -0.271429 *)
se = StandardDeviation[diff]/Sqrt[n];
t = InverseCDF[StudentTDistribution[n - 1], 1 - (1 - confidenceLevel)/2];
{lower, upper} = meanDiff + {-1, 1} t se
(* {-0.757051, 0.214194} *)
Yes it is surprising that such a common task is not available in the latest Statistics framework. PairedTTest[{data1,data2},"ConfidenceInterval"]
really ought to return what you're after (i.e. the object from PairedTTest[{data1,data2},0,"HypothesisTestData"]
ought to contain "ConfidenceInterval"
as a property).
Confidence Intervals existed in V 5.2's package "Statistics`ConfidenceIntervals`"
which was upgraded to the package HypothesisTesting`
. This, in turn, was superseded by the new (kernel) framework of which PairedTTest
is a member. To generate such a confidence interval then, go back to HypothesisTesting`
.
Needs["HypothesisTesting`"]
data1 = {19.4, 21.2, 17.1, 21.1, 19.1, 18.2, 19.6};
data2 = {19.6, 21.1, 18.2, 21.8, 19.5, 18.3, 19.1};
MeanCI[data1 - data2]
(* {-0.757051, 0.214194} *)
MeanCI
's default assumptions match that for a PairedTTest
confidence interval. To explicitly apply a "T-test confidence interval" the package's StudentTCI
can instead be used to define "My" expected syntax:
MyPairedTTest[{data1_, data2_}, 0, "ConfidenceInterval"] := Let[
diff = data1 - data2,
n = Length@diff,
m = Mean@diff,
se = StandardDeviation@diff/Sqrt@n,
StudentTCI[m, se, n - 1]]
(* {-0.757051, 0.214194} *)
N.b, the package's MeanDifferenceCI
will not do here since one of its assumptions is that both samples are independent (unlike the paired or matched case where individual effects are accounted for meaning that smaller differences can define significance; or equivalently, matched-pairs' confidence intervals are tighter)
MeanDifferenceCI[data1, data2]
(* {-1.91857, 1.37571} *)