'Possible to add second predictor that positively correlates with y but kills the effect of another predictor?

I simulated data that includes two x-variables, both positively correlated with y. How should I simply edit 'x2' so that it positively correlates with 'y' but removes the association between 'x1' and 'y'?

library(tidyverse)
library(jtools)

set.seed(123)

n = 50
d = tibble(y = rnorm(n, 50, 10))

d = d %>% 
  arrange(y) %>% 
  mutate(x1 = y + rnorm(nrow(d), 0, 3),
         x2 = y + rnorm(nrow(d), 0, 10))

m1 = lm(y ~ x1, data = d)
effect_plot(m1, "x1", interval = T)

enter image description here

m2 = lm(y ~ x1 + x2, data = d)
effect_plot(m2, "x1", interval = T)

enter image description here



Solution 1:[1]

You could make x2 so that it correlates closely to x1:

library(tidyverse)
library(jtools)

set.seed(123)

n = 50
d = tibble(y = rnorm(n, 50, 10))

d = d %>% 
  arrange(y) %>% 
  mutate(x1 = y + rnorm(nrow(d), 0, 3),
         x2 = x1 + rnorm(nrow(d), 0, 0.5))

This way x1 alone has a positive effect:

m1 = lm(y ~ x1, data = d)
effect_plot(m1, "x1", interval = T)

and x2 alone has a positive effect

m2 = lm(y ~ x2, data = d)
effect_plot(m2, "x2", interval = T)

But the effect of x1 when combined with x2 is effectively "killed"

m3 = lm(y ~ x1 + x2, data = d)
effect_plot(m3, "x1", interval = T)

Created on 2022-03-14 by the reprex package (v2.0.1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Allan Cameron