Hi Steve, thanks for pointing this factor thing out. At least, the crossed 
design worked with lmer(), which is the correct design. I am curious about the 
error message with nested design using lmer(), too.

John




________________________________
From: Steven McKinney <smckinney@bccrc.ca>

<r-sig-mixed-models@r-project.org>
Sent: Fri, October 8, 2010 3:07:54 PM
Subject: RE: [R-sig-ME] lmer give error message

Hi John,

> dat <- read.table ("dat.txt", sep='\t', row.names=1, header=T)
> sapply(dat, class)
      pid     visit       eye         y 
"integer" "integer"  "factor" "integer" 

Part of the issue is that the modeling machinery is set up
to work with factor variables.  lmer does not always convert
numerics to integers.  Set up factor versions of your
independent variable groups.


> dat$fpid <- as.factor(dat$pid)
> dat$fvisit <- as.factor(dat$visit)


>     ### crossed
> lmer(y~1+(1|fpid/fvisit)+(1|fpid/eye),dat)
Linear mixed model fit by REML 
Formula: y ~ 1 + (1 | fpid/fvisit) + (1 | fpid/eye) 
   Data: dat 
  AIC  BIC logLik deviance REMLdev
2848 2875  -1418     2835    2836
Random effects:
Groups      Name        Variance Std.Dev.
fvisit:fpid (Intercept) 0.92510  0.96182 
eye:fpid    (Intercept) 0.51945  0.72073 
fpid        (Intercept) 4.02193  2.00547 
fpid        (Intercept) 4.02191  2.00547 
Residual                0.89761  0.94742 
Number of obs: 718, groups: fvisit:fpid, 359; eye:fpid, 240; fpid, 120

Fixed effects:
            Estimate Std. Error t value
(Intercept)   4.0648     0.2702   15.04
> 

>     ### nested using lmer
> lmer(y~1+(1|fpid/fvisit/eye),dat)
Error in function (fr, FL, start, REML, verbose)  : 
  Number of levels of a grouping factor for the random effects
must be less than the number of observations

Issues of model specification, number of observations
per group, and groups missing data remain
to be worked out.


> with(dat, table(fpid, fvisit, eye))
, , eye = L

     fvisit
fpid  0 1 2
  1   1 1 1
  2   1 1 1
  3   1 1 1
...
  37  1 1 1
  38  1 0 1
  39  1 1 1
...
  119 1 1 1
  120 1 1 1

, , eye = R

     fvisit
fpid  0 1 2
  1   1 1 1
  2   1 1 1
  3   1 1 1
  4   1 1 1
  5   1 1 1
...
  117 1 1 1
  118 1 1 1
  119 1 1 1
  120 1 1 1



Steven McKinney


> -----Original Message-----
> From: r-sig-mixed-models-bounces@r-project.org 
>[mailto:r-sig-mixed-models-bounces@r-project.org] On
> Behalf Of array chip
> Sent: October-08-10 2:48 PM
> To: r-sig-mixed-models@r-project.org
> Subject: [R-sig-ME] lmer give error message
> 
> Hi, I have an ophthalmology dataset, that has as response variable "y", 
patient
> ID (pid), visit (0,1,2) and eye examined ("L' and "R").
> 
> Basically, each patient has 3 visit, during each visit, both eyes will be
> assessed and produce response measurements. The first question is should I 
>treat
> eye.examined and visit as crossed or nested? I think it should be treated as
> crossed because each eye will be assessed in each visit. On the other hand, 
the
> 2 measurements from the 2 eyes were from a given visit, so it looks like it 
can
> be treated as nested as well (eye %in% visit).
> 
> 
> The second question is, no matter which way I use, lmer() gives me error
> message, while lme() from nlme package can handle nested design easily. Also, 
>is
> there a way to specify corssed design in lme() as well?
> 
> 
> > dat <- read.table ("dat.txt", sep='\t', row.names=1, header=T)
> > library(nlme)
> 
>     ### nested using lme
> > lme(y~1, random=~1|pid/visit/eye,dat)
> Random effects:
>  Formula: ~1 | pid
>         (Intercept)
> StdDev:    2.881611
> 
>  Formula: ~1 | visit %in% pid
>         (Intercept)
> StdDev:   0.8154632
> 
>  Formula: ~1 | eye %in% visit %in% pid
>         (Intercept)  Residual
> StdDev:    1.065172 0.5321993
> 
>     ### nested using lmer
> > lmer(y~1+(1|pid/visit/eye),dat)
> Error: length(f1) == length(f2) is not TRUE
> In addition: Warning messages:
> 1: In visit:pid :
>   numerical expression has 718 elements: only the first used
> 2: In visit:pid :
>   numerical expression has 718 elements: only the first used
> 
>     ### crossed
> > lmer(y~1+(1|pid/visit)+(1|pid/eye),dat)
> Error: length(f1) == length(f2) is not TRUE
> In addition: Warning messages:
> 1: In visit:pid :
>   numerical expression has 718 elements: only the first used
> 2: In visit:pid :
>   numerical expression has 718 elements: only the first used
> 3: In eye:pid : numerical expression has 718 elements: only the first used
> 4: In eye:pid : numerical expression has 718 elements: only the first used
> 
> 
> Can anyone share their thoughts.
> 
> Thanks
> 
> John
> 
> 
> 



      
	[[alternative HTML version deleted]]


