Change Item-level analysis to participant-level analysis

Viewed 49

So here's a reproducible sample of my dataset.

set.seed(42)
n <- 100
dat <- data.frame(Participant=1:20, 
                  Environment=rep(LETTERS[1:2], n/2),
                  Condition=rep(LETTERS[25:26], n/2),
                  Gate= sample(1:5, n, replace=TRUE),
                  Block = sample(1:2, n, replace=TRUE),
                  Sound=rep(LETTERS[3:4], n/2),
                  Correct=sample(0:1, n, replace=TRUE)
)

When I calculate the standard error of this table,

dat %>% 
  group_by(Condition, Gate, Sound) %>% 
  summarize(Accuracy = mean(Correct),
            se = sd(Correct)/sqrt(length(Correct)))

it analyzes at the item-level(trial). I was hoping to transform the dataset to a more condensed version, where it groups data for each participant. For example, I would want to be able to see each participant's individual performance on Gate 1 in Condition Y with C sound. Here is an example to make it less abstract:

enter image description here

Here is an example of the code I use for graphs:

dat %>% 
  group_by(Condition, Gate, Sound) %>% 
  summarize(Accuracy = mean(Correct),
            se = sd(Correct)/sqrt(length(Correct)))%>%
  ggplot(aes(x = Gate, y = Accuracy, color = Sound, group = Sound)) + 
  geom_line() + 
  geom_errorbar(aes(ymin = Accuracy - se, ymax = Accuracy + se), color = "Black", size = .15, width = .3) +
  geom_point(size = 2)+ 
  scale_y_continuous(labels = scales::percent)+ 
  theme_minimal()+
  scale_color_brewer(palette = "Set1")

and I get a graph like this:

enter image description here

If I group by participant with this code:

dat %>% 
  group_by(Condition, Gate, Sound,Participant) %>% 
  summarize(Accuracy = mean(Correct),
            se = sd(Correct)/sqrt(length(Correct)))%>%
  ggplot(aes(x = Gate, y = Accuracy, color = Sound, group = Sound)) + 
  geom_line() + 
  geom_errorbar(aes(ymin = Accuracy - se, ymax = Accuracy + se), color = "Black", size = .15, width = .3) +
  geom_point(size = 2)+ 
  scale_y_continuous(labels = scales::percent)+ 
  theme_minimal()+
  scale_color_brewer(palette = "Set1")

I get a graph like this:

enter image description here

Which does not work. So I think I need to create a new dataset in R like the sample above(image 1), before conducting analysis. Maybe I could use the subset function to accomplish this somehow, like this:

participant_dat = subset(dat, ...) #Not sure where to go from here.
1 Answers

To get what I wanted, I grouped my data by participant using this code:

Participant_Data<- dat%>% 
  group_by(Condition, Gate, Sound, **Participant**) %>% 
  summarize(Accuracy = mean(Correct), 
            se = sd(Correct)/sqrt(length(Correct)))

Then I used the below code to plot a graph based on the condensed dataset.

Participant_Data%>% 
  group_by(Condition, Gate, Sound) %>%
  summarize(Proportion_Correct = mean(**Accuracy**), #finding the mean of each participant's accuracy
            standarderror = sd(Accuracy)/sqrt(length(Accuracy))) %>% #calculating standard error for participant accuracy (participant-level), and not trial accuracy(item-level)
  ggplot(aes(x = Gate, y = Proportion_Correct, color = Sound, group = Sound)) + 
  geom_line() + 
  geom_errorbar(aes(ymin = Proportion_Correct - standarderror, ymax = Proportion_Correct + standarderror), color = "Black", size = .15, width = .3) + #plotting error bars for each participant, not for each item
  geom_point(size = 2)+ 
  scale_y_continuous(labels = scales::percent)+ 
  facet_wrap(~Condition) + 
  theme_minimal() + 
  scale_color_brewer(palette = "Set1")
Related