- 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 change the order of independent variables for regression summary output in R?
To change the order of independent variables in regression Output, we can pass the variables in the sequence we want while creating the regression model.
For example, if we want to have three independent variables and we want to display first at the last position then it can be done as follows −
lm(DP1~ ind_var_3+ ind_var_2+ind_var_1,data=”data_frame_name”)
Example
Following snippet creates a sample dataframe −
iv1<-rnorm(20) iv2<-rnorm(20) iv3<-rnorm(20) DP1<-rnorm(20,1,0.05) df1<-data.frame(iv1,iv2,iv3,DP1) df1
The following dataframe is created −
output
iv1 iv2 iv3 DP1 1 0.27622283 0.3993088 0.009604179 0.9870641 2 -1.61822694 -0.8481482 0.455201989 1.0419490 3 -0.16453686 -1.4879353 -0.350820394 0.9798238 4 -1.05644448 -0.6567911 1.345854317 0.9589660 5 0.16128004 -1.5530191 1.248949489 1.0337228 6 0.26490779 0.1905057 0.664826658 0.9612587 7 0.75145959 -0.2902165 0.005533312 1.0167088 8 -0.11785438 0.6260407 1.116348214 1.0087205 9 0.25632653 -0.4080989 -0.314622661 0.9548039 10 -0.70829294 -1.4721428 0.303353402 0.9456278 11 0.96142734 -0.8047216 -1.423814934 1.0133855 12 0.47065716 -0.0145821 -0.871918075 1.0242987 13 -2.23836059 1.7323083 -1.417109201 0.9578229 14 0.76295739 -0.3704564 0.839145422 1.0706470 15 0.40626379 1.9601237 1.457727929 1.0253645 16 -0.75012537 -0.6982455 -1.512548488 0.9916308 17 -0.27124742 -0.9710179 0.284963380 0.9459357 18 -0.26442340 0.6065156 -0.498311289 1.0158016 19 -0.37278740 -0.2710638 0.643670976 0.9794339 20 -0.05907976 0.9741651 0.273533270 1.0329243
Now, to create a regression model for data in df1, add the following code to the above snippet −
Example
Model1<-lm(DP1~iv1+iv2+iv3,data=df1) summary(Model1)
Output
If you execute all the above given snippets as a single program, it generates the following Output −
Call: lm(formula = DP1 ~ iv1 + iv2 + iv3, data = df1) Residuals: Min 1Q Median 3Q Max -0.047785 -0.021889 0.000682 0.018709 0.071298 Coefficients: Estimate Std.rror t value Pr(>|t|) (Intercept) 1.000534 0.008392 119.230 <2e-16 *** iv1 0.016620 0.010299 1.614 0.126 iv2 0.005927 0.008287 0.715 0.485 iv3 0.004480 0.008982 0.499 0.625 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.0357 on 16 degrees of freedom Multiple R-squared: 0.1778, Adjusted R-squared: 0.02369 F-statistic: 1.154 on 3 and 16 DF, p-value: 0.3579
To create a regression model for data in df1 with different order of independent variables, add the following code to the above snippet −
Example
Model1<-lm(DP1~iv2+iv1+iv3,data=df1) summary(Model1)
Output
If you execute all the above given snippets as a single program, it generates the following Output −
Call: lm(formula = DP1 ~ iv2 + iv1 + iv3, data = df1) Residuals: Min 1Q Median 3Q Max -0.047785 -0.021889 0.000682 0.018709 0.071298 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 1.000534 0.008392 119.230 <2e-16 *** iv2 0.005927 0.008287 0.715 0.485 iv1 0.016620 0.010299 1.614 0.126 iv3 0.004480 0.008982 0.499 0.625 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.0357 on 16 degrees of freedom Multiple R-squared: 0.1778, Adjusted R-squared: 0.02369 F-statistic: 1.154 on 3 and 16 DF, p-value: 0.3579
Example
Following snippet creates a sample data frame −
x1<-rpois(20,4) x2<-rpois(20,2) x3<-rpois(20,2) x4<-rpois(20,5) y<-rpois(20,10) df2<-data.frame(x1,x2,x3,x4,y) df2
The following dataframe is created −
output
x1 x2 x3 x4 y 1 3 1 4 7 15 2 6 2 5 1 8 3 7 2 1 5 15 4 6 0 4 6 14 5 4 3 2 2 8 6 3 0 3 6 9 7 1 2 1 9 13 8 7 1 3 5 14 9 3 1 0 6 9 10 5 4 3 8 11 11 6 3 1 7 8 12 2 0 3 1 11 13 2 2 1 5 6 14 5 0 1 4 10 15 4 2 0 4 5 16 5 0 3 5 14 17 2 4 2 7 10 18 5 4 3 4 6 19 3 1 3 1 5 20 3 4 1 4 12
To create a regression model for data in df2 with different order of independent variables, add the following code to the above snippet −
Example
Model2<-lm(y~x3+x2+x4+x1,data=df2) summary(Model2)
Output
If you execute all the above given snippets as a single program, it generates the following Output −
Call: lm(formula = y ~ x3 + x2 + x4 + x1, data = df2) Residuals: Min 1Q Median 3Q Max -3.3049 -2.6574 -0.2113 1.6365 5.1192 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 4.7478 2.6181 1.813 0.0898 . x3 0.5544 0.5061 1.095 0.2906 x2 -0.6848 0.4622 -1.482 0.1591 x4 0.7880 0.2979 2.645 0.0184 * x1 0.3886 0.3839 1.012 0.3274 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 2.873 on 15 degrees of freedom Multiple R-squared: 0.4061, Adjusted R-squared: 0.2478 F-statistic: 2.565 on 4 and 15 DF, p-value: 0.08123
To Continue Learning Please Login
Login with Google