Home > OS >  Convert a list from data frame (emmGrid class)
Convert a list from data frame (emmGrid class)

Time:10-31

I would like to convert a list to dataframe (picture as below)

I did use do.call(rbind.data.frame, contrast), however, I got this Error in xi[[j]] : this S4 class is not subsettable. I still can read them separately. Anyone know about this thing?

This list I got when running the ART anova test by using the package ARTool

enter image description here

Update

This my orignial code to calculate and get the model done.

Organism_df_posthoc <- bird_metrics_long_new %>% 
  rbind(plant_metrics_long_new) %>% 
  mutate(Type = factor(Type, levels = c("Forest", "Jungle rubber", "Rubber", "Oil palm"))) %>% 
  mutate(Category = factor(Category)) %>% 
  group_by(Category) %>% 
  mutate_at(c("PD"), ~(scale(.) %>% as.vector())) %>% 
  ungroup() %>% 
  nest_by(n1) %>% 
  mutate(fit = list(art.con(art(PD ~ Category   Type   Category:Type, data = data), 
                                  "Category:Type",adjust = "tukey", interaction = T)))

And the output of fit is that I showed already.

CodePudding user response:

With rbind, instead of rbind.data.frame, there is a specific method for 'emmGrid' object and it can directly use the correct method by matching the class if we specify just rbind

do.call(rbind, contrast)

-output

wool tension emmean   SE df lower.CL upper.CL
 A    L         44.6 3.65 48     33.6     55.5
 A    M         24.0 3.65 48     13.0     35.0
 A    H         24.6 3.65 48     13.6     35.5
 B    L         28.2 3.65 48     17.2     39.2
 B    M         28.8 3.65 48     17.8     39.8
 B    H         18.8 3.65 48      7.8     29.8
 A    L         44.6 3.65 48     33.6     55.5
 A    M         24.0 3.65 48     13.0     35.0
 A    H         24.6 3.65 48     13.6     35.5
 B    L         28.2 3.65 48     17.2     39.2
 B    M         28.8 3.65 48     17.8     39.8
 B    H         18.8 3.65 48      7.8     29.8

Confidence level used: 0.95 
Conf-level adjustment: bonferroni method for 12 estimates 

The reason is that there is a specific method for rbind when we load the emmeans

> methods('rbind')
[1] rbind.data.frame  rbind.data.table* rbind.emm_list*   rbind.emmGrid*    rbind.grouped_df* rbind.zoo*  

The structure in the example created matches the OP's structure showed

enter image description here

By using rbind.data.frame, it doesn't match because the class is already emmGrid

data

library(multcomp)
library(emmeans)


warp.lm <- lm(breaks ~ wool*tension, data = warpbreaks)

warp.emmGrid <- emmeans(warp.lm, ~ tension | wool)
contrast <- list(warp.emmGrid, warp.emmGrid)

If the OP used 'ARTool' and if the columns are different, the above solution may not work because rbind requires all objects to have the same column names. We could convert to tibble by looping over the list with map (from purrr) and bind them

library(ARTool)
library(purrr)
library(tibble)
map_dfr(contrast, as_tibble)

-output

# A tibble: 42 × 8
   contrast estimate    SE    df t.ratio  p.value Moisture_pairwise Fertilizer_pairwise
   <chr>       <dbl> <dbl> <dbl>   <dbl>    <dbl> <fct>             <fct>              
 1 m1 - m2    -23.1   4.12  8.00  -5.61  0.00226  NA                NA                 
 2 m1 - m3    -33.8   4.12  8.00  -8.20  0.000169 NA                NA                 
 3 m1 - m4    -15.2   4.12  8.00  -3.68  0.0256   NA                NA                 
 4 m2 - m3    -10.7   4.12  8     -2.59  0.118    NA                NA                 
 5 m2 - m4      7.92  4.12  8      1.92  0.291    NA                NA                 
 6 m3 - m4     18.6   4.12  8      4.51  0.00849  NA                NA                 
 7 NA           6.83 10.9  24      0.625 0.538    m1 - m2           f1 - f2            
 8 NA          15.3  10.9  24      1.40  0.174    m1 - m3           f1 - f2            
 9 NA          -5.83 10.9  24     -0.533 0.599    m1 - m4           f1 - f2            
10 NA           8.50 10.9  24      0.777 0.445    m2 - m3           f1 - f2            
# … with 32 more rows

data

data(Higgins1990Table5, package = "ARTool")
m <- art(DryMatter ~ Moisture*Fertilizer   (1|Tray), data=Higgins1990Table5)
a1 <- art.con(m, ~ Moisture)
a2 <- art.con(m, "Moisture:Fertilizer", interaction = TRUE)
contrast <- list(a1, a2)
  • Related