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 stress levels by people’s level of physical symptoms between low number of symptoms and high number of symptoms.

Note: Stress levels were measured using the Perceived Stress Questionnaire, and physical symptoms were measured using a Patient Health Questionnaire.

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':    3119 obs. of  7 variables:
##  $ ResponseID  : chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ phys_sym    : chr  "high number of symptoms" "high number of symptoms" "high number of symptoms" "high number of symptoms" ...
##  $ usdream     : chr  "american dream is important and achievable for me" "american dream is important and achievable for me" "american dream is not important and maybe not achievable for me" "american dream is not important and maybe not achievable for me" ...
##  $ moa_maturity: num  3.67 3.33 3.67 3 3.67 ...
##  $ idea        : num  3.75 3.88 3.75 3.75 3.5 ...
##  $ mindful     : num  2.4 1.8 2.2 2.2 3.2 ...
##  $ stress      : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
# 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$phys_sym <- as.factor(d$phys_sym)

str(d)
## 'data.frame':    3119 obs. of  7 variables:
##  $ ResponseID  : chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ phys_sym    : Factor w/ 3 levels "high number of symptoms",..: 1 1 1 1 2 1 1 3 3 1 ...
##  $ usdream     : chr  "american dream is important and achievable for me" "american dream is important and achievable for me" "american dream is not important and maybe not achievable for me" "american dream is not important and maybe not achievable for me" ...
##  $ moa_maturity: num  3.67 3.33 3.67 3 3.67 ...
##  $ idea        : num  3.75 3.88 3.75 3.75 3.5 ...
##  $ mindful     : num  2.4 1.8 2.2 2.2 3.2 ...
##  $ stress      : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
table(d$phys_sym, useNA = "always")
## 
##   high number of symptoms    low number of symptoms medium number of symptoms 
##                       856                       585                      1678 
##                      <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$stress)
##    vars    n mean  sd median trimmed  mad min max range skew kurtosis   se
## X1    1 3119 3.05 0.6      3    3.05 0.59 1.3 4.7   3.4 0.04    -0.17 0.01
# also use a histogram to visualize your continuous variable

hist(d$stress)

# 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$stress, group=d$phys_sym)
## 
##  Descriptive statistics by group 
## group: high number of symptoms
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 856 3.38 0.55    3.4    3.38 0.59 1.6 4.7   3.1 -0.05    -0.22 0.02
## ------------------------------------------------------------ 
## group: low number of symptoms
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 585  2.7 0.55    2.7     2.7 0.59 1.4 4.4     3 0.06    -0.26 0.02
## ------------------------------------------------------------ 
## group: medium number of symptoms
##    vars    n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 1678    3 0.56      3       3 0.59 1.3 4.7   3.4 0.08    -0.01 0.01
# lastly, use a boxplot to examine your chosen continuous and categorical variables together

boxplot(d$stress~d$phys_sym)

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 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, phys_sym != "medium number of symptoms") # use subset() to remove all participants from the additional level

table(d$phys_sym, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##   high number of symptoms    low number of symptoms medium number of symptoms 
##                       856                       585                         0 
##                      <NA> 
##                         0
d$phys_sym <- droplevels(d$phys_sym) # use droplevels() to drop the empty factor

table(d$phys_sym, useNA = "always") # verify that now the entire factor level is removed 
## 
## high number of symptoms  low number of symptoms                    <NA> 
##                     856                     585                       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(stress~phys_sym, data =d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value Pr(>F)
## group    1   6e-04   0.98
##       1439

Levene’s test revealed that our data DOES NOT have significantly difference variances between the two comparison groups, low number of symptoms and high number of symptoms, on their levels of stress.

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 participants who responded with “medium number of physical symptoms” 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.

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$stress~d$phys_sym)  # t_output will now show in your Global Environment

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$stress by d$phys_sym
## t = 23.242, df = 1255.3, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group high number of symptoms and group low number of symptoms is not equal to 0
## 95 percent confidence interval:
##  0.6256091 0.7409626
## sample estimates:
## mean in group high number of symptoms  mean in group low number of symptoms 
##                              3.382944                              2.699658

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 <- cohen.d(d$stress~d$phys_sym)  # d_output will now show in your Global Environment

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: 1.246593 (large)
## 95 percent confidence interval:
##    lower    upper 
## 1.131929 1.361256
## Remember to always take the ABSOLUTE VALUE of the effect size value (i.e., it will never be negative)

10 Write Up Results

To test our hypothesis that participants who responded with high number of physical symptoms in our sample would report a significant difference in levels of stress than participants who responded with low number of physical symptoms, we used an independent samples t-test. This required us to drop the participants who responded with medium number of physical symptoms 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 (p = 0.98). Our data met all other assumptions of an independent samples t-test.

As predicted, we found that low number of symptoms (M = 2.70, SD = 0.55) reported a significant difference in levels of stress levels than high number of symptoms (M = 3.38, SD = 0.55); t(1255.30) = 23.24, p < 0.001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of 1.25 (large effect; Cohen, 1988).

References

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