Calculate effects using delta method in R -
i have written code calculate effects using delta method in r
i have dataframe dpcp
variables x1
,x2
,x3
,x4
, matrix of 1000 draws multivariate normal, m4[1000,4]
.
this code calculates effects, takes long run.
how can run faster:
n = nrow(dpcp) (i in 1: n) { (j in 1: 1000) { marg_effects[i, j] = (m4[j, 1] * dpcp[i, ] $x1) + (m4[j, 2] * dpcp[i, ] $x2)+ (m4[j, 3] * dpcp[i, ] $x3) + (m4[j, 4] * dpcp[i, ] $x4) } }
the code takes upwards of 5 hours 2000 observations.
you may want check this out first. however, speed computation, use matrix multiplication instead of looping
marg_effects <- as.matrix(dpcp) %*% t(m4)
the %*%
operator matrix multiplication , t()
function transpose m4
hope helps.
Comments
Post a Comment