# 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
We predict that people who have been diagnosed with depression will report significantly higher levels of loneliness than people diagnosed with anxiety, as measured by the Adult UCLA Loneliness Scale.
[Remember to revise the above hypothesis in you HW assignment.]
# you only need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d)
## 'data.frame': 349 obs. of 7 variables:
## $ X : int 1 31 49 57 86 113 133 179 190 208 ...
## $ relationship_status: chr "In a relationship/married and cohabiting" "In a relationship/married and cohabiting" "In a relationship/married and cohabiting" "In a relationship/married and cohabiting" ...
## $ mhealth : chr "none or NA" "none or NA" "none or NA" "anxiety disorder" ...
## $ big5_ext : num 2 5 5.67 4 5.33 ...
## $ pswq : num 4.94 3.94 2.94 2.81 3.5 ...
## $ isolation_a : num 2.25 2.5 2 1.25 3 1 1 2.75 2.75 1 ...
## $ support : num 2.5 2.5 3.83 4.17 1.33 ...
# 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)
table(d$mhealth, useNA = "always")
##
## anxiety disorder bipolar
## 28 4
## depression eating disorders
## 16 1
## none or NA obsessive compulsive disorder
## 283 4
## other ptsd
## 7 6
## <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$isolation_a)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 349 1.68 0.71 1.5 1.59 0.74 1 3.5 2.5 0.9 -0.28 0.04
# also use a histogram to visualize your continuous variable
hist(d$isolation_a)
# 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$isolation_a, 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 28 2.12 0.68 2.25 2.1 0.74 1 3.5 2.5 0.21 -0.91 0.13
## ------------------------------------------------------------
## group: bipolar
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 4 2.25 1.1 2.25 2.25 1.3 1 3.5 2.5 0 -2.14 0.55
## ------------------------------------------------------------
## group: depression
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 16 1.86 0.77 1.62 1.82 0.74 1 3.25 2.25 0.55 -1.19 0.19
## ------------------------------------------------------------
## group: eating disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1 3.5 NA 3.5 3.5 0 3.5 3.5 0 NA NA NA
## ------------------------------------------------------------
## group: none or NA
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 283 1.58 0.66 1.25 1.48 0.37 1 3.5 2.5 1.1 0.22 0.04
## ------------------------------------------------------------
## group: obsessive compulsive disorder
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 4 2.25 0.35 2.38 2.25 0.19 1.75 2.5 0.75 -0.53 -1.88 0.18
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 7 2.25 0.98 2 2.25 1.11 1.25 3.5 2.25 0.14 -2.05 0.37
## ------------------------------------------------------------
## group: ptsd
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 6 2.17 0.77 2.12 2.17 0.93 1 3 2 -0.19 -1.64 0.31
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$isolation_a~d$mhealth)
# If the IV has more than 2 levels, you must drop the additional levels so that you meet the first assumption of a t-test.
d <- subset(d, mhealth != "bipolar")
table(d$mhealth, useNA = "always") #verify that now there are no participants in the removed level
##
## anxiety disorder bipolar
## 28 0
## depression eating disorders
## 16 1
## none or NA obsessive compulsive disorder
## 283 4
## other ptsd
## 7 6
## <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 depression
## 28 16
## eating disorders none or NA
## 1 283
## obsessive compulsive disorder other
## 4 7
## ptsd <NA>
## 6 0
d <- subset(d, mhealth != "eating disorders")
table(d$mhealth, useNA = "always") #verify that now there are no participants in the removed level
##
## anxiety disorder depression
## 28 16
## eating disorders none or NA
## 0 283
## obsessive compulsive disorder other
## 4 7
## ptsd <NA>
## 6 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 depression
## 28 16
## none or NA obsessive compulsive disorder
## 283 4
## other ptsd
## 7 6
## <NA>
## 0
d <- subset(d, mhealth != "none or NA")
table(d$mhealth, useNA = "always") #verify that now there are no participants in the removed level
##
## anxiety disorder depression
## 28 16
## none or NA obsessive compulsive disorder
## 0 4
## other ptsd
## 7 6
## <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 depression
## 28 16
## obsessive compulsive disorder other
## 4 7
## ptsd <NA>
## 6 0
d <- subset(d, mhealth != "obsessive compulsive disorder")
table(d$mhealth, useNA = "always") #verify that now there are no participants in the removed level
##
## anxiety disorder depression
## 28 16
## obsessive compulsive disorder other
## 0 7
## ptsd <NA>
## 6 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 depression other ptsd
## 28 16 7 6
## <NA>
## 0
d <- subset(d, mhealth != "other")
table(d$mhealth, useNA = "always") #verify that now there are no participants in the removed level
##
## anxiety disorder depression other ptsd
## 28 16 0 6
## <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 depression ptsd <NA>
## 28 16 6 0
d <- subset(d, mhealth != "ptsd")
table(d$mhealth, useNA = "always") #verify that now there are no participants in the removed level
##
## anxiety disorder depression ptsd <NA>
## 28 16 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 depression <NA>
## 28 16 0
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(isolation_a~mhealth, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.5959 0.4445
## 42
As you can see, the data does not have significantly different variances between the two comparison groups.
[Revise the above statement for you HW assignment.]
When running a t-test, we can account for heterogeneity in our variance by using Welch’s t-test, which does not have the same assumption about variance as Student’s t-test (the general default type of t-test). 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 just 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.
My independent variable has more than two levels. To proceed with this analysis, I will drop the bipolar, eating disorders, obsessive compulsive disorder, ptsd, other, and none or NA participants from my sample. I will make a note to discuss this issue in my Method write-up and in my Discussion as a limitation of my study.
There are no issues regarding homogeneity in my data but I will use Welch’s t-test anyway instead of Student’s t-test in my analysis.
[Revise the above statements for you HW assignment.]
# 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$isolation_a~d$mhealth)
t_output
##
## Welch Two Sample t-test
##
## data: d$isolation_a by d$mhealth
## t = 1.1511, df = 28.1, p-value = 0.2594
## alternative hypothesis: true difference in means between group anxiety disorder and group depression is not equal to 0
## 95 percent confidence interval:
## -0.2069858 0.7382358
## sample estimates:
## mean in group anxiety disorder mean in group depression
## 2.125000 1.859375
# once again, we use the same formula, y~x, to calculate cohen's d
d_output <- cohen.d(d$isolation_a~d$mhealth)
d_output
##
## Cohen's d
##
## d estimate: 0.3740191 (small)
## 95 percent confidence interval:
## lower upper
## -0.2635285 1.0115667
To test our hypothesis that people who are diagnosed with depression in our sample would report significantly higher levels of loneliness than those diagnosed with anxeity disorders, we used an independent samples t-test. This required us to drop our bipolar, eating disorder, obsessive compulsive disorder, ptsd, other, and none or NA mental health participants 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 homoogeneity (p = .445). We still used Welch’s t-test, which does not assume homogeneity of variance. Our data met all assumptions of a t-test.
We found that there was not a significant difference between people diagnosed with depression (M = 1.86, SD = 0.77) than those diagnosed with anxiety disorder (M = 2.13, SD = 0.68); t(28.1) = 1.15, p = .26 (see Figure 1). The groups were not significantly different. The effect size was calculated using Cohen’s d, with a value of .37 (small effect; Cohen, 1988).
[Revise the above statements for you HW assignment.]
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.