AI Experiment Analysis

Loading Libraries

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

Importing Data

# # import your AI results dataset
d <- read.csv(file="Data/final_results.csv", header=T)

State Your Hypotheses & Chosen Tests

Participants who are exposed to negative portrayals of COVID-19 will have increased worry.

T-test

Participants who are extroverted will report lower worry

Correlation

Check Your Variables

This is just basic variable checking that is used across all HW assignments.

# # to view stats for all variables
# describe(d)
# 
# # 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.50   25.50 18.53  1.00  50.00 49.00  0.00
identity     2 50 50.20 29.60  51.50   50.40 38.55  2.00  99.00 97.00 -0.06
consent      3 50  1.46  0.50   1.00    1.45  0.00  1.00   2.00  1.00  0.16
age          4 50 45.40 18.44  35.50   42.98  5.19 23.00  86.00 63.00  1.04
race         5 50  4.06  1.25   4.00    3.98  1.48  2.00   6.00  4.00  0.62
gender       6 50  1.56  0.50   2.00    1.57  0.00  1.00   2.00  1.00 -0.23
manip_out    7 50 63.62 30.00  71.00   66.78 20.02  2.00 100.00 98.00 -1.00
survey1      8 50  3.73  0.15   3.75    3.73  0.19  3.25   4.00  0.75 -0.39
survey2      9 50  4.76  0.27   4.81    4.76  0.17  4.00   5.46  1.46 -0.24
ai_manip    10 50 49.28 36.28  48.50   48.98 51.89  1.00 100.00 99.00  0.02
condition   11 50  1.00  0.00   1.00    1.00  0.00  1.00   1.00  0.00   NaN
          kurtosis   se
id           -1.27 2.06
identity     -1.30 4.19
consent      -2.01 0.07
age          -0.33 2.61
race         -1.14 0.18
gender       -1.98 0.07
manip_out    -0.23 4.24
survey1       0.67 0.02
survey2       0.57 0.04
ai_manip     -1.80 5.13
condition      NaN 0.00
------------------------------------------------------------ 
condition: 2
          vars  n  mean    sd median trimmed   mad   min    max range  skew
id           1 50 25.50 14.58  25.50   25.50 18.53  1.00  50.00 49.00  0.00
identity     2 50 50.80 28.70  48.50   50.60 34.84  1.00 100.00 99.00  0.06
consent      3 50  1.46  0.50   1.00    1.45  0.00  1.00   2.00  1.00  0.16
age          4 50 41.68 14.25  35.00   39.52  4.45 20.00  89.00 69.00  1.38
race         5 50  3.78  1.30   3.00    3.62  0.00  1.00   7.00  6.00  0.95
gender       6 50  1.70  0.46   2.00    1.75  0.00  1.00   2.00  1.00 -0.85
manip_out    7 50 37.38 21.20  34.50   34.65 18.53  1.00  92.00 91.00  1.01
survey1      8 50  3.74  0.17   3.75    3.76  0.09  3.31   4.25  0.94 -0.41
survey2      9 50  3.74  0.17   3.75    3.76  0.09  3.31   4.25  0.94 -0.41
ai_manip    10 50 51.72 19.53  50.50   50.85 18.53 11.00  94.00 83.00  0.32
condition   11 50  2.00  0.00   2.00    2.00  0.00  2.00   2.00  0.00   NaN
          kurtosis   se
id           -1.27 2.06
identity     -1.25 4.06
consent      -2.01 0.07
age           1.31 2.01
race          0.27 0.18
gender       -1.31 0.07
manip_out     0.73 3.00
survey1       1.29 0.02
survey2       1.29 0.02
ai_manip     -0.26 2.76
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)

# 
# #convert any categorical variables to factors
d$condition <- as.factor(d$condition)

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 NO 
# # 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 a 38-year-old Black woman living in Atlanta. I’m passionate about my work as a community organizer, yet I o"| __truncated__ "I'm a 34-year-old Latina woman from San Antonio. Juggling work and family, I often worry about providing for my"| __truncated__ "I'm a 36-year-old White man living in Ohio. Balancing my job as a mechanic and raising two kids is tough. I wor"| __truncated__ "I’m a 28-year-old Latina woman living in Los Angeles. I’m passionate about art and use it as an escape from the"| __truncated__ ...
 $ consent  : chr  "I understand the instructions." "I understand these instructions." "I understand the instructions." "I understand the instructions." ...
 $ age      : int  38 34 36 28 82 34 32 63 49 82 ...
 $ race     : int  3 4 6 4 4 3 4 4 6 4 ...
 $ gender   : int  2 2 1 2 2 2 2 2 1 2 ...
 $ manip_out: chr  "Reading the article, I experienced a mix of hope and concern. The availability of a COVID-19 vaccine is certain"| __truncated__ "Reading the article \"Some Light in This Dark World (Pandemic)\" by Mya Fadda fills me with a mix of hope and r"| __truncated__ "Reading the article about the COVID-19 vaccine brings a mix of relief and cautious optimism. After months of un"| __truncated__ "Reading the article brings up a mix of emotions for me. On one hand, I'm relieved to hear about the COVID-19 va"| __truncated__ ...
 $ survey1  : num  3.94 3.56 3.81 3.69 3.62 ...
 $ survey2  : num  4.92 4.85 4.85 4.54 5 ...
 $ ai_manip : chr  "Reading the article heightened my worries about the disparity in vaccine access among marginalized communities."| __truncated__ "Reading the article filled me with a mix of hope and relief, prompting worries about my children's health and t"| __truncated__ "Reading about the COVID-19 vaccine impacted my worries significantly. It brought a sense of relief and cautious"| __truncated__ "Reading the article impacted my worries by highlighting both hope and uncertainty. While the potential of the C"| __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
