── 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
library(dplyr)library(BSDA)
Loading required package: lattice
Attaching package: 'BSDA'
The following object is masked from 'package:datasets':
Orange
Attaching package: 'psych'
The following objects are masked from 'package:ggplot2':
%+%, alpha
library(ggfortify) # PCA visualization
Open & Set Up Files
# read in the fileraw_results <-read.csv("num_data.csv") #numeric valuedem_results <-read.csv("results.csv") #text value final_cleaned <-read.csv("final_cleaned.csv")
Delete data from participants who did not pass attention check questions
# Remove rows where any of the specified columns does not contain "3"attent_check <- consent_col_deleted[(consent_col_deleted$V1.N6...Attention.Q =="3"| consent_col_deleted$V1.N6...Attention.Q ==""|is.na(consent_col_deleted$V1.N6...Attention.Q)) & (consent_col_deleted$V1.N6...Attention.Q.1=="3"| consent_col_deleted$V1.N6...Attention.Q.1==""|is.na(consent_col_deleted$V1.N6...Attention.Q.1)) & (consent_col_deleted$V2.N8...Attention.Q =="3"| consent_col_deleted$V2.N8...Attention.Q ==""|is.na(consent_col_deleted$V2.N8...Attention.Q)) & (consent_col_deleted$V2.N8...Attention.Q.1=="3"| consent_col_deleted$V2.N8...Attention.Q.1==""|is.na(consent_col_deleted$V2.N8...Attention.Q.1)), ]# Remove attention check columnscleaned_data <- attent_check[, !(names(attent_check) %in%c('V1.N6...Attention.Q','V1.N6...Attention.Q.1','V2.N8...Attention.Q','V2.N8...Attention.Q.1'))]
Convert Data
# Convert to integer emotional_total <-as.integer(cleaned_data$`V1.N6...Emotional.Q`)logical_total <-as.integer(cleaned_data$`V1.N6...Logical.Q`)# Replace NA values in 'emotional_total' with values from 'V2.N8...Emotional.Q'na_indices_emotional <-is.na(emotional_total)emotional_total[na_indices_emotional] <-as.integer(cleaned_data$`V2.N8...Emotional.Q`[na_indices_emotional])# Replace NA values in 'logical_total' with values from 'V2.N8...Logical.Q'na_indices_logical <-is.na(logical_total)logical_total[na_indices_logical] <-as.integer(cleaned_data$`V2.N8...Logical.Q`[na_indices_logical])# Combine into a new data framecleaned <-data.frame(emotional_total, logical_total)
ggplot(dem_results, aes(x = Age)) +geom_bar(fill ="blue", color ="black") +labs(title ="Distribution of Age Groups", x ="Age Group", y ="Count") +theme_minimal()
18-20 21-25 25-30 35-40
28 39 21 11
40+ Prefer not to say
13 4
Pie Chart Visualization
# Calculate percentages for Age Groupage_counts <-table(dem_results$Age)# Directly create age_df with correct column names and dataage_df <-data.frame(Category =names(age_counts), Count =as.integer(age_counts))# Calculate the Percentage columnage_df$Percentage <- (age_df$Count /sum(age_df$Count)) *100# ggplotggplot(age_df, aes(x ="", y = Percentage, fill = Category)) +geom_bar(stat ="identity", width =1) +coord_polar(theta ="y") +labs(title ="Pie Chart of Age Group Distribution", x =NULL, y =NULL) +theme_void() +theme(legend.title =element_blank())
Race
# Convert to character to ensure manipulation worksdem_results$Race <-as.character(dem_results$Race) dem_results$Race <-ifelse(grepl(",", dem_results$Race),"Two or More Races", dem_results$Race)
Bar Chart Visualization
ggplot(dem_results, aes(x = Race)) +geom_bar(fill ="red", color ="black") +labs(title ="Distribution of Race Groups", x ="Race Group", y ="Count") +theme_minimal()
ggplot(dem_results, aes(x = Gender)) +geom_bar(fill ="blue", color ="black") +labs(title ="Distribution of Gender Groups", x ="Gender Group", y ="Count") +theme_minimal()
Female Male Non-binary/Other Prefer not to say
59 47 9 1
Pie Chart Visualization
# Convert the gender table to a data frame for plottinggender_df <-as.data.frame(gender_table)names(gender_df) <-c("Gender", "Count")# Calculate percentagesgender_df$Percentage <- (gender_df$Count /sum(gender_df$Count)) *100ggplot(gender_df, aes(x ="", y = Percentage, fill = Gender)) +geom_bar(stat ="identity", width =1) +coord_polar(theta ="y") +labs(title ="Pie Chart of Gender Distribution", x =NULL, y =NULL) +theme_void() +theme(legend.title =element_blank())
Yes, we have a binary categorical IV – our two factors are emotional and logical.
Check #2: Do we have a continuous DV?
Yes, we have a continuous DV that ranges from 1 to 5.
Check #3: Are the observations dependent on each
other?
No, this can be eliminated due to randomness on whether the emotional or the logical comment set is given first
Check #4: Are the morality rating difference in the
two conditions normally distributed?
## Usage: Shapiro-Wilk normality test ## Note: sample size is very large (n > 30), so if our IV were continuous we can ignore data distribution and use parametric tests (b/c CLT assumes that sampling distribution is normalized) -- but we should still do distribution testing due to the high discretization of our DVshapiro_emo <-shapiro.test(emo_vector)shapiro_log <-shapiro.test(log_vector)# Calculate differencesdifference_vector <- emo_vector - log_vectorshapiro_diff <-shapiro.test(difference_vector)difference_df =data.frame(difference_vector)# Print resultsprint(shapiro_emo)
Shapiro-Wilk normality test
data: emo_vector
W = 0.85686, p-value = 3.288e-09
print(shapiro_log)
Shapiro-Wilk normality test
data: log_vector
W = 0.86053, p-value = 4.643e-09
print(shapiro_diff)
Shapiro-Wilk normality test
data: difference_vector
W = 0.94234, p-value = 8.224e-05
In both cases, p-value << 0.05, so we CANNOT assume normality. Fails normality test.
Because the individual sample Shapiro-Wilk test failed, we can try and just assess the difference between the two and see if it is normally distributed. But, still failed normality test.
Histogram Visualization
ggplot(data = difference_df, aes(x = difference_vector)) +geom_histogram(bins =7, color ="black", fill ="white")
Paired t-test (check for effect between
emotional vs. logical comments)
cleaned_emo_vector =as.vector(unlist(final_cleaned["emotional_total"]))cleaned_log_vector =as.vector(unlist(final_cleaned["logical_total"]))type_t_result =t.test(x =as.vector(cleaned_emo_vector), y =as.vector(cleaned_log_vector), paired =TRUE, alternative ="two.sided")type_t_result
Paired t-test
data: as.vector(cleaned_emo_vector) and as.vector(cleaned_log_vector)
t = 0.71301, df = 115, p-value = 0.4773
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
-0.1686118 0.3582670
sample estimates:
mean difference
0.09482759
Summary Bar Graphs for Main Effects
# Data Preparation and Statistics Calculationstats_summary <- final_cleaned %>%summarise(Mean_Emotional =mean(emotional_total, na.rm =TRUE),SD_Emotional =sd(emotional_total, na.rm =TRUE),Mean_Logical =mean(logical_total, na.rm =TRUE),SD_Logical =sd(logical_total, na.rm =TRUE) )# Transform data for plottingstats_plot <-data.frame(Condition =c("Emotional", "Logical"),Mean =c(stats_summary$Mean_Emotional, stats_summary$Mean_Logical),SD =c(stats_summary$SD_Emotional, stats_summary$SD_Logical))
# Plotting the bar graph with error barsggplot(stats_plot, aes(x = Condition, y = Mean, fill = Condition)) +geom_bar(stat ="identity", width =0.5) +geom_errorbar(aes(ymin = Mean - SD, ymax = Mean + SD), width =0.2, color ="black") +labs(title ="Mean Scores of Emotional vs. Logical Comments",x ="Condition",y ="Rating of Morality") +theme_minimal() +scale_fill_brewer(palette ="Pastel1") # Adding a color palette for visual distinction