如何获得成对参与者观察到的四种可能结果之一的频率?

How to get observed frequency of one of four possible outcomes in pairs of participants?

我有一个参与者对二元(例如,正确或不正确)问题(干预之前和之后)的第一个和第二个答案的数据集。不同意第一个答案的参与者在他们的第二个答案之前被分组。使用 R,我需要计算 只有配对的参与者的以下结果的频率。

  1. 错误的改正,正确的保留 他们的回答。
  2. 正确的改为错误的,错误的保留答案
  3. 两人都保留了第一个答案
  4. 双方都改变了他们的第一个答案(即他们交换)

相关变量是

Grp 1st 2nd Condition
2 0 0 Solo
3 0 0 Pair
3 1 0 Pair
4 0 0 Solo
5 0 1 Pair
... ... ... ...

我的第一次尝试是为每个 参与者的 答案获取描述。

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)))

https://rpubs.com/chapkovski/socrates