#' Generate simulated data using chen_lee
#'
#' @param n the number of observations
#' @param pD the number of endongenous variables
#' @return A list containing dependent variable Y, endogenous variable D, instrumental variable Z
#' @details This function produces as many instruments as endogenous variables.
#' @examples chen_lee(150,3)
chen_lee = function(n, pD){
B_D = rep( 1, pD)
Z = matrix(rnorm(n*pD), ncol = pD)
firstcol = c(0.4, 0.6, -0.2, seq(-1,1, len=pD-3))
firstcol = sqrt(0.4^2+0.6^2+0.2^2)*firstcol/(norm(firstcol, "2"))
V = diag(pD+1)
V[1,2:(pD+1)] = firstcol
V[2:(pD+1),1] = firstcol
errors = MASS::mvrnorm(n, mu = rep(0, pD+1), Sigma = 0.25*V)
eps = errors[,1]
nu = errors[,-1]# so the Pd nu's correlates with eps respectively while independent with each other
scale_D = c(1, 2, 1.5, seq(1,2, len = pD-3))
D = t(scale_D*t(pnorm(Z + nu)))
B_errors = c(1, 0.25, 0.15, seq(0.1, 1, len=pD - 3))
Y = 1 + D %*% B_D + (0.5 + D %*% B_errors)*eps
return(list("Y"= Y, "D"= D, "Z"=Z))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.