1 Loading Libraries

# install any packages you have not previously used, then comment them back out.

#install.packages("car")
#install.packages("effsize")

library(psych) # for the describe() command
library(car) # for the leveneTest() command
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:psych':
## 
##     logit
library(effsize) # for the cohen.d() command
## 
## Attaching package: 'effsize'
## The following object is masked from 'package:psych':
## 
##     cohen.d

2 Importing Data

d <- read.csv(file="Data/projectdata.csv", header=T)

# For the HW, you will import the project dataset you cleaned previously
# This will be the dataset you'll use for HWs throughout the rest of the semester

3 State Your Hypothesis

We predict that there will be a significant difference in openness by age between 18 to 25 and 36 to 45.

[Remember to revise the above hypothesis in you HW assignment.]

4 Check Your Variables

# you **only** need to check the variables you're using in the current analysis

## Checking the Categorical variable (IV)

str(d)
## 'data.frame':    931 obs. of  7 variables:
##  $ X        : int  321 401 520 1390 1422 2247 2526 2609 2689 2752 ...
##  $ age      : chr  "1 under 18" "4 between 36 and 45" "1 under 18" "5 over 45" ...
##  $ mhealth  : chr  "none or NA" "obsessive compulsive disorder" "none or NA" "none or NA" ...
##  $ big5_open: num  4 6 3.67 3 2 ...
##  $ iou      : num  2.48 2.81 2.22 1.48 1.74 ...
##  $ mfq_state: num  6 5 3 3.5 3.25 ...
##  $ pas_covid: num  2.33 4 3 2.89 2.67 ...
# if the categorical variable you're using is showing as a "chr" (character), you must change it to be a ** factor ** -- using the next line of code (as.factor)

d$age <- as.factor(d$age)

str(d)
## 'data.frame':    931 obs. of  7 variables:
##  $ X        : int  321 401 520 1390 1422 2247 2526 2609 2689 2752 ...
##  $ age      : Factor w/ 5 levels "1 under 18","2 between 18 and 25",..: 1 4 1 5 1 1 1 1 4 5 ...
##  $ mhealth  : chr  "none or NA" "obsessive compulsive disorder" "none or NA" "none or NA" ...
##  $ big5_open: num  4 6 3.67 3 2 ...
##  $ iou      : num  2.48 2.81 2.22 1.48 1.74 ...
##  $ mfq_state: num  6 5 3 3.5 3.25 ...
##  $ pas_covid: num  2.33 4 3 2.89 2.67 ...
table(d$age, useNA = "always")
## 
##          1 under 18 2 between 18 and 25 3 between 26 and 35 4 between 36 and 45 
##                 607                  55                   6                  86 
##           5 over 45                <NA> 
##                 177                   0
## Checking the Continuous variable (DV)

# you can use the describe() command on an entire dataframe (d) or just on a single variable within your dataframe -- which we will do here

describe(d$big5_open)
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 931 5.24 1.11   5.33    5.33 0.99   1   7     6 -0.76     0.54 0.04
# also use a histogram to visualize your continuous variable

hist(d$big5_open)

# use the describeBy() command to view the means and standard deviations by group
# it's very similar to the describe() command but splits the dataframe according to the 'group' variable

describeBy(d$big5_open, group=d$age)
## 
##  Descriptive statistics by group 
## group: 1 under 18
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 607 5.26 1.14   5.33    5.36 0.99   1   7     6 -0.88     0.85 0.05
## ------------------------------------------------------------ 
## group: 2 between 18 and 25
##    vars  n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 55 5.38 1.02   5.33    5.44 0.99 3.33   7  3.67 -0.49    -0.82 0.14
## ------------------------------------------------------------ 
## group: 3 between 26 and 35
##    vars n mean   sd median trimmed  mad  min max range skew kurtosis   se
## X1    1 6 4.56 1.03   4.17    4.56 0.74 3.67   6  2.33 0.43    -1.91 0.42
## ------------------------------------------------------------ 
## group: 4 between 36 and 45
##    vars  n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 86 5.24 1.12   5.33    5.31 0.99   2   7     5 -0.66     0.18 0.12
## ------------------------------------------------------------ 
## group: 5 over 45
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 177 5.19 1.05   5.33    5.24 0.99   2   7     5 -0.45    -0.35 0.08
# lastly, use a boxplot to examine your chosen continuous and categorical variables together

boxplot(d$big5_open~d$age)

5 Check Your Assumptions

5.1 T-test Assumptions

  • IV must have 2 levels
  • Data values must be independent (independent t-test only)
  • Data obtained via a random sample
  • Dependent variable must be normally distributed
  • Variances of the two groups are approximately equal
# If the IV has more than 2 levels, you must DROP any additional levels in order to meet the first assumption of a t-test.

