Hello All,
I am a novice user of R and LME4 - I hope I am sending this query to an appropriate forum. I am generating erroneous data, but I don't know if the reason is misuse of LME4 or the loop function.

I have an 824 x 27 dataframe with 5 factors and 22 response variables.
> head(r)
  REP ROW RAN        GEN OPR X20 X30 X40 X50 X60 X70 X80 X90 X100 X110 X120 X130 X140 X150 X160 X170 X180 X190 X200
1   1   1   1 cav4081281  GP  20  17  20  23  29  32  32  32   24   21   10    2    1    0    0    0    0    0    0
2   1   1  10 cav4081359  PH  14  31  30  25  21  21  26  12   26   25   16   18    6    4    1    0    0    0    0
3   1   1  11 cav4080437  GP   9  12  18  24  27  22  22  12   15    8    8    3    2    0    0    0    0    0    0
4   1   1  12 cav4081445  PH  20   9  18  22  20  11  10   7    2    4    2    5    0    0    0    0    0    0    0
5   1   1  13 cav4080209  PH  22  22  14  17  25  25  18  10    2    4    2    0    0    0    0    0    0    0    0
6   1   1  14 cav4081584  GP   8  15  11   8  20  14  25   9    3    0    1    0    0    0    0    0    0    0    0
   md  tc md90
1 140 263  100
2 160 276  120
3 140 182  100
4 130 130   90
5 120 161   80
6 120 114   80

I wrote a loop to run a model (VAR ~ (1|GEN) + (1|OPR) + (1|ROW) + (1|RAN)) on each response variable, to extract the variance for each factor (and the residual) and to build a dataframe of the variances across all the response variables. The names of the variables were in the vector "nam".

> nam
 [1] "X20"  "X30"  "X40"  "X50"  "X60"  "X70"  "X80"  "X90"  "X100" "X110" "X120" "X130" "X140" "X150" "X160" "X170"
[17] "X180" "X190" "X200" "md"   "tc"   "md90"

V1<-rep(0,length(nam))
V2<-rep(0,length(nam))
V3<-rep(0,length(nam))
V4<-rep(0,length(nam))
V5<-rep(0,length(nam))
x<-1
for(i in nam) {
  d<-r[,c("REP","ROW","RAN","GEN","OPR",i)]
  colnames(d)<-c("REP","ROW","RAN","GEN","OPR","VAR")
  r.mer<-lmer(VAR ~ (1|GEN) + (1|OPR) + (1|ROW) + (1|RAN), data=d)
  r.var <- data.frame(summary(r.mer)@REmat)
  V1[x]<-as.numeric(as.character(r.var[r.var$Groups=="GEN",'Variance']))
  V2[x]<-as.numeric(as.character(r.var[r.var$Groups=="OPR",'Variance']))
  V3[x]<-as.numeric(as.character(r.var[r.var$Groups=="RAN",'Variance']))
  V4[x]<-as.numeric(as.character(r.var[r.var$Groups=="ROW",'Variance']))
  V5[x]<-as.numeric(as.character(r.var[r.var$Groups=="Residual",'Variance']))
  x<-x+1
  }

df<-data.frame(nam,V1,V2,V3,V4,V5)
colnames(df)<-c("DEP", "GEN", "OPR", "RAN", "ROW","Residual")

That script gave the following output for the variances for the factors

> df
    VAR         GEN          OPR          RAN         ROW   Residual
