set working directory.

setwd("~/My Drive (okara004@ucr.edu)/classes/fall 2022 quarter/PSYC 211 -- stats/final project")

load the data file & libraries.

i’ll be using a data file given to me by my advisor (same one i used for all the problem sets).

## load the data file.
patience_data <- read.csv("patience22ucr_copy.csv")

## then load the libraries you'll need.
library(psych)
library(report)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.4 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.2      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ ggplot2::%+%()   masks psych::%+%()
## ✖ ggplot2::alpha() masks psych::alpha()
## ✖ dplyr::filter()  masks stats::filter()
## ✖ dplyr::lag()     masks stats::lag()
library(ggplot2)

select the data you’re interested in.

i am interested in data from the 3-factor patience scale (3-FPS) and the emotion regulation questionnaire (ERQ). i will also be selecting the participant demographic information.

patience_ERQ_gender <- subset(patience_data, select = c("sn", "patience_ss1", "patience_ss2", 
                                                    "patience_ss3", "patience_ss4", "patience_ss5", 
                                                    "patience_ss6", "patience_ss7", "patience_ss8", 
                                                    "patience_ss9", "patience_ss10", "patience_ss11", 
                                                    "erq1", "erq2", "erq3", "erq4", "erq5", "erq6",
                                                    "erq7", "erq8", "erq9", "erq10", "sex", "age", 
                                                    "eth", "eth_7_TEXT"))

calculate scale averages.

i will be averaging all the 3-FPS, ERQ–reappraisal, and ERQ–suppression items to get average patience, reappraisal, and suppression scores.

## first average all of the patience items and put these averages in a new column.
patience_ERQ_gender$patience_avg <- rowMeans(patience_ERQ_gender[ , c("patience_ss1", "patience_ss2", "patience_ss3", "patience_ss4", "patience_ss5", "patience_ss6",
                                                                      "patience_ss7", "patience_ss8", "patience_ss9", "patience_ss10", "patience_ss11")], 
                                             na.rm = TRUE)

## now average all of the ERQ reappraisal items and put these averages in a new column.
patience_ERQ_gender$reappraisal_avg <- rowMeans(patience_ERQ_gender[ , c("erq1", "erq3", "erq5", "erq7", "erq8", "erq10")], na.rm = TRUE)

## lastly average the ERQ suppression items and put these averages in a new column.
patience_ERQ_gender$suppression_avg <- rowMeans(patience_ERQ_gender[ , c("erq2", "erq4", "erq6", "erq9")], na.rm = TRUE)

remove subjects.

anyone who has missing data for all scale items (for any scale) will be removed. additionally, anyone who did not report being either male or female will be removed.

## these 2 participants did not have any data for the 3_FPS so i am removing them.
patience_ERQ_gender_2 <- patience_ERQ_gender[ -c(208, 216), ]

