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
# import the dataset you cleaned previously
# this will be the dataset you'll use throughout the rest of the semester
d <- read.csv(file="Data/mydata.csv", header=T)
I predict that people with psychiatric disabilities will report significantly more sense of belonging than people with chronic health disabilities, as measured by IDEA8.
# you only need to check the variables you're using in the current analysis
# although you checked them previously, it's always a good idea to look them over again and be sure that everything is correct
str(d)
## 'data.frame': 855 obs. of 6 variables:
## $ disability: chr "psychiatric" "other" "learning" "psychiatric" ...
## $ income : chr "rather not say" "rather not say" "1 low" "1 low" ...
## $ swb : num 1.83 5.83 5.33 5 2.67 ...
## $ idea : num 3.75 2.75 3.62 3.75 3.75 ...
## $ mindful : num 2.2 1.6 1.8 4.27 3.4 ...
## $ belong : num 3.6 2.4 3 3.5 3.3 2.7 3.8 2.6 3.8 4 ...
d$disability <- as.factor(d$disability)
table(d$disability, useNA = "always")
##
## chronic health learning other physical psychiatric
## 147 124 87 49 378
## sensory <NA>
## 70 0
# you can use the describe() command on an entire dataframe (d) or just on a single variable (d$pss)
describe(d$belong)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 855 3.3 0.6 3.3 3.32 0.59 1.4 5 3.6 -0.23 -0.21 0.02
# also use a histogram to examine your continuous variable
hist(d$belong)
# can 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$belong, group=d$disability)
##
## Descriptive statistics by group
## group: chronic health
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 147 3.24 0.62 3.3 3.27 0.59 1.5 4.6 3.1 -0.37 -0.21 0.05
## ------------------------------------------------------------
## group: learning
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 124 3.34 0.54 3.3 3.34 0.59 1.9 5 3.1 0.14 0.14 0.05
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 87 3.13 0.63 3.1 3.13 0.59 1.6 4.6 3 0 -0.49 0.07
## ------------------------------------------------------------
## group: physical
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 49 3.2 0.61 3.3 3.22 0.59 1.4 4.3 2.9 -0.37 0.17 0.09
## ------------------------------------------------------------
## group: psychiatric
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 378 3.4 0.58 3.4 3.41 0.59 1.7 4.6 2.9 -0.22 -0.4 0.03
## ------------------------------------------------------------
## group: sensory
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 70 3.11 0.65 3.2 3.13 0.74 1.7 4.5 2.8 -0.22 -0.71 0.08
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$belong~d$disability)
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
# 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(belong~disability, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 5 1.0453 0.3897
## 849
As you can see, our data is far from significant. 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 assumptions as Student’s t-test (the default type of t-test) about variance. 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 changing the homogeneity of our variance, even if we already have a solution for any potential problems.
My independent variable has more than two levels. To proceed with this analysis, I will drop the participants with sensory, physical, learning, and other disabilities 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.
My data also has some potential issues regarding homogeneity of variance. The Levene’s test was not significant, and it was not close to the significance threshold. To accommodate any potential heterogeneity of variance, I will use Welch’s t-test instead of Student’s t-test.
# once again, subetting to drop the "sensory" , "learning" , "other", "physical" groups
#d <- subset(d, disability != "sensory","learning","other","physical") *this code did not work so I opted for the one below*
d <- subset(d, !(disability %in% c("sensory","learning","other","physical")))
d$disability <- droplevels(d$disability) # using droplevels() to drop the empty factor
# very simple! we specify the dataframe alongside the variables instead of having a separate argument for the dataframe like we did for leveneTest()
t_output <- t.test(d$belong~d$disability)
t_output
##
## Welch Two Sample t-test
##
## data: d$belong by d$disability
## t = -2.7042, df = 251.96, p-value = 0.007314
## alternative hypothesis: true difference in means between group chronic health and group psychiatric is not equal to 0
## 95 percent confidence interval:
## -0.27570391 -0.04334371
## sample estimates:
## mean in group chronic health mean in group psychiatric
## 3.242857 3.402381
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$belong~d$disability)
d_output
##
## Cohen's d
##
## d estimate: -0.2701631 (small)
## 95 percent confidence interval:
## lower upper
## -0.46181852 -0.07850767
To test our hypothesis that people with chronic health disabilities would report significantly more sense of belonging than people with psychiatric disabilities, we used a two-sample or independent t-test. This required us to drop participants with sensory, physcial, learning, and other disabilities 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 = .6084). We also used Welch’s t-test, which does not assume homogeneity of variance. Our data met all other assumptions of a t-test.
Contrary to the hypothesis, we found that people with psychiatric disabilities (M = 3.40, SD = .58) reported significantly greater sense of belonging than people with psychiatric disabilities (M = 3.24, SD = .62); t(251.96) = -2.7042, p = .0073. (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .27 (small effect; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.