## NOTE: This is a FOUR STEP process!

d <- subset(d,  age != "1 under 18") # use subset() to remove all participants from the additional level

table(d$age, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##          1 under 18 2 between 18 and 25 3 between 26 and 35 4 between 36 and 45 
##                   0                  55                   6                  86 
##           5 over 45                <NA> 
##                 177                   0
d$age <- droplevels(d$age) # use droplevels() to drop the empty factor

table(d$age, useNA = "always") # verify that now the entire factor level is removed 
## 
## 2 between 18 and 25 3 between 26 and 35 4 between 36 and 45           5 over 45 
##                  55                   6                  86                 177 
##                <NA> 
##                   0
d <- subset(d,  age != "3 between 26 and 35") # use subset() to remove all participants from the additional level

table(d$age, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
## 2 between 18 and 25 3 between 26 and 35 4 between 36 and 45           5 over 45 
##                  55                   0                  86                 177 
##                <NA> 
##                   0
d$age <- droplevels(d$age) # use droplevels() to drop the empty factor

table(d$age, useNA = "always") # verify that now the entire factor level is removed
## 
## 2 between 18 and 25 4 between 36 and 45           5 over 45                <NA> 
##                  55                  86                 177                   0
d <- subset(d,  age != "5 over 45") # use subset() to remove all participants from the additional level

table(d$age, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
## 2 between 18 and 25 4 between 36 and 45           5 over 45                <NA> 
##                  55                  86                   0                   0
d$age <- droplevels(d$age) # use droplevels() to drop the empty factor

table(d$age, useNA = "always") # verify that now the entire factor level is removed
## 
## 2 between 18 and 25 4 between 36 and 45                <NA> 
##                  55                  86                   0
## Repeat ALL THE STEPS ABOVE if your IV has more levels that need to be DROPPED. Copy the 4 lines of code, and replace the level name in the subset() command.

5.2 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
# it 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(big5_open~age, data =d)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   1  0.3234 0.5705
##       139

Levene’s test revealed that our data does not have significantly different variances between the two comparison groups, 18 to 25 and 4 between 36 and 45, on their levels of openness.

[Revise the above statement for you HW assignment.]

When running a t-test, we can account for openness in our variance by using the Welch’s t-test, which does not have the same assumption about variance as the Student’s t-test (the general default type of t-test in statistics). R defaults to using Welch’s t-test so this doesn’t require any changes on our part! Even if your data has no issues with homogeneity of variance, you’ll still use Welch’s t-test – it handles the potential issues around variance well and there are no real downsides. We’re using Levene’s test here to get into the habit of checking the homogeneity of our variance, even if we already have the solution for any potential problems.

5.3 Issues with My Data

My independent variable has more than two levels. To proceed with this analysis, I will drop the under 18, 26 to 35, over 45 aged participants from my sample. I will make a note to discuss this issue in my method section write-up and in my discussion section as a limitation of my study.

6 Run a T-test

# Very simple! we use the same formula of y~x, where y is our DV and x is our IV

t_output <- t.test(d$big5_open~d$age)  # t_output will now show in your Global Environment

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$big5_open by d$age
## t = 0.79571, df = 122.76, p-value = 0.4277
## alternative hypothesis: true difference in means between group 2 between 18 and 25 and group 4 between 36 and 45 is not equal to 0
## 95 percent confidence interval:
##  -0.2162871  0.5070552
## sample estimates:
## mean in group 2 between 18 and 25 mean in group 4 between 36 and 45 
##                          5.381818                          5.236434

8 Calculate Cohen’s d - Effect Size

# once again, we use the same formula, y~x, to calculate cohen's d

# We **only** calculate effect size if the test is SIG!

# d_output will now show in your Global Environment

No calculation of Cohen’s d, because of the lack of significance.

9 View Effect Size

## Remember to always take the ABSOLAUTE VALUE of the effect size value (i.e., it will never be negative)

No Effect Size to measure, because Cohen’s d was not calculated.

10 Write Up Results

To test our hypothesis that people in the age groups of 18 to 25 and 36 to 45 in our sample would report a significant difference of openness, we used an independent samples t-test. This required us to drop participants under 18, between 26 to 35, and over 45 from our sample, as we are limited to a two-group comparison when using this test. We tested the homogeneity of variance with Levene’s test and found signs of homogeneity of variance (p = 0.571). We used Welch’s t-test, which does not assume homogeneity of variance.

Not as predicted, we found that participants between the age of 18 and 25 (M = 5.38, SD = 1.14) and participants between the age of 36 and 45 (M = 5.24, SD = 1.12) show no significant difference of openness; t(122.76) = 0.796, p = 0.428 (see Figure 1). There was no effect size calculated using Cohen’s d, because the hypothesis was insignificant.

[Revise the above statements for you HW assignment.]

References

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