Search code examples
rtidyversesimulationgenerate

Adding a new variable with a code/formula that generates a moderate association with a previous one


I try adding a new variable that has an association with the previous one. Is there a math/code trick/formula to increase the width of confidence bands in this association?

library(tidyverse)

d = tibble(a = rnorm(50, 100, 20))

#adding a new variable that correlates with the previous
d = d %>% mutate(b = a*10) #<- this is the formula

#plotting association
d %>% ggplot(aes(a, b))+
  geom_smooth(method = "lm")

enter image description here


Solution

  • Something like this ?

    d %>% 
    mutate(b = a*10 + rnorm(50, 0, 100)) %>%
    ggplot(aes(a, b)) + geom_smooth(method = "lm")       
    
    

    enter image description here