为数据集中的一组变量计算3组之间的效应大小 [英] Calculating effect sizes between 3 groups for a set of variables in a dataset
本文介绍了为数据集中的一组变量计算3组之间的效应大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想计算3种治疗对3个变量(x1,x2,x3)的作用大小。假设我有以下数据集:
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)
)
现在,我想针对每种治疗组合计算其规模效应。我发现此函数可以计算科恩的d。
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)
}
非常感谢您的帮助。
编辑:
例如,下面我可以计算三种治疗的效果大小(成对):一个变量x1。理想情况下,我想要一种通用的方式来获取数据集中所有变量的成对比较。
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
数据:
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))
这篇关于为数据集中的一组变量计算3组之间的效应大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文