# 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

## Multi-group Poisson Regression with Latent and Manifest Covariates

In this final model, we use a combination of manifest and latent covariates in the forml option, that is, one of the covariates is defined in the lv and the other is observed in the dataset. In addition, we specify a multi-group structural equation model using the group option.

m3 <- countreg(dv ~ eta1 + z11,
lv = list(eta1 = c("z41", "z42", "z43")),
group = "treat",
data = example01,
family = "poisson"
)
#> Computing starting values...done. Took: 1.2 s
#> Fitting the model...done. Took: 6.6 s
#> Computing standard errors...done. Took: 9 s
summary(m3)
#>
#>
#> --------------------- Group 1 ---------------------
#>
#> Regression:
#>        Estimate       SE   Est./SE    p-value
#> 1         2.782   0.0275    101.26   0.00e+00
#> z11      -0.127   0.0126    -10.05   0.00e+00
#> eta1     -0.101   0.0154     -6.56   5.24e-11
#>
#> Means:
#>        Estimate       SE   Est./SE   p-value
#> eta1       1.58   0.0798      19.8         0
#> z11        1.59   0.0625      25.5         0
#>
#> Variances:
#>        Estimate      SE   Est./SE   p-value
#> eta1       1.91   0.198      9.64         0
#> z11        1.68   0.114     14.68         0
#>
#> Covariances:
#>               Estimate       SE   Est./SE    p-value
#> eta1 ~~ z11      0.475   0.0993      4.79   1.67e-06
#>
#> Measurement Model:
#>               Estimate       SE   Est./SE    p-value
#> z42 ~ 1        -0.0597   0.1112    -0.537   0.591398
#> eta1 =~ z42     1.2631   0.0548    23.058   0.000000
#> z43 ~ 1        -0.3871   0.1169    -3.312   0.000928
#> eta1 =~ z43     1.3230   0.0590    22.434   0.000000
#> z41 ~~ z41      1.5117   0.1336    11.315   0.000000
#> z42 ~~ z42      1.4656   0.1590     9.220   0.000000
#> z43 ~~ z43      1.4894   0.1664     8.949   0.000000
#>
#>
#> --------------------- Group 2 ---------------------
#>
#> Regression:
#>        Estimate       SE   Est./SE    p-value
#> 1        2.8728   0.0232    123.96   0.000000
#> z11     -0.1053   0.0124     -8.52   0.000000
#> eta1    -0.0407   0.0117     -3.47   0.000523
#>
#> Means:
#>        Estimate       SE   Est./SE   p-value
#> eta1       1.64   0.0744      22.1         0
#> z11        1.55   0.0547      28.4         0
#>
#> Variances:
#>        Estimate       SE   Est./SE   p-value
#> eta1       2.18   0.2364      9.21         0
#> z11        1.38   0.0938     14.68         0
#>
#> Covariances:
#>               Estimate      SE   Est./SE    p-value
#> eta1 ~~ z11      0.641   0.101      6.33   2.43e-10
#>
#> Measurement Model:
#>               Estimate       SE   Est./SE    p-value
#> z42 ~ 1        -0.0597   0.1112    -0.537   5.91e-01
#> eta1 =~ z42     1.2631   0.0548    23.058   0.00e+00
#> z43 ~ 1        -0.3871   0.1169    -3.312   9.28e-04
#> eta1 =~ z43     1.3230   0.0590    22.434   0.00e+00
#> z41 ~~ z41      1.3420   0.1191    11.268   0.00e+00
#> z42 ~~ z42      1.5248   0.1506    10.124   0.00e+00
#> z43 ~~ z43      1.1282   0.1721     6.556   5.52e-11