# Introduction

The package can be used to estimate latent variable count regression models in one or multiple groups. In its simplest form, it can estimate ordinary Poisson or negative binomial regression models with manifest covariates in one group (similar to the glm()-function from the stats package or the glm.nb()-function from the MASS package). In its most complex form, it can regress a count variable on multiple manifest and latent covariates within multiple groups. Let’s see, how it works!

library(lavacreg)
#> This is lavacreg 0.1-2
#> lavacreg is BETA software! Please report any bugs.

## Simple Poisson Regression Model

As said before, the simplest case that can be estimated with lavacrag is an ordinary Poisson regression model, regressing a count outcome Y on a manifest covariate Z with \begin{align*} E(Y|Z) &= \mu_Y = \exp(\beta_0 + \beta_1 \cdot Z)\\ Y &\sim \mathcal{P}(\lambda = \mu_Y) \end{align*} In our example dataset, we can fit this model and compare it to the output of the glm()-function from the stats package:

# Usage of main function: countreg(y ~ z, data = d, family = "poisson")
m0 <- countreg(dv ~ z11, data = example01, family = "poisson")
#> Fitting the model...done. Took: 0.1 s
#> Computing standard errors...done. Took: 0.1 s
m1 <- glm(dv ~ z11, data = example01, family = poisson())

summary(m0)
#>
#>
#> --------------------- Group 1 ---------------------
#>
#> Regression:
#>       Estimate       SE   Est./SE   p-value
#> 1        2.759   0.0146       189         0
#> z11     -0.138   0.0081       -17         0
#>
#> Means:
#>       Estimate       SE   Est./SE   p-value
#> z11       1.58   0.0418      37.8         0
#>
#> Variances:
#>       Estimate       SE   Est./SE   p-value
#> z11       1.52   0.0729      20.9         0
summary(m1)
#>
#> Call:
#> glm(formula = dv ~ z11, family = poisson(), data = example01)
#>
#> Deviance Residuals:
#>     Min       1Q   Median       3Q      Max
#> -4.7673  -1.0555  -0.1332   0.9342   4.3367
#>
#> Coefficients:
#>              Estimate Std. Error z value Pr(>|z|)
#> (Intercept)  2.759062   0.014636  188.51   <2e-16 ***
#> z11         -0.137692   0.008095  -17.01   <2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> (Dispersion parameter for poisson family taken to be 1)
#>
#>     Null deviance: 2144.8  on 870  degrees of freedom
#> Residual deviance: 1844.0  on 869  degrees of freedom
#> AIC: 5588.4
#>
#> Number of Fisher Scoring iterations: 4

## Negative Binomial Regression with Latent Covariate

In the next step, we add a latent covariate to the model. That is, we use the option lv to specify a list of latent variables giving the names of the latent variables and a character vector of indicators measuring the latent variable. We can use the name of the latent variable within the forml option. In addition, we change family to be “nbinom” in oder to estimate a negative binomial regression, that is, adding a dispersion parameter to the model:

m2 <- countreg(dv ~ eta1,
lv = list(eta1 = c("z41", "z42", "z43")),
data = example01,
family = "nbinom"
)
#> Computing starting values...done. Took: 0.3 s
#> Fitting the model...done. Took: 2.5 s
#> Computing standard errors...done. Took: 1.5 s
summary(m2)
#>
#>
#> --------------------- Group 1 ---------------------
#>
#> Regression:
#>        Estimate       SE   Est./SE    p-value
#> 1        2.6866   0.0238    112.90   0.00e+00
#> eta1    -0.0836   0.0119     -7.04   1.99e-12
#>              Estimate      SE   Est./SE   p-value
#> Dispersion       9.77   0.874      11.2         0
#>
#> Means:
#>        Estimate       SE   Est./SE   p-value
#> eta1       1.62   0.0604      26.9         0
#>
#> Variances:
#>        Estimate      SE   Est./SE   p-value
#> eta1       1.94   0.165      11.8         0
#>
#> Measurement Model:
#>               Estimate       SE   Est./SE    p-value
#> z42 ~ 1         -0.119   0.1155     -1.03   0.302099
#> eta1 =~ z42      1.293   0.0572     22.61   0.000000
#> z43 ~ 1         -0.443   0.1221     -3.63   0.000282
#> eta1 =~ z43      1.350   0.0619     21.80   0.000000
#> z41 ~~ z41       1.452   0.0924     15.70   0.000000
#> z42 ~~ z42       1.456   0.1231     11.83   0.000000
#> z43 ~~ z43       1.276   0.1376      9.27   0.000000