TiagoOlivoto / metan

Package for multi-environment trial analysis
https://tiagoolivoto.github.io/metan/
GNU General Public License v3.0
35 stars 17 forks source link

Custom names of function arguments do not work #2

Closed myaseen208 closed 4 years ago

myaseen208 commented 4 years ago

Some functions argument do not take custom names as given in the following example

library(metan)
data("data_ge")
data_ge
# A tibble: 420 x 5
   ENV   GEN   REP      GY    HM
   <fct> <fct> <fct> <dbl> <dbl>
 1 E1    G1    1      2.17  44.9
 2 E1    G1    2      2.50  46.9
 3 E1    G1    3      2.43  47.8
 4 E1    G2    1      3.21  45.2
 5 E1    G2    2      2.93  45.3
 6 E1    G2    3      2.56  45.5
 7 E1    G3    1      2.77  46.7
 8 E1    G3    2      3.62  43.2
 9 E1    G3    3      2.28  47.8
10 E1    G4    1      2.36  47.9
# … with 410 more rows
> 
> anova_joint(
+     .data   = data_ge
+   , env     = ENV
+   , gen     = GEN
+   , rep     = REP
+   , resp    = GY
+   )
variable GY 
---------------------------------------------------------------------------
Joint ANOVA table
---------------------------------------------------------------------------
    Source     Df Sum Sq Mean Sq F value   Pr(>F)
       ENV  13.00 279.57 21.5057   62.33 0.00e+00
  REP(ENV)  28.00   9.66  0.3451    3.57 3.59e-08
       GEN   9.00  13.00  1.4439   14.93 2.19e-19
   GEN:ENV 117.00  31.22  0.2668    2.76 1.01e-11
 Residuals 252.00  24.37  0.0967      NA       NA
     CV(%)  11.63     NA      NA      NA       NA
 MSR+/MSR-   6.71     NA      NA      NA       NA
    OVmean   2.67     NA      NA      NA       NA
---------------------------------------------------------------------------

All variables with significant (p < 0.05) genotype-vs-environment interaction
Done!
Variable GY 
---------------------------------------------------------------------------
$anova
     Source         Df     Sum Sq     Mean Sq   F value
1       ENV  13.000000 279.573552 21.50565785 62.325457
2  REP(ENV)  28.000000   9.661516  0.34505416  3.568548
3       GEN   9.000000  12.995044  1.44389374 14.932741
4   GEN:ENV 117.000000  31.219565  0.26683389  2.759595
5 Residuals 252.000000  24.366674  0.09669315        NA
6     CV(%)  11.627790         NA          NA        NA
7 MSR+/MSR-   6.708789         NA          NA        NA
8    OVmean   2.674242         NA          NA        NA
        Pr(>F)
1 0.000000e+00
2 3.593191e-08
3 2.190118e-19
4 1.005191e-11
5           NA
6           NA
7           NA
8           NA

$model
Call:
   aov(formula = mean ~ GEN + ENV + GEN:ENV + ENV/REP, data = data)

Terms:
                      GEN       ENV   GEN:ENV   ENV:REP
Sum of Squares   12.99504 279.57355  31.21956   9.66152
Deg. of Freedom         9        13       117        28
                Residuals
Sum of Squares   24.36667
Deg. of Freedom       252

Residual standard error: 0.3109552
Estimated effects may be unbalanced

$augment
# A tibble: 420 x 11
   ENV   GEN   REP    mean   hat sigma fitted    resid
   <fct> <fct> <fct> <dbl> <dbl> <dbl>  <dbl>    <dbl>
 1 E1    G1    1      2.17 0.4   0.311   2.42 -0.255  
 2 E1    G1    2      2.50 0.400 0.311   2.40  0.101  
 3 E1    G1    3      2.43 0.4   0.311   2.27  0.154  
 4 E1    G2    1      3.21 0.400 0.311   2.96  0.249  
 5 E1    G2    2      2.93 0.400 0.312   2.94 -0.00492
 6 E1    G2    3      2.56 0.400 0.311   2.81 -0.244  
 7 E1    G3    1      2.77 0.4   0.311   2.95 -0.176  
 8 E1    G3    2      3.62 0.400 0.306   2.92  0.696  
 9 E1    G3    3      2.28 0.4   0.309   2.80 -0.521  
