TTest
更多信息和选项
- TTest 检验零假设
与替代假设
: -


data 

{data1,data2} 

- 其中 μi 是 datai 的总体均值.
- 默认情况下,返回概率值或者
值. - 小的
值表明
不大可能为真. - dspec 中的数据可以是单变量 {x1,x2,…} 或者多变量 {{x1,y1,…},{x2,y2,…},…}.
- 变量 μ0 可以是一个实数,或者长度等于数据维度的实向量.
- TTest 假设数据服从正态分布,但是对于该假定是相当健壮的. TTest 也假定在两个样本的情况下,样本是独立的.
- TTest[dspec,μ0,"HypothesisTestData"] 返回一个 HypothesisTestData 对象 htd,可以使用 htd["property"] 的形式来提取额外检验结果和属性.
- TTest[dspec,μ0,"property"] 可以用于直接给出 "property" 的值.
- 与检验结果的报告相关的属性包括:
-
"DegreesOfFreedom" 检验中所用的自由度 "PValue"
值列表"PValueTable"
值组成的格式化表格"ShortTestConclusion" 检验结论的简短描述 "TestConclusion" 检验结论的描述 "TestData" 检验统计量和
值对的列表"TestDataTable"
值和检验统计量组成的格式化表格"TestStatistic" 检验统计量组成的列表 "TestStatisticTable" 检验统计量组成的格式化表格 - 对于单变量样本,TTest 执行学生
检验. 检验统计量假设服从 StudentTDistribution[df]. - 对于多变量样本,TTest 执行霍特林
检验. 检验统计量假设服从HotellingTSquareDistribution[p,df],其中 p 是 data 的维度. - 自由度 df,用于指定检验统计量的分布,取决于样本规模、样本数,以及在两个单变量样本情况下,等方差检验的结果.
- 可以使用以下选项:
-
AlternativeHypothesis "Unequal" 备择假设的不等性 SignificanceLevel 0.05 诊断和报告的分界点 VerifyTestAssumptions Automatic 需要验证的假设 - 对于 TTest,选择一个临界值
,使得当且仅当
时,否定
. 用于 "TestConclusion" 和 "ShortTestConclusion" 属性的
值由 SignificanceLevel 选项控制.
值也用于假设诊断检验中,包括正态性检验、等方差检验和对称性检验.
值在缺省情况下默认为 0.05. - 在 TTest 中,VerifyTestAssumptions 的已命名设置包括:
-
"Normality" 验证所有数据服从正态分布 "EqualVariance" 验证 data1 和 data2 具有相等的方差
范例
打开所有单元 关闭所有单元基本范例 (3)
data = RandomVariate[NormalDistribution[0.05, 1], 10^4];TTest[data]TTest[data, Automatic, "TestDataTable"]BlockRandom[SeedRandom[1];data1 = RandomVariate[NormalDistribution[1.85, 1], 1000];
data2 = RandomVariate[NormalDistribution[0, 1], 1000]];Mean[data1] - Mean[data2]SmoothHistogram[{data1, data2 + 2}]TTest[{data1, data2}, 2]BlockRandom[SeedRandom[1];data1 = RandomVariate[MultinormalDistribution[{1, 2}, IdentityMatrix[2]], 1000];
data2 = RandomVariate[MultinormalDistribution[{0, 0}, IdentityMatrix[2]], 1000]];Mean[data1] - Mean[data2]Histogram3D[{data1, Transpose[Transpose[data2] + {1, 2}]}]TTest[{data1, data2}, {1, 2}]范围 (13)
检验 (10)
SeedRandom[1];
data1 = RandomVariate[NormalDistribution[0, 1], 500];
data2 = RandomVariate[NormalDistribution[3, 1], 500];TTest[data1]TTest[data2]使用 Automatic 等价于对零点的均值测试:
data = RandomVariate[NormalDistribution[0, 1], 500];TTest[data, 0]TTest[data, Automatic]SeedRandom[1];
data1 = RandomVariate[NormalDistribution[3, 1], 500];
data2 = RandomVariate[NormalDistribution[0, 1], 200];TTest[data1, 3]TTest[data2, 3]SeedRandom[1];data = RandomVariate[MultinormalDistribution[{.1, 0, -.05, 0}, IdentityMatrix[4]], 10^3];TTest[data]TTest[data, {0.1, 0, -.05, 0}]SeedRandom[1];data1 = RandomVariate[NormalDistribution[0, 1], 100];
data2 = RandomVariate[NormalDistribution[1, 1], 150];
data3 = RandomVariate[NormalDistribution[0, 2], 130];TTest[{data1, data2}, 0]TTest[{data1, data3}, 0]SeedRandom[1];data1 = RandomVariate[NormalDistribution[3, 1], 135];
data2 = RandomVariate[NormalDistribution[0, 1], 100];TTest[{data1, data2}, 3]TTest[{data2, data1}, 3]SeedRandom[1];data1 = RandomVariate[MultinormalDistribution[{.5, 0, -.5, 0}, IdentityMatrix[4]], 122];data2 = RandomVariate[MultinormalDistribution[{-.5, 0, .5, 0}, IdentityMatrix[4]], 135];TTest[{data1, data2}]TTest[{data1, data2}, {1, 0, -1, 0}]创建一个 HypothesisTestData 对象来进行重复性质提取:
SeedRandom[1];data = RandomVariate[NormalDistribution[], {2, 10^4}];ℋ = TTest[data, 0, "HypothesisTestData"];ℋ["Properties"]从一个 HypothesisTestData 对象中提取一些属性:
SeedRandom[2];data = RandomVariate[NormalDistribution[], {2, 10^4}];ℋ = TTest[data, 0, "HypothesisTestData"];ℋ["PValue"]ℋ["TestStatistic"]ℋ["DegreesOfFreedom"]SeedRandom[2];data = RandomVariate[NormalDistribution[], {2, 10^4}];ℋ = TTest[data, 0, "HypothesisTestData"];ℋ["PValue", "TestStatistic", "DegreesOfFreedom"]报告 (3)
SeedRandom[1];data = RandomVariate[NormalDistribution[], {2, 20}];ℋ = TTest[data, 0, "HypothesisTestData"];ℋ["TestDataTable"]SeedRandom[1];data = RandomVariate[NormalDistribution[], {1000, 25}];res = Table[TTest[v, 0, "TestData", VerifyTestAssumptions -> None], {v, data}];ListPlot[res, FrameLabel -> {"T", "p-value"}, Frame -> True, PlotRange -> All]SeedRandom[1];data = RandomVariate[NormalDistribution[], {2, 10^2}];ℋ = TTest[data, 0, "HypothesisTestData"];ℋ["PValueTable"]ℋ["PValue"]ℋ["TestStatisticTable"]ℋ["TestStatistic"]选项 (11)
AlternativeHypothesis (3)
SeedRandom[1];
data = RandomVariate[NormalDistribution[], 100];TTest[data, 0, AlternativeHypothesis -> "Unequal"]TTest[data, 0, AlternativeHypothesis -> Automatic]SeedRandom[1];
data = RandomVariate[NormalDistribution[], 100];TTest[data, 0, AlternativeHypothesis -> "Unequal"]TTest[data, 0, AlternativeHypothesis -> "Less"]TTest[data, 0, AlternativeHypothesis -> "Greater"]SeedRandom[1];
data1 = RandomVariate[NormalDistribution[2.9, 1], 1000];
data2 = RandomVariate[NormalDistribution[0, 1], 1000];Mean[data1] - Mean[data2]TTest[{data1, data2}, 3, AlternativeHypothesis -> "Less"]TTest[{data1, data2}, 2.9, AlternativeHypothesis -> "Less"]SignificanceLevel (2)
data = BlockRandom[SeedRandom[2];RandomVariate[StudentTDistribution[3], 50]];TTest[data, 0, SignificanceLevel -> .0001]TTest[data, 0]显著性水平也被用于 "TestConclusion" 和 "ShortTestConclusion":
BlockRandom[SeedRandom[1];data = RandomVariate[NormalDistribution[0, 1], 100]];Mean[data]ℋ1 = TTest[data, .2, "HypothesisTestData", SignificanceLevel -> .1];ℋ2 = TTest[data, .2, "HypothesisTestData", SignificanceLevel -> .005];ℋ1["TestConclusion"]//TraditionalFormℋ2["TestConclusion"]//TraditionalFormℋ1["ShortTestConclusion"]ℋ2["ShortTestConclusion"]VerifyTestAssumptions (6)
data1 = RandomVariate[NormalDistribution[0, 1], 100];
data2 = RandomVariate[NormalDistribution[0, 2], 100];TTest[{data1, data2}, 0, VerifyTestAssumptions -> Automatic]TTest[{data1, data2}, 0, VerifyTestAssumptions -> None]data1 = RandomVariate[NormalDistribution[0, 1], 100];
data2 = RandomVariate[NormalDistribution[0, 2], 100];TTest[{data1, data2}, 0, VerifyTestAssumptions -> All]TTest[{data1, data2}, 0, VerifyTestAssumptions -> None]data1 = RandomVariate[NormalDistribution[0, 1], 100];
data2 = RandomVariate[NormalDistribution[0, 2], 100];TTest[{data1, data2}, 0, VerifyTestAssumptions -> "EqualVariance"]TTest[{data1, data2}, 0, VerifyTestAssumptions -> "Normality"]设置相等方差假设为 False:
TTest[{data1, data2}, 0, VerifyTestAssumptions -> "EqualVariance" -> False]data = RandomVariate[CauchyDistribution[0, 1], 100];TTest[data, 0, VerifyTestAssumptions -> "EqualVariance"]TTest[data, 0, VerifyTestAssumptions -> All]data = RandomVariate[MultinormalDistribution[{0, 0, 0}, IdentityMatrix[3]], 10 ^ 4];TTest[data, Automatic, "TestDataTable", VerifyTestAssumptions -> All]//AbsoluteTimingTTest[data, Automatic, "TestDataTable", VerifyTestAssumptions -> None]//AbsoluteTimingdata = RandomVariate[NormalDistribution[], {1000, 100}];AbsoluteTiming[T = Quiet@TTest[#, Automatic, "TestStatistic"]& /@ data;]AbsoluteTiming[T2 = Quiet@TTest[#, Automatic, "TestStatistic", VerifyTestAssumptions -> None]& /@ data;]SmoothHistogram[{T, T2}]应用 (4)
SeedRandom[1];data1 = RandomVariate[NormalDistribution[0, 1], 100];
data2 = RandomVariate[NormalDistribution[0, 1], 100];
data3 = RandomVariate[NormalDistribution[2, 1], 100];BoxWhiskerChart[{data1, data2, data3}]TTest[{data1, data2}, Automatic, "TestDataTable"]TTest[{data1, data3}, Automatic, "TestDataTable"]光的传代时间的测量的”第三级“在1882年由 Newcomb 记录. 给定值除以1000再加24给出光穿过一个已知距离的以百万分之一秒计的时间. 现在真实值被认为是33.02:
lspeed = ExampleData[{"Statistics", "NewcombLightSpeed"}];SmoothHistogram[lspeed, PlotRange -> All]ChauvenetOutlier[d_, data_] := Probability[x <= d, xNormalDistribution[Mean[data], StandardDeviation[data]]] < (1/2Length[data])outliers = Select[lspeed, ChauvenetOutlier[#, lspeed]&]newcomb = DeleteCases[lspeed, Alternatives@@outliers];一个大量数据上的
检验暗示光速的 Newcomb 测试显著地低于实际:
Mean[newcomb]//NTTest[newcomb, 33.02, "TestDataTable"]记录从两个实验卷心菜品种中各取30个样本的维他命 C 含量和一头卷心菜的重量:
ExampleData[{"Statistics", "Cabbages"}, "ColumnDescriptions"]cabbageData = ExampleData[{"Statistics", "Cabbages"}];cultivar = cabbageData[[All, 1]];w39 = Pick[cabbageData[[All, 3]], cultivar, "c39"];
w52 = Pick[cabbageData[[All, 3]], cultivar, "c52"];
c39 = Pick[cabbageData[[All, 4]], cultivar, "c39"];
c52 = Pick[cabbageData[[All, 4]], cultivar, "c52"];{SmoothHistogram[{w39, w52}, PlotLabel -> "Head Weight"], SmoothHistogram[{c39, c52}, PlotLabel -> "Vitamin C", PlotLegends -> {"c39", "c52"}]}TTest[{c52, c39}, 0, "TestDataTable"]对于 c52 而言重量数据不是正态分布的,因此使用 MannWhitneyTest 来显示一个显著较轻的卷心菜产生了显著多量的维他命 C:
DistributionFitTest[w52]MannWhitneyTest[{w52, w39}, 0, "TestDataTable"]从三种鸢尾花中各取五十个样本. 这些样本包含了对鸢尾花的花萼和花瓣的长和宽的测量. 很难区分 virginica 和 versicolor 的区别:
iris = ExampleData[{"Statistics", "FisherIris"}];species = iris[[All, -1]];versicolorData = Pick[iris[[All, 1 ;; -2]], species, "versicolor"];
virginicaData = Pick[iris[[All, 1 ;; -2]], species, "virginica"];TTest[{versicolorData, virginicaData}, 0, "TestDataTable"]clmnDescr = ExampleData[{"Statistics", "FisherIris"}, "ColumnDescriptions"][[1 ;; -2]];
lgnd = Placed[LineLegend[{RGBColor[0.368417, 0.506779, 0.709798], RGBColor[0.880722, 0.611041, 0.142051]}, {"versicolor", "virginica"}, LegendLayout -> "Row"], Bottom];Legended[Table[SmoothHistogram[{versicolorData[[All, i]], virginicaData[[All, i]]}, PlotLabel -> clmnDescr[[i]]], {i, Length[clmnDescr]}], lgnd]属性和关系 (11)
对于单变量数据,检验统计量在
下满足 StudentTDistribution:
data = RandomVariate[NormalDistribution[], {1000, 15}];T = Table[TTest[i, Automatic, "TestStatistic", VerifyTestAssumptions -> None], {i, data}];DistributionFitTest[T, StudentTDistribution[14]]对于多变量数据,检验统计量在
下满足 HotellingTSquareDistribution:
data = RandomVariate[BinormalDistribution[.5], {1000, 25}];T = Table[TTest[i, Automatic, "TestStatistic", VerifyTestAssumptions -> None], {i, data}];DistributionFitTest[T, HotellingTSquareDistribution[2, 23]]𝒹1 = RandomVariate[NormalDistribution[0, 1], 100];
𝒹2 = RandomVariate[NormalDistribution[0, 1], 95];
𝒹3 = RandomVariate[NormalDistribution[0, 5], 100];L[x_] := Length[x];v[x_] := Variance[x]TTest[𝒹1, 0, "DegreesOfFreedom"]L[𝒹1] - 1TTest[{𝒹1, 𝒹2}, 0, "DegreesOfFreedom"]L[𝒹1] + L[𝒹2] - 2TTest[{𝒹1, 𝒹3}, 0, "DegreesOfFreedom"](((v[𝒹1]/L[𝒹1]) + (v[𝒹3]/L[𝒹3]))^2/((v[𝒹1]/L[𝒹1]))^2 / (L[𝒹1] - 1) + ((v[𝒹3]/L[𝒹3]))^2 / (L[𝒹3] - 1))自由度的类型由 VerifyTestAssumptions 控制:
𝒹1 = RandomVariate[NormalDistribution[0, 1], 95];
𝒹2 = RandomVariate[NormalDistribution[0, 5], 100];TTest[{𝒹1, 𝒹2}, 0, "DegreesOfFreedom", VerifyTestAssumptions -> {"EqualVariance" -> True, "Normality"}]TTest[{𝒹1, 𝒹2}, 0, "DegreesOfFreedom", VerifyTestAssumptions -> {"EqualVariance" -> False, "Normality"}]data = RandomVariate[MultinormalDistribution[{1, 2, 3}, IdentityMatrix[3]], 100];MahalanobisDistanceSquared[data_, mu_] := With[{inv = Inverse@Covariance[data], m = Mean[data]}, (m - mu).inv.(m - mu)]Subscript[μ, 0] = {1, 2, 3};T2 = Length[data] * MahalanobisDistanceSquared[data, Subscript[μ, 0]]在
下, 检验统计量满足 HotellingTSquareDistribution[p,n-1]:
pvalue = SurvivalFunction[HotellingTSquareDistribution[3, 99], T2]TTest[data, {1, 2, 3}, "TestDataTable"]如果总体变量已知,可以使用更有力的 ZTest:
σ = .25;data = RandomVariate[NormalDistribution[2.5, Sqrt[σ]], {1000, 15}];T = TTest[#, 2, VerifyTestAssumptions -> None]& /@ data;Z = ZTest[#, σ, 2, VerifyTestAssumptions -> None]& /@ data;α = 0.05;Probability[x < α, xT]//NProbability[x < α, xZ]//NTTest 对于从正态性的轻度偏离仍然是健壮的:
data = BlockRandom[SeedRandom[1];RandomVariate[𝒹 = StudentTDistribution[5], {1000, 35}]];Plot[{PDF[𝒹, x], PDF[NormalDistribution[], x]}, {x, -4, 4}, PlotLegends -> {"𝒹", NormalDistribution}]pvals = TTest[#, VerifyTestAssumptions -> None]& /@ data;DistributionFitTest[pvals, UniformDistribution[]]data = BlockRandom[SeedRandom[1];RandomVariate[𝒹 = CauchyDistribution[0, 1], {1000, 35}]];Plot[{PDF[𝒹, x], PDF[NormalDistribution[], x]}, {x, -4, 4}, PlotLegends -> {"𝒹", NormalDistribution}]tpvals = TTest[#, VerifyTestAssumptions -> None]& /@ data;srpvals = SignedRankTest /@ data//Quiet;对于 SignedRankTest,
值可以用常规方式解释,但对于 TTest 则不行:
DistributionFitTest[tpvals, UniformDistribution[]]DistributionFitTest[srpvals, UniformDistribution[]]对于非正态数据的二样本检定,应使用 MannWhitneyTest:
data1 = RandomVariate[LaplaceDistribution[0, 1], {1000, 35}];
data2 = RandomVariate[LaplaceDistribution[.5, 1], {1000, 25}];t = MapThread[TTest[{#1, #2}, VerifyTestAssumptions -> None]&, {data1, data2}];mw = MapThread[MannWhitneyTest[{#1, #2}]&, {data1, data2}];对于非正态数据, MannWhitneyTest 比 TTest 更有力:
Probability[x ≤ .05, xt]//NProbability[x ≤ 0.05, xmw]//NTTest 只在输入为 TimeSeries 时对数值有效:
ts = TemporalData[TimeSeries, {{{1.224578634529677, 0.47929635789978015, 0.6572781300178168,
0.21496048742669355, 0.7299608014554928, -0.2495111111278263, -1.3286551762002712,
0.552725018274874, 0.19272112205837066, 1.1809144012420882, -1.1671 ... 40938613662046, 1.052394590214582, 0.9345044123980388, 0.38537803109557855,
-0.48660931166089394, -0.71203560340161}}, {{0, 100, 1}}, 1, {"Continuous", 1},
{"Discrete", 1}, 1, {ValueDimensions -> 1, ResamplingMethod -> None}}, False, 10.1];TTest[ts]TTest[ts["Values"]]TTest 在输入为 TemporalData 时会处理所有数值:
td = TemporalData[Automatic, {{{-0.25275046867718637, -0.7175779198306353, -1.9370139837317764,
0.006665621735740701, -0.3730807122324292, 0.6740106823161018, 0.8562214990564344,
0.955785083955732, 1.7020898014886303, 1.8523009430646802, 0.244 ... 3951759101545, -1.1611722313627828, 1.1602446901533021,
1.1052173095128992, 1.1089143920161917, -0.13837156328402}}, {{0, 100, 1}}, 2,
{"Continuous", 2}, {"Discrete", 1}, 1, {ValueDimensions -> 1, ResamplingMethod -> None}}, False,
10.1];TTest[td]data = td["ValueList"]//Flatten;
TTest[data]{data1, data2} = td["ValueList"];TTest[{data1, data2}]可能存在的问题 (2)
TTest 假设数据是正态分布的:
data = RandomVariate[ParetoDistribution[1, 2], 100];TTest[data]SignTest[data]data = BlockRandom[SeedRandom[3];RandomVariate[SuzukiDistribution[0.3, 38], {15, 2}]];Quiet[TTest[data], {TTest::nortst}]巧妙范例 (1)
data = RandomVariate[NormalDistribution[], {2500, 100}];T1 = TTest[#, 0, "TestStatistic", VerifyTestAssumptions -> None]& /@ data;T2 = TTest[#, 1, "TestStatistic", VerifyTestAssumptions -> None]& /@ data;SmoothHistogram[{T1, T2}, Filling -> Axis, PlotLegends -> {"SubscriptBox[H, 0] is True", "SubscriptBox[H, 0] is False"}, PlotStyle -> Thick]相关指南
-
▪
- 假设检验
文本
Wolfram Research (2010),TTest,Wolfram 语言函数,https://reference.wolfram.com/language/ref/TTest.html.
CMS
Wolfram 语言. 2010. "TTest." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/TTest.html.
APA
Wolfram 语言. (2010). TTest. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/TTest.html 年
BibTeX
@misc{reference.wolfram_2026_ttest, author="Wolfram Research", title="{TTest}", year="2010", howpublished="\url{https://reference.wolfram.com/language/ref/TTest.html}", note=[Accessed: 11-September-2026]}
BibLaTeX
@online{reference.wolfram_2026_ttest, organization={Wolfram Research}, title={TTest}, year={2010}, url={https://reference.wolfram.com/language/ref/TTest.html}, note=[Accessed: 11-September-2026]}