d$condition <- as.factor(d$condition)

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  0.0284 0.8665
      98               

Pearson’s Correlation Coefficient Assumptions

  • Should have two measurements for each participant for each variable (confirmed by earlier procedures – we dropped any participants with missing data)
  • Variables should be continuous and normally distributed, or assessments of the relationship may be inaccurate (will do below)
  • Outliers should be identified and removed, or results will be inaccurate (will do below)
  • Relationship between the variables should be linear, or they will not be detected (will do below)

Run a Multiple Linear Regression

To check the assumptions for Pearson’s correlation coefficient, we run our regression and then check our diagnostic plots.

# # second hypothesis use the lm() command to run the regression
# # dependent/outcome variable on the left, independent/predictor variables on the right
reg_model <- lm(survey1 ~ survey2 , data = d)

Check linearity with Residuals vs Fitted plot

For some examples of good Residuals vs Fitted plot and ones that show serious errors, check out this page.

For your homework, you’ll simply need to generate this plot and talk about how your plot compares to the good and problematic plots linked to above. Is it closer to the ‘good’ plots or one of the ‘bad’ plots? This is going to be a judgement call, and that’s okay! In practice, you’ll always be making these judgement calls as part of a team, so this assignment is just about getting experience with it, not making the perfect call.

plot(reg_model, 1)

Check for outliers using Cook’s distance and a Residuals vs Leverage plot

For your homework, you’ll simply need to generate these plots, assess Cook’s distance in your dataset, and then identify any potential cases that are prominent outliers.

# # Cook's distance
plot(reg_model, 4)

# # Residuals vs Leverage
plot(reg_model, 5)

You can check out this page for some other examples of this type of plot. (Notice that the Scale-Location plot is the third in the grids.)

For your homework, you’ll simply need to generate this plot and talk about how your plot compares to the ones pictured. Is it closer to the ‘good’ plots or one of the ‘bad’ plots? Again, this is a judgement call! It’s okay if feel uncertain, and you won’t be penalized for that.

plot(reg_model, 3)

Issues with My Data

Describe any issues and why they’re problematic here.

  • While checking linearity with the residuals vs fitted plot, we found some issues with linearity, the plot was more similar to a bad plot than a good one. -In Cook’s distance and a Residuals vs Leverage plot we found no outliers -For Levene’s Test for homogeneity of variance, we found it to be not significant.

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_output <- t.test(d$survey1~d$condition)

View Test Output (not significant)

t_output

    Welch Two Sample t-test

data:  d$survey1 by d$condition
t = -0.35006, df = 95.408, p-value = 0.7271
alternative hypothesis: true difference in means between group 1 and group 2 is not equal to 0
95 percent confidence interval:
 -0.07504729  0.05254729
sample estimates:
mean in group 1 mean in group 2 
        3.73125         3.74250 

Calculate Cohen’s d

# # once again, we use our formula to calculate cohen's d
# d_output <- cohen.d(d$pss~d$pet)

View Effect Size

  • Trivial: < .2
  • Small: between .2 and .5
  • Medium: between .5 and .8
  • Large: > .8
#d_output

Run a Correlation Test

Create a Correlation Matrix

d2 <- subset(d, select=c(survey1, survey2))
corr_output_m <- corr.test(d2)

View Test Output

  • Strong effect: Between |0.50| and |1|
  • Moderate effect: Between |0.30| and |0.49|
  • Weak effect: Between |0.10| and |0.29|
  • Trivial effect: Less than |0.09|
corr_output_m
Call:corr.test(x = d2)
Correlation matrix 
        survey1 survey2
survey1    1.00    0.12
survey2    0.12    1.00
Sample Size 
[1] 100
Probability values (Entries above the diagonal are adjusted for multiple tests.) 
        survey1 survey2
survey1    0.00    0.25
survey2    0.25    0.00

 To see confidence intervals of the correlations, print with the short=FALSE option
#not significant

Write Up Results

t-Test

  • Our results for the T-test were not significant for hypothesis 1,

Write-up of your results goes here. Check past labs/HWs for template.

Correlation Test

Write-up of your results goes here. Check past labs/HWs for template. Depending on how many variables you have here, I may need to help you tweak your table output.

  • The correlation test was not significant for hypothesis 2
Variable M SD 1
Worry 3.74 0.16
Extraversion 4.25 0.56 .12
[-.08, .31]
Note:
M and SD are used to represent mean and standard deviation, respectively. Values in square brackets indicate the 95% confidence interval. The confidence interval is a plausible range of population correlations that could have caused the sample correlation.
* indicates p < .05
** indicates p < .01.

References

Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.