- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to test for the difference between two regression coefficients in R?
To test for the difference between two regression coefficients, we can follow the below steps −
- First of all, creating data frame.
- Then creating a regression model.
- After that testing the difference between regression coefficients using LienarHypothesis function of car package.
Create the data frame
Let's create a data frame as shown below −
x1<-rnorm(20) x2<-rnorm(20,5,1) y1<-rnorm(20) df<-data.frame(x1,x2,y1) df
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x1 x2 y1 1 -0.20412848 4.556105 0.45908013 2 0.30523549 5.569055 -0.03171895 3 0.04370042 5.699348 -0.36329671 4 -1.17038676 3.946887 -0.36719554 5 0.44386866 5.421256 -1.22089962 6 0.73872256 4.206500 -0.54798050 7 -1.25760889 4.302561 0.86247603 8 -1.91528711 4.956543 1.46603539 9 -1.53261347 4.281640 1.13248696 10 0.18349241 5.587560 1.84692077 11 -0.87403060 5.563882 1.01042402 12 0.11175919 6.364895 0.12010507 13 1.25899545 4.760614 -0.33105484 14 -0.17737332 3.466253 0.70906765 15 -0.69325729 4.905586 -1.02842953 16 0.32417246 5.873589 -0.28118196 17 -0.46810874 5.690111 -0.03787195 18 1.26463339 4.674473 -0.80026871 19 -1.11437137 6.029646 1.77880908 20 -0.51711414 6.120406 -1.87254637
Creating regression model
Use lm function to create the regression model with x1 and x2 as independent variable and y1 as dependent variable −
x1<-rnorm(20) x2<-rnorm(20,5,1) y1<-rnorm(20) df<-data.frame(x1,x2,y1) Model_1<-lm(y1~x1+x2,data=df) summary(Model_1)
Output
Call: lm(formula = y1 ~ x1 + x2) Residuals: Min 1Q Median 3Q Max -2.0904 -0.2602 0.1675 0.3739 1.9845 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 0.20785 1.40412 0.148 0.8841 x1 -0.54075 0.24709 -2.188 0.0429 * x2 -0.04406 0.27069 - 0.163 0.8726 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.9463 on 17 degrees of freedom Multiple R-squared: 0.225, Adjusted R-squared: 0.1338 F-statistic: 2.467 on 2 and 17 DF, p-value: 0.1146
Testing for the difference between coefficient of x1 and x2
Using LinearHypothesis function of car package to test the difference between coefficient of x1 and x2 −
x1<-rnorm(20) x2<-rnorm(20,5,1) y1<-rnorm(20) df<-data.frame(x1,x2,y1) Model_1<-lm(y1~x1+x2,data=df) summary(Model_1) library(car) linearHypothesis(Model_1,"x1 = x2")
Output
Linear hypothesis test Hypothesis: x1 - x2 = 0 Model 1: restricted model Model 2: y1 ~ x1 + x2 Res.Df RSS Df Sum of Sq F Pr(>F) 1 18 16.719 2 17 15.222 1 1.4974 1.6723 0.2132
Advertisements