galamm

An R package for estimating generalized additive mixed models with latent variables

https://github.com/lcbc-uio/galamm

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (19.0%) to scientific vocabulary

Keywords

generalized-additive-models hierarchical-models item-response-theory latent-variable-models structural-equation-models
Last synced: 6 months ago · JSON representation

Repository

An R package for estimating generalized additive mixed models with latent variables

Basic Info
Statistics
  • Stars: 30
  • Watchers: 2
  • Forks: 2
  • Open Issues: 1
  • Releases: 5
Topics
generalized-additive-models hierarchical-models item-response-theory latent-variable-models structural-equation-models
Created almost 5 years ago · Last pushed 8 months ago
Metadata Files
Readme Changelog Contributing License Code of conduct Codemeta

README.Rmd

---
output: 
  github_document:
    fig_width: 6
    fig_height: 4
bibliography: inst/REFERENCES.bib
link-citations: yes
---



```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)
```

# Generalized Additive Latent and Mixed Models galamm website


[![CRAN status](https://www.r-pkg.org/badges/version/galamm)](https://cran.r-project.org/package=galamm)
[![Status at rOpenSci Software Peer Review](https://badges.ropensci.org/615_status.svg)](https://github.com/ropensci/software-review/issues/615)
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![R-CMD-check](https://github.com/LCBC-UiO/galamm/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/LCBC-UiO/galamm/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/LCBC-UiO/galamm/branch/main/graph/badge.svg)](https://app.codecov.io/gh/LCBC-UiO/galamm?branch=main)


```{r srr-tags1, eval = FALSE, echo = FALSE}
#' @srrstats {G1.0} Primary and secondary references to literature in the paragraph below.
#' @srrstats {G1.1} It is stated in the paragraph below that this is the first implementation of the algorithm developed in Sørensen, Fjell, and Walhovd (2023).
#' @srrstats {G1.3} Statistical terminology defined in detail in the references in the paragraph below. The main paper, Sørensen, FJell, and Walhovd (2023) is open access.
```


galamm estimates generalized additive latent and mixed models (GALAMMs). This is the first package implementing the model framework and the computational algorithms introduced in @sorensenLongitudinalModelingAgeDependent2023. It is an extension of the GLLAMM framework for multilevel latent variable modeling detailed in @rabe-heskethGeneralizedMultilevelStructural2004 and @skrondalGeneralizedLatentVariable2004, in particular by efficiently handling crossed random effects and semiparametric estimation.

## What Can the Package Do?

Many applications, particularly in the social sciences, require modeling capabilities beyond what is easily supported and computationally feasible with popular R packages like [mgcv](https://cran.r-project.org/package=mgcv) [@woodGeneralizedAdditiveModels2017], [lavaan](https://lavaan.ugent.be/) [@rosseelLavaanPackageStructural2012], [lme4](https://cran.r-project.org/package=lme4) [@batesFittingLinearMixedEffects2015], and [OpenMx](https://openmx.ssri.psu.edu/) [@nealeOpenMxExtendedStructural2016], as well as the Stata based [GLLAMM](http://www.gllamm.org/) software [@rabe-heskethGeneralizedMultilevelStructural2004;@rabe-heskethMaximumLikelihoodEstimation2005]. In particular, to maximally utilize large datasets available today, it is typically necessary to combine tools from latent variable modeling, hierarchical modeling, and semiparametric estimation. While this is possible with Bayesian hierarchical models and tools like [Stan](https://mc-stan.org/), it requires considerable expertise and may be beyond scope for a single data analysis project. 

The goal of galamm is to enable estimation of models with an arbitrary number of grouping levels, both crossed and hierarchical, and any combination of the following features (click the links to go to the relevant vignette):

- [Linear mixed models with factor structures](https://lcbc-uio.github.io/galamm/articles/lmm_factor.html).
- [Generalized linear mixed models with factor structures](https://lcbc-uio.github.io/galamm/articles/glmm_factor.html).
- [Linear mixed models with heteroscedastic residuals](https://lcbc-uio.github.io/galamm/articles/lmm_heteroscedastic.html).
- [Mixed models with mixed response types](https://lcbc-uio.github.io/galamm/articles/mixed_response.html).
- [Generalized additive mixed models with factor structures](https://lcbc-uio.github.io/galamm/articles/semiparametric.html).
- [Interactions between latent and observed covariates](https://lcbc-uio.github.io/galamm/articles/latent_observed_interaction.html).

```{r srr-tags2, eval = FALSE, echo = FALSE}
#' @srrstats {G1.6} Code for comparing the performance of galamm to the of PLmixed for example models is provided in the vignette on linear mixed models with factor structures.
```


Random effects are defined using [lme4](https://cran.r-project.org/package=lme4) syntax, and the syntax for factor structures are close to that of [PLmixed](https://cran.r-project.org/package=PLmixed) [@rockwoodEstimatingComplexMeasurement2019]. However, for the types of models supported by both PLmixed and galamm, galamm is usually considerably faster. Smooth terms, as in generalized additive mixed models, use the same syntax as [mgcv](https://cran.r-project.org/package=mgcv).

For most users, it should not be necessary to think about how the actual computations are performed, although they are detailed in the [optimization vignette](https://lcbc-uio.github.io/galamm/articles/optimization.html). In short, the core computations are done using sparse matrix methods supported by [RcppEigen](https://cran.r-project.org/package=RcppEigen) [@batesFastElegantNumerical2013] and automatic differentiation using the C++ library [autodiff](https://autodiff.github.io/) [@lealAutodiffModernFast2018]. Scaling of the algorithm is investigated further in [the vignette on computational scaling](https://lcbc-uio.github.io/galamm/articles/scaling.html).

## Where Do I Start?

```{r srr-tags3, eval = FALSE, echo = FALSE}
#' @srrstats {G1.3} Statistical terminology defined in the introductory vignette.
```


To get started, take a look at the [introductory vignette](https://lcbc-uio.github.io/galamm/articles/galamm.html).

## Installation

Install the package from CRAN using

``` r
install.packages("galamm")
```

You can install the development version of galamm from [GitHub](https://github.com/) with:

``` r
# install.packages("remotes")
remotes::install_github("LCBC-UiO/galamm")
```

## Examples

```{r}
library(galamm)
```


### Mixed Response Model

The dataframe `mresp` contains simulated data with mixed response types.

```{r}
head(mresp)
```

Responses in rows with `itemgroup = "a"` are normally distributed while those in rows with `itemgroup = "b"` are binomially distributed. For a given subject, identified by the `id` variable, both responses are associated with the same underlying latent variable. We hence need to model this process jointly, and the model is set up as follows:


```{r, message=FALSE}
mixed_resp <- galamm(
  formula = y ~ x + (0 + loading | id),
  data = mresp,
  family = c(gaussian, binomial),
  family_mapping = ifelse(mresp$itemgroup == "a", 1L, 2L),
  load_var = "itemgroup",
  lambda = matrix(c(1, NA), ncol = 1),
  factor = "loading"
)
```

The summary function gives some information about the model fit.

```{r}
summary(mixed_resp)
```


### Generalized Additive Mixed Model with Factor Structures

The dataframe `cognition` contains simulated for which latent ability in three cognitive domains is measured across time. We focus on the first cognitive domain, and estimate a smooth trajectory for how the latent ability depends on time.

We start by reducing the data.

```{r}
dat <- subset(cognition, domain == 1)
dat$item <- factor(dat$item)
```

Next we define the matrix of factor loadings, where `NA` denotes unknown values to be estimated.

```{r}
loading_matrix <- matrix(c(1, NA, NA), ncol = 1)
```

We then compute the model estimates, containing both a smooth term for the latent ability and random intercept for subject and timepoints.

```{r}
mod <- galamm(
  formula = y ~ 0 + item + sl(x, factor = "loading") +
    (0 + loading | id / timepoint),
  data = dat,
  load_var = "item",
  lambda = loading_matrix,
  factor = "loading"
)
```

We finally plot the estimated smooth term.

```{r}
plot_smooth(mod)
```


## How to cite this package

```{r}
citation("galamm")
```

## Acknowledgement

Some parts of the code base for galamm has been derived from internal functions of the R packages, [gamm4](https://cran.r-project.org/package=gamm4) (authors: Simon Wood and Fabian Scheipl), [lme4](https://cran.r-project.org/package=lme4) (authors: Douglas Bates, Martin Maechler, Ben Bolker, and Steven Walker), and [mgcv](https://cran.r-project.org/package=mgcv) (author: Simon Wood), as well the C++ library [autodiff](https://autodiff.github.io/) (author: Allan Leal). In accordance with the [CRAN Repository Policy](https://cran.r-project.org/web/packages/policies.html), all these authors are listed as contributors in the `DESCRIPTION` file. If you are among these authors, and don't want to be listed as a contributor to this package, please let me know, and I will remove you.

## Contributing

Contributions are very welcome, see [CONTRIBUTING.md](https://github.com/LCBC-UiO/galamm/blob/main/.github/CONTRIBUTING.md) for general guidelines.

## References

Owner

  • Name: Center for Lifespan Changes in Brain and Cognition, University of Oslo
  • Login: LCBC-UiO
  • Kind: organization
  • Email: hukommelse@psykologi.uio.no
  • Location: Oslo, Norway

Online repository of supplemental materials and scripts. Part of @Lifebrain.

CodeMeta (codemeta.json)

{
  "@context": "https://doi.org/10.5063/schema/codemeta-2.0",
  "@type": "SoftwareSourceCode",
  "identifier": "galamm",
  "description": "Estimates generalized additive latent and mixed models using maximum marginal likelihood, as defined in Sorensen et al. (2023) <doi:10.1007/s11336-023-09910-z>, which is an extension of Rabe-Hesketh and Skrondal (2004)'s unifying framework for multilevel latent variable modeling <doi:10.1007/BF02295939>. Efficient computation is done using sparse matrix methods, Laplace approximation, and automatic differentiation. The framework includes generalized multilevel models with heteroscedastic residuals, mixed response types, factor loadings, smoothing splines, crossed random effects, and combinations thereof. Syntax for model formulation is close to 'lme4' (Bates et al. (2015) <doi:10.18637/jss.v067.i01>) and 'PLmixed' (Rockwood and Jeon (2019) <doi:10.1080/00273171.2018.1516541>).",
  "name": "galamm: Generalized Additive Latent and Mixed Models",
  "relatedLink": [
    "https://lcbc-uio.github.io/galamm/",
    "https://CRAN.R-project.org/package=galamm"
  ],
  "codeRepository": "https://github.com/LCBC-UiO/galamm",
  "issueTracker": "https://github.com/LCBC-UiO/galamm/issues",
  "license": "https://spdx.org/licenses/GPL-3.0",
  "version": "0.2.3",
  "programmingLanguage": {
    "@type": "ComputerLanguage",
    "name": "R",
    "url": "https://r-project.org"
  },
  "runtimePlatform": "R version 4.5.1 (2025-06-13)",
  "provider": {
    "@id": "https://cran.r-project.org",
    "@type": "Organization",
    "name": "Comprehensive R Archive Network (CRAN)",
    "url": "https://cran.r-project.org"
  },
  "author": [
    {
      "@type": "Person",
      "givenName": "ystein",
      "familyName": "Srensen",
      "email": "oystein.sorensen@psykologi.uio.no",
      "@id": "https://orcid.org/0000-0003-0724-3542"
    }
  ],
  "contributor": [
    {
      "@type": "Person",
      "givenName": "Douglas",
      "familyName": "Bates"
    },
    {
      "@type": "Person",
      "givenName": "Ben",
      "familyName": "Bolker"
    },
    {
      "@type": "Person",
      "givenName": "Martin",
      "familyName": "Maechler"
    },
    {
      "@type": "Person",
      "givenName": "Allan",
      "familyName": "Leal"
    },
    {
      "@type": "Person",
      "givenName": "Fabian",
      "familyName": "Scheipl"
    },
    {
      "@type": "Person",
      "givenName": "Steven",
      "familyName": "Walker"
    },
    {
      "@type": "Person",
      "givenName": "Simon",
      "familyName": "Wood"
    }
  ],
  "maintainer": [
    {
      "@type": "Person",
      "givenName": "ystein",
      "familyName": "Srensen",
      "email": "oystein.sorensen@psykologi.uio.no",
      "@id": "https://orcid.org/0000-0003-0724-3542"
    }
  ],
  "softwareSuggestions": [
    {
      "@type": "SoftwareApplication",
      "identifier": "covr",
      "name": "covr",
      "provider": {
        "@id": "https://cran.r-project.org",
        "@type": "Organization",
        "name": "Comprehensive R Archive Network (CRAN)",
        "url": "https://cran.r-project.org"
      },
      "sameAs": "https://CRAN.R-project.org/package=covr"
    },
    {
      "@type": "SoftwareApplication",
      "identifier": "gamm4",
      "name": "gamm4",
      "provider": {
        "@id": "https://cran.r-project.org",
        "@type": "Organization",
        "name": "Comprehensive R Archive Network (CRAN)",
        "url": "https://cran.r-project.org"
      },
      "sameAs": "https://CRAN.R-project.org/package=gamm4"
    },
    {
      "@type": "SoftwareApplication",
      "identifier": "knitr",
      "name": "knitr",
      "provider": {
        "@id": "https://cran.r-project.org",
        "@type": "Organization",
        "name": "Comprehensive R Archive Network (CRAN)",
        "url": "https://cran.r-project.org"
      },
      "sameAs": "https://CRAN.R-project.org/package=knitr"
    },
    {
      "@type": "SoftwareApplication",
      "identifier": "PLmixed",
      "name": "PLmixed",
      "provider": {
        "@id": "https://cran.r-project.org",
        "@type": "Organization",
        "name": "Comprehensive R Archive Network (CRAN)",
        "url": "https://cran.r-project.org"
      },
      "sameAs": "https://CRAN.R-project.org/package=PLmixed"
    },
    {
      "@type": "SoftwareApplication",
      "identifier": "rmarkdown",
      "name": "rmarkdown",
      "provider": {
        "@id": "https://cran.r-project.org",
        "@type": "Organization",
        "name": "Comprehensive R Archive Network (CRAN)",
        "url": "https://cran.r-project.org"
      },
      "sameAs": "https://CRAN.R-project.org/package=rmarkdown"
    },
    {
      "@type": "SoftwareApplication",
      "identifier": "testthat",
      "name": "testthat",
      "version": ">= 3.0.0",
      "provider": {
        "@id": "https://cran.r-project.org",
        "@type": "Organization",
        "name": "Comprehensive R Archive Network (CRAN)",
        "url": "https://cran.r-project.org"
      },
      "sameAs": "https://CRAN.R-project.org/package=testthat"
    }
  ],
  "softwareRequirements": {
    "1": {
      "@type": "SoftwareApplication",
      "identifier": "lme4",
      "name": "lme4",
      "provider": {
        "@id": "https://cran.r-project.org",
        "@type": "Organization",
        "name": "Comprehensive R Archive Network (CRAN)",
        "url": "https://cran.r-project.org"
      },
      "sameAs": "https://CRAN.R-project.org/package=lme4"
    },
    "2": {
      "@type": "SoftwareApplication",
      "identifier": "Matrix",
      "name": "Matrix",
      "provider": {
        "@id": "https://cran.r-project.org",
        "@type": "Organization",
        "name": "Comprehensive R Archive Network (CRAN)",
        "url": "https://cran.r-project.org"
      },
      "sameAs": "https://CRAN.R-project.org/package=Matrix"
    },
    "3": {
      "@type": "SoftwareApplication",
      "identifier": "memoise",
      "name": "memoise",
      "provider": {
        "@id": "https://cran.r-project.org",
        "@type": "Organization",
        "name": "Comprehensive R Archive Network (CRAN)",
        "url": "https://cran.r-project.org"
      },
      "sameAs": "https://CRAN.R-project.org/package=memoise"
    },
    "4": {
      "@type": "SoftwareApplication",
      "identifier": "methods",
      "name": "methods"
    },
    "5": {
      "@type": "SoftwareApplication",
      "identifier": "mgcv",
      "name": "mgcv",
      "provider": {
        "@id": "https://cran.r-project.org",
        "@type": "Organization",
        "name": "Comprehensive R Archive Network (CRAN)",
        "url": "https://cran.r-project.org"
      },
      "sameAs": "https://CRAN.R-project.org/package=mgcv"
    },
    "6": {
      "@type": "SoftwareApplication",
      "identifier": "nlme",
      "name": "nlme",
      "provider": {
        "@id": "https://cran.r-project.org",
        "@type": "Organization",
        "name": "Comprehensive R Archive Network (CRAN)",
        "url": "https://cran.r-project.org"
      },
      "sameAs": "https://CRAN.R-project.org/package=nlme"
    },
    "7": {
      "@type": "SoftwareApplication",
      "identifier": "Rcpp",
      "name": "Rcpp",
      "provider": {
        "@id": "https://cran.r-project.org",
        "@type": "Organization",
        "name": "Comprehensive R Archive Network (CRAN)",
        "url": "https://cran.r-project.org"
      },
      "sameAs": "https://CRAN.R-project.org/package=Rcpp"
    },
    "8": {
      "@type": "SoftwareApplication",
      "identifier": "Rdpack",
      "name": "Rdpack",
      "provider": {
        "@id": "https://cran.r-project.org",
        "@type": "Organization",
        "name": "Comprehensive R Archive Network (CRAN)",
        "url": "https://cran.r-project.org"
      },
      "sameAs": "https://CRAN.R-project.org/package=Rdpack"
    },
    "9": {
      "@type": "SoftwareApplication",
      "identifier": "stats",
      "name": "stats"
    },
    "10": {
      "@type": "SoftwareApplication",
      "identifier": "R",
      "name": "R",
      "version": ">= 3.5.0"
    },
    "SystemRequirements": "C++17"
  },
  "fileSize": "26104.358KB",
  "citation": [
    {
      "@type": "ScholarlyArticle",
      "datePublished": "2024",
      "author": [
        {
          "@type": "Person",
          "givenName": "{\\O}ystein",
          "familyName": "S{\\o}rensen"
        }
      ],
      "name": "Multilevel Semiparametric Latent Variable Modeling in R with \"galamm\"",
      "identifier": "10.1007/s11336-023-09910-z",
      "@id": "https://doi.org/10.1007/s11336-023-09910-z",
      "sameAs": "https://doi.org/10.1007/s11336-023-09910-z",
      "isPartOf": {
        "@type": "PublicationIssue",
        "datePublished": "2024",
        "isPartOf": {
          "@type": [
            "PublicationVolume",
            "Periodical"
          ],
          "name": "Multivariate Behavioral Research"
        }
      }
    },
    {
      "@type": "ScholarlyArticle",
      "datePublished": "2023",
      "author": [
        {
          "@type": "Person",
          "givenName": "{\\O}ystein",
          "familyName": "S{\\o}rensen"
        },
        {
          "@type": "Person",
          "givenName": "Kristine B.",
          "familyName": "Walhovd"
        },
        {
          "@type": "Person",
          "givenName": "Anders M.",
          "familyName": "Fjell"
        }
      ],
      "name": "Longitudinal Modeling of Age-Dependent Latent Traits with Generalized Additive Latent and Mixed Models",
      "identifier": "10.1007/s11336-023-09910-z",
      "pagination": "456-486",
      "@id": "https://doi.org/10.1007/s11336-023-09910-z",
      "sameAs": "https://doi.org/10.1007/s11336-023-09910-z",
      "isPartOf": {
        "@type": "PublicationIssue",
        "issueNumber": "2",
        "datePublished": "2023",
        "isPartOf": {
          "@type": [
            "PublicationVolume",
            "Periodical"
          ],
          "volumeNumber": "88",
          "name": "Psychometrika"
        }
      }
    }
  ],
  "releaseNotes": "https://github.com/LCBC-UiO/galamm/blob/master/NEWS.md",
  "readme": "https://github.com/LCBC-UiO/galamm/blob/main/README.md",
  "contIntegration": [
    "https://github.com/LCBC-UiO/galamm/actions/workflows/R-CMD-check.yaml",
    "https://app.codecov.io/gh/LCBC-UiO/galamm?branch=main"
  ],
  "developmentStatus": "https://www.repostatus.org/#active",
  "review": {
    "@type": "Review",
    "url": "https://github.com/ropensci/software-review/issues/615",
    "provider": "https://ropensci.org"
  },
  "keywords": [
    "structural-equation-models",
    "generalized-additive-models",
    "item-response-theory",
    "hierarchical-models",
    "latent-variable-models"
  ]
}

GitHub Events

Total
  • Create event: 8
  • Release event: 1
  • Issues event: 16
  • Watch event: 1
  • Delete event: 6
  • Issue comment event: 2
  • Push event: 40
  • Pull request review event: 2
  • Pull request event: 11
  • Fork event: 1
Last Year
  • Create event: 8
  • Release event: 1
  • Issues event: 16
  • Watch event: 1
  • Delete event: 6
  • Issue comment event: 2
  • Push event: 40
  • Pull request review event: 2
  • Pull request event: 11
  • Fork event: 1

Packages

  • Total packages: 1
  • Total downloads:
    • cran 353 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 6
  • Total maintainers: 1
cran.r-project.org: galamm

Generalized Additive Latent and Mixed Models

  • Versions: 6
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 353 Last month
Rankings
Forks count: 21.3%
Dependent packages count: 27.9%
Stargazers count: 28.0%
Dependent repos count: 36.9%
Average: 40.0%
Downloads: 86.1%
Last synced: 6 months ago

Dependencies

.github/workflows/R-CMD-check.yaml actions
  • actions/checkout v2 composite
  • r-lib/actions/check-r-package v2 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/pkgdown.yaml actions
  • JamesIves/github-pages-deploy-action 4.1.4 composite
  • actions/checkout v2 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
DESCRIPTION cran
  • R >= 3.5.0 depends
  • Rcpp * imports
  • lme4 * imports
  • Matrix * suggests
  • PLmixed * suggests
  • covr * suggests
  • dplyr * suggests
  • gamm4 * suggests
  • knitr * suggests
  • memoise * suggests
  • nlme * suggests
  • purrr * suggests
  • rmarkdown * suggests
  • testthat >= 3.0.0 suggests
  • tidyr * suggests
.github/workflows/lint.yaml actions
  • actions/checkout v3 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/test-coverage.yaml actions
  • actions/checkout v3 composite
  • actions/upload-artifact v3 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite