Demonstration that positive (or negative) raw data correlations in general do not result in postive (or negative) linear regression coefficients

Generate data

y  <- c(1.25,2.14,3.02,3.53,4.22,1.02)
x1 <- c(1,2,3,4,5,2)
x2 <- c(1.73,2.66,3.58,4.59,5.86,4.14)
dat <- data.frame(y=y, x1=x1, x2=x2)

Correlation table

cor(dat)
##            y        x1        x2
## y  1.0000000 0.9398212 0.7246516
## x1 0.9398212 1.0000000 0.9151660
## x2 0.7246516 0.9151660 1.0000000

Linear regression

summary(fit <- lm(y ~ x1 + x2, data=dat))
## 
## Call:
## lm(formula = y ~ x1 + x2, data = dat)
## 
## Residuals:
##          1          2          3          4          5          6 
## -5.454e-02  3.735e-02  1.119e-01 -1.177e-01  2.295e-02 -3.564e-05 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.09163    0.13449   8.117 0.003909 ** 
## x1           1.47840    0.07695  19.212 0.000308 ***
## x2          -0.73149    0.07777  -9.406 0.002546 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1021 on 3 degrees of freedom
## Multiple R-squared:  0.9962, Adjusted R-squared:  0.9936 
## F-statistic: 390.3 on 2 and 3 DF,  p-value: 0.0002369

Variance inflation factors (should be < 10)

car::vif(fit)
##       x1       x2 
## 6.154935 6.154935

Get regression coefficients

cf.mod <- coef(fit)

Calculate z on a grid of x-y values

x1.seq <- seq(min(x1),max(x1),length.out=25)
x2.seq <- seq(min(x2),max(x2),length.out=25)
z <- t(outer(x1.seq, x2.seq, function(x,y) cf.mod[1]+cf.mod[2]*x+cf.mod[3]*y))

Draw the plane with “plot_ly” and add points with “add_trace”

https://stackoverflow.com/a/44324365

library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
cols <- c("#f5cb11", "#b31d83")

p <- plot_ly(x=~x1.seq, y=~x2.seq, z=~z,
             colors = c("#f5cb11", "#b31d83"),
             type="surface") %>%
    add_trace(data=dat, x=x1, y=x2, z=y, mode="markers", type="scatter3d",
              marker = list(color=cols, opacity=0.7, symbol=105)) %>%
    layout(scene = list(
               aspectmode = "manual", aspectratio = list(x=1, y=1, z=1),
               xaxis = list(title = "X1", range = c(0,6)),
               yaxis = list(title = "X2", range = c(0,6)),
               zaxis = list(title = "Y", range = pretty(z)[c(1,6)])))
p