How to Perform a Cramer-Von Mises Test in R

Last Updated : 23 Jul, 2025

The Cramer-Von-Mises test is a non-parametric test used to determine if a sample comes from a specified distribution. It's an alternative to the more commonly used Kolmogorov-Smirnov test and is particularly useful for assessing goodness-of-fit. This article will guide you through performing a Cramer-Von Mises test in R.

What is the Cramer-Von Mises Test?

The Cramer-Von-Mises test evaluates the null hypothesis that a sample comes from a specified distribution, such as the normal distribution. It compares the empirical cumulative distribution function (ECDF) of the sample with the cumulative distribution function (CDF) of the specified distribution.

Now we will discuss Steps to Perform a Cramer-Von Mises Test in R Programming Language.

1. Install and Load Necessary Packages

To perform a Cramer-Von Mises test in R, you'll need the goftest package. If you don't have it installed, you can install it using install. packages().

R
install.packages("goftest")
library(goftest)

2. Generate or Load Your Data

For this example, we'll generate a sample data set from a normal distribution. In practice, you would load your actual data.

R
# Generate a sample of 100 observations from a normal distribution
set.seed(123)
data <- rnorm(100, mean = 0, sd = 1)

3. Perform the Cramer-Von Mises Test

Use the cvm.test() function from the goftest package to perform the test. This function requires the sample data and the distribution to test against.

R
# Perform the Cramer-Von Mises test
result <- cvm.test(data, "pnorm", mean = 0, sd = 1)

# Print the result
print(result)

Output:

	Cramer-von Mises test of goodness-of-fit
Null hypothesis: Normal distribution
with parameters mean = 0, sd = 1
Parameters assumed to be fixed

data: data
omega2 = 0.109, p-value = 0.5434

The output of the Cramer-Von Mises test evaluates whether a given sample of data follows a normal distribution with a specified mean and standard deviation.

  • The null hypothesis states that the sample data comes from a normal distribution with a mean of 0 and a standard deviation of 1. This is the hypothesis that the test aims to either reject or fail to reject based on the data.
  • Parameters of the normal distribution (mean = 0 and sd = 1) are assumed to be fixed and not estimated from the data. In other words, these values are predefined and not derived from the sample data.
  • Since the p-value (0.5434) is much greater than 0.05, we fail to reject the null hypothesis. This means that there is no significant evidence to suggest that the sample data does not come from a normal distribution with mean = 0 and sd = 1. In other words, the sample data is consistent with coming from the specified normal distribution.

The Cramer-Von Mises test result indicates that your sample data does not significantly deviate from a normal distribution with the specified parameters (mean = 0, sd = 1). The test statistic (ω2=0.109) and the high p-value (0.5434) support the conclusion that the sample data is well-fit by the assumed normal distribution.

Conclusion

The Cramer-Von Mises test is a powerful non-parametric test for assessing the goodness-of-fit of a sample to a specified distribution. By using the goftest package in R, you can easily perform this test for various distributions, interpret the results, and make informed decisions about your data. This guide provided a step-by-step approach to conducting the test, helping you ensure that your sample data fits the assumed distribution appropriately.

Comment

Explore