[R-sig-ME] lme4

Ben Bolker bbolker at gmail.com
Sat Jul 15 04:10:27 CEST 2017

On 17-07-14 09:26 PM, Dan Selechnik wrote:
> Hello,
> My name is Dan and I'm a PhD student in Australia. I was hoping that I
> could ask you for some help with using lme4. I have a dataset in which I
> have PC1 as a response variable. Population, treatment, RBC, and
> population*treatment are my explanatory variables. ID is a random factor.
> (I have attached the CSV file here)...
> I am trying to run a power analysis, and first to fit my data using lmer.
> First I read my data into R:
> pc1=read.csv("R-PowerAnalysis.csv", header=TRUE)
> Then I attempt to fit:
> fm1=lmer(pc1$PC1 ~ pc1$RBC + pc1$Population + pc1$Treatment +
> pc1$Population*pc1$Treatment + (1|pc1$ID), data=pc1, REML=FALSE)

   A small point, but in general you should *not* use pc1$ in specifying
your formula: instead,

fm1=lmer(PC1 ~ RBC + Population*Treatment + (1|ID), data=pc1,

(also, the * operator includes both the main effects of Population and
Treatment and their interaction). But that should be tangential to your

> However, this fails, returning the message:
> Error: number of levels of each grouping factor must be < number of
> observations
> My number of populations and treatments is much less than my number of
> observations, so I am not sure why I am getting this error... 

  That's not your problem.  lme4 is referring to the number of levels of
the *grouping factor*, which is ID (not Population or Treatment).  Your
ID variable must contain a single observation per group (cheating and
looking at the data you sent me offline, I can see that's true).

If you had sent the results of summary(pc1), we could have guessed this:
ID is coded as an integer so we don't know for sure that it consists of
the values 1..20, but since the min is 1 and the max is 20 and mean is
10.5, we can guess that that's the case ...

       ID        Population         Treatment       RBC
 Min.   : 1.00   QLD:10     LPS-Injection:10   Min.   :-113.00
 1st Qu.: 5.75   WA :10     PBS-Injection:10   1st Qu.:  22.00
 Median :10.50                                 Median :  49.00
 Mean   :10.50                                 Mean   :  53.55
 3rd Qu.:15.25                                 3rd Qu.: 107.00
 Max.   :20.00                                 Max.   : 181.00
 Min.   :-4.5411
 1st Qu.: 0.1017
 Median : 1.1470
 Mean   : 0.6258
 3rd Qu.: 1.9251
 Max.   : 3.2004

Also, when I
> run this, it works fine:
> fm1=lm(pc1$PC1 ~ pc1$RBC + pc1$Population + pc1$Treatment +
> pc1$Population*pc1$Treatment + (1|pc1$ID), data=pc1)

If you look at the results of this model:

                        (Intercept)                                  RBC
                            1.47759                              0.01318
                       PopulationWA               TreatmentPBS-Injection
                           -1.38234                             -1.85619
                         1 | IDTRUE  PopulationWA:TreatmentPBS-Injection
                                 NA                              0.24754

you can see that something funny is happening to the (1|ID) term ...

> I was hoping I could ask for your assistance in figuring out what may be
> the problem. Thank you very much.
> Cheers,
> Dan
> _______________________________________________
> R-sig-mixed-models at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models

More information about the R-sig-mixed-models mailing list