## now take a look at the different reported genders.
table(patience_ERQ_gender_2$sex)
## 
##   1   2   3   4 
##  92 115   3   4
## remove anyone who reported a gender other than male/female.
gender_TABLE <- table(patience_ERQ_gender_2$sex <= 2, patience_ERQ_gender_2$sn)
gender_TABLE
##        
##         1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
##   FALSE    0    0    0    0    0    0    1    0    0    0    0    0    0    0
##   TRUE     1    1    1    1    1    1    0    1    1    1    1    1    1    1
##        
##         1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
##   FALSE    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   TRUE     1    1    1    1    1    1    1    1    1    1    1    1    1    1
##        
##         1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
##   FALSE    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   TRUE     1    1    1    1    1    1    1    1    1    1    1    1    1    1
##        
##         1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
##   FALSE    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   TRUE     1    1    1    1    1    1    1    1    1    1    1    1    1    1
##        
##         1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
##   FALSE    0    0    0    0    0    0    0    1    0    0    0    0    0    0
##   TRUE     1    1    1    1    1    1    1    0    1    1    1    1    1    1
##        
##         1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
##   FALSE    0    0    0    0    0    0    0    0    0    0    0    0    1    0
##   TRUE     1    1    1    1    1    1    1    1    1    1    1    1    0    1
##        
##         1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
##   FALSE    0    0    0    0    0    0    1    0    0    0    0    0    0    0
##   TRUE     1    1    1    1    1    1    0    1    1    1    1    1    1    1
##        
##         1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
##   FALSE    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   TRUE     1    1    1    1    1    1    1    1    1    1    1    1    1    1
##        
##         1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
##   FALSE    0    0    0    0    0    0    0    1    0    0    0    0    0    0
##   TRUE     1    1    1    1    1    1    1    0    1    1    1    1    1    1
##        
##         1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
##   FALSE    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   TRUE     1    1    1    1    1    1    1    1    1    1    1    1    1    1
##        
##         1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
##   FALSE    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   TRUE     1    1    1    1    1    1    1    1    1    1    1    1    1    1
##        
##         1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
##   FALSE    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   TRUE     1    1    1    1    1    1    1    1    1    1    1    1    1    1
##        
##         1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
##   FALSE    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   TRUE     1    1    1    1    1    1    1    1    1    1    1    1    1    1
##        
##         1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
##   FALSE    0    0    0    0    0    0    0    0    0    0    0    0    1    0
##   TRUE     1    1    1    1    1    1    1    1    1    1    1    1    0    1
##        
##         1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1208 1209 1210
##   FALSE    0    0    0    0    0    0    0    1    0    0    0    0    0    0
##   TRUE     1    1    1    1    1    1    1    0    1    1    1    1    1    1
##        
##         1211 1212 1213 1214
##   FALSE    0    0    0    0
##   TRUE     1    1    1    1
genderMF_LIST <- which(gender_TABLE[2 , ] == 1)
length(genderMF_LIST) ### we're left with 207 subjects, which is the total of male + female found above in the table
## [1] 207
patience_ERQ_gender_3 <- patience_ERQ_gender_2[patience_ERQ_gender_2$sn %in% names(genderMF_LIST), ]

gather participant demographic information.

## first look at the information on participant sex.
table(patience_ERQ_gender_3$sex) ### 1 is male, 2 is female
## 
##   1   2 
##  92 115
115/207 # 55.56% women
## [1] 0.5555556
## then look at the descriptive stats on participant age.
describe(patience_ERQ_gender_3$age)
##    vars   n mean  sd median trimmed  mad min max range skew kurtosis   se
## X1    1 207 20.5 2.2     20   20.23 1.48  18  34    16 2.73    12.72 0.15
## lastly look at the information on participant race/ethnicity.
table(patience_ERQ_gender_3$eth) # 1 is white, 2 is black/african diaspora, 3 is native american/other native person, 4 is asian, 5 is native hawaiian/pacific islander, 6 is latino/a/x, 7 and 8 are other
## 
##  1  2  3  4  6  7  8 
## 18  6  1 88 73  9 12
18/207 # 8.7% white
## [1] 0.08695652
6/207 # 2.9% black
## [1] 0.02898551
1/207 # 0.48% native american
## [1] 0.004830918
88/207 # 42.51% asian
## [1] 0.4251208
73/207 # 35.27% latinx
## [1] 0.352657
(9+12)/207 # 10.14% other
## [1] 0.1014493

run your analyses.

## first you'll need to split your sex variable into 2 factors -- male & female.
patience_ERQ_gender_3$sex <- as.factor(patience_ERQ_gender_3$sex)
patience_ERQ_gender_4 <- patience_ERQ_gender_3
levels(patience_ERQ_gender_4$sex) <- list("male" = "1", "female" = "2")

## create female/male reappraisal variables.
female_reapp <- patience_ERQ_gender_4$reappraisal_avg[patience_ERQ_gender_4$sex == "female"]
male_reapp <- patience_ERQ_gender_4$reappraisal_avg[patience_ERQ_gender_4$sex == "male"]

