Skip to content

Instantly share code, notes, and snippets.

@duarteguilherme
Last active June 17, 2018 12:40
Show Gist options
  • Save duarteguilherme/7a1ba4b9081d3370e470958564792e8c to your computer and use it in GitHub Desktop.
Save duarteguilherme/7a1ba4b9081d3370e470958564792e8c to your computer and use it in GitHub Desktop.
library(tidyverse)
# Colliders
x1 <- rnorm(1000)
y <- 1.2 + 1.6 * x1 + rnorm(1000)
z <- 0.6 + 1.3*x1 + 1.5*y + rnorm(1000)
df1 <- tibble(x1 = x1, y = y, z = z)
new_x1 <- rnorm(1000)
new_y <- 1.2 + 1.6 * new_x1 + rnorm(1000)
new_z <- rnorm(1000)
df2 <- tibble(x1 = new_x1 , y = new_y, z = new_z)
modelo1 <- lm(y ~ z + x1)
modelo2 <- lm(y ~ x1)
# Prediction
modelo_collider_dados_reais <- qplot(predict(modelo1, newdata = df1), y) + theme_bw() + ggtitle("Collider com Dados Reais")
modelo_sem_collider_dados_reais <- qplot(predict(modelo2, newdata = df1), y) + theme_bw() + ggtitle("Sem Collider com Dados Reais")
# Intervention
modelo_collider_dados_inventados <- qplot(predict(modelo1, newdata = df2), new_y) + theme_bw() + ggtitle("Collider com Dados Inventados")
modelo_sem_collider_dados_inventados <- qplot(predict(modelo2, newdata = df2), new_y) + theme_bw() + ggtitle("Sem Collider com Dados Inventados")
library(gridExtra)
grid.arrange(modelo_collider_dados_inventados, modelo_sem_collider_dados_inventados, modelo_collider_dados_reais, modelo_sem_collider_dados_reais)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment