# How to estimate Variance Components in a mixed linear model ?

GROUPS:
 Hello, How do I fit a mixed linear model to my data using Mathematica? The model contains random and fixed effects. In addition, I am interested in estimating variance components for each random effect used in my model! Any hints that steer me into the right direct are appreciated!Martin
2 years ago
9 Replies
2 years ago
 I don't think that Mathematica has built in functionality for mixed linear models. You may have to program this yourself. Alternatively, you could use R to do so. If you want to do it via Mathematica, you have RLink that may come in useful.
2 years ago
 Bruce and Asim, many thanks for your advice and suggestions! I hope that in the future the Mathematica staff addresses this gap!
2 years ago
 RLink seems to be very promissing!
2 years ago
 M Bohn 1 Vote I started to work with RLink and this is actually the way to go! So far it works flawlessly running mixed linear models and estimating variance components using R package "lme4" within Mathematica.
 M Bohn 1 Vote As Asim suggested, I used R via RLink to estimate variance componets. The sample data set "Example_DataSet1.csv" is availalbe here: https://dl.dropboxusercontent.com/u/15488405/Example_DataSet1.csv. All factors in the model are regarded as random effects.I used the following code: Needs["RLink`"] InstallR[ ]  REvaluate["   {   FAC1=as.factor(example$Fac1) FAC2=as.factor(example$Fac2)   FAC3=as.factor(example$Fac3) FAC4=as.factor(example$Fac4)  VAR1=as.numeric(example\$Var1)  }  "];REvaluate["hist(VAR1)"]StringJoin@Riffle[#, "\n"] &@ REvaluate["   {  library(lme4)  fit2 = lmer(formula= VAR1 ~ (1|FAC1) + (1|FAC2%in%FAC1) + (1|FAC4) + (1|FAC1:FAC4))  summary.text = capture.output(print( summary(fit2)))  }  "]