Kodiologist - 3 years ago 259
R Question

# lme4: Random slopes shared by all observations

I'm using R's

`lme4`
. Suppose I have a mixed-effects logistic-regression model where I want some random slopes shared by every observation. They're supposed to be random in the sense that these random slopes should all come from a single normal distribution. This is essentially the same thing as ridge regression, but without choosing a penalty size with cross-validation.

I tried the following code:

``````library(lme4)

ilogit = function(v)
1 / (1 + exp(-v))

set.seed(20)

n = 100

x1 = rnorm(n)
x2 = rnorm(n)
x3 = rnorm(n)
x4 = rnorm(n)
x5 = rnorm(n)

y.p = ilogit(.5 + x1 - x2)
y = rbinom(n = n, size = 1, prob = y.p)

m1 = glm(
y ~ x1 + x2 + x3 + x4 + x5,
family = binomial)
print(round(d = 2, unname(coef(m1))))

m2 = glmer(
y ~ ((x1 + x2 + x3 + x4 + x5)|1),
family = binomial)
print(round(d = 2, unname(coef(m2))))
``````

This yields:

``````Loading required package: Matrix
[1]  0.66  1.14 -0.78 -0.01 -0.16  0.25
Error: (p <- ncol(X)) == ncol(Y) is not TRUE
Execution halted
``````

What did I do wrong? What's the right way to do this?