Open In App

How to Use the linearHypothesis() Function in R

Last Updated : 16 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • H0 denotes the null hypothesis.
  • C is a matrix representing the coefficients of the linear combination being tested.
  • β represents the vector of coefficients in the regression model.
  • 0 signifies a vector of zeros.

Syntax

linearHypothesis(model, hypothesis.matrix)

Where,

  • model is the fitted regression model for which hypotheses are to be tested.
  • hypothesis.matrix is a matrix specifying the linear hypotheses to be tested.

Implemention of linearHypothesis() Function in R

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).

  • The output provides the results of the linear hypothesis test, including the Residual Degrees of Freedom (Res.Df), Residual Sum of Squares (RSS), the change in the RSS between the restricted and full models, the F-statistic (F), and the corresponding p-value (Pr(>F)).
  • In this case, the p-value is 0.5459, which indicates that we fail to reject the null hypothesis at conventional significance levels, suggesting that there is no significant difference between the coefficients of X1 and X2.

Perform linearHypothesis() Function on mtcars dataset

R
# 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).

  • The output provides the results of the linear hypothesis test, including the Residual Degrees of Freedom (Res.Df), Residual Sum of Squares (RSS), the change in the RSS between the restricted and full models, the F-statistic (F), and the corresponding p-value (Pr(>F)).
  • In this case, the p-value is 0.3321, which indicates that we fail to reject the null hypothesis at conventional significance levels. Therefore, we don’t have sufficient evidence to conclude that the sum of the coefficients of ‘cyl’ and ‘disp’ is significantly different from zero.

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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads