如何在R語言中檢驗兩個迴歸係數的差異?
為了檢驗兩個迴歸係數之間的差異,我們可以按照以下步驟進行:
- 首先,建立資料框。
- 然後建立一個迴歸模型。
- 之後,使用car包中的LinearHypothesis函式檢驗迴歸係數之間的差異。
建立資料框
讓我們建立一個如下所示的資料框:
x1<-rnorm(20) x2<-rnorm(20,5,1) y1<-rnorm(20) df<-data.frame(x1,x2,y1) df
執行上述指令碼後,將生成以下輸出(由於隨機化,此輸出可能因您的系統而異):
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
建立迴歸模型
使用lm函式建立迴歸模型,其中x1和x2作為自變數,y1作為因變數:
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)
輸出
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
檢驗x1和x2係數之間的差異
使用car包中的LinearHypothesis函式檢驗x1和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")
輸出
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
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP