# 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
There will be a significant difference in perceived social support by people’s level of relationship status, between those who are single (never married) and those who are in a relationship/married but living apart. Specifically, individuals who are in a relationship/married but living apart will report higher levels of social support.
# you **only** need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d)
## 'data.frame': 666 obs. of 7 variables:
## $ X : int 520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
## $ relationship_status: chr "Single, never married" "Single, never married" "Prefer not to say" "Single, never married" ...
## $ pet : chr "cat" "other" "no pets" "no pets" ...
## $ mfq_26 : num 2.7 4.55 4.8 3.8 4.5 4 5.8 4.2 4.5 5.25 ...
## $ rse : num 2.6 3.1 3.7 3 3 3 4 3.8 2.5 4 ...
## $ phq : num 1.56 1.44 1.11 1.33 1.44 ...
## $ support : num 2.83 3 4 4 3.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$relationship_status <- as.factor(d$relationship_status)
str(d)
## 'data.frame': 666 obs. of 7 variables:
## $ X : int 520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
## $ relationship_status: Factor w/ 5 levels "In a relationship/married and cohabiting",..: 5 5 3 5 5 3 5 5 5 2 ...
## $ pet : chr "cat" "other" "no pets" "no pets" ...
## $ mfq_26 : num 2.7 4.55 4.8 3.8 4.5 4 5.8 4.2 4.5 5.25 ...
## $ rse : num 2.6 3.1 3.7 3 3 3 4 3.8 2.5 4 ...
## $ phq : num 1.56 1.44 1.11 1.33 1.44 ...
## $ support : num 2.83 3 4 4 3.67 ...
table(d$relationship_status, useNA = "always")
##
## In a relationship/married and cohabiting
## 1
## In a relationship/married but living apart
## 67
## Prefer not to say
## 70
## Single, divorced or widowed
## 2
## Single, never married
## 526
## <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$support)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 666 3.48 0.93 3.5 3.51 0.99 1 5 4 -0.28 -0.64 0.04
# also use a histogram to visualize your continuous variable
hist(d$support)
# 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$support, group=d$relationship_status)
##
## Descriptive statistics by group
## group: In a relationship/married and cohabiting
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1 2.67 NA 2.67 2.67 0 2.67 2.67 0 NA NA NA
## ------------------------------------------------------------
## group: In a relationship/married but living apart
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 67 3.6 0.98 3.67 3.66 1.24 1.17 5 3.83 -0.44 -0.71 0.12
## ------------------------------------------------------------
## group: Prefer not to say
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 70 3.25 0.91 3.25 3.26 0.86 1.33 5 3.67 -0.09 -0.48 0.11
## ------------------------------------------------------------
## group: Single, divorced or widowed
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2 3.25 1.53 3.25 3.25 1.61 2.17 4.33 2.17 0 -2.75 1.08
## ------------------------------------------------------------
## group: Single, never married
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 526 3.5 0.92 3.5 3.53 0.99 1 5 4 -0.3 -0.63 0.04
# lastly, use a boxplot to examine your chosen continuous and categorical variables together
boxplot(d$support~d$relationship_status)
# 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, relationship_status != "Single, divorced or widowed") # use subset() to remove all participants from the additional level
table(d$relationship_status, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## In a relationship/married and cohabiting
## 1
## In a relationship/married but living apart
## 67
## Prefer not to say
## 70
## Single, divorced or widowed
## 0
## Single, never married
## 526
## <NA>
## 0
d$relationship_status <- droplevels(d$relationship_status) # use droplevels() to drop the empty factor
table(d$relationship_status, useNA = "always") # verify that now the entire factor level is removed
##
## In a relationship/married and cohabiting
## 1
## In a relationship/married but living apart
## 67
## Prefer not to say
## 70
## Single, never married
## 526
## <NA>
## 0
d <- subset(d, relationship_status != "Prefer not to say") # use subset() to remove all participants from the additional level
table(d$relationship_status, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## In a relationship/married and cohabiting
## 1
## In a relationship/married but living apart
## 67
## Prefer not to say
## 0
## Single, never married
## 526
## <NA>
## 0
d$relationship_status <- droplevels(d$relationship_status) # use droplevels() to drop the empty factor
table(d$relationship_status, useNA = "always") # verify that now the entire factor level is removed
##
## In a relationship/married and cohabiting
## 1
## In a relationship/married but living apart
## 67
## Single, never married
## 526
## <NA>
## 0
d <- subset(d, relationship_status != "In a relationship/married and cohabiting") # use subset() to remove all participants from the additional level
table(d$relationship_status, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## In a relationship/married and cohabiting
## 0
## In a relationship/married but living apart
## 67
## Single, never married
## 526
## <NA>
## 0
d$relationship_status <- droplevels(d$relationship_status) # use droplevels() to drop the empty factor
table(d$relationship_status, useNA = "always") # verify that now the entire factor level is removed
##
## In a relationship/married but living apart
## 67
## Single, never married
## 526
## <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.
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(support~relationship_status, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.5147 0.4734
## 591
Levene’s test revealed that our data does not have significantly different variances between the two comparison groups, single (never married) and in a relationship but living apart, on their levels of social support.
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.
My independent variable has more than two levels. To proceed with this analysis, I will drop the single (divorced or widowed), in a relationship/married and cohabiting, and prefer not to say 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 did not have an issue regarding homogeneity of variance. However, I will also use Welch’s 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$support~d$relationship_status) # t_output will now show in your Global Environment
t_output
##
## Welch Two Sample t-test
##
## data: d$support by d$relationship_status
## t = 0.81756, df = 81.463, p-value = 0.416
## alternative hypothesis: true difference in means between group In a relationship/married but living apart and group Single, never married is not equal to 0
## 95 percent confidence interval:
## -0.1480183 0.3545332
## sample estimates:
## mean in group In a relationship/married but living apart
## 3.601990
## mean in group Single, never married
## 3.498733
# 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$support~d$relationship_status) # d_output will now show in your Global Environment
# d_output
## Remember to always take the ABSOLAUTE VALUE of the effect size value (i.e., it will never be negative)
To test our hypothesis that individuals in our sample would report significantly higher levels of social support than individuals who are single and never married, we used an independent samples t-test. This required us to drop our single (divorced or widowed), in a relationship/married and cohabiting, and prefer not to say 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 that is was not significant (p < .473). Although this suggests that the equal-variances assumption was met, we used Welch’s t-test as an alternative for the group comparison.
Contrary to what we predicted, we found that individuals in a relationship/married but living apart (M = 3.60, SD = 0.98) did not report significantly higher levels of social support than individuals who are single and never married (M = 3.49, SD = 0.92); t(81.46) = 0.82 , p < .416 (see Figure 1). The effect size was not calculated as t-value was not statistically significant.