R programming language logo

Random effect models with lmer function in R

Published on  
19 August 2021
Nik Samoylov image
Nik Samoylov
Founder

Random effects are everywhere in survey data. Let's try to do appropriate modelling for them in R!


This note was prepared for Conjointly researchers, but it would be a shame to hide it inside internal documentation. It is very technical. You do not need to know any of this to use the tools. 😊

Understanding random effects models starts with Jared Knowles’ article Getting Started with Mixed Effect Models in R. Now, let’s get our hands dirty and generate some random data:

library(lme4)
library(data.table)

N=300

# Assign real coefficients
real_coefficients=list(
    blue=10,
    yellow=-10,
    green=5,
    speed=5,
    intercept=50
)

# Colour is a property of the object
data=data.table(
    colour=sample(c("blue","yellow","green"),N,replace=T),
    speed=runif(N,1,50)
)

# And assigning innate preference for each colour
for(i in names(real_coefficients)){
  data[colour==i,  innate_preference:= real_coefficients[[i]]]
}

# Actual preference is sum of innate preference for colour, innate preference for speed and error
data[,preference:=
  real_coefficients[["intercept"]]+
  innate_preference+
  (real_coefficients[["speed"]]*speed)+
  rnorm(N)
]

# Review what we have
data

Let’s estimate a simple model

summary(lm(preference~colour+speed,data=data))

Compare its coefficients to real_coefficients.

Let’s add random effect: measurement instrument (an intercept)

data$instrument=sample(c(-10,0,10),N,replace=T)

data[, new_preference:=preference+instrument]

# Explore results of model
model=lmer(new_preference~colour+speed + (1 | instrument), data = data)
summary(model)
coef(model)$instrument

Let’s add random effect: speed of measurement instrument (a slope)

data$instrument_speed=sample(c(-10,0,10),N,replace=T)

data[, new_preference:=preference+speed*instrument_speed+rnorm(N)]

# Explore results of model
model=lmer(new_preference~colour+speed + (speed | instrument_speed), data = data)
summary(model)
coef(model)$instrument_speed

That’s the end! Now we understand mixed effect models!


Read these articles next:

Can you make perceptual maps from fake data? (A replication study)
Creating a perception map

Can you make perceptual maps from fake data? (A replication study)

This study investigates the claim that synthetic LLM-generated data could be used effectively for creating perceptual maps in market research contexts.

View article
Kellogg's vs. Kellanova: Which Do Consumers Prefer?
Comparing the new and old Kellogg Logo

Kellogg's vs. Kellanova: Which Do Consumers Prefer?

To assess the effectiveness of Kellogg's recent rebrand to Kellanova, the Business Name Evaluator and Logo Test surveys compare the business names and their logos to see which respondents preferred.

View article
Are consumers passing on Ray-Ban Meta glasses?
Ray-Ban Meta smart glasses Credits: Meta / EssilorLuxottica

Are consumers passing on Ray-Ban Meta glasses?

Ray-Ban Meta glasses were dubbed 'pervert glasses' amid growing privacy concerns in 2026. Will real consumers pass on the product? We ran a concept test to find out.

View article