1
$\begingroup$

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) 
$\endgroup$
5
  • 1
    $\begingroup$ 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. $\endgroup$ Sep 25 '20 at 7:57
  • $\begingroup$ Can I use one of these iterative optimizations algorithms to do it? $\endgroup$
    – Lefty
    Sep 25 '20 at 8:01
  • 1
    $\begingroup$ Yes, you can, and you may find an example here: stats.stackexchange.com/questions/385670/… $\endgroup$ Sep 25 '20 at 8:35
  • $\begingroup$ 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! $\endgroup$ Sep 25 '20 at 11:21
  • $\begingroup$ Do you mean that you want to do all the computations by hand ? $\endgroup$
    – Wayne B
    Sep 26 '20 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.