Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to create a regression model in R with interaction between all combinations of two variables?
The easiest way to create a regression model with interactions is inputting the variables with multiplication sign that is * but this will create many other combinations that are of higher order. If we want to create the interaction of two variables combinations then power operator can be used as shown in the below examples.
Example1
x1<−rnorm(10) x2<−rnorm(10,1,0.2) x3<−rnorm(10,1,0.04) y<−rnorm(10,5,1) M1<−lm(y~(x1+x2+x3)^2) summary(M1) Call: lm(formula = y ~ (x1 + x2 + x3)^2) Residuals: 1 2 3 4 5 6 7 8 0.47052 −0.39362 0.37762 −0.80668 0.41637 −0.04845 0.00832 0.27097 9 10 0.14218 −0.43722 Coefficients: Estimate Std. Error t value Pr(>|t|)
Output
(Intercept) 0.2893 172.6567 0.002 0.999 x1 28.5300 25.1856 1.133 0.340 x2 7.9753 191.0616 0.042 0.969 x3 3.3123 168.1906 0.020 0.986 x1:x2 1.2607 16.6937 0.076 0.945 x1:x3 −28.3810 19.4585 −1.459 0.241 x2:x3 −6.2240 186.3458 −0.033 0.975
Residual standard error: 0.7372 on 3 degrees of freedom Multiple R−squared: 0.7996, Adjusted R−squared: 0.3989 F−statistic: 1.995 on 6 and 3 DF, p−value: 0.3048
Example2
z1<−runif(100,1,2) z2<−runif(100,1,4) z3<−runif(100,1,5) z4<−runif(100,2,5) z5<−runif(100,2,10) y<−runif(100,1,10) M3<−lm(y~(z1+z2+z3+z4+z5)^2) summary(M3) Call: lm(formula = y ~ (z1 + z2 + z3 + z4 + z5)^2) Residuals: Min 1Q Median 3Q Max −5.4732 −2.0570 0.0582 2.1667 5.3376
Output
Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) −2.03476 14.52311 −0.140 0.8889 z1 3.14344 6.80702 0.462 0.6454 z2 3.85518 3.05398 1.262 0.2103 z3 −1.88782 2.16124 −0.873 0.3849 z4 2.75794 3.11048 0.887 0.3778 z5 −0.70359 1.05400 −0.668 0.5063 z1:z2 −2.09623 1.24757 −1.680 0.0966 . z1:z3 0.17328 0.97128 0.178 0.8588 z1:z4 0.53514 1.26533 0.423 0.6734 z1:z5 0.02687 0.43087 0.062 0.9504 z2:z3 0.15894 0.34335 0.463 0.6446 z2:z4 −0.72427 0.43987 −1.647 0.1034 z2:z5 0.22560 0.16570 1.362 0.1770 z3:z4 −0.16602 0.33847 −0.491 0.6251 z3:z5 0.30484 0.12536 2.432 0.0171 * z4:z5 −0.19887 0.17768 −1.119 0.2662 −−− Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.792 on 84 degrees of freedom
Multiple R−squared: 0.1587, Adjusted R−squared: 0.008411
F−statistic: 1.056 on 15 and 84 DF, p−value: 0.4091
Advertisements
