如何解决如何在成对的参与者中获得四种可能结果之一的观察频率?
我有一个数据集,其中包含参与者对二元(例如,正确或不正确)问题(干预前后)的第一个和第二个答案。不同意第一个答案的参与者在获得第二个答案之前被分组。使用 R,我需要为仅配对的参与者计算以下结果的频率。
相关变量是
- 组号。这分配给个人和配对。因此,只有重复的组号才代表对。
- 第一个和第二个答案(针对每位参与者)。
组 | 第一个 | 第二个 | 条件 |
---|---|---|---|
2 | 0 | 0 | 单人 |
3 | 0 | 0 | 配对 |
3 | 1 | 0 | 配对 |
4 | 0 | 0 | 单人 |
5 | 0 | 1 | 配对 |
... | ... | ... | ... |
我的第一次尝试是获得每个参与者答案的描述。
describe(data$condition=="pair" & data$first.answer==0 & data$second.answer==1
describe(data$condition=="pair" & data$first.answer==1 & data$second.answer==0)
describe(data$condition=="pair" & data$first.answer==0 & data$second.answer==0)
describe(data$condition=="pair" & data$first.answer==1 & data$second.answer==1)
但是当需要将这种分析应用于组时,我陷入了困境。
如何分析每个组(在 R 中)以确定上述百分比?
解决方法
library(dplyr)
my_data %>%
filter(Condition == "Pair") %>%
count(Grp,`1st`,`2nd`) %>%
group_by(Grp) %>%
mutate(share = n / sum(n)) %>%
ungroup()
# Grp `1st` `2nd` n share
# <int> <int> <int> <int> <dbl>
#1 3 0 0 1 0.5
#2 3 1 0 1 0.5
#3 5 0 1 1 1
,
铸造和绘制数据似乎有效。类似于以下内容:
widerData <- data %>%
select(-participant) %>%
pivot_wider(names_from = id_in_group,values_from = c(first_answer,second_answer)) %>%
mutate(
typology = case_when(
treatment %in% treatments &
second_answer_1 + second_answer_2 == 2 ~ 'Both changed to correct',treatment %in% treatments &
second_answer_1 + second_answer_2 == 0 ~ 'Both changed to incorrect',treatment %in% treatments &
second_answer_1 == 0 &
second_answer_2 == 1 ~ 'Both kept old positions',treatment %in% treatments &
second_answer_1 == 1 & second_answer_2 == 0 ~ 'Position interchange',!(treatment %in% treatments) &
first_answer_1 == second_answer_1 ~ 'Single old position kept',!(treatment %in% chat_treatments) &
first_answer_1 != second_answer_1 ~ 'Single position changed'
)
)
bar_fun <- function(df) {
df %>%
group_by(treatment,typology) %>%
tally() %>%
group_by(treatment) %>%
mutate(freq = n / sum(n)) %>%
ggplot(aes(x = typology,y = freq,fill=freq)) + geom_bar(stat = 'identity',show.legend = FALSE) +
facet_wrap(~ treatment) +
theme(axis.text.x = element_text(angle = 90)) +
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
geom_text(
aes(label = percent_format(accuracy = 1)(freq)),position = position_dodge(width = 0.9),vjust = -0.25
)+
ylab('Share of participants')+
ylim(0,1)
}
bar_fun(widerData%>% filter((treatment %in% treatments)))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。