# 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
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
# you **only** need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d)
## 'data.frame': 685 obs. of 7 variables:
## $ X : int 520 2814 3146 3295 717 6056 4753 2044 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" ...
## $ mhealth : chr "none or NA" "none or NA" "none or NA" "none or NA" ...
## $ iou : num 2.22 1.52 1.78 1.85 2.22 ...
## $ mfq_26 : num 2.7 4.55 4.8 3.8 4.5 4 5.8 4.5 5.25 5.15 ...
## $ pss : num 2.75 2.25 3 2 1.75 2 1 3 1.25 1.25 ...
## $ gad : num 1.14 1.29 1 1 1.14 ...
# 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$mhealth <- as.factor(d$mhealth)
str(d$mhealth)
## Factor w/ 8 levels "anxiety disorder",..: 5 5 5 5 5 5 5 5 5 5 ...
table(d$mhealth, useNA = "always")
##
## anxiety disorder bipolar
## 79 3
## depression eating disorders
## 11 19
## none or NA obsessive compulsive disorder
## 530 16
## other ptsd
## 18 9
## <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$pss)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 685 3.14 0.94 3.25 3.15 1.11 1 5 4 -0.13 -0.73 0.04
# also use a histogram to visualize your continuous variable
hist(d$pss)
# 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$pss,group=d$mhealth)
##
## Descriptive statistics by group
## group: anxiety disorder
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 79 3.59 0.84 3.75 3.65 0.74 1.25 5 3.75 -0.64 -0.04 0.09
## ------------------------------------------------------------
## group: bipolar
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3 4.5 0.5 4.5 4.5 0.74 4 5 1 0 -2.33 0.29
## ------------------------------------------------------------
## group: depression
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 11 3.39 0.63 3.5 3.42 0.74 2.25 4.25 2 -0.33 -1.3 0.19
## ------------------------------------------------------------
## group: eating disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 19 3.87 0.82 4 3.91 0.74 2 5 3 -0.54 -0.7 0.19
## ------------------------------------------------------------
## group: none or NA
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 530 2.99 0.93 3 2.99 1.11 1 5 4 0.01 -0.68 0.04
## ------------------------------------------------------------
## group: obsessive compulsive disorder
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 16 3.52 0.82 3.62 3.55 0.93 1.75 4.75 3 -0.36 -0.87 0.2
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 18 3.68 0.73 3.88 3.7 0.93 2.25 4.75 2.5 -0.26 -1.27 0.17
## ------------------------------------------------------------
## group: ptsd
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 9 3.81 0.89 4.25 3.81 0.37 2 4.5 2.5 -0.93 -0.77 0.3
# lastly, use a boxplot to examine your chosen continuous and categorical variables together
boxplot(d$pss~d$mhealth)
# 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, mhealth != "depression") # use subset() to remove all participants from the additional level
table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## anxiety disorder bipolar
## 79 3
## depression eating disorders
## 0 19
## none or NA obsessive compulsive disorder
## 530 16
## other ptsd
## 18 9
## <NA>
## 0
d$mhealth<- droplevels(d$mhealth) # use droplevels() to drop the empty factor
table(d$mhealth, useNA = "always") # verify that now the entire factor level is removed
##
## anxiety disorder bipolar
## 79 3
## eating disorders none or NA
## 19 530
## obsessive compulsive disorder other
## 16 18
## ptsd <NA>
## 9 0
d <- subset(d, mhealth != "eating disorders") # use subset() to remove all participants from the additional level
table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## anxiety disorder bipolar
## 79 3
## eating disorders none or NA
## 0 530
## obsessive compulsive disorder other
## 16 18
## ptsd <NA>
## 9 0
d$mhealth<- droplevels(d$mhealth) # use droplevels() to drop the empty factor
table(d$mhealth, useNA = "always") # verify that now the entire factor level is removed
##
## anxiety disorder bipolar
## 79 3
## none or NA obsessive compulsive disorder
## 530 16
## other ptsd
## 18 9
## <NA>
## 0
d <- subset(d, mhealth != "other") # use subset() to remove all participants from the additional level
table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## anxiety disorder bipolar
## 79 3
## none or NA obsessive compulsive disorder
## 530 16
## other ptsd
## 0 9
## <NA>
## 0
d$mhealth<- droplevels(d$mhealth) # use droplevels() to drop the empty factor
table(d$mhealth, useNA = "always") # verify that now the entire factor level is removed
##
## anxiety disorder bipolar
## 79 3
## none or NA obsessive compulsive disorder
## 530 16
## ptsd <NA>
## 9 0
d <- subset(d, mhealth != "ptsd") # use subset() to remove all participants from the additional level
table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## anxiety disorder bipolar
## 79 3
## none or NA obsessive compulsive disorder
## 530 16
## ptsd <NA>
## 0 0
d$mhealth<- droplevels(d$mhealth) # use droplevels() to drop the empty factor
table(d$mhealth, useNA = "always") # verify that now the entire factor level is removed
##
## anxiety disorder bipolar
## 79 3
## none or NA obsessive compulsive disorder
## 530 16
## <NA>
## 0
d <- subset(d, mhealth != "obsessive compulsive disorder") # use subset() to remove all participants from the additional level
table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## anxiety disorder bipolar
## 79 3
## none or NA obsessive compulsive disorder
## 530 0
## <NA>
## 0
d$mhealth<- droplevels(d$mhealth) # use droplevels() to drop the empty factor
table(d$mhealth, useNA = "always") # verify that now the entire factor level is removed
##
## anxiety disorder bipolar none or NA <NA>
## 79 3 530 0
d <- subset(d, mhealth != "bipolar") # use subset() to remove all participants from the additional level
table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## anxiety disorder bipolar none or NA <NA>
## 79 0 530 0
d$mhealth<- droplevels(d$mhealth) # use droplevels() to drop the empty factor
table(d$mhealth, useNA = "always") # verify that now the entire factor level is removed
##
## anxiety disorder none or NA <NA>
## 79 530 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.
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(pss~mhealth,d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 3.3456 0.06788 .
## 607
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Levene’s test revealed that participants who reported having an anxiety disorder will have significantly higher perceived-stress scores than participants who reported having no psychological disorders.
Levene’s test was not significant, meaning that the variances between the two groups were approximately equal. Even though the homogeneity-of-variance assumption was met, I will still use Welch’s t test. ## Issues with My Data
My independent variable originally had more than two levels. To conduct the independent-samples t test, I removed participants from the other mental-health categories. My final analysis compared participants with an anxiety disorder to participants reporting no psychological disorders. I will discuss the removal of the other groups as a limitation of my study.
Levene’s test was not significant, indicating that the homogeneity-of-variance assumption was met.
# 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$pss~d$mhealth) # t_output will now show in your Global Environment
t_output
##
## Welch Two Sample t-test
##
## data: d$pss by d$mhealth
## t = 5.9163, df = 108.73, p-value = 3.878e-08
## alternative hypothesis: true difference in means between group anxiety disorder and group none or NA is not equal to 0
## 95 percent confidence interval:
## 0.4022148 0.8074699
## sample estimates:
## mean in group anxiety disorder mean in group none or NA
## 3.594937 2.990094
# 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$pss~d$mhealth) # d_output will now show in your Global Environment
## Remember to always take the ABSOLAUTE VALUE of the effect size value (i.e., it will never be negative)
To test our hypothesis that participants with an anxiety disorder would have higher perceived stress than participants with no psychological disorders, we used an independent-samples t test. Since this test can only compare two groups, we removed participants from the other mental-health categories. We used Levene’s test to check the homogeneity of variance and found that it was not significant. This means that the variances between the two groups were about equal and the assumption was met. We still used Welch’s t test. Our data met all the other assumptions for an independent-samples t test.
As predicted, participants with an anxiety disorder (M = 3.59 , SD = 0.84 ) reported significantly higher levels of perceived stress than participants with no psychological disorders(M = 2.99, SD = 0.93 ); t(108.6) = 5.84, p <.001. The effect size was calculated using Cohen’s d, with a value of 0.66 (medium effect; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.