The question as, you asked it, is really a programming question and would have been more suitable for StackOverflow. But as it is worth to clarify some of the underlying ideas I'm answering it here with a little bit more context.
rzi()
from mpath
expects you to supply regressor matrices without intercept but expects that the first element of the coefficient vector is the intercept. (The documentation explains this incorrectly, though.) The intercept is then always included in the model and a logit link is always used for the zero-inflation probabilities.
Your code however had no intercepts at all. And it already computed the full vector of Poisson means $\lambda_i$ and zero-inflation probabilities $\pi_i$. From there it is easy to generate a zero-inflated Poisson variable from first principles rather than a supplied function where you struggled with the correct usage. Hence, I'll explain the latter here using somewhat simplified R code.
First, we fix the dimensions: n
is the number of observations and p
the number of "real" regressors excluding the intercept:
n <- 50
p <- 20
Then we set up the true coefficients including a zero intercept in both parts:
beta <- c(0, 1.8, 0.5, -0.3, rep(0, p - 3))
gamma <- c(0, 0.4, -0.5, 0.9, rep(0, p - 3))
The real regressors are multivariate normal with standard normal marginals and an AR(1) correlation structure $S_{i, j} = 0.5^{|i - j|}$.
corr <- matrix(0, nrow = 20, ncol = 20)
corr <- 0.5^abs(col(corr) - row(corr))
x <- MASS::mvrnorm(n, rep(0, p), corr)
z <- MASS::mvrnorm(n, rep(0, p), corr)
x <- cbind(1, x)
z <- cbind(1, z)
To compute the expectation $\log(\lambda_i) = x_i^\top \beta$ of the Poisson component a log-link is used (inverse: exp), and for the probabilitiy $\mathrm{logit}(\pi_i) = z_i^\top \gamma$ for zero-inflation a logit link is used (inverse: cumulative distribution function of the logistic distribution).
lambda <- exp(x %*% beta)
pi <- plogis(z %*% gamma)
Then with probability $\pi_i$ we observe a zero and with probability $1 - \pi_i$ we observe something from a Poisson($\lambda_i$) distribution. This can be compactly written using rpois()
for the Poisson distribution multiplied with 0 vs. 1 from a Bernoulli distribution rbinom(size = 1)
.
y <- rpois(n, lambda = lambda) * rbinom(n, prob = 1 - pi, size = 1)