## test for homoscedasticity.
var.test(female_reapp, male_reapp)
## 
##  F test to compare two variances
## 
## data:  female_reapp and male_reapp
## F = 1.2093, num df = 114, denom df = 91, p-value = 0.3463
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  0.813651 1.781595
## sample estimates:
## ratio of variances 
##           1.209252
## awesome!! variances are equal. let's continue.

## run an independent-samples t-test to test the difference in means between male reappraisal & female reappraisal.
MF_reapp_test <- t.test(female_reapp, male_reapp, 
                        var.equal = TRUE)
MF_reapp_test
## 
##  Two Sample t-test
## 
## data:  female_reapp and male_reapp
## t = 1.2856, df = 205, p-value = 0.2
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.07057077  0.33506352
## sample estimates:
## mean of x mean of y 
##  3.607246  3.475000
report(MF_reapp_test)
## Warning: Missing values detected. NAs dropped.
## Effect sizes were labelled following Cohen's (1988) recommendations.
## 
## The Two Sample t-test testing the difference between female_reapp and
## male_reapp (mean of x = 3.61, mean of y = 3.48) suggests that the effect is
## positive, statistically not significant, and very small (difference = 0.13, 95%
## CI [-0.07, 0.34], t(205) = 1.29, p = 0.200; Cohen's d = 0.18, 95% CI [-0.10,
## 0.45])
## get the descriptives of the male/female reappraisal variables.
describe(female_reapp)
##    vars   n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 115 3.61 0.77   3.67     3.6 0.74 1.67 6.67     5 0.28     1.35 0.07
describe(male_reapp)
##    vars  n mean  sd median trimmed  mad min max range skew kurtosis   se
## X1    1 92 3.48 0.7    3.5    3.52 0.74 1.5   5   3.5 -0.5     0.06 0.07
## create female/male suppression variables.
female_supp <- patience_ERQ_gender_4$suppression_avg[patience_ERQ_gender_4$sex == "female"]
male_supp <- patience_ERQ_gender_4$suppression_avg[patience_ERQ_gender_4$sex == "male"]

## test for homoscedasticity.
var.test(female_supp, male_supp)
## 
##  F test to compare two variances
## 
## data:  female_supp and male_supp
## F = 1.083, num df = 114, denom df = 91, p-value = 0.6952
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  0.7286791 1.5955384
## sample estimates:
## ratio of variances 
##           1.082966
## awesome!! variances are equal. let's continue.

## run an independent-samples t-test to test the difference in means between male suppression & female suppression.
MF_supp_test <- t.test(female_supp, male_supp, 
                        var.equal = TRUE)
MF_supp_test
## 
##  Two Sample t-test
## 
## data:  female_supp and male_supp
## t = -1.2956, df = 205, p-value = 0.1966
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.37688683  0.07797378
## sample estimates:
## mean of x mean of y 
##  3.184783  3.334239
report(MF_supp_test)
## Warning: Missing values detected. NAs dropped.
## Effect sizes were labelled following Cohen's (1988) recommendations.
## 
## The Two Sample t-test testing the difference between female_supp and male_supp
## (mean of x = 3.18, mean of y = 3.33) suggests that the effect is negative,
## statistically not significant, and very small (difference = -0.15, 95% CI
## [-0.38, 0.08], t(205) = -1.30, p = 0.197; Cohen's d = -0.18, 95% CI [-0.46,
## 0.09])
## get the descriptives of the male/female suppression variables.
describe(female_supp)
##    vars   n mean   sd median trimmed  mad  min max range skew kurtosis   se
## X1    1 115 3.18 0.84      3    3.18 1.11 1.25   5  3.75 0.05    -0.51 0.08
describe(male_supp)
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 92 3.33 0.81   3.25    3.33 0.74 1.5   5   3.5 0.03    -0.55 0.08
## create male/female patience variables.
female_patience <- patience_ERQ_gender_4$patience_avg[patience_ERQ_gender_4$sex == "female"]
male_patience <- patience_ERQ_gender_4$patience_avg[patience_ERQ_gender_4$sex == "male"]

