Wald Test in R With Examples, A Wald test is a statistical test that compares the fit of two models, one nested within the other.
The test involves estimating the difference between the two models, calculating the variance of that difference, and then comparing the difference to its estimated variance.
In this article, we will show how to perform a Wald test in R with inbuilt datasets.
Formulation of the Hypothesis:
Before conducting the Wald test, it is necessary to formulate the null and alternative hypotheses.
The null hypothesis (H0) assumes that the two models fit the data equally well. It is usually written as:
H0: The difference in the fit between the two models is not significant.
The alternative hypothesis (H1) assumes that one model fits the data significantly better than the other. It is usually written as:
H1: The difference in the fit between the two models is significant.
In the following sections, we will provide examples of how to perform a Wald test in R.
Example 1: Testing the Effectiveness of Vitamin C in Preventing Cold
In this example, we will use the built-in “Orange” dataset in R to test the effectiveness of Vitamin C in preventing a cold.
The dataset has information on the growth of orange trees given different doses of Vitamin C.
First, we need to load the dataset:
Next, we can create two models- one with and one without the use of Vitamin C. We need to fit both the models:
model1<- lm(circumference ~ age, data = Orange) model2 <- lm(circumference ~ age + Tree, data = Orange)
Here, model1 is our null model and model2 is our alternative model. We are assuming that the null model is nested within the alternative model.
Now, we can use the “wald.test” function from the “lmtest” package to perform the Wald test:
library(lmtest) wald_test <- waldtest(model1, model2) wald_test Wald test Model 1: circumference ~ age Model 2: circumference ~ age + Tree Res.Df Df F Pr(>F) 1 33 2 29 4 12.711 4.289e-06 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
The output will display the Wald test statistic, degrees of freedom, the p-value, and a conclusion based on the test results.
In this case, since the p-value is very small, we reject the null hypothesis and conclude that Vitamin C has a significant effect on the growth of orange trees.
In this article, we demonstrated how to perform a Wald test in R using inbuilt datasets.
By utilizing the examples provided in this article, researchers can use the Wald test to test hypotheses regarding the difference in fit between two models or the difference in means between two datasets.