library(afex) # to run the ANOVA and plot results
library(psych) # for the describe() command
library(ggplot2) # to visualize our results
library(expss) # for the cross_cases() command
library(car) # for the leveneTest() command
library(emmeans) # for posthoc tests
library(effsize) # for the cohen.d() command
library(apaTables) # to create our correlation table
library(kableExtra) # to create our correlation table
library(sjPlot) # to visualize our results
AI Experiment Analysis
Loading Libraries
Importing Data
# # import your AI results dataset
<- read.csv(file="Data/final_resultsc.csv", header=T) d
State Your Hypotheses & Chosen Tests
H1:Participants who experience higher levels of experiencing isolation will report increased levels of worry compared to participants who experienced lower levels of experiencing isolation.
H2:Participants who experience higher levels of experiencing isolated and worry will report lower levels of self-esteem.
Both of these hypotheses will be tested using a t-test.
Check Your Variables
This is just basic variable checking that is used across all HW assignments.
# to view stats for all variables
describe(d)
vars n mean sd median trimmed mad min max range skew
id 1 100 50.50 29.01 50.5 50.50 37.06 1.0 100.0 99.0 0.00
identity* 2 100 50.50 29.01 50.5 50.50 37.06 1.0 100.0 99.0 0.00
consent* 3 100 1.99 0.10 2.0 2.00 0.00 1.0 2.0 1.0 -9.70
age 4 100 37.40 11.84 34.0 35.70 2.97 18.0 84.0 66.0 1.75
race 5 100 4.47 1.71 4.5 4.56 2.22 1.0 7.0 6.0 -0.26
gender 6 100 2.05 0.61 2.0 2.00 0.00 1.0 7.0 6.0 6.34
manip_out* 7 100 50.50 29.01 50.5 50.50 37.06 1.0 100.0 99.0 0.00
survey1 8 100 3.01 0.48 3.0 3.03 0.74 2.0 3.6 1.6 -0.24
survey2 9 100 2.70 0.23 2.7 2.71 0.15 1.9 3.3 1.4 -0.64
ai_manip* 10 100 50.50 29.01 50.5 50.50 37.06 1.0 100.0 99.0 0.00
Condition 11 100 1.50 0.50 1.5 1.50 0.74 1.0 2.0 1.0 0.00
kurtosis se
id -1.24 2.90
identity* -1.24 2.90
consent* 93.06 0.01
age 3.77 1.18
race -1.48 0.17
gender 46.33 0.06
manip_out* -1.24 2.90
survey1 -1.45 0.05
survey2 2.27 0.02
ai_manip* -1.24 2.90
Condition -2.02 0.05
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d, group = "Condition")
Descriptive statistics by group
Condition: 1
vars n mean sd median trimmed mad min max range skew
id 1 50 25.50 14.58 25.5 25.50 18.53 1.0 50.0 49.0 0.00
identity 2 50 50.32 31.22 47.5 50.23 43.74 1.0 99.0 98.0 0.02
consent 3 50 2.00 0.00 2.0 2.00 0.00 2.0 2.0 0.0 NaN
age 4 50 37.20 11.33 34.0 35.35 3.71 21.0 79.0 58.0 1.97
race 5 50 4.44 1.72 4.5 4.50 2.22 2.0 7.0 5.0 -0.19
gender 6 50 2.14 0.83 2.0 2.00 0.00 1.0 7.0 6.0 4.72
manip_out 7 50 66.44 26.42 71.5 68.97 21.50 1.0 100.0 99.0 -0.84
survey1 8 50 2.98 0.45 3.0 2.98 0.74 2.5 3.5 1.0 0.09
survey2 9 50 2.71 0.19 2.7 2.71 0.15 2.1 3.2 1.1 -0.18
ai_manip 10 50 47.90 28.20 48.5 47.67 30.39 1.0 100.0 99.0 0.01
Condition 11 50 1.00 0.00 1.0 1.00 0.00 1.0 1.0 0.0 NaN
kurtosis se
id -1.27 2.06
identity -1.43 4.41
consent NaN 0.00
age 4.39 1.60
race -1.66 0.24
gender 22.99 0.12
manip_out -0.42 3.74
survey1 -1.78 0.06
survey2 1.98 0.03
ai_manip -1.09 3.99
Condition NaN 0.00
------------------------------------------------------------
Condition: 2
vars n mean sd median trimmed mad min max range skew
id 1 50 75.50 14.58 75.5 75.50 18.53 51.0 100.0 49.0 0.00
identity 2 50 50.68 26.94 53.5 50.73 31.13 2.0 100.0 98.0 -0.03
consent 3 50 1.98 0.14 2.0 2.00 0.00 1.0 2.0 1.0 -6.65
age 4 50 37.60 12.44 34.0 36.10 2.97 18.0 84.0 66.0 1.53
race 5 50 4.50 1.72 5.0 4.62 1.48 1.0 7.0 6.0 -0.33
gender 6 50 1.96 0.20 2.0 2.00 0.00 1.0 2.0 1.0 -4.55
manip_out 7 50 34.56 22.00 35.5 32.92 23.72 2.0 96.0 94.0 0.51
survey1 8 50 3.04 0.51 3.2 3.08 0.44 2.0 3.6 1.6 -0.50
survey2 9 50 2.70 0.26 2.7 2.71 0.15 1.9 3.3 1.4 -0.73
ai_manip 10 50 53.10 29.86 57.5 53.23 43.00 4.0 99.0 95.0 -0.04
Condition 11 50 2.00 0.00 2.0 2.00 0.00 2.0 2.0 0.0 NaN
kurtosis se
id -1.27 2.06
identity -1.09 3.81
consent 43.12 0.02
age 3.02 1.76
race -1.35 0.24
gender 19.13 0.03
manip_out -0.11 3.11
survey1 -1.24 0.07
survey2 1.53 0.04
ai_manip -1.44 4.22
Condition NaN 0.00
# also use histograms and scatterplots to examine your continuous variables
hist(d$survey1)
hist(d$survey2)
plot(d$survey1, d$survey2)
# and table() and cross_cases() to examine your categorical variables
# you may not need the cross_cases code
table(d$Condition)
1 2
50 50
#cross_cases(d, IV1, IV2)
# and boxplot to examine any categorical variables with continuous variables
boxplot(d$survey1~d$Condition)
boxplot(d$survey2~d$Condition)
#convert any categorical variables to factors
$Condition <- as.factor(d$Condition) d
Check Your Assumptions
t-Test Assumptions
- Data values must be independent (independent t-test only) (confirmed by data report)
- Data obtained via a random sample (confirmed by data report)
- IV must have two levels (will check below)
- Dependent variable must be normally distributed (will check below. if issues, note and proceed)
- Variances of the two groups must be approximately equal, aka ‘homogeneity of variance’. Lacking this makes our results inaccurate (will check below - this really only applies to Student’s t-test, but we’ll check it anyway)
Checking IV levels
# preview the levels and counts for your IV
table(d$Condition, useNA = "always")
1 2 <NA>
50 50 0
# note that the table() output shows you exactly how the levels of your variable are written. when recoding, make sure you are spelling them exactly as they appear
# to drop levels from your variable
# this subsets the data and says that any participant who is coded as 'BAD' should be removed
#d <- subset(d, IV != "BAD")
#table(d$iv, useNA = "always")
# to combine levels
# this says that where any participant is coded as 'BAD' it should be replaced by 'GOOD'
#d$iv_rc[d$iv == "BAD"] <- "GOOD"
#table(d$iv, useNA = "always")
# check your variable types
str(d)
'data.frame': 100 obs. of 11 variables:
$ id : int 1 2 3 4 5 6 7 8 9 10 ...
$ identity : chr "I’m 34, an Asian woman navigating life as a software developer in Silicon Valley. I often worry about being ove"| __truncated__ "I’m a 34-year-old Asian woman named Mei, striving to balance my ambitious career in tech with my family's expec"| __truncated__ "I’m a 42-year-old Black woman named Jasmine, who works as a healthcare professional. My friends describe me as "| __truncated__ "I’m a 32-year-old Asian woman named Mei, living in San Francisco. I often find myself torn between my cultural "| __truncated__ ...
$ consent : chr "I understand these instructions." "I understand these instructions." "I understand these instructions." "I understand these instructions." ...
$ age : int 34 34 42 32 31 21 44 37 33 29 ...
$ race : int 2 2 3 2 6 6 6 2 6 6 ...
$ gender : int 2 2 2 2 2 2 2 2 2 2 ...
$ manip_out: chr "There was a time during a critical project at work when I felt completely isolated. I was the only woman in a t"| __truncated__ "A few months ago, I attended a tech conference where I was one of the few women of color in the room. Surrounde"| __truncated__ "There was a period in my life when I felt completely isolated, and it was during a particularly demanding time "| __truncated__ "There was a time when I felt completely isolated during a pivotal moment in my career. I had just taken on a hi"| __truncated__ ...
$ survey1 : num 2.5 3 3.5 2.5 3 3.5 2.5 3.5 2.5 2.5 ...
$ survey2 : num 2.7 2.6 2.9 2.7 2.7 2.6 2.8 2.6 3.2 2.7 ...
$ ai_manip : chr "I answered the questions based on my experiences with isolation and the impact it has on my self-esteem and men"| __truncated__ "I answered the questions reflecting my feelings of isolation and self-doubt as a woman of color in tech. My exp"| __truncated__ "I answered based on my experiences of feeling isolated during stressful times at work, which amplified my self-"| __truncated__ "I answered the questions based on my experiences of isolation during a high-pressure project, which magnified m"| __truncated__ ...
$ Condition: Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
# make sure that your IV is recognized as a factor by R
# if you created a new _rc variable make sure to use that one instead
$Condition <- as.factor(d$Condition) d
Testing Homogeneity of Variance with Levene’s Test
We can test whether the variances of our two groups are equal using Levene’s test. The null hypothesis is that the variance between the two groups is equal, which is the result we want. So when running Levene’s test we’re hoping for a non-significant result!
# use the leveneTest() command from the car package to test homogeneity of variance
# uses the same 'formula' setup that we'll use for our t-test: formula is y~x, where y is our DV and x is our IV
leveneTest(survey1~Condition, data = d)
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 1 1.2613 0.2642
98
t-Test Assumptions
- Data values must be independent (independent t-test only) (confirmed by data report)
- Data obtained via a random sample (confirmed by data report)
- IV must have two levels (will check below)
- Dependent variable must be normally distributed (will check below. if issues, note and proceed)
- Variances of the two groups must be approximately equal, aka ‘homogeneity of variance’. Lacking this makes our results inaccurate (will check below - this really only applies to Student’s t-test, but we’ll check it anyway)
Checking IV levels
# # preview the levels and counts for your IV
table(d$Condition, useNA = "always")
1 2 <NA>
50 50 0
#
# # note that the table() output shows you exactly how the levels of your variable are written. when recoding, make sure you are spelling them exactly as they appear
#
# # to drop levels from your variable
# # this subsets the data and says that any participant who is coded as 'BAD' should be removed
# d <- subset(d, IV != "BAD")
#
# table(d$iv, useNA = "always")
#
# # to combine levels
# # this says that where any participant is coded as 'BAD' it should be replaced by 'GOOD'
# d$iv_rc[d$iv == "BAD"] <- "GOOD"
#
# table(d$iv, useNA = "always")
#
# # check your variable types
str(d)
'data.frame': 100 obs. of 11 variables:
$ id : int 1 2 3 4 5 6 7 8 9 10 ...
$ identity : chr "I’m 34, an Asian woman navigating life as a software developer in Silicon Valley. I often worry about being ove"| __truncated__ "I’m a 34-year-old Asian woman named Mei, striving to balance my ambitious career in tech with my family's expec"| __truncated__ "I’m a 42-year-old Black woman named Jasmine, who works as a healthcare professional. My friends describe me as "| __truncated__ "I’m a 32-year-old Asian woman named Mei, living in San Francisco. I often find myself torn between my cultural "| __truncated__ ...
$ consent : chr "I understand these instructions." "I understand these instructions." "I understand these instructions." "I understand these instructions." ...
$ age : int 34 34 42 32 31 21 44 37 33 29 ...
$ race : int 2 2 3 2 6 6 6 2 6 6 ...
$ gender : int 2 2 2 2 2 2 2 2 2 2 ...
$ manip_out: chr "There was a time during a critical project at work when I felt completely isolated. I was the only woman in a t"| __truncated__ "A few months ago, I attended a tech conference where I was one of the few women of color in the room. Surrounde"| __truncated__ "There was a period in my life when I felt completely isolated, and it was during a particularly demanding time "| __truncated__ "There was a time when I felt completely isolated during a pivotal moment in my career. I had just taken on a hi"| __truncated__ ...
$ survey1 : num 2.5 3 3.5 2.5 3 3.5 2.5 3.5 2.5 2.5 ...
$ survey2 : num 2.7 2.6 2.9 2.7 2.7 2.6 2.8 2.6 3.2 2.7 ...
$ ai_manip : chr "I answered the questions based on my experiences with isolation and the impact it has on my self-esteem and men"| __truncated__ "I answered the questions reflecting my feelings of isolation and self-doubt as a woman of color in tech. My exp"| __truncated__ "I answered based on my experiences of feeling isolated during stressful times at work, which amplified my self-"| __truncated__ "I answered the questions based on my experiences of isolation during a high-pressure project, which magnified m"| __truncated__ ...
$ Condition: Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
#
# # make sure that your IV is recognized as a factor by R
# # if you created a new _rc variable make sure to use that one instead
$Condition <- as.factor(d$Condition) d
Testing Homogeneity of Variance with Levene’s Test
We can test whether the variances of our two groups are equal using Levene’s test. The null hypothesis is that the variance between the two groups is equal, which is the result we want. So when running Levene’s test we’re hoping for a non-significant result!
# use the leveneTest() command from the car package to test homogeneity of variance
# uses the same 'formula' setup that we'll use for our t-test: formula is y~x, where y is our DV and x is our IV
leveneTest(survey2~Condition, data = d)
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 1 3.0236 0.0852 .
98
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Issues with My Data
With Checking my IV levels, I did not run code to drop or combine levels since the IV already had two levels. Confirmed homogeneity of variance using Levene’s test with Condition and suvery1 being non-significant (p value = 0.2642). However, When running the levene’s test for condition and survey2, the results showed to be significant (p value = 0.0852). This makes it that condition and survey2 are heterogeneity of variance and can be a problem since it can lead to inaccurate results. As a result, Welch’s t-test will be used.
Run Your Analysis
Run a t-Test
# very simple! we specify the dataframe alongside the variables instead of having a separate argument for the dataframe like we did for leveneTest()
<- t.test(d$survey1~d$Condition) t_output
View Test Output
t_output
Welch Two Sample t-test
data: d$survey1 by d$Condition
t = -0.607, df = 96.364, p-value = 0.5453
alternative hypothesis: true difference in means between group 1 and group 2 is not equal to 0
95 percent confidence interval:
-0.2476603 0.1316603
sample estimates:
mean in group 1 mean in group 2
2.984 3.042
Calculate Cohen’s d
# # once again, we use our formula to calculate cohen's d
<- cohen.d(d$survey1~d$Condition) d_output
View Effect Size
- Trivial: < .2
- Small: between .2 and .5
- Medium: between .5 and .8
- Large: > .8
d_output
Cohen's d
d estimate: -0.1213997 (negligible)
95 percent confidence interval:
lower upper
-0.5186586 0.2758592
Run a t-Test
# very simple! we specify the dataframe alongside the variables instead of having a separate argument for the dataframe like we did for leveneTest()
<- t.test(d$survey2~d$Condition) t_output
View Test Output
t_output
Welch Two Sample t-test
data: d$survey2 by d$Condition
t = 0.21945, df = 88.457, p-value = 0.8268
alternative hypothesis: true difference in means between group 1 and group 2 is not equal to 0
95 percent confidence interval:
-0.08055134 0.10055134
sample estimates:
mean in group 1 mean in group 2
2.706 2.696
Calculate Cohen’s d
# once again, we use our formula to calculate cohen's d
<- cohen.d(d$survey2~d$Condition) d_output
View Effect Size
- Trivial: < .2
- Small: between .2 and .5
- Medium: between .5 and .8
- Large: > .8
d_output
Cohen's d
d estimate: 0.04388995 (negligible)
95 percent confidence interval:
lower upper
-0.3530513 0.4408312
Write Up Results
t-Test
I tested my hypothesis was participants who experience higher levels of experiencing isolation will report increased levels of worry compared to participants who experienced lower levels of experiencing isolation, by using an independent samples t-test. The data met all of the assumptions of a t-test, however, it did not find a significant difference, t = (96.364) = -0.607, p = 0.5453, d = -0.12, 95% [-0.24, 0.13]. (refer to figure).
The effect size was negligible according to the Cohen (1988).
t-Test
Tested my hypothesis participants who experience higher levels of experiencing isolated and worry will report lower levels of self-esteem, by using Welch Two sample t-test. The data was found to be heterogeneity of variance not homogeneity of variance after checking it with the Levene’s test. However, it still did not find a significant difference, t = (88.457) = 0.21945, p = 0.8268, d = 0.04, 95% [-0.08, 0.10]. (refer to figure 2).
The effect size was negligible according to the Cohen (1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.