1

i need help in the following problem.

I generated a list containing 1000 comparative.dataand i want to run 1000 pgls using each of these comparative.data. I tried to use lapply function for this, using the following code:

pg <- lapply(obj, function(z){pgls(formula = y ~ x, cd[[z]], lambda = "ML")})

obj is a list of 1000 data.frames with my data. cd is my list of 1000 comparative.data.

When i tried to run this code the followin error returned:

 Error in pgls(formula = y ~ x, cd[[z]], lambda = "ML") : 
  object 'z' not found

I can not see where is the error's source

Thanks in advance


More informations

obj is used to generate the comparative.data. To generate the 1000 comparative.data using the 1000 data frames in obj list, i used:

cd <- lapply(1:1000, function(x) comparative.data(phy = phylogeny, 
                                            data = as.data.frame(obj[[x]]), 
                                            names.col = species_name,
                                            vcv=T, vcv.dim=3))

To run one pgls for the hundredth comparative.data the code is:

mod <- pgls(formula = y ~ x, cd[[100]], lambda = "ML")

Calling the hundredth obj and hundredth cd

obj[[100]]
# A tibble: 136 x 3
# Groups:   Binomial, herbivores [136]
   Binomial                            herbivores      tm
 * <chr>                                    <dbl>   <dbl>
 1 Abies_alba                                 30. 0.896  
 2 Abies_balsamea                              2. 0.990  
 3 Abies_borisii-regis                         1. 0.940  
 4 Alcea_rosea                                 7. 0.972  
 5 Amaranthus_caudatus                         1. 0.173  
 6 Amaranthus_hybridus_subsp._cruentus         1. 0.310  
 7 Aquilegia_vulgaris                          9. 0.365  
 8 Arabidopsis_thaliana                        8. 0.00280
 9 Arabis_alpina                               2. 0.978  
10 Ariocarpus_fissuratus                       1. 0.930  
# ... with 126 more rows


cd[[100]]
Comparative dataset of 136 taxa:
Phylogeny: tree 
   136 tips, 134 internal nodes
   chr [1:136] "Mercurialis_annua" "Manihot_esculenta" 
"Malpighia_emarginata" "Comarum_palustre" ...
VCV matrix present:
   VCV.array [1:136, 1:136, 1:16] 61.9 189.3 189.3 189.3 189.3 ...
Data: as.data.frame(obj[[x]]) 
   $ herbivores: num [1:136] 4 1 1 5 19 21 7 4 4 2 ...
   $ tm        : num [1:136] 0.516 0.915 1.013 0.46 0.236 ...
tales_alencar
  • 141
  • 1
  • 10

1 Answers1

0

Since cd was created from obj, there is no need to reference obj in lapply call but simply pass your list of comparative.data which you can do by object:

# BELOW d IS DATA FRAME OBJECT PASSED INTO LAPPLY LOOP
pg_list <- lapply(cd, function(d) pgls(formula = y ~ x, d, lambda = "ML"))

Or by index:

# BELOW i IS INTEGER VALUE PASSED INTO LAPPLY LOOP
pg_list <- lapply(seq_along(cd), function(i) pgls(formula = y ~ x, cd[[i]], lambda = "ML"))

Alternatively, you can combine both lapply calls, assuming you do not need the intermediate object, cd list, for other purposes:

# BELOW x IS OBJECT PASSED INTO LAPPLY LOOP
pg_list  <- lapply(obj, function(x) {    
                   cd <- comparative.data(phy = phylogeny, 
                                          data = as.data.frame(x), 
                                          names.col = species_name,
                                          vcv=T, vcv.dim=3))

                   pgls(formula = y ~ x, cd, lambda = "ML")
            })
Parfait
  • 104,375
  • 17
  • 94
  • 125