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

问题描述

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

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

相关变量是

  • 组号。这分配给个人和配对。因此,只有重复的组号才代表对。
  • 一个和第二个答案(针对每位参与者)。
一个 第二个 条件
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)))

Graph of frequencies of four outcomes in one of the two types of pairs

https://rpubs.com/chapkovski/socrates

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...