Home > other >  Multiple independent variables to get effect sizses in R
Multiple independent variables to get effect sizses in R

Time:01-24

A sample of the data is as follows:

    data<-read.table (text=" Class  Site    Group   Value
    M   N   KO  20
    N   S   MO  17
    M   N   KO  18
    N   S   MO  19
    M   N   KO  16
    N   S   MO  14
    M   N   KO  13
    N   S   MO  12
    M   S   KO  16
    N   S   KO  15
    M   N   KO  17
    N   S   MO  19
    M   N   KO  15
    N   S   MO  14
    M   N   KO  16
    M   S   MO  15
    M   S   KO  14
    N   S   KO  16
    N   N   KO  13
    N   S   MO  12
    ", header=TRUE)
    cohen.d(data$Value,data$Group)

I want to get the effect sizes for each column using the Value column.

I can get them for each variable:

   

 library(effsize)
    cohen.d(data$Value,data$Group)

but I want to get effect sizes within a loop or perhaps map or using simple codes.

Here is the outcome

Varaiable    d estimate
Class   0.38
Site    0.32
Group   0.21

CodePudding user response:

You can use sapply here (and grep to remove the Value column):

sapply(names(data)[-grep("Value", names(data))], 
       function(x) effsize::cohen.d(data$Value, data[,x]))

Output:

#           Class       Site        Group      
# method     "Cohen's d" "Cohen's d" "Cohen's d"
# name       "d"         "d"         "d"        
# estimate   0.3878974   0.3210194   0.2123977  
# sd         2.320201    2.336308    2.354074   
# conf.int   numeric,2   numeric,2   numeric,2  
# var        0.2037616   0.2109097   0.2094612  
# conf.level 0.95        0.95        0.95       
# magnitude  small       small       small  

You could use other *apply functions here with some slight tweaks, but the output is suboptimal compared to sapply. For posterity some alternative approaches are below in case they fit other cases's needs better:

lapply(data[-grep("Value", names(data))], 
       function(x) effsize::cohen.d(data$Value, x))

apply(data[-grep("Value", names(data))], 2,
      function(x) effsize::cohen.d(data$Value, x))
  •  Tags:  
  • r
  • Related