1   X20 3.00653e+00 7.343150e+00 1.244540e+00 9.81330e-01 47.4381400
2   X30 3.01214e+00 3.639450e+00 6.503000e+00 8.37070e-01 38.3033800
3   X40 2.29210e+00 2.757720e+00 5.435720e+00 1.72730e-01 52.4143100
4   X50 1.54230e+00 6.169000e+00 6.952900e+00 0.00000e+00 66.7947000
5   X60 1.68590e+00 4.946000e+00 9.901300e+00 1.70760e+00 64.7840000
6   X70 2.24270e+00 3.252900e+00 2.186060e+01 1.60280e+00 56.7381000
7   X80 7.65630e-13 3.789400e+00 1.925900e+01 7.62160e-01 62.2970000
8   X90 3.09235e+00 1.867460e+00 1.326717e+01 4.39990e-01 52.9374400
9  X100 2.28680e+00 3.274170e+00 9.502640e+00 1.51760e-01 50.8769100
10 X110 3.51698e-01 2.187736e+00 7.965044e+00 3.11580e-02 46.7098570
11 X120 1.35180e-14 1.106200e+00 2.799100e+00 4.28500e-01 36.0830000
12 X130 8.14080e-01 6.478100e-01 8.532200e-01 1.90280e-01 19.3702300
13 X140 2.33873e-01 3.533400e-02 4.561980e-01 2.13488e-01  8.7317510
14 X150 8.55910e-12 5.431500e-02 1.675000e-01 1.28550e-01  4.4138000
15 X160 3.59020e-11 3.027500e-03 7.088500e-02 5.45780e-03  2.9042000
16 X170 1.01000e-21 2.112200e-18 1.774500e-20 2.91800e-03  0.3114900
17 X180 1.46200e-03 0.000000e+00 9.351500e-13 8.51970e-04  0.0957340
18 X190 5.87010e-10 2.854100e-06 0.000000e+00 9.77520e-07  0.0012137
19 X200 0.00000e+00 0.000000e+00 0.000000e+00 0.00000e+00  0.0000000
20   md 0.00000e+00 0.000000e+00 0.000000e+00 0.00000e+00  0.0000000
21   tc 0.00000e+00 0.000000e+00 0.000000e+00 0.00000e+00  0.0000000
22 md90 0.00000e+00 0.000000e+00 0.000000e+00 0.00000e+00  0.0000000

The last 4 rows are incorrect. The X200 variable contains only zeros. If I remove it from "nam" I get:

    VAR         GEN          OPR          RAN         ROW     Residual
1   X20 3.00653e+00 7.343150e+00 1.244540e+00 9.81330e-01   47.4381400
2   X30 3.01214e+00 3.639450e+00 6.503000e+00 8.37070e-01   38.3033800
3   X40 2.29210e+00 2.757720e+00 5.435720e+00 1.72730e-01   52.4143100
4   X50 1.54230e+00 6.169000e+00 6.952900e+00 0.00000e+00   66.7947000
5   X60 1.68590e+00 4.946000e+00 9.901300e+00 1.70760e+00   64.7840000
6   X70 2.24270e+00 3.252900e+00 2.186060e+01 1.60280e+00   56.7381000
7   X80 7.65630e-13 3.789400e+00 1.925900e+01 7.62160e-01   62.2970000
8   X90 3.09235e+00 1.867460e+00 1.326717e+01 4.39990e-01   52.9374400
9  X100 2.28680e+00 3.274170e+00 9.502640e+00 1.51760e-01   50.8769100
10 X110 3.51698e-01 2.187736e+00 7.965044e+00 3.11580e-02   46.7098570
11 X120 1.35180e-14 1.106200e+00 2.799100e+00 4.28500e-01   36.0830000
12 X130 8.14080e-01 6.478100e-01 8.532200e-01 1.90280e-01   19.3702300
13 X140 2.33873e-01 3.533400e-02 4.561980e-01 2.13488e-01    8.7317510
14 X150 8.55910e-12 5.431500e-02 1.675000e-01 1.28550e-01    4.4138000
15 X160 3.59020e-11 3.027500e-03 7.088500e-02 5.45780e-03    2.9042000
16 X170 1.01000e-21 2.112200e-18 1.774500e-20 2.91800e-03    0.3114900
17 X180 1.46200e-03 0.000000e+00 9.351500e-13 8.51970e-04    0.0957340
18 X190 5.87010e-10 2.854100e-06 0.000000e+00 9.77520e-07    0.0012137
19   md 2.89880e+01 8.265300e-07 5.343600e+02 8.63220e+00  434.2600000
20   tc 2.51227e+02 4.455530e+02 6.241890e+02 1.82090e+01 2102.9840000
21 md90 1.66229e+01 4.896000e+00 3.070164e+02 5.29350e+00  307.7217000

If I run lmer on X200 alone I get:
> r.mer<-lmer(X200 ~ (1|GEN) + (1|OPR) + (1|ROW) + (1|RAN), data=r)
Error in mer_finalize(ans) : Downdated X'X is not positive definite, 1.

The problem occurs when I try to fit the model to a vector of zeros. The easiest thing is to scrub that variable from my dataframe. However, I don't understand why my loop doesn't function correctly for the four iterations following the error, nor why it returns the zero as the output for variance. Would it help if I could suppress the error message from lmer?

Thanks,
Anton

Dr Anton Wasson, BSc(Hons), LLB(Hons), PhD

	[[alternative HTML version deleted]]

