Determining the Suitability of Your Model: Goodness-of-Fit Tests in R
In this tutorial, we will learn how to perform goodness-of-fit tests in R using the stats package.
Goodness-of-fit tests are statistical tests that are used to evaluate whether a sample comes from a population with a specific distribution. These tests compare the observed data to what is expected if the data follow a certain distribution.
In this tutorial, we will learn how to perform goodness-of-fit tests in R using the stats package.
Kolmogorov-Smirnov Test
The Kolmogorov-Smirnov (K-S) test is a nonparametric test that can be used to evaluate whether a sample comes from a population with a specific continuous distribution.
To perform the K-S test in R, we can use the ks.test function from the stats package.
# Load the stats packagelibrary(stats)
# Sample datasample <- c(0.5, 0.4, 0.35, 0.3, 0.25)
# Perform the K-S testks.test(sample, "pnorm", mean = 0, sd = 1)The ks.test function takes three arguments: the sample data, the name of the distribution to test against, and the parameters of the distribution. In this case, we are testing whether the sample comes from a normal distribution with a mean of 0 and a standard deviation of 1.
The function returns a list containing the K-S statistic, the p-value, and the method used. The K-S statistic represents the maximum difference between the observed cumulative distribution function and the expected cumulative distribution function for the given distribution. The p-value is the probability that the difference between the observed and expected cumulative distribution functions is due to chance.
If the p-value is below a certain threshold (usually 0.05), we can reject the null hypothesis that the sample comes from the specified distribution and conclude that there is a significant difference between the two.
Anderson-Darling Test
The Anderson-Darling test is another goodness-of-fit test that can be used to evaluate whether a sample comes from a population with a specific distribution. It is a more powerful test than the K-S test, but it is also more sensitive to deviations from normality.
To perform the Anderson-Darling test in R, we can use the ad.test function from the stats package.
# Load the stats packagelibrary(stats)
# Sample datasample <- c(0.5, 0.4, 0.35, 0.3, 0.25)
# Perform the Anderson-Darling testad.test(sample)The ad.test function takes one argument: the sample data. The function returns a list containing the Anderson-Darling statistic and the p-value.
The Anderson-Darling statistic represents the difference between the observed and expected cumulative distribution functions. The p-value is the probability that the difference between the observed and expected cumulative distribution functions is due to chance.
If the p-value is below a certain threshold (usually 0.05), we can reject the null hypothesis that the sample comes from a normally distributed population and conclude that there is a significant difference between the two.
It’s important to note that the Anderson-Darling test is sensitive to the sample size, so it is recommended to use a sample size of at least 20 for the test to be reliable.
Chi-Square Test
The chi-square (χ²) test is a goodness-of-fit test that can be used to evaluate whether a sample comes from a population with a specific discrete distribution.
To perform the chi-square test in R, we can use the chisq.test function from the stats package.
# Load the stats packagelibrary(stats)
# Sample datasample <- c(0.5, 0.4, 0.35, 0.3, 0.25)
# Expected frequenciesexpected <- c(1, 1, 1, 1, 1)
# Perform the chi-square testchisq.test(sample, p = expected)The chisq.test function takes two arguments: the sample data and the expected frequencies. The function returns a list containing the chi-square statistic, the p-value, the degrees of freedom, and the method used.
The chi-square statistic represents the difference between the observed and expected frequencies. The p-value is the probability that the difference between the observed and expected frequencies is due to chance.
If the p-value is below a certain threshold (usually 0.05), we can reject the null hypothesis that the sample comes from the specified distribution and conclude that there is a significant difference between the two.
Conclusion
In this tutorial, we learned how to perform goodness-of-fit tests in R using the stats package. We saw examples of the Kolmogorov-Smirnov test, the Anderson-Darling test, and the chi-square test.
It’s important to choose the appropriate goodness-of-fit test based on the nature of the data and the distribution being tested. The K-S test is suitable for continuous data, the Anderson-Darling test is suitable for normally distributed data, and the chi-square test is suitable for discrete data.
References:
- R stats package documentation: https://stat.ethz.ch/R-manual/R-devel/library/stats/html/00Index.html
- “Nonparametric Statistics for Non-Statisticians: A Step-by-Step Approach” by Anne Salib (2007)
Comments