[R] Multiple lmer runs using 2 'for' loops

A Singh Aditi.Singh at bristol.ac.uk
Tue Jul 28 19:32:48 CEST 2009


Dear Mark and the R list,

I apologise greatly for not having thanked you earlier for your help with 
my last mail, which involved trying to sort out variance components using 
two methods: a direct looped lmer, and a two-step analysis involving 
regressions of AFLP markers on lmer residuals for the same result (or so I 
hoped).

I was in India for ten days and no CRAN mirror in the vicinity made it very 
difficult to be able to work with R (the closest Thai and Singaporean ones 
not working too efficiently at that time, though I'm not sure how this 
normally works).

The code for isolating residuals from an lmer() and then regressing them on 
marker data did work (I got intercept values for each marker), but I have 
been advised to use that as an alternative corroborative approach for 
later, and stick to the original looped lmer() as my principal approach.

For that, the code that I was using, with some fresh modifications, is as 
follows:

> vc<-read.table("E:\\R\\Text Files\\Testvcomp10.txt", header=T, sep="", 
dec=".", na.strings=NA, strip.white=T)
> attach(vc)

> colms<-(vc)[4:13]

> family<-factor(family)


> for (c in 1:levels(family))
+ {
+ print("Family")
+ print(c)
+ {
+ for (i in 1:length(colms))
+ {
+ print(anova(fit<-lmer(peg.no~1 + (1|c/i), na.action=na.exclude, vcdf)))
+
+ }}}


..and its giving me the following copious errors:

[1] "Family"
[1] 1
Error in model.frame.default(data = vcdf, na.action = na.exclude, formula = 
peg.no ~  :
  variable lengths differ (found for 'c')
In addition: Warning message:
In 1:levels(family) :
  numerical expression has 60 elements: only the first used

Error in anova(fit <- lmer(peg.no ~ 1 + (1 | c), na.action = na.exclude,  :
  error in evaluating the argument 'object' in selecting a method for 
function 'anova'
Error in print(anova(fit <- lmer(peg.no ~ 1 + (1 | c), na.action = 
na.exclude,  :
  error in evaluating the argument 'x' in selecting a method for function 
'print'
>




I then tried to use a slightly different method using suggestions made by 
Mark (albeit those were for the other model), which was as follows:

> fit<-lmer(peg.no~1 + (1|family/colms), na.action=na.exclude, vcdf)

Error in model.frame.default(data = vcdf, na.action = na.exclude, formula = 
peg.no ~  :
  invalid type (list) for variable 'colms'

## this I understand, because 'colms' is actually a number of marker 
columns.



> for (c in 1:levels(family))
+ {
+ print ("Family")
+ print(c)
+ {
+ for (i in 1:length(colms))
+ {
+ print ("Marker")
+ print(i)
+
+ print(anova(fit))
+ }}}
[1] "Family"
[1] 1
[1] "Marker"
[1] 1
Error: object 'fit' not found
In addition: Warning message:
In 1:levels(family) :
  numerical expression has 60 elements: only the first used
Error in anova(fit) :
  error in evaluating the argument 'object' in selecting a method for 
function 'anova'
Error in print(anova(fit)) :
  error in evaluating the argument 'x' in selecting a method for function 
'print'
>



All I need to do is loop such that lmer runs once for each marker, for each 
family. For this data set it means 10 markers * 60 families.
['colms' contains the 10 columns of markers, and factor 'family' has 60 
levels].

I need estimates of how much variance is explained by each marker in each 
family, which is the end point of this exercise- and the significance of 
variance explained by each marker- which is why am trying to print 'anova' 
results for each run of the lmer.




Files:


<http://www.filesanywhere.com/fs/v.aspx?v=896d6b88616173be71ab> (excel-
.xlsx)

<http://www.filesanywhere.com/fs/v.aspx?v=896d6b88616174a76e9e>
(.txt file)


Apologies for repetitive mails, but it won't work.


Thank you very much again,

Aditi




----------------------
A Singh
Aditi.Singh at bristol.ac.uk
School of Biological Sciences
University of Bristol




More information about the R-help mailing list