I would like to calculate the effect sizes of 3 treatments on 3 variables (x1, x2, x3). Suppose I have the following dataset:
set.seed(1234)
data <- data.frame(
dose=factor(c(rep(1,25), rep(2,35), rep(3,40)),
labels = c("low", "middle", "high")),
x1 = rnorm(100, 0, 2),
x2 = rnorm(100, 3, 3),
x3 = rnorm(100, 9, 4)
)
Now, I would like to calculate, for each combination of treatments, its effect size. I have found this function to calculate Cohen's d.
cohens_d <- function(x, y) {
lx <- length(x)- 1
ly <- length(y)- 1
md <- abs(mean(x) - mean(y))
csd <- lx * var(x) + ly * var(y)
csd <- csd/(lx + ly)
csd <- sqrt(csd)
cd <- md/csd
# Hedges'g
cd*(1-(3/(4*(length(x)+length(y)-9))))
#print(cd)
}
Thank you very much for your help.
EDIT:
For example, below I can compute the effect size of the three treatments (pairwise) in one variable x1. Ideally, I would like a generalizable way to get these pairwise comparisons for all the variables in my dataset.
cohens_d(data$x1[data$dose=="low"], data$x1[data$dose=="middle"])
cohens_d(data$x1[data$dose=="low"], data$x1[data$dose=="high"])
cohens_d(data$x1[data$dose=="middle"], data$x1[data$dose=="high"])
df1$dose <- as.character(df1$dose) # convert dose from factor to character
selected_cols <- colnames( df1 )[2:4] # select columns prefixed with 'x'
library("reshape2") # load reshape2 library
df1 <- melt( data = df1, id = "dose", measure.vars =selected_cols , value.name = 'value') # melt df1 data frame
# compute cohensD
cohens_df1 <- with(df1, sapply( selected_cols, # loop through column names
function( x ) combn( unique(dose), 2 , # loop through pairs of dose combinations
function( y ) cohens_d( df1[ variable %in% x & dose %in% y[1], 'value' ],
df1[ variable %in% x & dose %in% y[2], 'value' ] ))))
# assign row names
rownames(cohens_df1) <- combn( unique(df1$dose), 2 , function( y ) paste( y, collapse = '_' ) )
cohens_df1
# x1 x2 x3
# low_middle 0.3319591 0.09511378 0.321519422
# low_high 0.4982017 0.03265765 0.337651450
# middle_high 0.8221889 0.10799662 0.006570862
Data:
set.seed(1234)
df1 <- data.frame( dose = factor(c(rep(1,25), rep(2,35), rep(3,40)), levels = c(1, 2, 3), labels = c("low", "middle", "high")),
x1 = rnorm(100, 0, 2),
x2 = rnorm(100, 3, 3),
x3 = rnorm(100, 9, 4))
来源:https://stackoverflow.com/questions/42217598/calculating-effect-sizes-between-3-groups-for-a-set-of-variables-in-a-dataset