#Load Libraries library(foreign) library(tidyr) library(dplyr) library(stringr) library(ggplot2) library(haven)

#Set Working Directory and Load the Data

setwd(“/Users/a91981/Documents/DATA 10920 SEA/UCSD/R Directory”)

d = read.spss(“/Users/a91981/Documents/DATA 10920 SEA/UCSD/R Directory/problem_sets/data/Tamiretal2008ReplicationData.sav”, to.data.frame=T)

head(d) colnames(d)

#Range of Likert Score unique(d$Game1Angry1) #Highest: 7 #Lowest: 1

#Cleaning the data

tail(d)

filtered_d <- d |> filter(is.na(DoNotUse) | DoNotUse == 0) #Getting rid of unnecessary columns

filtered_d = filtered_d |> select(c(“Subject”, “Cond”), contains(“Game”), -contains(“Intro”), -c(“WhichGames”, “GameComments”), starts_with(“DinerDashWith”), c(“SOFMusicEnemies”, “SOFNoMusicEnemies”))

rating_hyp_d = filtered_d |> filter(is.na(DoNotUseVideoGamePerformanceData)) %>% select(-DoNotUseVideoGamePerformanceData, -starts_with(“DinerDash”), -starts_with(“SOF”))

performance_hyp_d <- filtered_d |> select(-contains(“Game”))

tiny_demo_d = head(performance_hyp_d, 2)

#Wide-form data:

tiny_demo_d

#Long-form version:

tiny_demo_d |> pivot_longer(cols=-c(“Subject”, “Cond”), names_to=‘Measurement’, values_to=‘Value’)

#Converting the performance dataset

performance_hyp_long_d = performance_hyp_d |> pivot_longer(cols=-c(“Subject”, “Cond”), names_to=‘Measurement’, values_to=‘Score’)

head(performance_hyp_long_d)

rating_hyp_long_d = rating_hyp_d |> pivot_longer(cols = -c(“Subject”, “Cond”), # Transform all columns except “Subject” and “Cond” names_to = ‘Measurement’, # Rename the key column to “Measurement” values_to = ‘Rating’)
head(rating_hyp_long_d)

Splitting Columns

tiny_demo_mutate <- head(performance_hyp_long_d, 10)

tiny_demo_mutate = tiny_demo_mutate |> mutate(

)

performance_hyp_long_d = performance_hyp_long_d |> mutate(

ConfrontationalGame = grepl("SOF", Measurement), 


WithMusic = !grepl("NoMusic|WithoutMusic", Measurement),


Cond = ifelse(Cond > 3, Cond - 3, Cond),


MusicCondition = factor(Cond, levels = 1:3, labels = c("Anger", "Exciting", "Neutral"))
) 

rating_hyp_long_d = rating_hyp_long_d |> mutate( IsRecall = grepl(“Friends|Strangers”, Measurement) )

rating_hyp_long_d = rating_hyp_long_d |> mutate(

GameNumber = as.numeric(substr(rating_hyp_long_d$Measurement, 5, 5)),


ConfrontationalGame = GameNumber <= 2, 


Emotion = str_extract(Measurement, "Angry|Neutral|Excited|Exciting|Calm"),


Emotion = ifelse(Emotion == "Excited", "Exciting", 
          ifelse(Emotion == "Calm", "Neutral", Emotion)) 
) 

#Performance Hypothesis

performance_hyp_long_d |> group_by(ConfrontationalGame, WithMusic) |> summarize(AvgScore = mean(Score, na.rm=T))

performance_hyp_long_d = performance_hyp_long_d |> group_by(ConfrontationalGame, WithMusic) |> mutate(z_scored_performance = scale(Score)) |> ungroup()

#Rating Hypothesis rating_summary_d = rating_hyp_long_d |> group_by(IsRecall, Measurement) |> summarize(MeanRating = mean(Rating, na.rm = TRUE)) |> ungroup()

rating_summary_d #Bar Plot

ggplot(rating_summary_d, aes(x=ConfrontationalGame, y=MeanRating, fill=Emotion)) + geom_bar(position=“dodge”, stat=“identity”) + scale_fill_brewer(palette=“Set1”)

ggplot(rating_summary_d, aes(x=ConfrontationalGame, y=MeanRating, fill=Emotion)) + geom_bar(position=“dodge”, stat=“identity”) + scale_fill_brewer(palette=“Set1”)

#Performance Hypothesis

performance_diff_d = performance_hyp_long_d |>

mutate(WithMusic = factor(WithMusic, levels=c(F, T), labels=c(“PreMusic”, “PostMusic”))) |>

select(-c(“Score”, “Measurement”)) |> pivot_wider(names_from=WithMusic, values_from=z_scored_performance) |> mutate(ImprovementScore=PostMusic-PreMusic)

performance_diff_d

performance_summary_d = performance_diff_d |> group_by(ConfrontationalGame, WithMusic) |>
summarize(MeanImprovementScore = mean(ImprovementScore, na.rm = TRUE), .groups = “drop”)

performance_summary_d

ggplot(performance_summary_d, aes(x = ConfrontationalGame, y = MeanImprovementScore, fill = ConfrontationalGame)) + geom_bar(stat = “identity”) +
labs(title = “Mean Improvement Score by Game”, x = “Confrontational Game”, y = “Mean Improvement Score”) + theme_minimal() +
scale_fill_brewer(palette = “Set1”)