EQ3 <- sapply(1:(2*N), function(n){
# Market Mechanisms
demand <- qd_fun(P)
supply <- qs_fun(P)
# Compute EQ (what we observe)
eq_id <- which.min( abs(demand-supply) )
eq <- c(P=P[eq_id], Q=demand[eq_id])
# Return Equilibrium Observations
return(eq)
})
xy3_sim <- data.frame(t(EQ3), cost='1', T=1:ncol(EQ3))
xy3_pre_sim <- xy3_sim[xy3_sim$T <= N , ]
xy3_post_sim <- xy3_sim[xy3_sim$T > N , ]
# Plot Price Data
par(mfrow=c(1, 2))
plot(P ~ T, xy2_sim, main=NA, pch=16, col=cols, cex=.5)
title('Effect of Cost Shock on Price', font.main=1)
lines(x1_sim, predict(regP1), col=rgb(0, 0, 0, .8), lwd=2)
lines(x2_sim, predict(regP2), col=rgb(0, 0, 1, .8), lwd=2)
# W/ Control group
points(P ~ T, xy3_sim, pch=16, col=rgb(1, 0, 0, .5), cex=.5)
regP3a <- loess(P ~ T, xy3_pre_sim)
x3a_sim <- regP3a$x
lines(x3a_sim, predict(regP3a), col=rgb(1, 0, 0, .8), lwd=2)
regP3b <- loess(P ~ T, xy3_post_sim)
x3b_sim <- regP3b$x
lines(x3b_sim, predict(regP3b), col=rgb(1, 0, 0, .8), lwd=2)
# Plot Quantity Data
plot(Q ~ T, xy2_sim, main=NA, pch=17, col=cols, cex=.5)
title('Effect of Cost Shock on Quantity', font.main=1)
lines(x1_sim, predict(regQ1), col=rgb(0, 0, 0, .8), lwd=2)
lines(x2_sim, predict(regQ2), col=rgb(0, 0, 1, .8), lwd=2)
# W/ Control group
points(Q ~ T, xy3_sim, pch=16, col=rgb(1, 0, 0, .5), cex=.5)
regQ3a <- loess(Q ~ T, xy3_pre_sim)
lines(x3a_sim, predict(regQ3a), col=rgb(1, 0, 0, .8), lwd=2)
regQ3b <- loess(Q ~ T, xy3_post_sim)
lines(x3b_sim, predict(regQ3b), col=rgb(1, 0, 0, .8), lwd=2)