0

I'm trying to calculate some statistic called FAR for each group in my data. I wrote a function for calculating the statistic, and it looks like this:

FAR <- function(data){
  FAs = sum(data$response %in% c(0,1) & data$correct_response=="No")
  NF = sum(data$correct_response=="No")
  return(FAs/NF)
}

My data frame is called subjects_no_imagery, and it has a column called random_participant_ID that I'd like to group by. I then want to calculate FAR for each group, like so:

subjects_no_imagery %>% group_by(random_participant_ID) %>% dplyr::summarize(far = FAR())

I keep getting the following error:

subjects_no_imagery %>% group_by(random_participant_ID) %>% dplyr::summarize(far = FAR()) Error: Problem with summarise() input far. x argument "data" is missing, with no default ℹ Input far is FAR(). ℹ The error occured in group 1: random_participant_ID = "2X2DPZG20H". Run rlang::last_error() to see where the error occurred.

If I pass the data frame as an argument:

subjects_no_imagery %>% group_by(random_participant_ID) %>% dplyr::summarize(FAR = FAR(subjects_no_imagery))

then it runs without error, but I get this output, where instead of having a different RAF for each participant, all of the FARs are the same (and equal the the average across all the participants):

random_participant_ID   FAR
   <chr>                 <dbl>
 1 2X2DPZG20H            0.196
 2 4GF789C0F0            0.196
 3 93JKYDEZVY            0.196
 4 9J9QM4C7LV            0.196

The command

subjects_no_imagery %>% group_by(random_participant_ID) %>% dplyr::summarize(FAR = FAR(subjects_no_imagery %>% group_by(random_participant_ID)))

also produces the same result.

Any ideas how I can get the FAR statistic for each group?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213

1 Answers1

0

The problem was that FAR function required a dataset, and summarize only works with functions that just use columns.

Instead, I should have done the following:

subjects_no_imagery_stats <- subjects_no_imagery %>% group_by(random_participant_ID) %>% 
  summarize(FAs = sum(response %in% c(0,1) & correct_response=="No"), NF = sum(correct_response=="No"), FA_rate = FAs/NF)