# 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 there will be a significant difference in resiliency by people’s relationship status, between individuals who are single (and never married) and individuals who are in a relationship/married but living apart.
# you **only** need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d)
## 'data.frame': 380 obs. of 7 variables:
## $ X : int 6350 6608 6612 6715 6726 6778 6859 6860 6929 6980 ...
## $ relationship_status: chr "Single, never married" "Single, never married" "Single, never married" "Single, never married" ...
## $ mhealth : chr "none or NA" "obsessive compulsive disorder" "none or NA" "none or NA" ...
## $ big5_neu : num 6 3.33 4.67 3 5 ...
## $ edeq12 : num 1.83 2.75 1.25 1.92 1.67 ...
## $ brs : num 2.17 3.17 3.17 3 2 ...
## $ isolation_c : num 2.5 3.25 2.75 3 3.5 3.25 1.75 2 3 3.5 ...
# 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': 380 obs. of 7 variables:
## $ X : int 6350 6608 6612 6715 6726 6778 6859 6860 6929 6980 ...
## $ relationship_status: Factor w/ 5 levels "In a relationship/married and cohabiting",..: 5 5 5 5 5 3 5 5 5 5 ...
## $ mhealth : chr "none or NA" "obsessive compulsive disorder" "none or NA" "none or NA" ...
## $ big5_neu : num 6 3.33 4.67 3 5 ...
## $ edeq12 : num 1.83 2.75 1.25 1.92 1.67 ...
## $ brs : num 2.17 3.17 3.17 3 2 ...
## $ isolation_c : num 2.5 3.25 2.75 3 3.5 3.25 1.75 2 3 3.5 ...
table(d$relationship_status, useNA = "always")
##
## In a relationship/married and cohabiting
## 1
## In a relationship/married but living apart
## 49
## Prefer not to say
## 29
## Single, divorced or widowed
## 1
## Single, never married
## 300
## <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$brs)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 380 2.63 0.87 2.67 2.62 0.99 1 5 4 0.15 -0.59 0.04
# also use a histogram to visualize your continuous variable
hist(d$brs)
# 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$brs, 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 3.17 NA 3.17 3.17 0 3.17 3.17 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 49 2.41 0.9 2.17 2.36 0.99 1 4.67 3.67 0.59 -0.19 0.13
## ------------------------------------------------------------
## group: Prefer not to say
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 29 2.49 1.01 2.33 2.47 1.24 1 4.33 3.33 0.26 -1.1 0.19
## ------------------------------------------------------------
## group: Single, divorced or widowed
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1 3 NA 3 3 0 3 3 0 NA NA NA
## ------------------------------------------------------------
## group: Single, never married
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 300 2.67 0.84 2.67 2.67 0.99 1 5 4 0.09 -0.57 0.05
# lastly, use a boxplot to examine your chosen continuous and categorical variables together
boxplot(d$brs~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 != "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
## 49
## Prefer not to say
## 29
## Single, divorced or widowed
## 1
## Single, never married
## 300
## <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
## 49
## Prefer not to say
## 29
## Single, divorced or widowed
## 1
## Single, never married
## 300
## <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 but living apart
## 49
## Prefer not to say
## 0
## Single, divorced or widowed
## 1
## Single, never married
## 300
## <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
## 49
## Single, divorced or widowed
## 1
## Single, never married
## 300
## <NA>
## 0
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 but living apart
## 49
## Single, divorced or widowed
## 0
## Single, never married
## 300
## <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
## 49
## Single, never married
## 300
## <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(brs~relationship_status, data =d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.0543 0.8158
## 347
Levene’s test revealed that our data has no signficantly different variances between the two comparison groups, single and never married and in a relationship/married but living apart, on their levels of resiliency.
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 divorced/widowed participants, participants who are in a relationship and cohabiting, and participants who answered “Prefer not to say” 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.
Although my data has no issues regarding homogeneity of variance, as Levene’s test was not significant, I will still use Welch’s t-test instead of Student’s t-test in my analysis for the purposes of this class.
# 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$brs~d$relationship_status) # t_output will now show in your Global Environment
t_output
##
## Welch Two Sample t-test
##
## data: d$brs by d$relationship_status
## t = -1.9051, df = 62.361, p-value = 0.06138
## 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.53868537 0.01292573
## sample estimates:
## mean in group In a relationship/married but living apart
## 2.411565
## mean in group Single, never married
## 2.674444
To test our hypothesis that there will be a signficicant difference in resiliency between individuals who are single and individuals who are in a relationship/married but living apart, we used an independent samples t-test. This required us to drop our widowed/divorced participants, participants who are in a relationship/married and cohabiting, and participants who answered “Prefer not to say” 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 no signs of heterogeneity (p > .05). However, for the purposes of this class, 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 our prediction, we found that single, never married pariticipants (M = 2.67, SD = 0.84) reported no signficantly different levels of resiliency than participants who are in a relationship/married but living apart (M = 2.41, SD = 0.9); t(62.361) = -1.9051, ns. (see Figure 1).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.