Skip to content
Prev 181384 / 398502 Next

Naming a random effect in lmer

On Sun, May 24, 2009 at 10:41 PM, Leigh Ann Starcevich <lah at peak.org> wrote:
But what you tried is not what I wrote.   The paste function creates a
character string - that's all.  You could create a numeric variable or
a factor by parsing and evaluating the resulting character string but
that is rarely a good way of doing things.  See

library(fortunes)
fortune("rethink")

What I wrote was to evaluate the expression

Z29 <- Z2 + Z3 + Z4 + Z5 + Z6 + Z7 + Z9 + Z9

within the data frame and that does work.  See the attached.

I still don't really understand the approach.  It is going about
things in an awkward way and I suspect it is the result of someone
with experience in SAS or SPSS trying to emulate their approach in R.
It is (or was, the last time I used either of those systems) common to
name variables like Z2, Z3, Z4, ... so you can access groups of
variables in those languages.  In R that is unnecessary because data
can be packaged into arbitrary, self-describing structures, including
matrices.  If the natural thing to do with the variables Z2, Z3, ...,
Z13 is to accumulate them in sums then it would be much simpler if you
create a matrix from them and use that.
-------------- next part --------------

R version 2.9.0 (2009-04-17)
Copyright (C) 2009 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
Loading required package: Matrix
Loading required package: lattice

Attaching package: 'Matrix'


	The following object(s) are masked from package:stats :

	 xtabs 


	The following object(s) are masked from package:base :

	 rcond
R version 2.9.0 (2009-04-17) 
i486-pc-linux-gnu 

locale:
LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=C;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lme4_0.999375-31   Matrix_0.999375-26 lattice_0.17-25   

loaded via a namespace (and not attached):
[1] grid_2.9.0
'data.frame':	105 obs. of  20 variables:
 $ Year    : num  2008 2008 2008 2008 2008 ...
 $ WYear   : num  0 0 0 0 0 0 0 1 1 1 ...
 $ Site    : num  4 18 26 40 67 75 94 4 18 26 ...
 $ LogY    : num  0.849 0.809 0.734 1.467 0.716 ...
 $ Y       : num  2.34 2.25 2.08 4.34 2.05 ...
 $ Z2      : num  0.472 0.472 0.472 0.472 0.472 ...
 $ Z3      : num  -0.456 -0.456 -0.456 -0.456 -0.456 ...
 $ Z4      : num  0.394 0.394 0.394 0.394 0.394 ...
 $ Z5      : num  -0.308 -0.308 -0.308 -0.308 -0.308 ...
 $ Z6      : num  0.219 0.219 0.219 0.219 0.219 ...
 $ Z7      : num  -0.142 -0.142 -0.142 -0.142 -0.142 ...
 $ Z8      : num  0.0833 0.0833 0.0833 0.0833 0.0833 ...
 $ Z9      : num  -0.044 -0.044 -0.044 -0.044 -0.044 ...
 $ Z10     : num  0.0207 0.0207 0.0207 0.0207 0.0207 ...
 $ Z11     : num  -0.0085 -0.0085 -0.0085 -0.0085 -0.0085 ...
 $ Z12     : num  0.00295 0.00295 0.00295 0.00295 0.00295 ...
 $ Z13     : num  -0.00082 -0.00082 -0.00082 -0.00082 -0.00082 ...
 $ Z14     : num  0.000158 0.000158 0.000158 0.000158 0.000158 ...
 $ WYearCen: num  -7 -7 -7 -7 -7 -7 -7 -6 -6 -6 ...
 $ Z29     : num  0.218 0.218 0.218 0.218 0.218 ...
Z29
  -1.63314896653237  -0.592612960445885  -0.394922672042026  -0.233838363771464 
                  7                   7                   7                   7 
 -0.214767257996307 -0.0925109179084174 -0.0376120692964267  0.0181504978689960 
                  7                   7                   7                   7 
 0.0208109028608321   0.129704969588931   0.197693713890356    0.21834978504526 
                  7                   7                   7                   7 
  0.227443614453709   0.269153880224658    2.11810584406015 
                  7                   7                   7
Linear mixed model fit by REML 
Formula: LogY ~ WYear + (1 + WYear | Site) + (1 | Z29) 
   Data: testsamp 
 Subset: WYear <= 10 
    AIC    BIC logLik deviance REMLdev
 -147.5 -131.1  80.77   -167.7  -161.5
Random effects:
 Groups   Name        Variance  Std.Dev. Corr  
 Z29      (Intercept) 0.0095237 0.097589       
 Site     (Intercept) 0.0765445 0.276667       
          WYear       0.0262909 0.162145 0.046 
 Residual             0.0011282 0.033589       
Number of obs: 77, groups: Z29, 11; Site, 7

Fixed effects:
              Estimate Std. Error t value
(Intercept)  0.9857992  0.1183912   8.327
WYear       -0.0002676  0.0619989  -0.004

Correlation of Fixed Effects:
      (Intr)
WYear -0.020
user  system elapsed 
 26.245   0.188  26.512