Contenuto principale

swtest

R2026b

Shapiro–Wilk test

Since R2026b

Description

h = swtest(x) returns a test decision, using the Shapiro–Wilk test, for the null hypothesis that the data in vector x comes from a normal distribution. The alternative hypothesis is that the data does not come from such a distribution. The result h is 1 if the test rejects the null hypothesis at the 5% significance level, and 0 otherwise.

example

h = swtest(x,Name=Value) returns a test decision with additional options specified by one or more name-value arguments. For example, you can change the significance level, or calculate the p-value using a Monte Carlo approximation.

Tip

It is recommended that you specify MonteCarloTolerance when the sample data x contains more than 5000 values.

example

[h,p] = swtest(___) also returns the p-value p, using any of the input argument combinations in the previous syntaxes.

example

[h,p,swstat,critval] = swtest(___) also returns the test statistic swstat and the critical value critval for the test.

example

Examples

collapse all

Load the data set.

load carbig

Test the null hypothesis that the car mileage, in miles per gallon (MPG), follows a normal distribution across different models of cars.

h = swtest(MPG)
h = 
1

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

Load the data set.

load carbig

Test the null hypothesis that the car mileage, in miles per gallon (MPG), follows a normal distribution across different models of cars at the 1% significance level.

[h,p] = swtest(MPG,Alpha=0.01)
h = 
1
p = 
1.1834e-07

Both the returned value h = 1 and the returned p-value less than α = 0.01 indicate that swtest rejects the null hypothesis.

Load the data set.

load carbig

Test the null hypothesis that the car mileage, in miles per gallon (MPG), follows a normal distribution across different models of cars. Use a Monte Carlo simulation to obtain an estimated p-value.

rng(0,"twister") % For reproducibility
[h,p,swstat,critval] = swtest(MPG,MonteCarloTolerance=0.0001)
h = 
1
p = 
0
swstat = 
0.9680
critval = 
0.9926

The returned value h = 1 indicates that swtest rejects the null hypothesis at the default 5% significance level. Because a Shapiro–Wilk test statistic value close to 1 indicates normality, the fact that swstat is smaller than the critical value critval also indicates rejection of the null hypothesis.

Input Arguments

collapse all

Sample data for the hypothesis test, specified as a numeric vector. The function treats NaN values in x as missing values and ignores them. After the function removes NaN values, x must contain at least three observations.

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=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.

Example: [h,p] = swtest(x,Alpha=0.01,MonteCarloTolerance=0.01) specifies a hypothesis test at the 1% significance level, and determines the p-value p using a Monte Carlo simulation with a maximum Monte Carlo standard error of 0.01 for p.

Significance level of the hypothesis test, specified as a scalar value in the range (0,1).

Example: Alpha=0.01

Data Types: single | double

Maximum Monte Carlo standard error for the p-value p, specified as a positive scalar value. If you specify a value for MonteCarloTolerance, the function computes a Monte Carlo approximation for p directly, instead of using the piecewise approximation method of [1]. The function selects a sufficiently large number of Monte Carlo replications to ensure the Monte Carlo standard error for p is less than the value specified for MonteCarloTolerance. It is recommended that you specify MonteCarloTolerance when the sample data x contains more than 5000 values.

Example: MonteCarloTolerance=0.001

Data Types: single | double

Output Arguments

collapse all

Hypothesis test result, returned as 1 or 0.

  • A value of 1 indicates the rejection of the null hypothesis at the Alpha significance level.

  • A value of 0 indicates a failure to reject the null hypothesis at the Alpha significance level.

p-value of the hypothesis test, returned as a scalar value in the range (0,1). p is the probability of observing a test statistic 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.

  • If you do not specify MonteCarloTolerance, the function computes p using the piecewise approximation method of [1]. p is a numeric scalar in the range [0.001,0.50]. If p is outside the tabulated range, the function displays a warning and returns either the smallest or largest tabulated value.

  • If you specify MonteCarloTolerance, the function conducts a Monte Carlo simulation to compute a more accurate p-value, and returns p as a scalar value in the range (0,1). It is recommended that you specify MonteCarloTolerance when the sample data x contains more than 5000 values.

Test statistic for the Shapiro–Wilk test, returned as a nonnegative scalar value. A test statistic value close to 1 indicates normality. Lower values indicate departures from normality. For more information, see Shapiro–Wilk test.

Critical value for the Shapiro–Wilk test at the Alpha significance level, returned as a nonnegative scalar value. If you specify MonteCarloTolerance, the function determines the critical value using a Monte Carlo simulation. The null hypothesis is rejected when swstat < critval.

More About

collapse all

References

[1] Royston, Patrick. “Remark AS R94: A Remark on Algorithm AS 181: The W-Test for Normality.” Applied Statistics 44, no. 4 (1995): 547. https://doi.org/10.2307/2986146.

[2] Shapiro, S. S., and M. B. Wilk. “An Analysis of Variance Test for Normality (Complete Samples).” Biometrika 52, nos. 3–4 (1965): 591–611. https://doi.org/10.1093/biomet/52.3-4.591.

Version History

Introduced in R2026b