---
title: "Statistical Data Analysis"
author: Your Name Here
format:
  html:
    df-print: kable
    theme: cosmo
    toc: true
    toc-depth: 3
    embed-resources: true
execute:
  echo: true
  editor: source
---

```{r setup}
library(tidyverse)

options(scipen=999) # turn off scientific notation

```

# Download data

We are going to download data from [Faul et al. 2025](https://psycnet.apa.org/doi/10.1037/emo0001518). In this study, participants studied emotionally negative, positive, and neutral images. They then completed a memory test for these images, which included measures of:

-   accuracy of the discrimination between old and new images
-   ratings of subjective memory vividness
-   errors in reconstructing the visual features of the images (here we'll look at color saturation).

Participants were sampled across the lifespan from 19-78 years of age. Thus, this dataset is a rich dataset for looking at the effects of within-subjects and between-subjects variables on multiple dependent measures, as well as relationships between these variables.

```{r}
if (!file.exists('data')) { # Download only once
  
  # Create directory
  dir.create('data')
  
  # Download the variable key
  download.file('https://osf.io/download/hcwj4/','data/Key.csv')
  
  # Download data
  download.file('https://osf.io/download/9nvu3/','data/Final_Data_307_Participants.csv')
  
} else {
  print('The data directory already exists. Check to see if everything has been downloaded already.')
}

# Print the list of files
cat('List of files and folders in the data directory:\n')
list.files('data')

```

There are two files: the `Key.csv` file, which gives a description of all of the variables included in the dataset, and the `Final_Data_307_Participants.csv` file, which includes all of the data. The dataset includes a row for every experimental trial for every participant.

# Load data

```{r}
df <- read.csv('data/Final_Data_307_Participants.csv')
var_key <- read.csv('data/Key.csv')

glimpse(df)

# How many participants are there?
n_subs # <- 

# How many trials are there per participant?
n_trials # <- 
```

There are `r n_subs` participants and `r n_trials` trials per participant, for a total of `r nrow(df)` rows of data.

Before you continue on: What kind of data wrangling and data tidying do you need to do? Make a list of everything you might want to do to make this dataset easier to work with. 

# Data wrangling

Let's select only a subset of the data to keep for further analysis. We'll keep some demographic information about the participant, the key conditions for each experimental trial, and the main response information for each trial.

## Create and select variables

You must replace `function_name` with the correct function to use here!

```{r}
df.tidy <- df |>
  # rename some variables
  function_name(Old_or_New = Retrieval_Image_Type,
         Vividness = Retrieval_Vividness_Response,
         Error = Error_ColorSat_Reconstruction) |>
  
  # create new variables
  function_name(Accuracy = function_name(Old_New_Response_Type=="HIT" ~ 1,
                              Old_New_Response_Type=="MISS" ~ 0,
                              Old_New_Response_Type=="CR" ~ 1,
                              Old_New_Response_Type=="FA" ~ 0,
                              TRUE ~ NA),
         ErrorAbs = abs(Error),
         Age_Group = function_name(Age<35 ~ 'Younger Age',
                               Age>=35 & Age < 65 ~ 'Middle Age',
                               Age>65 ~ 'Older Age'),
         
         # convert some to factors
         Subject_ID = factor(Subject_ID),
         Emotion = factor(Retrieval_Emotion_Category)
         ) |>
  
  # select a subset of variables
  function_name(Subject_ID, Age, Age_Group, Gender, BDI_Sum,    # subject information
         Emotion, Old_or_New,                 # trial information
         Vividness, Accuracy, Error, ErrorAbs # response information
         ) 

glimpse(df.tidy)
```

Here, `Age`, `Gender`, and `BDI_Sum` are between-subjects variables, and `Emotion` and `Old_or_New` are within-subjects conditions. `Vividness`, `Accuracy`, and `Error`, and `ErrorAbs` correspond to the participants' responses to each trial, and they will serve as our dependent measures.

We will also create another dataframe that averages across trials in the same condition, so that we have one row per condition per participant.

## Summarize across trials

```{r}
# Get the average data
# First, group by subject number and emotion category
# then calculate meanVivid, meanAccuracy, meanError, meanErrorAbs
df.avg <- df.tidy |>
  # FILL IN HERE

```

## Re-join with subject information

```{r}
# Get the subject information
df.sub_info <- df.tidy |>
  select(Subject_ID, Age, Age_Group, Gender, BDI_Sum) |>
  unique()

# Join the subject information to the average data
df.avg <- # FILL IN HERE

glimpse(df.avg)

```

# Stats

## Example t-test

Add your own annotations!

```{r}
neg <- df.avg |>
  filter(Emotion=="Negative") |> 
  arrange(Subject_ID)
neu <- df.avg |>
  filter(Emotion=="Neutral") |> 
  arrange(Subject_ID)
# why use the arrange function??

t.test(neg$meanAccuracy, neu$meanAccuracy, paired=TRUE)
```

## Example ANOVA

Add your own annotations!

```{r}
# ANOVA
library(afex)
results <- aov_ez(id = "Subject_ID",
                  dv = "meanAccuracy",
                  within = "Emotion",
                  data = df.avg)
print(results)

# Follow-up tests
library(emmeans)
emmeans(results, pairwise ~ Emotion)
```

Notes:

-   The default output for `aov_ez` applies Greenhouse-Geisser correction automatically (when appropriate) and reports generalized eta-squared (ges), which is the effect size most appropriate for within-subjects designs.
-   The `emmeans` function gives estimated marginal means plus pairwise contrasts with Tukey-adjusted p-values, which is the common next step after a significant ANOVA result.
