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

There will be a significant difference in Openness by the people’s level of Education between equivalently to not completing high school and AP/IB completion

Note: Conscientiousness was measured using the International Personality Item Pool (IPIP) in this dataset.

[Remember to revise the above hypothesis in you HW assignment. Then delete line 45 and this reminder.]

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':    687 obs. of  7 variables:
##  $ X          : int  520 2814 3146 3295 717 6056 4753 5365 1965 1246 ...
##  $ education  : chr  "1 equivalent to not completing high school" "prefer not to say" "2 equivalent to high school completion" "prefer not to say" ...
##  $ ethnicity  : chr  "Prefer not to say" "White - British, Irish, other" "Asian/Asian British - Indian, Pakistani, Bangladeshi, other" "Asian/Asian British - Indian, Pakistani, Bangladeshi, other" ...
##  $ big5_open  : num  3.67 4.33 5.67 6 5.67 ...
##  $ mfq_state  : num  3 4.38 4.88 4.88 4.88 ...
##  $ gad        : num  1.14 1.29 1 1 1.14 ...
##  $ isolation_c: num  1 1 1 1 1 1 1 1 1 1 ...
# 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$education <- as.factor(d$education)

str(d)
## 'data.frame':    687 obs. of  7 variables:
##  $ X          : int  520 2814 3146 3295 717 6056 4753 5365 1965 1246 ...
##  $ education  : Factor w/ 6 levels "1 equivalent to not completing high school",..: 1 6 2 6 1 1 1 2 1 1 ...
##  $ ethnicity  : chr  "Prefer not to say" "White - British, Irish, other" "Asian/Asian British - Indian, Pakistani, Bangladeshi, other" "Asian/Asian British - Indian, Pakistani, Bangladeshi, other" ...
##  $ big5_open  : num  3.67 4.33 5.67 6 5.67 ...
##  $ mfq_state  : num  3 4.38 4.88 4.88 4.88 ...
##  $ gad        : num  1.14 1.29 1 1 1.14 ...
##  $ isolation_c: num  1 1 1 1 1 1 1 1 1 1 ...
table(d$education, useNA = "always")
## 
##              1 equivalent to not completing high school 
##                                                     276 
##                  2 equivalent to high school completion 
##                                                     259 
## 3 equivalent to vocational/technical program completion 
##                                                       3 
##                        4 equivalent to AP/IB completion 
##                                                      91 
##                                  5 undergraduate degree 
##                                                       1 
##                                       prefer not to say 
##                                                      57 
##                                                    <NA> 
##                                                       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 687 5.25 1.13   5.33    5.35 0.99   1   7     6 -0.82     0.72 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$education)
## 
##  Descriptive statistics by group 
## group: 1 equivalent to not completing high school
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 276  5.2 1.23   5.33    5.32 0.99   1   7     6 -0.95        1 0.07
## ------------------------------------------------------------ 
## group: 2 equivalent to high school completion
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 259 5.33 1.05   5.33     5.4 0.99   2   7     5 -0.68     0.31 0.07
## ------------------------------------------------------------ 
## group: 3 equivalent to vocational/technical program completion
##    vars n mean   sd median trimmed  mad  min  max range  skew kurtosis   se
## X1    1 3 5.89 0.51      6    5.89 0.49 5.33 6.33     1 -0.21    -2.33 0.29
## ------------------------------------------------------------ 
## group: 4 equivalent to AP/IB completion
##    vars  n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 91 5.27 1.05   5.33    5.35 0.99 2.33   7  4.67 -0.66     0.16 0.11
## ------------------------------------------------------------ 
## group: 5 undergraduate degree
##    vars n mean sd median trimmed mad  min  max range skew kurtosis se
## X1    1 1 5.33 NA   5.33    5.33   0 5.33 5.33     0   NA       NA NA
## ------------------------------------------------------------ 
## group: prefer not to say
##    vars  n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 57 5.08 1.15   5.33    5.11 1.48 2.67   7  4.33 -0.31    -0.99 0.15
# lastly, use a boxplot to examine your chosen continuous and categorical variables together

boxplot(d$big5_open~d$education)

5 Check Your Assumptions

5.1 T-test Assumptions

  • IV must have two 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 approx. 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, education != "2 equivalent to high school completion") # use subset() to remove all participants from the additional level

