Open In App

How to Use the linearHypothesis() Function in R

In statistics, understanding how variables relate to each other is crucial. This helps in making smart decisions. When we build regression models, we need to check if certain combinations of variables are statistically significant. In R Programming Language a tool called linear hypothesis () in the "car" package for this purpose. Also, this article gives a simple guide on using linear hypothesis () in R for such analyses.

What is a linear hypothesis?

The linear hypothesis () function is a tool in R's "car" package used to test linear hypotheses in regression models. It helps us to determine if certain combinations of variables have a significant impact on our model's outcome.

H0 : Cβ = 0

Syntax

linearHypothesis(model, hypothesis.matrix)

Where,

Implemention of linearHypothesis() Function in R

# Load the required package
library(car)

# Generate example data
set.seed(123)
df <- data.frame(
  Y = rnorm(100),  # Dependent variable
  X1 = rnorm(100), # Predictor variable 1
  X2 = rnorm(100), # Predictor variable 2
  X3 = rnorm(100)  # Predictor variable 3
)

# Fit a multiple linear regression model
fit <- lm(Y ~ X1 + X2 + X3, data = df)

# Define the hypothesis matrix
# Let's test if the coefficients of X1 and X2 are equal
hypothesis_matrix <- matrix(c(0, 1, -1, 0), nrow = 1)

# Perform the hypothesis test
test_result <- linearHypothesis(fit, hypothesis_matrix)

# Print the test results
print(test_result)

Output:

Linear hypothesis test

Hypothesis:
X1 - X2 = 0

Model 1: restricted model
Model 2: Y ~ X1 + X2 + X3

  Res.Df    RSS Df Sum of Sq      F Pr(>F)
1     97 81.062                           
2     96 80.753  1   0.30903 0.3674 0.5459

The hypothesis being tested is whether the coefficients of X1 and X2 are equal (i.e., X1 - X2 = 0).

Perform linearHypothesis() Function on mtcars dataset

# Load the required package
library(car)

# Use built-in dataset 'mtcars'
data("mtcars")

# Fit a multiple linear regression model
fit <- lm(mpg ~ cyl + disp + hp + drat, data = mtcars)

# Define the hypothesis matrix
# Let's test if the coefficients of 'cyl' and 'disp' sum up to zero
hypothesis_matrix <- matrix(c(0, 1, 1, 0, 0), nrow = 1)

# Perform the hypothesis test
test_result <- linearHypothesis(fit, hypothesis_matrix)

# Print the test results
print(test_result)

Output:

Linear hypothesis test

Hypothesis:
cyl  + disp = 0

Model 1: restricted model
Model 2: mpg ~ cyl + disp + hp + drat

  Res.Df    RSS Df Sum of Sq      F Pr(>F)
1     28 253.75                           
2     27 244.90  1    8.8493 0.9756 0.3321

The hypothesis being tested is whether the coefficients of 'cyl' and 'disp' sum up to zero (i.e., cyl + disp = 0).

Conclusion

The `linearHypothesis()` function in R's "car" package provides a straightforward method for testing linear hypotheses in regression models. The significance of specific variable combinations, analysts can make informed decisions about the model's predictive power.

Article Tags :