# 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 the need to belong scores between individuals with disabilities and those without disabilities.
# you only need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d)
## 'data.frame': 557 obs. of 7 variables:
## $ ResponseId: chr "R_12G7bIqN2wB2N65" "R_3lLnoV2mYVYHFvf" "R_1gTNDGWsqikPuEX" "R_3G1XvswZmPZTkMU" ...
## $ age : chr "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
## $ disability: chr "psychiatric" "other" "learning" "psychiatric" ...
## $ socmeduse : int 34 37 26 23 35 30 40 34 38 42 ...
## $ npi : num 0.0769 0.7692 0 0 0.0769 ...
## $ belong : num 3.6 2.4 3 3.5 3.3 2.7 3.8 2.6 3.8 4 ...
## $ swb : num 1.83 5.83 5.33 5 2.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$disability <- as.factor(d$disability)
table(d$disability, useNA = "always")
##
## chronic health learning other physical psychiatric
## 93 82 61 28 244
## sensory <NA>
## 49 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$belong)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 557 3.28 0.61 3.3 3.29 0.59 1.5 5 3.5 -0.18 -0.32 0.03
# also use a histogram to visualize your continuous variable
hist(d$belong)
# 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 93 3.21 0.64 3.3 3.25 0.59 1.5 4.6 3.1 -0.49 -0.21 0.07
## ------------------------------------------------------------
## group: learning
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 82 3.34 0.56 3.4 3.34 0.59 1.9 5 3.1 -0.02 0.02 0.06
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 61 3.15 0.57 3.2 3.16 0.59 2 4.4 2.4 -0.09 -0.72 0.07
## ------------------------------------------------------------
## group: physical
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 28 3.2 0.62 3.3 3.2 0.59 2.1 4.3 2.2 -0.09 -0.96 0.12
## ------------------------------------------------------------
## group: psychiatric
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 244 3.36 0.59 3.3 3.36 0.74 1.7 4.6 2.9 -0.02 -0.62 0.04
## ------------------------------------------------------------
## group: sensory
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 49 3.13 0.69 3.2 3.15 0.74 1.7 4.5 2.8 -0.25 -0.68 0.1
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$belong~d$disability)
# 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, disability != "chronic health")
table(d$disability, useNA = "always") #verify that now there are no participants in the removed level
##
## chronic health learning other physical psychiatric
## 0 82 61 28 244
## sensory <NA>
## 49 0
d$disability <- droplevels(d$disability) # use droplevels() to drop the empty factor
table(d$disability, useNA = "always") #verify that now the entire factor level is removed
##
## learning other physical psychiatric sensory <NA>
## 82 61 28 244 49 0
# 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, disability != "learning")
table(d$disability, useNA = "always") #verify that now there are no participants in the removed level
##
## learning other physical psychiatric sensory <NA>
## 0 61 28 244 49 0
d$disability <- droplevels(d$disability) # use droplevels() to drop the empty factor
table(d$disability, useNA = "always") #verify that now the entire factor level is removed
##
## other physical psychiatric sensory <NA>
## 61 28 244 49 0
# 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, disability != "other")
table(d$disability, useNA = "always") #verify that now there are no participants in the removed level
##
## other physical psychiatric sensory <NA>
## 0 28 244 49 0
d$disability <- droplevels(d$disability) # use droplevels() to drop the empty factor
table(d$disability, useNA = "always") #verify that now the entire factor level is removed
##
## physical psychiatric sensory <NA>
## 28 244 49 0
# 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, disability != "sensory")
table(d$disability, useNA = "always") #verify that now there are no participants in the removed level
##
## physical psychiatric sensory <NA>
## 28 244 0 0
d$disability <- droplevels(d$disability) # use droplevels() to drop the empty factor
table(d$disability, useNA = "always") #verify that now the entire factor level is removed
##
## physical psychiatric <NA>
## 28 244 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(belong~disability, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 8e-04 0.977
## 270
As you can see, the data wasn’t 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 other,chronic health, learning and sensory 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.
[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$belong~d$disability)
t_output
##
## Welch Two Sample t-test
##
## data: d$belong by d$disability
## t = -1.3123, df = 32.923, p-value = 0.1985
## alternative hypothesis: true difference in means between group physical and group psychiatric is not equal to 0
## 95 percent confidence interval:
## -0.41184745 0.08889663
## sample estimates:
## mean in group physical mean in group psychiatric
## 3.200000 3.361475
# once again, we use the same formula, y~x, to calculate cohen's d
d_output <- cohen.d(d$belong~d$disability)
d_output
##
## Cohen's d
##
## d estimate: -0.2713431 (small)
## 95 percent confidence interval:
## lower upper
## -0.6648448 0.1221587
To test our hypothesis that people with disability in our sample would report significantly higher levels of the need to belong than with people without, we used an independent samples t-test. This required us to drop our other, chronic health, learning, and sensory 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 homogeneity (p = 0.977). This suggests that there is an increased chance of Type I error. To correct for this issue, we used Welch’s t-test, which does not assume homogeneity of variance. Our data met all other assumptions of a t-test.
I did not find support for my hypothesis, we found that Physical (M = 3.2, SD = 0.62) wasn’t significantly different from Psychiatric (M = 3.36, SD = 0.59); t(32.923) = -1.3123, p = 0.1985 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .27 (small effect; Cohen, 1988). This effect size is meaningless.
[Revise the above statements for you HW assignment.]
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.