require 'daru' alien_species = Daru::DataFrame.from_csv '../examples/data/alien_species.csv' # mixed_models expects that all variable names in the data frame are ruby Symbols: alien_species.vectors = Daru::Index.new(alien_species.vectors.map { |v| v.to_sym }) alien_species.head require 'mixed_models' model_fit = LMM.from_formula(formula: "Aggression ~ Age + Species + (Age | Location)", data: alien_species) puts "Fixed effects:" puts model_fit.fix_ef puts "Random effects:" puts model_fit.ran_ef model_fit.fix_ef_summary model_fit.ran_ef_summary puts "REML criterion used: \t#{model_fit.reml}" puts "Residual variance: \t#{model_fit.sigma2}" puts "Formula: \t" + model_fit.formula puts "Variance of the intercept due to 'location' (i.e. variance of b0): \t#{model_fit.sigma_mat[0,0]}" puts "Variance of the effect of 'age' due to 'location' (i.e. variance of b1): \t#{model_fit.sigma_mat[1,1]}" puts "Covariance of b0 and b1: \t#{model_fit.sigma_mat[0,1]}" puts "Residual standard deviation: \t#{model_fit.sigma}" puts "REML criterion: \t#{model_fit.deviance}" puts "Fitted values at the population level:" model_fit.fitted(with_ran_ef: false) puts "Model residuals:" model_fit.residuals require 'gnuplotrb' include GnuplotRB x, y = model_fit.fitted, model_fit.residuals fitted_vs_residuals = Plot.new([[x,y], with: 'points', pointtype: 6, notitle: true], xlabel: 'Fitted', ylabel: 'Residuals') bin_width = (y.max - y.min)/10.0 bins = (y.min..y.max).step(bin_width).to_a rel_freq = Array.new(bins.length-1){0.0} y.each do |r| 0.upto(bins.length-2) do |i| if r >= bins[i] && r < bins[i+1] then rel_freq[i] += 1.0/y.length end end end bins_center = bins[0...-1].map { |b| b + bin_width/2.0 } residuals_hist = Plot.new([[bins_center, rel_freq], with: 'boxes', notitle: true], style: 'fill solid 0.5') require 'distribution' observed = model_fit.residuals.sort n = observed.length theoretical = (1..n).to_a.map { |t| Distribution::Normal.p_value(t.to_f/n.to_f) * model_fit.sigma} qq_plot = Plot.new([[theoretical, observed], with: 'points', pointtype: 6, notitle: true], ['x', with: 'lines', notitle: true], xlabel: 'Normal theoretical quantiles', ylabel: 'Observed quantiles', title: 'Q-Q plot of the residuals')