Introduction

This document analyzes the switching behavior of participants in Stage 1 and Stage 2 of the experiment. We will create two bar graphs: - Figure 1: Bar graph comparing the number of participants switching vs not switching in Stage 1. - Figure 2: Bar graph comparing the number of participants switching vs not switching in Stage 2, broken down by Stage 1 behavior.

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Lab3_cleaned_raw <- read.csv("C:/Users/Dan Haran/Downloads/Lab3_cleaned_raw.csv")  # Correct file path

stage1_tasks <- paste0("task", 1:10, "_choice")
stage2_tasks <- paste0("task", 11:20, "_choice")

Lab3_cleaned_raw$stage1_reverse <- apply(Lab3_cleaned_raw[stage1_tasks], 1, function(x) length(unique(x)) > 1)

Lab3_cleaned_raw$stage2_reverse <- apply(Lab3_cleaned_raw[stage2_tasks], 1, function(x) length(unique(x)) > 1)

find_switch_location <- function(choices) {
  for (i in seq_along(choices)[-length(choices)]) {
    if (choices[i] != choices[i + 1]) return(i + 1)  # Return the row number (1-based)
  }
  return(NA)
}

Lab3_cleaned_raw$stage1_switch_location <- apply(Lab3_cleaned_raw[stage1_tasks], 1, find_switch_location)
Lab3_cleaned_raw$stage2_switch_location <- apply(Lab3_cleaned_raw[stage2_tasks], 1, find_switch_location)

Lab3_cleaned_raw$switch_behavior <- apply(Lab3_cleaned_raw, 1, function(row) {
  if (row['stage1_reverse'] == TRUE && row['stage2_reverse'] == TRUE) {
    return('Switch in Stage 1 and Stage 2')
  } else if (row['stage1_reverse'] == TRUE && row['stage2_reverse'] == FALSE) {
    return('Switch in Stage 1 but not Stage 2')
  } else if (row['stage1_reverse'] == FALSE && row['stage2_reverse'] == TRUE) {
    return('No Switch in Stage 1 but Switch in Stage 2')
  } else {
    return('No Switch in Stage 1 and Stage 2')
  }
})

# Figure 1: Bar Graph comparing the number of participants switching vs not switching in Stage 1
ggplot(data = as.data.frame(table(Lab3_cleaned_raw$stage1_reverse)), aes(x = Var1, y = Freq)) +
  geom_bar(stat = "identity", fill = c("skyblue", "salmon")) +
  labs(title = "Stage 1: Switching vs Not Switching",
       x = "Switching in Stage 1 (False = No Switch, True = Switch)",
       y = "Number of Participants") +
  theme_minimal() +
  scale_x_discrete(labels = c("False", "True"))

# Figure 2: Bar Graph comparing the number of participants switching vs not switching in Stage 2,
# but breaking them into four compartments based on Stage 1 and Stage 2 switching behavior
stage2_summary <- Lab3_cleaned_raw %>%
  group_by(stage1_reverse, stage2_reverse) %>%
  summarise(count = n()) %>%
  spread(stage2_reverse, count, fill = 0)
## `summarise()` has grouped output by 'stage1_reverse'. You can override using
## the `.groups` argument.
stage2_summary_long <- stage2_summary %>%
  gather(key = "stage2_reverse", value = "count", -stage1_reverse)
## Plot for Stage 2: Switching Patterns Based on Stage 1 Behavior
ggplot(stage2_summary_long, aes(x = as.factor(stage1_reverse), y = count, fill = as.factor(stage2_reverse))) +
  geom_bar(stat = "identity", position = "stack") +
  labs(title = "Stage 2: Switching Patterns Based on Stage 1 Behavior",
       x = "Switching in Stage 1",
       y = "Number of Participants",
       fill = "Switching in Stage 2") +
  theme_minimal() +
  scale_x_discrete(labels = c("False", "True"))

Essay

In this experiment, I used RStudio to analyze data regarding decision-making and choice reversals across two stages. The tasks involved making choices between two options, and the objective was to understand how prior decisions in one stage influenced future choices. My approach to the experiment was methodical and based on an analysis of the choices made during the tasks. I started by loading the dataset into RStudio using the read.csv() function, which pointed to the file stored in my Downloads folder. Once the data was loaded, I used the str() function to inspect the dataset’s structure and ensure that all relevant variables, like task choices, were correctly imported.

Next, I created new columns in the dataset to track whether participants switched between options in each stage. For Stage 1, I focused on tasks 1 to 10 and checked whether participants’ choices differed between consecutive tasks. The same analysis was applied to Stage 2, which included tasks 11 to 20. I created two columns, stage1_reverse and stage2_reverse, that flagged whether a participant switched their choice during each stage. These new variables helped identify switching behavior across the tasks.

To capture the exact moment when a switch occurred, I added two more columns, stage1_switch_location and stage2_switch_location. These columns indicated the specific task in which a participant’s choice changed. For example, if a participant switched from Option A to Option B between task 3 and task 4 in Stage 1, the corresponding value in the stage1_switch_location column would mark task 4 as the point of the switch. This additional layer of analysis allowed me to track the exact task where decision changes happened.

The tasks in the experiment involved choosing between two options—Option A and Option B—across ten tasks in each of the two stages. The objective was to test whether participants tended to switch from one option to the other as the tasks progressed. In RStudio, I used the apply() function to check if there was a difference between each task’s choice and the previous task’s choice. If a participant switched from Option A to Option B, or vice versa, I marked it as a “switch” and recorded the task where this change occurred.

The decisions I made in RStudio were based on the need to track switching behavior, as the experiment’s core goal was to observe how choices in earlier tasks influenced decisions in later ones. By creating columns to monitor these switches, I was able to quantify the reversal behavior of participants, which was essential for understanding patterns in their decision-making. To visualize the results, I used the ggplot2 package to create bar graphs that compared the number of participants who switched versus those who did not switch in both Stage 1 and Stage 2. These graphs allowed me to observe trends and patterns, providing insight into whether participants were more likely to stick with their initial choice or reverse it across the two stages.

Through this process in RStudio, I was able to effectively analyze the switching behavior of participants and understand how prior decisions impacted future choices. The combination of data manipulation, logical analysis, and visualization allowed for a clear picture of the experiment’s outcomes, providing valuable insights into decision-making patterns across multiple tasks.