## test for homoscedasticity.
var.test(female_patience, male_patience)
## 
##  F test to compare two variances
## 
## data:  female_patience and male_patience
## F = 1.3491, num df = 114, denom df = 91, p-value = 0.1378
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  0.9077202 1.9875724
## sample estimates:
## ratio of variances 
##           1.349058
## awesome!! variances are equal. let's continue.

## run an independent-samples t-test to test the difference in means between male patience & female patience.
MF_patience_test <- t.test(female_patience, male_patience, 
                    var.equal = TRUE)
MF_patience_test
## 
##  Two Sample t-test
## 
## data:  female_patience and male_patience
## t = -0.56972, df = 205, p-value = 0.5695
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.1829218  0.1009060
## sample estimates:
## mean of x mean of y 
##  3.316206  3.357213
report(MF_patience_test)
## Warning: Missing values detected. NAs dropped.
## Effect sizes were labelled following Cohen's (1988) recommendations.
## 
## The Two Sample t-test testing the difference between female_patience and
## male_patience (mean of x = 3.32, mean of y = 3.36) suggests that the effect is
## negative, statistically not significant, and very small (difference = -0.04,
## 95% CI [-0.18, 0.10], t(205) = -0.57, p = 0.569; Cohen's d = -0.08, 95% CI
## [-0.35, 0.19])
## get the descriptives of the male/female patience variables.
describe(female_patience)
##    vars   n mean   sd median trimmed  mad  min  max range  skew kurtosis   se
## X1    1 115 3.32 0.55   3.27    3.31 0.54 1.36 4.64  3.27 -0.16     0.79 0.05
describe(male_patience)
##    vars  n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 92 3.36 0.47   3.36    3.35 0.54 2.45 4.45     2 0.09    -0.67 0.05

graph your results.

## create a graph for the reappraisal results (after calculating 95% CIs).
reapp_info <- patience_ERQ_gender_4 %>%
  group_by(sex) %>%
  summarise(
    n = n(), 
    sd = sd(reappraisal_avg, na.rm = TRUE), 
    reappraisal_avg = mean(reappraisal_avg, na.rm = TRUE) 
    ) %>%
  mutate(se = sd/sqrt(n))  %>% 
  mutate(ci = se * 1.96)
            
ggplot(reapp_info, aes(sex, reappraisal_avg)) +
  geom_col(fill = c("blue", "pink")) +
  geom_errorbar(aes(ymin = reappraisal_avg - ci, ymax = reappraisal_avg + ci), width = 0.3) +
  xlab("Gender") +
  ylab("Reappraisal Tendency")

## create a graph for the suppression results (after calcualting 95% CIs).
supp_info <- patience_ERQ_gender_4 %>%
  group_by(sex) %>%
  summarise(
    n = n(), 
    sd = sd(suppression_avg, na.rm = TRUE), 
    suppression_avg = mean(suppression_avg, na.rm = TRUE) 
    ) %>%
  mutate(se = sd/sqrt(n))  %>% 
  mutate(ci = se * 1.96)
            
ggplot(supp_info, aes(sex, suppression_avg)) +
  geom_col(fill = c("blue", "pink")) +
  geom_errorbar(aes(ymin = suppression_avg - ci, ymax = suppression_avg + ci), width = 0.3) +
  xlab("Gender") +
  ylab("Suppression Tendency")

## create a graph for the patience results (after calcualting 95% CIs).
patience_info <- patience_ERQ_gender_4 %>%
  group_by(sex) %>%
  summarise(
    n = n(), 
    sd = sd(patience_avg, na.rm = TRUE), 
    patience_avg = mean(patience_avg, na.rm = TRUE) 
    ) %>%
  mutate(se = sd/sqrt(n))  %>% 
  mutate(ci = se * 1.96)
            
ggplot(patience_info, aes(sex, patience_avg)) +
  geom_col(fill = c("blue", "pink")) +
  geom_errorbar(aes(ymin = patience_avg - ci, ymax = patience_avg + ci), width = 0.3) +
  xlab("Gender") +
  ylab("Patience")