Build Status

Fit statistical (linear) models with fixed and mixed (random) effects in Ruby.


Linear mixed models

  • Support for the formula language of the R package lme4 makes model specification convenient and user friendly. An expanation of the lme4 formula interface can be found in the lme4 vignette, or on stackexchange (example). Types of linear mixed models that can be fit by mixed_models with the formula interface include:

    • Models with interaction fixed or random effects (e.g. y ~ x + z + x:z + (x + z + x:z | g)),
    • Models with multiple crossed random effects (e.g. y ~ x + (x|g) + (x|h)),
    • Models with nested random effects (e.g. y ~ x + (x|g) + (x|g:h)).
  • Flexible model specification capabilities with the possibility to pass the random effects covariance structure as a Proc, when fitting a model from raw madel matrices.

  • Possibility of singular fits (i.e models with random effects variance equal to zero).

  • Likelihood ratio test for nested models (Chi squared or bootstrap based).

  • Many types of hypotheses tests for the fixed and random effects (based on LRT, bootstrap or the Wald Z statistic).

  • Four types of bootstrap confidence intervals for the fixed effects coefficient estimates, as well as Wald Z confidence intervals. All bootstrap based methods are performed in parallel by default.

  • Prediction on new data and prediction intervals.


To install the latest stable version:

$ gem install mixed_models

Alternatively, you can add the line gem 'mixed_models' to your application's Gemfile, and then execute:

$ bundle

Ruby is required in version >=2.0 because keyword arguments are excessively used in mixed_models.


Linear mixed models (LMM) tutorials

Analysis of real data

  • This IRuby notebook shows an application of mixed_models to real data, which originate from blog posts from various sources in 2010-2012. The analyzed data set is of nontrivial size (~50000x300) and nontrivial structure (redundant variables, missing values, data transformations, etc.).

More usage examples

Some other examples in form of Ruby code can be found in the examples folder.

Further examples can be found in several BLOG posts (see below).

Some relevant BLOG posts


The development version of mixed_models can be installed using the command line:

git clone https://github.com/agisga/mixed_models.git
cd mixed_models/
bundle install
bundle exec rake install

The automatic tests can be run with bundle exec rspec spec.


  1. Fork it ( https://github.com/[my-github-username]/mixed_models/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request