Step 1: Load the data files

# set the directory
setwd("~/Desktop/r_test")

# load data
library(tidyverse)
library(ltm)
library(ggplot2)

# load data files
par_info <- read.csv("participant.info.csv") %>%
  rename(id = id_number) # create matching variable "id" across data files
scales <- read.csv("scales.data.csv") 

Steps 2 & 3: Merge and wrangle that data!

# merge data frames by column name "id"
mydata <- merge(par_info, scales, by = "id") %>% 
  filter(is.na(attention)) %>%  #remove participants who failed attention check
  mutate(d4 = recode(d4, '1' = 7, '2'= 6, '3'= 5, '4'= 4, '5'= 3, '6'= 2, '7'= 1)) %>% #reverse score
  mutate(d8 = recode(d8, '1' = 7, '2'= 6, '3'= 5, '4'= 4, '5'= 3, '6'= 2, '7'= 1)) %>% 
  mutate(d12 = recode(d12, '1' = 7, '2'= 6, '3'= 5, '4'= 4, '5'= 3, '6'= 2, '7'= 1)) %>% 
  mutate(d16 = recode(d16, '1' = 7, '2'= 6, '3'= 5, '4'= 4, '5'= 3, '6'= 2, '7'= 1))

Step 4: Cronbach’s alpha

# create separate data sets for each scale
CHIPS_data <- mydata %>% 
  dplyr::select(starts_with("c"))

Neuro_data <- mydata %>% 
  dplyr::select(starts_with("n"))

CES_D_data <- mydata %>% 
  dplyr::select(starts_with("d"))

# calculate cronbach's alpha by scale accounting for missing variables
CHIPS_cronbach <- cronbach.alpha(CHIPS_data, na.rm = TRUE)
Neuro_cronbach <- cronbach.alpha(Neuro_data, na.rm = TRUE)
CES_D_cronbach <- cronbach.alpha(CES_D_data, na.rm = TRUE)

# Check if the alpha value is numeric and not NA
if (is.numeric(CES_D_cronbach$alpha) && !is.na(CES_D_cronbach$alpha)) {
# Format the alpha value with three decimal places
  formatted_alpha <- sprintf("%.3f", CES_D_cronbach$alpha)
} else {
  formatted_alpha <- "N/A"
}

These three scales all show high internal consistency, suggesting that the items within each scale measure the same construct.

Step 5: Compute mean scale scores

# compute variables for the CHIPS, Neuroticism, and CES-D total and mean scale scores
# account for missing variables (na.rm = TRUE)
mydata <- mydata %>%
  mutate(CHIPS_sum = rowSums(CHIPS_data, na.rm = TRUE)) %>% # I like to have a total score variable for each
  mutate(CHIPS_mean = rowMeans(CHIPS_data, na.rm = TRUE)) %>%
  mutate(Neuro_sum = rowSums(Neuro_data, na.rm = TRUE)) %>%
  mutate(Neuro_mean = rowMeans(Neuro_data, na.rm = TRUE)) %>%
  mutate(CES_D_sum = rowSums(CES_D_data, na.rm = TRUE)) %>%
  mutate(CES_D_mean = rowMeans(CES_D_data, na.rm = TRUE))

Step 6: Create a simplified dataset

# You could create a straightforward simplified dataset...
simpledata <- mydata %>% 
  filter(gender %in% c(1, 2)) %>%  # include only those who identify as male ("1") and female ("2")
  dplyr::select(id, age, gender, CHIPS_mean, Neuro_mean, CES_D_mean) # included variables

# ...or you could do this!
# create new data frame with self-identified gender 2 (female)
female_data <- mydata %>%
  filter(gender == 2) %>%
  dplyr::select(id, age, female_CHIPS = CHIPS_mean, female_Neuro = Neuro_mean, female_CESD = CES_D_mean)

