# install.packages("librarian", quiet = T)
# install.packages("ggdag")
librarian::shelf(
dagitty, tibble, stargazer, ggplot2, ggdag,
quiet = T
)
theme_set(theme_void())
g <- dagitty("dag {
X <- Z -> Y
X -> Y
}")
coordinates(g) <- list(
x=c(X=1,Z=1.5,Y=2),
y=c(X=1,Z=0,Y=1) )
ggdag(g)
# Generating random numbers
set.seed(1)
n <- 1000
Z <- rnorm(1000)
X <- 4*Z + rnorm(1000)
Y <- X + 3*Z + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z)
# Regressions
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
stargazer(lm_1, lm_2,
type = "text",
column.labels = c("NoControl", "UsingControl")
)
g <- dagitty("dag {
X <- Z <- U -> Y
X -> Y
}")
coordinates(g) <- list(
x=c(X=1,Z=2,U=3,Y=4),
y=c(X=2,Z=1,U=0,Y=2) )
ggdag(g)
set.seed(1)
n <- 1000
U <- rnorm(1000)
Z <- 2*U + rnorm(1000)
X <- 4*Z + rnorm(1000)
Y <- X + 3*U + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z,U=U)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
stargazer(lm_1, lm_2,
type = "text",
column.labels = c("NoControl", "UsingControl")
)
g <- dagitty("dag {
X <- U -> Z -> Y
X -> Y
}")
coordinates(g) <- list(
x=c(X=1,Z=3,U=2,Y=4),
y=c(X=2,Z=1,U=0,Y=2) )
ggdag(g)
set.seed(1)
n <- 1000
U <- rnorm(1000)
Z <- 2*U + rnorm(1000)
X <- 4*U + rnorm(1000)
Y <- X + 3*Z + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z,U=U)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
stargazer(lm_1, lm_2,
type = "text",
column.labels = c("NoControl", "UsingControl")
)
g <- dagitty("dag {
X <- Z -> M -> Y
X -> M
}")
coordinates(g) <- list(
x=c(X=1,Z=1.5,M=2,Y=3),
y=c(X=1,Z=0,M=1,Y=1) )
ggdag(g)
set.seed(1)
n <- 1000
Z <- rnorm(1000)
X <- 2*Z + rnorm(1000)
M <- X + 8*Z + rnorm(1000)
Y <- M + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z,M=M)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
lm_3 <- lm(Y ~ X + Z + M, d) #Threats of confounding
stargazer(lm_1, lm_2, lm_3,
type = "text",
column.labels = c("NoControl", "UsingControl", "ThreatsConfounding")
)
g <- dagitty("dag {
Z <- U -> M -> Y
Z -> X -> M
}")
coordinates(g) <- list(
x=c(X=1,Z=1.25,U=1.5,M=2,Y=3),
y=c(X=1,Z=0.5,U=0,M=1,Y=1) )
ggdag(g)
set.seed(1)
n <- 1000
U <- rnorm(1000)
Z <- 3*U + rnorm(1000)
X <- 2*Z + rnorm(1000)
M <- X + 5*U + rnorm(1000)
Y <- M + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z,M=M, U=U)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
lm_3 <- lm(Y ~ X + Z + M, d) #Threats of confounding
stargazer(lm_1, lm_2, lm_3,
type = "text",
column.labels = c("NoControl", "UsingControl", "ThreatsConfounding")
)
g <- dagitty("dag {
X <- U -> Z -> M -> Y
X -> M
}")
coordinates(g) <- list(
x=c(X=1,Z=1.5,U=1.25,M=2,Y=3),
y=c(X=1,Z=0.5,U=0,M=1,Y=1) )
ggdag(g)
set.seed(1)
n <- 1000
U <- rnorm(1000)
Z <- 4*U + rnorm(1000)
X <- 2*U + rnorm(1000)
M <- X + Z + rnorm(1000)
Y <- M + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z,M=M, U=U)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
lm_3 <- lm(Y ~ X + Z + M, d) #Threats of confounding
stargazer(lm_1, lm_2, lm_3,
type = "text",
column.labels = c("NoControl", "UsingControl", "ThreatsConfounding")
)
g <- dagitty("dag {
X <- U1 -> Z <- U2 -> Y
X -> Y
}")
coordinates(g) <- list(
x=c(X=1,U1=1,Z=2,U2=3,Y=3),
y=c(X=1,U1=0,Z=0,U2=0,Y=1) )
ggdag(g)
set.seed(1)
n <- 1000
U1 <- rnorm(1000)
U2 <- rnorm(1000)
Z <- 0.5*U1 + 0.3*U2 + rnorm(1000)
X <- 2*U1 + rnorm(1000)
Y <- X + 7*U2 + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z,U1=U1, U2=U2)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
stargazer(lm_1, lm_2,
type = "text",
column.labels = c("NoControl", "UsingControl")
)
g <- dagitty("dag {
X <- U1 -> Z <- U2 -> Y
X -> Y
Z -> Y
}")
coordinates(g) <- list(
x=c(X=1,U1=1,Z=2,U2=3,Y=3),
y=c(X=1,U1=0,Z=0,U2=0,Y=1) )
ggdag(g)
set.seed(1)
n <- 1000
U1 <- rnorm(1000)
U2 <- rnorm(1000)
Z <- 0.5*U1 + 0.3*U2 + rnorm(1000)
X <- 2*U1 + rnorm(1000)
Y <- X + Z + 7*U2 + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z,U1=U1, U2=U2)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
stargazer(lm_1, lm_2,
type = "text",
column.labels = c("NoControl", "UsingControl")
)
g <- dagitty("dag {
X -> Y <-Z
}")
coordinates(g) <- list(
x=c(X=1,Y=2,Z=3),
y=c(X=1,Y=1,Z=0) )
ggdag(g)
set.seed(1)
n <- 1000
Z <- rnorm(1000)
X <- rnorm(1000)
Y <- X + 7*Z + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
stargazer(lm_1, lm_2,
type = "text",
column.labels = c("NoControl", "UsingControl")
)
g <- dagitty("dag {
Z -> X -> Y
}")
coordinates(g) <- list(
x=c(X=2,Y=3,Z=1),
y=c(X=1,Y=1,Z=0) )
ggdag(g)
set.seed(1)
n <- 1000
Z <- rnorm(1000)
X <- 4.5*Z + rnorm(1000)
Y <- X + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
stargazer(lm_1, lm_2,
type = "text",
column.labels = c("NoControl", "UsingControl")
)
g <- dagitty("dag {
Z -> X <- U -> Y
X -> Y
}")
coordinates(g) <- list(
x=c(Z=0.5,X=1,U=1.5,Y=2),
y=c(Z=0,X=1,U=0.5,Y=1) )
ggdag(g)
set.seed(1)
n <- 1000
Z <- rnorm(1000)
U <- rnorm(1000)
X <- 4.5*Z + 6*U + rnorm(1000)
Y <- X + 3*U + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z, U=U)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
stargazer(lm_1, lm_2,
type = "text",
column.labels = c("NoControl", "UsingControl")
)
g <- dagitty("dag {
X -> Z -> Y
}")
coordinates(g) <- list(
x=c(X=1,Z=2,Y=3),
y=c(X=0,Z=0,Y=0) )
ggdag(g)
set.seed(1)
n <- 1000
X <- rnorm(1000)
Z <- X + rnorm(1000)
Y <- Z + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
stargazer(lm_1, lm_2,
type = "text",
column.labels = c("NoControl", "UsingControl")
)
g <- dagitty("dag {
X -> M -> Y
M -> Z
}")
coordinates(g) <- list(
x=c(X=1,Z=2,Y=3, M=2),
y=c(X=0,Z=0.5,Y=0, M=0) )
ggdag(g)
set.seed(1)
n <- 1000
X <- rnorm(1000)
M <- X + rnorm(1000)
Z <- 5*M + rnorm(1000)
Y <- M + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z, M=M)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
stargazer(lm_1, lm_2,
type = "text",
column.labels = c("NoControl", "UsingControl")
)
g <- dagitty("dag {
X -> Z <- U -> Y
Z -> Y
}")
coordinates(g) <- list(
x=c(X=1,Z=2,U=2.5,Y=3),
y=c(X=1,Z=1,U=0,Y=1) )
ggdag(g)
set.seed(1)
n <- 1000
X <- rnorm(1000)
U <- rnorm(1000)
Z <- X + 4*U + rnorm(1000)
Y <- Z + 3*U + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z,U=U)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
stargazer(lm_1, lm_2,
type = "text",
column.labels = c("NoControl", "UsingControl")
)
g <- dagitty("dag {
X -> M -> Y
Z -> M
}")
coordinates(g) <- list(
x=c(X=1,M=2,Y=3,Z=2),
y=c(X=1,M=1,Y=1,Z=0) )
ggdag(g)
set.seed(1)
n <- 1000
Z <- rnorm(1000)
X <- rnorm(1000)
M <- X + 4*Z + rnorm(1000)
Y <- M + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z,M=M)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
stargazer(lm_1, lm_2,
type = "text",
column.labels = c("NoControl", "UsingControl")
)
g <- dagitty("dag {
Z <- X -> Y
}")
coordinates(g) <- list(
x=c(X=1,Z=2,Y=3),
y=c(X=0,Z=1,Y=0) )
ggdag(g)
set.seed(1)
n <- 1000
X <- rnorm(1000)
Z <- 2*X + rnorm(1000)
Y <- X + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
stargazer(lm_1, lm_2,
type = "text",
column.labels = c("NoControl", "UsingControl")
)
g <- dagitty("dag {
X -> Z -> W <- U -> Y
X -> Y
}")
coordinates(g) <- list(
x=c(X=1,Z=1.5,W=2,U=2,Y=3),
y=c(X=1,Z=1.5,W=2,U=0,Y=1) )
ggdag(g)
set.seed(1)
n <- 1000
U <- rnorm(1000)
X <- rnorm(1000)
Z <- 2*X + rnorm(1000)
W <- 3*Z + 5*U + rnorm(1000)
Y <- X + 4*U + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z, W=W, U=U)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
stargazer(lm_1, lm_2,
type = "text",
column.labels = c("NoControl", "UsingControl")
)
g <- dagitty("dag {
X -> Z <- U -> Y
X -> Y
}")
coordinates(g) <- list(
x=c(X=1,U=2,Z=2,Y=3),
y=c(X=1,U=0,Z=2,Y=1) )
ggdag(g)
set.seed(1)
n <- 1000
U <- rnorm(1000)
X <- rnorm(1000)
Z <- 2*X + 2.5*U + rnorm(1000)
Y <- X + 4*U + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z, U=U)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
stargazer(lm_1, lm_2,
type = "text",
column.labels = c("NoControl", "UsingControl")
)
g <- dagitty("dag {
Z <- X -> Y -> Z
X -> Z
}")
coordinates(g) <- list(
x=c(X=1,Z=2,Y=3),
y=c(X=0,Y=0,Z=1) )
ggdag(g)
set.seed(1)
n <- 1000
X <- rnorm(1000)
Y <- X + rnorm(1000)
Z <- 2*X + 2.5*Y + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
stargazer(lm_1, lm_2,
type = "text",
column.labels = c("NoControl", "UsingControl")
)
g <- dagitty("dag {
X -> Y -> Z
}")
coordinates(g) <- list(
x=c(X=1,Z=2,Y=3),
y=c(X=0,Y=0,Z=1) )
ggdag(g)
set.seed(1)
n <- 1000
X <- rnorm(1000)
Y <- X + rnorm(1000)
Z <- 2.5*Y + rnorm(1000)
# Create a dataframe
d <- tibble(X=X,Y=Y,Z=Z)
lm_1 <- lm(Y ~ X, d)
lm_2 <- lm(Y ~ X + Z, d)
stargazer(lm_1, lm_2,
type = "text",
column.labels = c("NoControl", "UsingControl")
)