我有一个来自4个参与者(通过id
识别)的数据框,其中分别测量了三个不同时间的变量a
和b
:
pre
post1
post2
我需要计算以下两者之间的变化的效果大小(科恩d):
pre
至post1
post1
至post2
在每个变量(a
和b
)上。
有没有一种方法可以使用purrr::map
进行计算?
数据:
library(tidyverse)
set.seed(1234)
df <- data.frame(
vars = rep(letters[1:2], 12),
value = sample(1:100, 24),
phase = rep(factor(c("pre","post1","post2"), levels = c("pre","post1","post2")),4)
)
在我的预期输出中,我先计算百分比变化,然后求出平均值。最后,我的最后一列d
是效果大小。生成它的代码:
library(effsize)
## Calculate Effect Size for `a` in `pre` - `post1`
a1 <- df %>%
filter(vars == "a" & phase != "post2") %>%
mutate(phase = fct_drop(phase)) %>%
cohen.d(value ~ phase, data = ., paired = T) %>%
.$estimate
## Calculate Effect Size for `a` in `post1` - `post2`
a2 <- df %>%
filter(vars == "a" & phase != "pre") %>%
mutate(phase = fct_drop(phase)) %>%
cohen.d(value ~ phase, data = ., paired = T) %>%
.$estimate
## Calculate Effect Size for `b` in `pre` - `post1`
b1 <- df %>%
filter(vars == "b" & phase != "post2") %>%
mutate(phase = fct_drop(phase)) %>%
cohen.d(value ~ phase, data = ., paired = T) %>%
.$estimate
## Calculate Effect Size for `b` in `post1` - `post2`
b2 <- df %>%
filter(vars == "b" & phase != "pre") %>%
mutate(phase = fct_drop(phase)) %>%
cohen.d(value ~ phase, data = ., paired = T) %>%
.$estimate
# Mutate eff sizes to data frame
df %>%
spread(phase, value) %>%
mutate("pre - post1" = (post1/pre-1)*100, "post1 - post2" = (post2/post1-1)*100) %>% # calculate % change
select(-pre, -post1, -post2) %>%
gather("phase", "pct_change", 3:4) %>%
group_by(phase, vars) %>%
summarise(Mean_pct_change = mean(pct_change)) %>% # calculate mean % change
ungroup() %>%
mutate(d = c(a2, b2, a1, b1)) # mutate effect sizes
# A tibble: 4 x 4
phase vars Mean_pct_change d
<chr> <fct> <dbl> <dbl>
1 post1 - post2 a 158. -0.988
2 post1 - post2 b -37.1 0.558
3 pre - post1 a 690. -3.36
4 pre - post1 b 11.6 -0.383
答案 0 :(得分:4)
这是使用purrr
的一种方法。看起来您问题的症结在于仅获取当前正在保存为单个向量的效果大小估计向量,因此,我仅着眼于获取该向量(即您使用.$estimate
提取的向量)。
我保存了两个向量,您正在使用的变量之一:"a" "a" "b" "b"
以及要排除的阶段之一:"post2" "pre" "post2" "pre"
通过purrr::map2_*
函数,您可以沿这两个向量进行映射,并且对于每次迭代,请保留变量并排除相位。计算是您已经拥有的(除了将.$estimate
换成更整洁的[[
之外)。如果使用map2
,我将得到一个长度为1的数字矢量列表。但是,使用map2_dbl
,您将得到一个包含所有返回值的数字矢量。
library(tidyverse)
library(effsize)
vars <- rep(c("a", "b"), each = 2)
excl_phase <- rep(c("post2", "pre"), times = 2)
map2_dbl(vars, excl_phase, function(v, p) {
df %>%
filter(vars == v, phase != p) %>%
mutate(phase = fct_drop(phase)) %>%
cohen.d(value ~ phase, data = ., paired = T) %>%
`[[`("estimate")
})
#> [1] -1.5663301 0.2495671 -0.2716963 0.0659341
保存该矢量,并像以前一样使用它。
答案 1 :(得分:3)
如果您可以满足基数R的要求,这是一个解决方案。
Error: [$compile:ctreq] Controller 'modalController', required by directive 'modalDirective', can't be found!