table(d$education, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##              1 equivalent to not completing high school 
##                                                     276 
##                  2 equivalent to high school completion 
##                                                       0 
## 3 equivalent to vocational/technical program completion 
##                                                       3 
##                        4 equivalent to AP/IB completion 
##                                                      91 
##                                  5 undergraduate degree 
##                                                       1 
##                                       prefer not to say 
##                                                      57 
##                                                    <NA> 
##                                                       0
 d$education <- droplevels(d$education) # use droplevels() to drop the empty factor

table(d$education, useNA = "always") # verify that now the entire factor level is removed 
## 
##              1 equivalent to not completing high school 
##                                                     276 
## 3 equivalent to vocational/technical program completion 
##                                                       3 
##                        4 equivalent to AP/IB completion 
##                                                      91 
##                                  5 undergraduate degree 
##                                                       1 
##                                       prefer not to say 
##                                                      57 
##                                                    <NA> 
##                                                       0
 d <- subset(d, education != "3 equivalent to vocational/technical program completion") # use subset() to remove all participants from the additional level

table(d$education, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##              1 equivalent to not completing high school 
##                                                     276 
## 3 equivalent to vocational/technical program completion 
##                                                       0 
##                        4 equivalent to AP/IB completion 
##                                                      91 
##                                  5 undergraduate degree 
##                                                       1 
##                                       prefer not to say 
##                                                      57 
##                                                    <NA> 
##                                                       0
 d$education <- droplevels(d$education) # use droplevels() to drop the empty factor

table(d$education, useNA = "always") # verify that now the entire factor level is removed 
## 
## 1 equivalent to not completing high school 
##                                        276 
##           4 equivalent to AP/IB completion 
##                                         91 
##                     5 undergraduate degree 
##                                          1 
##                          prefer not to say 
##                                         57 
##                                       <NA> 
##                                          0
 d <- subset(d, education != "5 undergraduate degree") # use subset() to remove all participants from the additional level

table(d$education, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
## 1 equivalent to not completing high school 
##                                        276 
##           4 equivalent to AP/IB completion 
##                                         91 
##                     5 undergraduate degree 
##                                          0 
##                          prefer not to say 
##                                         57 
##                                       <NA> 
##                                          0
 d$education <- droplevels(d$education) # use droplevels() to drop the empty factor

table(d$education, useNA = "always") # verify that now the entire factor level is removed 
## 
## 1 equivalent to not completing high school 
##                                        276 
##           4 equivalent to AP/IB completion 
##                                         91 
##                          prefer not to say 
##                                         57 
##                                       <NA> 
##                                          0
 d <- subset(d, education != "prefer not to say") # use subset() to remove all participants from the additional level

table(d$education, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
## 1 equivalent to not completing high school 
##                                        276 
##           4 equivalent to AP/IB completion 
##                                         91 
##                          prefer not to say 
##                                          0 
##                                       <NA> 
##                                          0
 d$education <- droplevels(d$education) # use droplevels() to drop the empty factor

table(d$education, useNA = "always") # verify that now the entire factor level is removed 
## 
## 1 equivalent to not completing high school 
##                                        276 
##           4 equivalent to AP/IB completion 
##                                         91 
##                                       <NA> 
##                                          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~education, data =d)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   1  1.5446 0.2147
##       365

Levene’s test revealed that our data has no sig differences variances between the two comparison education levels, not completing high school and AP/IB completion, on their levels of openness.

When running a t-test, we can account for heterogeneity 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 non-binary participants from my sample. I will make a note to discuss this issue in my Methods section write-up and in my Discussion section as a limitation of my study.

My data also has an issue regarding homogeneity of variance, as Levene’s test was not significant. To accommodate for this heterogeneity of variance, I will use Welch’s t-test instead of Student’s t-test in my analysis.

[Revise the above statements for you HW assignment. Then delete this reminder.]

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$education)  # 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$education
## t = -0.5049, df = 177.51, p-value = 0.6143
## alternative hypothesis: true difference in means between group 1 equivalent to not completing high school and group 4 equivalent to AP/IB completion is not equal to 0
## 95 percent confidence interval:
##  -0.3286548  0.1947428
## sample estimates:
## mean in group 1 equivalent to not completing high school 
##                                                 5.204106 
##           mean in group 4 equivalent to AP/IB completion 
##                                                 5.271062

8 Calculate Cohen’s d - Effect Size

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

9 Write Up Results

To test our hypothesis that people’s Openness would report significantly different levels based on education levels, we used an independent-samples t-test . This required us to drop our completing high school, undergraduate degree, and prefer not to say from our sample, as we are limited to a two-level comparison when using this test. We tested the homogeneity of variance with Levene’s test and found signs of heterogeneity (p = 0.61). This suggests that there is an increased chance of Type I error. To correct for this issue, we used Welch’s t-test, which does not assume homogeneity of variance. Our data met all other assumptions of an independent samples t-test.

Contrary to my prediction, I found that people who did not complete high school (M = 5.20 , SD = 1.23) did not report significantly higher levels of openness than people do completed an AP/IB program (M = 5.27, SD = 1.05 ); t(-0.50) = 177.51, p= .0.61 (see Figure 1).

[Revise the above statements for you HW assignment.]

References

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