# Solve Mixed model by hand on R

I would like to understand how to solve a "simple" mixed model by hand on R and not by using an available package (like nlme, etc).

As example let's get the data from this case,

library(mlmRev)
library(lme4)
library(rstanarm)
library(ggplot2)
# Make Male the reference category and rename variable
Gcsemv$$female <- relevel(Gcsemv$$gender, "M")

# Use only total score on coursework paper
GCSE <- subset(x = Gcsemv,
select = c(school, student, female, course))

# Count unique schools and students
J <- length(unique(GCSE\$school))
N <- nrow(GCSE)

M2 <- lmer(formula = course ~ 1 + female + (1 | school),
data = GCSE,
REML = FALSE)
summary(M2)

• It is not clear what you mean by to solve a mixed model by hand. Mixed models cannot be solved (i.e., find the maximum likelihood estimates) by hand they require iterative optimization algorithms. For linear mixed, models you only have a close-form solution for the fixed effects if you know the values of the variance components. Namely, for known variance components, the fixed effects are estimated using generalized least squares. Sep 25 '20 at 7:57
• Can I use one of these iterative optimizations algorithms to do it? Sep 25 '20 at 8:01
• Yes, you can, and you may find an example here: stats.stackexchange.com/questions/385670/… Sep 25 '20 at 8:35
• If you can write down and program the likelihood function, then you can find the maxlik solution by optimizing the likelihood, multiple R packages will help! Sep 25 '20 at 11:21
• Do you mean that you want to do all the computations by hand ? Sep 26 '20 at 13:02