10 E1    G4    1      2.36 0.4   0.311   2.65 -0.286  
# … with 410 more rows, and 3 more variables: stdres <dbl>,
#   se.fit <dbl>, factors <chr>

$details
# A tibble: 10 x 2
   Parameters mean               
   <chr>      <chr>              
 1 Mean       "2.67"             
 2 SE         "NA"               
 3 SD         "NA"               
 4 CV         "NA"               
 5 Min        "0.67 (G10 in E11)"
 6 Max        "5.09 (G8 in E5)"  
 7 MinENV     "E11 (1.37)"       
 8 MaxENV     "E3 (4.06)"        
 9 MinGEN     "G10 (2.47) "      
10 MaxGEN     "G8 (3) "          

---------------------------------------------------------------------------

> 
> library(tidyverse)
> tb1 <-
+   data_ge %>% 
+   mutate(
+       Env     = ENV
+     , Gen     = GEN
+     , Rep     = REP
+   ) %>% 
+   select(Env, Gen, Rep, GY)
> 
> tb1
# A tibble: 420 x 4
   Env   Gen   Rep      GY
   <fct> <fct> <fct> <dbl>
 1 E1    G1    1      2.17
 2 E1    G1    2      2.50
 3 E1    G1    3      2.43
 4 E1    G2    1      3.21
 5 E1    G2    2      2.93
 6 E1    G2    3      2.56
 7 E1    G3    1      2.77
 8 E1    G3    2      3.62
 9 E1    G3    3      2.28
10 E1    G4    1      2.36
# … with 410 more rows
> 
> anova_joint(
+    .data   = tb1
+   , env     = Env
+   , gen     = Gen
+   , rep     = Rep
+   , resp    = GY
+ )
Error: Can't subset columns that don't exist.
x The columns `ENV`, `GEN`, and `REP` don't exist.
Run `rlang::last_error()` to see where the error occurred.
TiagoOlivoto commented 4 years ago

Dear @myaseen208 , thank you so much for your comment. This bug was fixed in the development version some days ago and will be implemented in the next release to CRAN soon. Functions can now have custom names rather than ENV, GEN, and REP only. Just install metan from github with devtools to see the changes.

devtools::install_github("TiagoOlivoto/metan")
library(metan)

Here we use metan's function colnames_to_title() to create custom names

tb1 <- 
  data_ge %>% 
colnames_to_title()
tb1
#> # A tibble: 420 x 5
#>    Env   Gen   Rep      Gy    Hm
#>    <fct> <fct> <fct> <dbl> <dbl>
#>  1 E1    G1    1      2.17  44.9
#>  2 E1    G1    2      2.50  46.9
#>  3 E1    G1    3      2.43  47.8
#>  4 E1    G2    1      3.21  45.2
#>  5 E1    G2    2      2.93  45.3
#>  6 E1    G2    3      2.56  45.5
#>  7 E1    G3    1      2.77  46.7
#>  8 E1    G3    2      3.62  43.2
#>  9 E1    G3    3      2.28  47.8
#> 10 E1    G4    1      2.36  47.9
#> # ... with 410 more rows