# create new data frame with self-identified gender 1 (male)
male_data <- mydata %>%
  filter(gender == 1) %>%
  dplyr::select(id, age, male_CHIPS = CHIPS_mean, male_Neuro = Neuro_mean, male_CESD = CES_D_mean)

# Combine the male and female data frames
smldata <- bind_rows(male_data, female_data)

smldata <- smldata %>%
  dplyr::select(id, age, female_CHIPS, male_CHIPS, female_Neuro, male_Neuro, female_CESD, male_CESD)

Step 7: Run two-sample t-tests

# two-sample t-test: female vs male on mean scale scores
t_test_CHIPS <- t.test(smldata$female_CHIPS, smldata$male_CHIPS, alternative = "two.sided",
                        mu = 0, paired = FALSE, conf.level = .95, var.equal = TRUE)

t_test_Neuro <- t.test(smldata$female_Neuro, smldata$male_Neuro, alternative = "two.sided",
                       mu = 0, paired = FALSE, conf.level = .95, var.equal = TRUE)

t_test_CESD <- t.test(smldata$female_CESD, smldata$male_CESD, alternative = "two.sided",
                       mu = 0, paired = FALSE, conf.level = .95, var.equal = TRUE)

The results of the two-sample t-test comparing females and males by their CHIPS scale mean scores:

 print(t_test_CHIPS)
## 
##  Two Sample t-test
## 
## data:  smldata$female_CHIPS and smldata$male_CHIPS
## t = -4.3031, df = 587, p-value = 1.973e-05
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.26607859 -0.09930783
## sample estimates:
## mean of x mean of y 
## 0.5341185 0.7168117

The results of the two-sample t-test comparing females and males by their Neuroticism scale mean scores:

 print(t_test_Neuro)
## 
##  Two Sample t-test
## 
## data:  smldata$female_Neuro and smldata$male_Neuro
## t = -9.1457, df = 587, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.2457486 -0.1588600
## sample estimates:
## mean of x mean of y 
## 0.4277675 0.6300718

The results of the two-sample t-test comparing females and males by their CES-D scale mean scores:

 print(t_test_CESD)
## 
##  Two Sample t-test
## 
## data:  smldata$female_CESD and smldata$male_CESD
## t = -3.8477, df = 587, p-value = 0.0001323
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.5239522 -0.1698224
## sample estimates:
## mean of x mean of y 
##  2.483140  2.830027

Step 8: FIGURES!

### Step 8: Figures:  ###
#find the means for each scale by gender
mean_CHIPS_male <- round(mean(male_data$male_CHIPS, na.rm = TRUE), 2)
mean_CHIPS_female <- round(mean(female_data$female_CHIPS, na.rm = TRUE), 2)

mean_Neuro_male <- round(mean(male_data$male_Neuro, na.rm = TRUE), 2)
mean_Neuro_female <- round(mean(female_data$female_Neuro, na.rm = TRUE), 2)

mean_CESD_male <- round(mean(male_data$male_CESD, na.rm = TRUE), 2)
mean_CESD_female <- round(mean(female_data$female_CESD, na.rm = TRUE), 2)

# set the theme
theme_set(theme_minimal(base_family = "Times New Roman"))

# create a violin plot for CHIPS mean scores
ggplot(simpledata, aes(y = factor(gender), x = CHIPS_mean, fill = factor(gender))) +
  geom_violin(alpha = 0.45) +
  geom_boxplot(width = .35, alpha = 0.10) +
  labs(y = "Gender", x = "CHIPS Mean Score") +
  ggtitle("Comparison of CHIPS Mean Scores by Gender") +
  scale_fill_manual(
    values = c("1" = "orange", "2" = "maroon"),
    breaks = c("1", "2"),  # Specify the original breaks
    labels = c("Male (n = 329)", "Female (n = 260)")  # Specify the desired labels
  ) +
  labs(fill = "Gender") +  # Rename the legend title to "Gender"
  scale_y_discrete(labels = c("1" = "", "2" = "")) + # Change y-axis labels
  theme(
    plot.title = element_text(size = 16, face = "bold"), # Title font
    axis.title = element_text(size = 14, face = "bold"), # Axis label font
    axis.text = element_text(size = 12, face = "bold"), # Axis tick label font
    legend.text = element_text(size = 12, face = "bold") # Legend font
  )+
  geom_text(
    aes(x = mean_CHIPS_male, label = paste("mean =", mean_CHIPS_male), y = 1),
    vjust = -3.75,hjust = .9,
    size = 3.5, color = "black"
  ) +
  geom_text(
    aes(x = mean_CHIPS_female, label = paste("mean =",  mean_CHIPS_female), y = 2),
    vjust = -3.75, hjust = .8,
    size = 3.5, color = "black"
  )

