I have written a very basic function that calculates the Cohen's d effect size from 2 means and 2 standard errors of mean (SEM). It simply takes the difference in the means and divides it by the pooled standard deviations (obtained from the SEM by multiplying it by the square root of n)
effect_size <- function(n, mean1, mean2, sem1, sem2){
a <- diff(c(mean1, mean2))/
((sqrt(n)*sem1)+(sqrt(n)*sem2))/2
return(a)
}
The function works with the following codes:
effect_size(6, 1.40285, 1.439808, 0.750384, 0.73885)
effect_size(6, try$pbs_m[1], try$nm_m[1], try$pbs_sem[1], try$nm_sem[1])
effect_size(6, try$pbs_m[2], try$nm_m[2], try$pbs_sem[2], try$nm_sem[2])
effect_size(6, try$pbs_m[3], try$nm_m[3], try$pbs_sem[3], try$nm_sem[3])
effect_size(6, try$pbs_m[4], try$nm_m[4], try$pbs_sem[4], try$nm_sem[4])
effect_size(6, try$pbs_m[5], try$nm_m[5], try$pbs_sem[5], try$nm_sem[5])
effect_size(6, try$pbs_m[6], try$nm_m[6], try$pbs_sem[6], try$nm_sem[6])
effect_size(6, try$pbs_m[7], try$nm_m[7], try$pbs_sem[7], try$nm_sem[7])
effect_size(6, try$pbs_m[8], try$nm_m[8], try$pbs_sem[8], try$nm_sem[8])
effect_size(6, try$pbs_m[9], try$nm_m[9], try$pbs_sem[9], try$nm_sem[9])
effect_size(6, try$pbs_m[10], try$nm_m[10], try$pbs_sem[10], try$nm_sem[10])
but when I try to add it to supply it fails and a df called try that looks like:
X pbs_m pbs_sem nm_m nm_sem
1 TNFα 1.402850 0.750384 1.439808 0.738850
2 IL6 0.781183 0.951991 2.687149 2.148349
3 IFNγ 1.267959 0.787331 1.630742 1.700653
4 SNAI1 1.328036 0.915758 0.684646 0.713751
5 SNAI2 0.876023 0.946581 0.321736 0.240060
6 SNAI3 1.124685 1.226003 0.706217 0.969728
sapply(try, FUN = effect_size(6, try$pbs_m, try$nm_m, try$pbs_sem, try$nm_sem))
I get this error:
Error in match.fun(FUN) : 'effect_size(6, try$pbs_m, try$nm_m, try$pbs_sem, try$nm_sem)' is not a function, character or symbol In addition: Warning message: In diff(c(mean1, mean2))/((sqrt(n) * sem1) + (sqrt(n) * sem2)) : longer object length is not a multiple of shorter object length
could anyone help, please? the df columns are identical lengths and it seems to run just fine with the [] indexes
You don't need sapply
, it would call the function on each column, not on all the columns. Try instead
try <- read.table(text = " X pbs_m pbs_sem nm_m nm_sem
1 TNFα 1.402850 0.750384 1.439808 0.738850
2 IL6 0.781183 0.951991 2.687149 2.148349
3 IFNγ 1.267959 0.787331 1.630742 1.700653
4 SNAI1 1.328036 0.915758 0.684646 0.713751
5 SNAI2 0.876023 0.946581 0.321736 0.240060
6 SNAI3 1.124685 1.226003 0.706217 0.969728", header = TRUE)
effect_size <- function(n, mean1, mean2, sem1, sem2){
s1 <- sqrt(n)*sem1
s2 <- sqrt(n)*sem2
d <- mean2 - mean1
d/(s1 + s2)/2
}
effect_size(6, try$pbs_m, try$nm_m, try$pbs_sem, try$nm_sem)
#> [1] 0.005065705 0.125487424 0.029764166 -0.080595709 -0.095347590
#> [6] -0.038902499
Created on 2024-04-25 with reprex v2.1.0
Also, the way it is called, diff
doesn't subtract mean1
from mean2
, it combines the vector c(mean1, mean2)
producing a vector twice the number of rows of try
, then computes the differences of each pair of consecutive numbers of this vector.
With the data in the question
c(mean1, mean2)
is a vector of length 12;diff(c(mean1, mean2))
computes 11 differences.This is not what you want.