What is the coeftest( ) Function?
coeftest() is a function in R that provides coefficient hypothesis testing for a variety of general linear model objects. It is particularly useful for testing hypotheses on the model parameters after the model has been fitted.
The primary role of
coeftest() is to conduct statistical hypothesis tests on the coefficients of a fitted regression model to determine if the predictors in the model are statistically significant. If a predictor is statistically significant, it means that there is evidence to suggest that the predictor variable is correlated with the outcome variable.
In essence, the
coeftest() function is used to determine the statistical significance of each coefficient in the model, as it provides the p-values for each predictor. A low p-value (usually less than 0.05) suggests that the corresponding predictor is statistically significant.
Prerequisites: Installing and Loading the Necessary Packages
To use the
coeftest() function, we need to have the
sandwich packages installed. The
sandwich package provides heteroskedasticity-consistent covariance matrix estimators, which is an essential part of
If you haven’t installed these packages, you can do so with the following commands:
Once installed, load these packages in your R environment using the
Performing Hypothesis Testing with coeftest( )
The basic usage of the
coeftest() function looks like this:
coeftest(object, vcov. = NULL, ...)
object is a fitted model object,
vcov. is a function to compute the covariance matrix, and
... are additional arguments to be passed to the
Let’s consider a practical example. Suppose we have a dataset
mtcars (which is included in R) and we wish to fit a linear regression model predicting
mpg (miles per gallon) from
hp (horsepower) and
First, we fit a linear model using the
data(mtcars) model <- lm(mpg ~ hp + wt, data = mtcars) summary(model)
summary() function provides an overview of the model, including the coefficients. However, if we want to get a more robust standard error estimate, we need the
Here’s how to apply
coeftest() to our model:
This command will output the coefficients, their standard errors, the z-statistic, and corresponding p-values.
If we want to use a heteroskedasticity-consistent covariance matrix estimator (i.e., to compute standard errors that are robust to heteroskedasticity), we can pass the
vcov = vcovHC argument:
coeftest(model, vcov = vcovHC)
In this example,
vcovHC() is a function from the
sandwich package that computes heteroskedasticity-consistent covariance matrix estimators.
Interpretation of the coeftest( ) Output
coeftest() function outputs four columns:
- The first column
Estimateprovides the estimated coefficients for the intercept and each predictor variable.
- The second column
Std. Errorgives the standard error for each coefficient.
- The third column
z valuefor models estimated with robust standard errors) gives the test statistic value.
- The fourth column
Pr(>|z|)for models estimated with robust standard errors) provides the two-tailed p-value for the hypothesis test.
The p-value tests the null hypothesis that each coefficient is equal to zero, given the other predictors in the model. A small p-value (typically less than 0.05) leads us to reject the null hypothesis, suggesting that the predictor is statistically significant.
The Flexibility of coeftest( )
coeftest() function is extremely flexible and can be used with a variety of model objects beyond the simple linear regression models. This includes, but is not limited to, generalized linear models (
glm), mixed-effects models (
lmer), and survival models (
coeftest() with these other model types, you simply substitute the model object into the
coeftest() function. For example:
# Fit a generalized linear model model_glm <- glm(vs ~ hp + wt, family = binomial(), data = mtcars) # Apply coeftest coeftest(model_glm, vcov = vcovHC)
This flexibility makes
coeftest() a valuable function for various statistical modeling and hypothesis testing tasks.
coeftest() function in R is a powerful tool for performing hypothesis testing on the coefficients of a fitted model. This function provides detailed information on each predictor’s significance, allowing data analysts and researchers to make informed decisions based on their models.