# create a violin plot for Neuro mean scores
ggplot(simpledata, aes(y = factor(gender), x = Neuro_mean, fill = factor(gender))) +
  geom_violin(alpha = 0.45) +
  geom_boxplot(width = .35, alpha = 0.10) +
  labs(y = "Gender", x = "Neuroticism Mean Score") +
  ggtitle("Comparison of Neuroticism Mean Scores by Gender") +
  scale_fill_manual(
    values = c("1" = "orange", "2" = "maroon"),
    breaks = c("1", "2"),  # Specify the original breaks
    labels = c("Male (n = 329)", "Female (n = 260)")  # Specify the desired labels
  ) +
  labs(fill = "Gender") +  # Rename the legend title to "Gender"
  scale_y_discrete(labels = c("1" = "", "2" = "")) + # Change y-axis labels
  theme(
    plot.title = element_text(size = 16, face = "bold"), # Title font
    axis.title = element_text(size = 14, face = "bold"), # Axis label font
    axis.text = element_text(size = 12, face = "bold"), # Axis tick label font
    legend.text = element_text(size = 12, face = "bold") # Legend font
  )+
  geom_text(
    aes(x = mean_Neuro_male, label = paste("mean =", mean_Neuro_male), y = 1),
    vjust = -3.75,hjust = 0.5,
    size = 3.5, color = "black"
  ) +
  geom_text(
    aes(x = mean_Neuro_female, label = paste("mean =",  mean_Neuro_female), y = 2),
    vjust = -3.75, hjust = 0.5,
    size = 3.5, color = "black"
  )

# create a violin plot for CES-D scores
ggplot(simpledata, aes(y = factor(gender), x = CES_D_mean, fill = factor(gender))) +
  geom_violin(alpha = 0.45) +
  geom_boxplot(width = .35, alpha = 0.10) +
  labs(y = "Gender", x = "CES-D Mean Score") +
  ggtitle("Comparison of CES-D Mean Scores by Gender") +
  scale_fill_manual(
    values = c("1" = "orange", "2" = "maroon"),
    breaks = c("1", "2"),  # Specify the original breaks
    labels = c("Male (n = 329)", "Female (n = 260)")  # Specify the desired labels
  ) +
  labs(fill = "Gender") +  # Rename the legend title to "Gender"
  scale_y_discrete(labels = c("1" = "", "2" = "")) + # Change y-axis labels
  theme(
    plot.title = element_text(size = 16, face = "bold"), # Title font
    axis.title = element_text(size = 14, face = "bold"), # Axis label font
    axis.text = element_text(size = 12, face = "bold"), # Axis tick label font
    legend.text = element_text(size = 12, face = "bold") # Legend font
  )+
  geom_text(
    aes(x = mean_CESD_male, label = paste("mean =", mean_CESD_male), y = 1),
    vjust = -4,hjust = .8,
    size = 3.5, color = "black"
  ) +
  geom_text(
    aes(x = mean_CESD_female, label = paste("mean =",  mean_CESD_female), y = 2),
    vjust = -4, hjust = .9,
    size = 3.5, color = "black"
  )

###Go Jays!
The end.