ttest (original) (raw)

One-sample and paired-sample _t_-test

Syntax

Description

[h](#btqjrr4%5Fsep%5Fshared-h) = ttest([x](#btqjrr4-x)) returns a test decision for the null hypothesis that the data in x comes from a normal distribution with mean equal to zero and unknown variance, using the one-sample _t_-test. The alternative hypothesis is that the population distribution does not have a mean equal to zero. The result h is 1 if the test rejects the null hypothesis at the 5% significance level, and 0 otherwise.

example

[h](#btqjrr4%5Fsep%5Fshared-h) = ttest([x](#btqjrr4-x),[y](#btqjrr4-y)) returns a test decision for the null hypothesis that the data in x – y comes from a normal distribution with mean equal to zero and unknown variance, using the paired-sample _t_-test.

example

[h](#btqjrr4%5Fsep%5Fshared-h) = ttest([x](#btqjrr4-x),[y](#btqjrr4-y),[Name,Value](#namevaluepairarguments)) returns a test decision for the paired-sample _t_-test with additional options specified by one or more name-value pair arguments. For example, you can change the significance level or conduct a one-sided test.

example

[h](#btqjrr4%5Fsep%5Fshared-h) = ttest([x](#btqjrr4-x),[m](#btqjrr4-m)) returns a test decision for the null hypothesis that the data in x comes from a normal distribution with mean m and unknown variance. The alternative hypothesis is that the mean is not m.

example

[h](#btqjrr4%5Fsep%5Fshared-h) = ttest([x](#btqjrr4-x),[m](#btqjrr4-m),[Name,Value](#namevaluepairarguments)) returns a test decision for the one-sample _t_-test with additional options specified by one or more name-value pair arguments. For example, you can change the significance level or conduct a one-sided test.

example

[[h](#btqjrr4%5Fsep%5Fshared-h),[p](#btqjrr4%5Fsep%5Fshared-p)] = ttest(___) also returns the _p_-value, p, of the test, using any of the input arguments from the previous syntax groups.

example

[[h](#btqjrr4%5Fsep%5Fshared-h),[p](#btqjrr4%5Fsep%5Fshared-p),[ci](#btqjrr4%5Fsep%5Fshared-ci),[stats](#btqjrr4-stats)] = ttest(___) also returns the confidence interval ci for the mean of x, or of x – y for the paired _t_-test, and the structure stats containing information about the test statistic.

example

Examples

collapse all

_t_-Test for Mean Equal to Zero

Load the sample data. Create a vector containing the third column of the stock returns data.

load stockreturns x = stocks(:,3);

Test the null hypothesis that the sample data comes from a population with mean equal to zero.

[h,p,ci,stats] = ttest(x)

stats = struct with fields: tstat: -2.6065 df: 99 sd: 1.6027

The returned value h = 1 indicates that ttest rejects the null hypothesis at the 5% significance level.

_t_-Test at Different Significance Level

Load the sample data. Create a vector containing the third column of the stock returns data.

load stockreturns x = stocks(:,3);

Test the null hypothesis that the sample data are from a population with mean equal to zero at the 1% significance level.

h = ttest(x,0,'Alpha',0.01)

The returned value h = 0 indicates that ttest does not reject the null hypothesis at the 1% significance level.

Paired-Sample _t_-Test

Load the sample data. Create vectors containing the first and second columns of the data matrix to represent students’ grades on two exams.

load examgrades x = grades(:,1); y = grades(:,2);

Test the null hypothesis that the pairwise difference between data vectors x and y has a mean equal to zero.

The returned value of h = 0 indicates that ttest does not reject the null hypothesis at the default 5% significance level.

Paired-Sample _t_-Test at Different Significance Level

Load the sample data. Create vectors containing the first and second columns of the data matrix to represent students’ grades on two exams.

load examgrades x = grades(:,1); y = grades(:,2);

Test the null hypothesis that the pairwise difference between data vectors x and y has a mean equal to zero at the 1% significance level.

[h,p] = ttest(x,y,'Alpha',0.01)

The returned value of h = 0 indicates that ttest does not reject the null hypothesis at the 1% significance level.

_t_-Test for a Hypothesized Mean

Load the sample data. Create a vector containing the first column of the students' exam grades data.

load examgrades x = grades(:,1);

Test the null hypothesis that sample data comes from a distribution with mean m = 75.

The returned value of h = 0 indicates that ttest does not reject the null hypothesis at the 5% significance level.

One-Sided _t_-Test

Load the sample data. Create a vector containing the first column of the students’ exam grades data.

load examgrades x = grades(:,1);

Plot a histogram of the exam grades data and fit a normal density function.

histfit(x) xlabel("Grade") ylabel("Frequency")

Figure contains an axes object. The axes object with xlabel Grade, ylabel Frequency contains 2 objects of type bar, line.

Use a right-tailed _t_-test to test the null hypothesis that the data comes from a population with mean equal to 65, against the alternative that the mean is greater than 65.

[h,,,stats] = ttest(x,65,"Tail","right")

stats = struct with fields: tstat: 12.5726 df: 119 sd: 8.7202

The returned value of h = 1 indicates that ttest rejects the null hypothesis at the default significance level of 5%, in favor of the alternative hypothesis that the data comes from a population with a mean greater than 65.

Plot the corresponding Student's _t-_distribution, the returned _t_-statistic, and the critical _t_-value. Calculate the critical _t_-value for the default confidence level of 95% by using tinv.

nu = stats.df; k = linspace(-15,15,300); tdistpdf = tpdf(k,nu); tval = stats.tstat

tvalpdf = tpdf(tval,nu); tcrit = tinv(0.95,nu)

plot(k,tdistpdf) hold on scatter(tval,tvalpdf,"filled") xline(tcrit,"--") legend(["Student's t pdf", "t-Statistic", ... "Critical Cutoff"])

Figure contains an axes object. The axes object contains 3 objects of type line, scatter, constantline. These objects represent Student's t pdf, t-Statistic, Critical Cutoff.

The orange dot represents the _t_-statistic and is located to the right of the dashed black line that represents the critical _t_-value.

Input Arguments

collapse all

x — Sample data

vector | matrix | multidimensional array

Sample data, specified as a vector, matrix, or multidimensional array. ttest performs a separate _t_-test along each column and returns a vector of results. If y sample data is specified, x and y must be the same size.

Data Types: single | double

y — Sample data

vector | matrix | multidimensional array

Sample data, specified as a vector, matrix, or multidimensional array. If y sample data is specified, x and y must be the same size.

Data Types: single | double

m — Hypothesized population mean

0 (default) | scalar value

Hypothesized population mean, specified as a scalar value.

Data Types: single | double

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'Tail','right','Alpha',0.01 conducts a right-tailed hypothesis test at the 1% significance level.

Significance level of the hypothesis test, specified as the comma-separated pair consisting of 'Alpha' and a scalar value in the range (0,1).

Example: 'Alpha',0.01

Data Types: single | double

Dimension of the input matrix along which to test the means, specified as the comma-separated pair consisting of 'Dim' and a positive integer value. For example, specifying 'Dim',1 tests the column means, while 'Dim',2 tests the row means.

Example: 'Dim',2

Data Types: single | double

Tail — Type of alternative hypothesis

'both' (default) | 'right' | 'left'

Type of alternative hypothesis to evaluate, specified as the comma-separated pair consisting of 'Tail' and one of:

ttest tests the null hypothesis that the population mean is m against the specified alternative hypothesis.

Example: 'Tail','right'

Output Arguments

collapse all

Hypothesis test result, returned as 1 or 0.

_p_-value of the test, returned as a scalar value in the range [0,1].p is the probability of observing a test statistic that is as extreme as, or more extreme than, the observed value under the null hypothesis. A small value of p indicates that the null hypothesis might not be valid.

Confidence interval for the true population mean, returned as a two-element vector containing the lower and upper boundaries of the 100 × (1 – Alpha)% confidence interval.

stats — Test statistics

structure

Test statistics, returned as a structure containing the following:

More About

collapse all

One-Sample t-Test

The one-sample _t_-test is a parametric test of the location parameter when the population standard deviation is unknown.

The test statistic is

where x¯ is the sample mean, _μ_ is the hypothesized population mean, s is the sample standard deviation, and n is the sample size. Under the null hypothesis, the test statistic has Student’s t distribution with n – 1 degrees of freedom.

Multidimensional Array

A multidimensional array has more than two dimensions. For example, if x is a 1-by-3-by-4 array, then x is a three-dimensional array.

First Nonsingleton Dimension

The first nonsingleton dimension is the first dimension of an array whose size is not equal to 1. For example, if x is a 1-by-2-by-3-by-4 array, then the second dimension is the first nonsingleton dimension of x.

Tips

Extended Capabilities

GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a