Using AWK to Calculate Correct Responses

Hello,

I am trying to count how many times a subject makes a correct switch or a correct stay response in a simple task. I have data on which condition they were in (here, labeled "IMAGINE" and "RECALL"), as well as whether they made a left or right button response, and whether the outcome was an error or a correct outcome.

In the IMAGINE condition, I am coding their response as correct if they receive a correct outcome and make the same response on the next trial. I am also coding their response as correct if they receive an error outcome and switch their response on the next trial.

In the RECALL condition, I am coding their response as correct if they make the same response as they did on the previous trial, regardless of whether they received a correct or error outcome on that trial.

Here is a sample of the output I am trying to run through awk:

IMAGINE
left
correct
IMAGINE
left
error
RECALL
left
correct
IMAGINE
right
correct

The data are in groups of three. For example, the first three lines can be thought of as: "Condition = IMAGINE, Response = left, Outcome = correct".

What I want to do is tally how many correct or incorrect switches they made in each condition. In this example, the first response results in a correct outcome, and the subject makes the same response on the next trial, which would be counted as a correct response. On the RECALL trial, the participant also makes a correct response, since the response is the same as the last trial, even though that trial resulted in an error outcome. The last trial is actually an incorrect response, since it is in the IMAGINE condition and the last outcome was correct, and they switched their response.

Is awk the best tool to use for this? My problem is that I need to store the previous response and outcome, and then compare it against the current response and outcome, taking into account which condition it is. Any help would be greatly appreciated.

Thanks,

-Jahn

Can you show us what would be the desired output for this sample data?

Yes; in this example, I would expect the output to be:

IMAGINE condition: totalCorrect = 2, totalIncorrect = 1
RECALL condition: totalCorrect = 1, totalIncorrect = 0

Thanks,

-Jahn

So it is as simple as counting "correct" and "error" words in those two sections? If so, then try this:

awk '{x=$0;getline;getline;if (x=="IMAGINE"){a[$0]++}else{b[$0]++}}END{print "IMAGINE condition: totalCorrect = "a["correct"]", totalIncorect = "a["error"];print "RECALL condition: totalCorrect = "b["correct"]", totalIncorect = "b["error"]}' file

Not quite. "Correct" and "Error" refer to the outcome that they received, not whether they made the right response or not. For example, in the last IMAGINE condition the participant makes an incorrect response; since the last trial resulted in a correct outcome, they should have made the same response.

Let me know if that makes sense!

-Jahn

I think the correct output in this case should be:

IMAGINE condition: totalCorrect = 1 totalIncorect = 1
RECALL condition: totalCorrect = 1 totalIncorect = 0

Because you cannot determine whether first record (IMAGINE) is correct or not, as you don't have previous record to analyze response. Try this script:

#!/usr/bin/perl
open I, "$ARGV[0]";
chomp($prev_trial=<I>);
chomp($prev_resp=<I>);
chomp($prev_out=<I>);
while ($prev_trial){
  chomp($trial=<I>);
  chomp($resp=<I>);
  chomp($out=<I>);
  if ($trial eq "IMAGINE"){
    if (($resp eq $prev_resp && $prev_out eq "correct") || ($resp ne $prev_resp && $prev_out eq "error")){
      $im_corr++;
    } else {
      $im_inco++;
    }
  } elsif ($trial eq "RECALL"){
    if ($resp eq $prev_resp){
      $re_corr++;
    } else {
      $re_inco++;
    }
  }
  $prev_trial=$trial;
  $prev_resp=$resp;
  $prev_out=$out;
} 
print "IMAGINE condition: totalCorrect = ", ($im_corr)?$im_corr:0, " totalIncorect = ", ($im_inco)?$im_inco:0, "\n";
print "RECALL condition: totalCorrect = ", ($re_corr)?$re_corr:0, " totalIncorect = ", ($re_inco)?$re_inco:0, "\n";

Run it like this: ./script.pl your_file