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-0
#> 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.2 s
#> Computing standard errors...done. Took: 0.1 s
m1 <- glm(dv ~ z11, data = example01, family = poisson())
                
summary(m0)
#>   lhs op rhs    dest type group par_free        par          SE
#> 1      %   w  groupw <NA>     1        1  6.7691671 0.033891754
#> 2  dv  ~   1 regcoef <NA>     1        2  2.7590148 0.014636007
#> 3  dv  ~ z11 regcoef <NA>     1        3 -0.1376593 0.008095389
#> 4 z11  ~   1       z mean     1        4  1.5792611 0.041792618
#> 5 z11 ~~ z11       z  var     1        5  1.5213083 0.072912680
#> 6  dv ~~  dv  sigmaw size     1        0  0.0000000          NA
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: 4 s
#> Computing standard errors...done. Took: 2.5 s
summary(m2)
#>     lhs op  rhs    dest   type group par_free         par         SE
#> 1        %    w  groupw   <NA>     1        1  6.76964197 0.03388371
#> 2    dv  ~    1 regcoef   <NA>     1        2  2.68663313 0.02379601
#> 3    dv  ~ eta1 regcoef   <NA>     1        3 -0.08359125 0.01188202
#> 4   z41  ~    1      mm     nu     1        0  0.00000000         NA
#> 5  eta1 =~  z41      mm lambda     1        0  1.00000000         NA
#> 6   z42  ~    1      mm     nu     1        4 -0.11917309 0.11548397
#> 7  eta1 =~  z42      mm lambda     1        5  1.29327899 0.05719989
#> 8   z43  ~    1      mm     nu     1        6 -0.44339473 0.12211127
#> 9  eta1 =~  z43      mm lambda     1        7  1.34991108 0.06193403
#> 10 eta1  ~    1 lv_grid   mean     1        8  1.62188325 0.06037340
#> 11 eta1 ~~ eta1 lv_grid    var     1        9  1.93859883 0.16490032
#> 12   dv ~~   dv  sigmaw   size     1       10  9.77343464 0.87386838
#> 13  z41 ~~  z41  sigmaw   veps     1       11  1.45156140 0.09244556
#> 14  z42 ~~  z42  sigmaw   veps     1       12  1.45590733 0.12307052
#> 15  z43 ~~  z43  sigmaw   veps     1       13  1.27641140 0.13764269

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.3 s
#> Fitting the model...done. Took: 8.5 s
#> Computing standard errors...done. Took: 11.3 s
summary(m3)
#>      lhs op  rhs    dest     type group par_free         par         SE
#> 1  treat  %    w  groupw     <NA>     1        1  6.05912528 0.04833676
#> 2     dv  ~    1 regcoef     <NA>     1        2  2.78210242 0.02747382
#> 3     dv  ~  z11 regcoef     <NA>     1        3 -0.12670638 0.01261108
#> 4     dv  ~ eta1 regcoef     <NA>     1        4 -0.10132733 0.01543693
#> 5    z41  ~    1      mm       nu     1        0  0.00000000         NA
#> 6   eta1 =~  z41      mm   lambda     1        0  1.00000000         NA
#> 7    z42  ~    1      mm       nu     1        5 -0.05967178 0.11116074
#> 8   eta1 =~  z42      mm   lambda     1        6  1.26310127 0.05478004
#> 9    z43  ~    1      mm       nu     1        7 -0.38713853 0.11690551
#> 10  eta1 =~  z43      mm   lambda     1        8  1.32299844 0.05897220
#> 11  eta1  ~    1 lv_grid     mean     1        9  1.58142694 0.07981689
#> 12  eta1 ~~ eta1 lv_grid      var     1       10  1.91143485 0.19823917
#> 13   z11  ~    1       z     mean     1       11  1.59253246 0.06253208
#> 14   z11 ~~  z11       z      var     1       12  1.67760248 0.11425722
#> 15  eta1 ~~  z11       z cov_z_lv     1       13  0.47538478 0.09925245
#> 16    dv ~~   dv  sigmaw     size     1        0  0.00000000         NA
#> 17   z41 ~~  z41  sigmaw     veps     1       14  1.51170895 0.13360365
#> 18   z42 ~~  z42  sigmaw     veps     1       15  1.46559039 0.15896507
#> 19   z43 ~~  z43  sigmaw     veps     1       16  1.48943922 0.16644346
#> 20 treat  %    w  groupw     <NA>     2       17  6.09358579 0.04751105
#> 21    dv  ~    1 regcoef     <NA>     2       18  2.87281686 0.02317606
#> 22    dv  ~  z11 regcoef     <NA>     2       19 -0.10529937 0.01236109
#> 23    dv  ~ eta1 regcoef     <NA>     2       20 -0.04073700 0.01174385
#> 24   z41  ~    1      mm       nu     2        0  0.00000000         NA
#> 25  eta1 =~  z41      mm   lambda     2        0  1.00000000         NA
#> 26   z42  ~    1      mm       nu     2       21 -0.05967178 0.11116074
#> 27  eta1 =~  z42      mm   lambda     2       22  1.26310127 0.05478004
#> 28   z43  ~    1      mm       nu     2       23 -0.38713853 0.11690551
#> 29  eta1 =~  z43      mm   lambda     2       24  1.32299844 0.05897220
#> 30  eta1  ~    1 lv_grid     mean     2       25  1.64428240 0.07442472
#> 31  eta1 ~~ eta1 lv_grid      var     2       26  2.17708130 0.23636010
#> 32   z11  ~    1       z     mean     2       27  1.55289387 0.05473276
#> 33   z11 ~~  z11       z      var     2       28  1.37709205 0.09381845
#> 34  eta1 ~~  z11       z cov_z_lv     2       29  0.64066362 0.10118807
#> 35    dv ~~   dv  sigmaw     size     2        0  0.00000000         NA
#> 36   z41 ~~  z41  sigmaw     veps     2       30  1.34201922 0.11910312
#> 37   z42 ~~  z42  sigmaw     veps     2       31  1.52475385 0.15060507
#> 38   z43 ~~  z43  sigmaw     veps     2       32  1.12820338 0.17208252