anova_joint(
  .data   = tb1
  , env     = Env
  , gen     = Gen
  , rep     = Rep
  , resp    = Gy
)
#> variable Gy 
#> ---------------------------------------------------------------------------
#> Joint ANOVA table
#> ---------------------------------------------------------------------------
#>     Source     Df Sum Sq Mean Sq F value   Pr(>F)
#>        ENV  13.00 279.57 21.5057   62.33 0.00e+00
#>   REP(ENV)  28.00   9.66  0.3451    3.57 3.59e-08
#>        GEN   9.00  13.00  1.4439   14.93 2.19e-19
#>    GEN:ENV 117.00  31.22  0.2668    2.76 1.01e-11
#>  Residuals 252.00  24.37  0.0967      NA       NA
#>      CV(%)  11.63     NA      NA      NA       NA
#>  MSR+/MSR-   6.71     NA      NA      NA       NA
#>     OVmean   2.67     NA      NA      NA       NA
#> ---------------------------------------------------------------------------
#> 
#> All variables with significant (p < 0.05) genotype-vs-environment interaction
#> Done!
#> Variable Gy 
#> ---------------------------------------------------------------------------
#> $anova
#>      Source         Df     Sum Sq     Mean Sq   F value       Pr(>F)
#> 1       ENV  13.000000 279.573552 21.50565785 62.325457 0.000000e+00
#> 2  REP(ENV)  28.000000   9.661516  0.34505416  3.568548 3.593191e-08
#> 3       GEN   9.000000  12.995044  1.44389374 14.932741 2.190118e-19
#> 4   GEN:ENV 117.000000  31.219565  0.26683389  2.759595 1.005191e-11
#> 5 Residuals 252.000000  24.366674  0.09669315        NA           NA
#> 6     CV(%)  11.627790         NA          NA        NA           NA
#> 7 MSR+/MSR-   6.708789         NA          NA        NA           NA
#> 8    OVmean   2.674242         NA          NA        NA           NA
#> 
#> $model
#> Call:
#>    aov(formula = mean ~ GEN + ENV + GEN:ENV + ENV/REP, data = data)
#> 
#> Terms:
#>                       GEN       ENV   GEN:ENV   ENV:REP Residuals
#> Sum of Squares   12.99504 279.57355  31.21956   9.66152  24.36667
#> Deg. of Freedom         9        13       117        28       252
#> 
#> Residual standard error: 0.3109552
#> Estimated effects may be unbalanced
#> 
#> $augment
#> # A tibble: 420 x 11
#>    ENV   GEN   REP    mean   hat sigma fitted    resid  stdres se.fit factors
#>    <fct> <fct> <fct> <dbl> <dbl> <dbl>  <dbl>    <dbl>   <dbl>  <dbl> <chr>  
#>  1 E1    G1    1      2.17 0.4   0.311   2.42 -0.255   -1.06    0.197 G1_1   
#>  2 E1    G1    2      2.50 0.400 0.311   2.40  0.101    0.420   0.197 G1_2   
#>  3 E1    G1    3      2.43 0.4   0.311   2.27  0.154    0.640   0.197 G1_3   
#>  4 E1    G2    1      3.21 0.400 0.311   2.96  0.249    1.04    0.197 G2_1   
#>  5 E1    G2    2      2.93 0.400 0.312   2.94 -0.00492 -0.0204  0.197 G2_2   
#>  6 E1    G2    3      2.56 0.400 0.311   2.81 -0.244   -1.01    0.197 G2_3   
#>  7 E1    G3    1      2.77 0.4   0.311   2.95 -0.176   -0.729   0.197 G3_1   
#>  8 E1    G3    2      3.62 0.400 0.306   2.92  0.696    2.89    0.197 G3_2   
#>  9 E1    G3    3      2.28 0.4   0.309   2.80 -0.521   -2.16    0.197 G3_3   
#> 10 E1    G4    1      2.36 0.4   0.311   2.65 -0.286   -1.19    0.197 G4_1   
#> # ... with 410 more rows
#> 
#> $details
#> # A tibble: 10 x 2
#>    Parameters mean               
#>    <chr>      <chr>              
#>  1 Mean       "2.67"             
#>  2 SE         "0.05"             
#>  3 SD         "0.92"             
#>  4 CV         "34.56"            
#>  5 Min        "0.67 (G10 in E11)"
#>  6 Max        "5.09 (G8 in E5)"  
#>  7 MinENV     "E11 (1.37)"       
#>  8 MaxENV     "E3 (4.06)"        
#>  9 MinGEN     "G10 (2.47) "      
#> 10 MaxGEN     "G8 (3) "          
#> 
#> ---------------------------------------------------------------------------

ge_winners(
  .data = tb1
  , env = Env
  , gen = Gen
  , resp = everything()
)
#> # A tibble: 14 x 3
#>    ENV   Gy    Hm   
#>    <fct> <chr> <chr>
#>  1 E1    G2    G5   
#>  2 E10   G8    G5   
#>  3 E11   G8    G6   
#>  4 E12   G8    G6   
#>  5 E13   G8    G8   
#>  6 E14   G3    G7   
#>  7 E2    G3    G5   
#>  8 E3    G2    G10  
#>  9 E4    G10   G10  
#> 10 E5    G8    G10  
#> 11 E6    G3    G4   
#> 12 E7    G7    G8   
#> 13 E8    G8    G8   
#> 14 E9    G4    G10

I'm closing this issue now. Fell free to open another issue if you encounter a clear bug. I'll try to fix it as soon as possible. Regards,