1 Loading Libraries

# 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

2 Importing Data

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

3 State Your Hypothesis

There will be a significant difference in positive identity by people’s level of disability, between psychiatric and physical.

4 Check Your Variables

# you **only** need to check the variables you're using in the current analysis

## Checking the Categorical variable (IV)

str(d)
## 'data.frame':    773 obs. of  7 variables:
##  $ ResponseID      : chr  "R_12G7bIqN2wB2N65" "R_3lLnoV2mYVYHFvf" "R_1gTNDGWsqikPuEX" "R_2QLjdu3yoxqQ21c" ...
##  $ gender          : chr  "m" "f" "f" "f" ...
##  $ disability      : chr  "psychiatric" "other" "learning" "psychiatric" ...
##  $ pipwd           : num  2.33 2.33 3 2.93 3.33 ...
##  $ moa_independence: num  3.5 2.83 3.67 3.17 3.5 ...
##  $ belong          : num  3.6 2.4 3 3.3 2.7 3.8 2.6 3.8 2.4 3.1 ...
##  $ support         : num  5.17 2.67 4.5 3.92 5.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)

str(d)
## 'data.frame':    773 obs. of  7 variables:
##  $ ResponseID      : chr  "R_12G7bIqN2wB2N65" "R_3lLnoV2mYVYHFvf" "R_1gTNDGWsqikPuEX" "R_2QLjdu3yoxqQ21c" ...
##  $ gender          : chr  "m" "f" "f" "f" ...
##  $ disability      : Factor w/ 6 levels "chronic health",..: 5 3 2 5 3 3 5 1 1 2 ...
##  $ pipwd           : num  2.33 2.33 3 2.93 3.33 ...
##  $ moa_independence: num  3.5 2.83 3.67 3.17 3.5 ...
##  $ belong          : num  3.6 2.4 3 3.3 2.7 3.8 2.6 3.8 2.4 3.1 ...
##  $ support         : num  5.17 2.67 4.5 3.92 5.67 ...
table(d$disability, useNA = "always")
## 
## chronic health       learning          other       physical    psychiatric 
##            137            108             76             44            346 
##        sensory           <NA> 
##             62              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$pipwd)
##    vars   n mean  sd median trimmed  mad  min max range skew kurtosis   se
## X1    1 773 2.91 0.7   2.93    2.92 0.69 1.13   5  3.87 0.01    -0.06 0.03
# also use a histogram to visualize your continuous variable

hist(d$pipwd)

# 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$pipwd, 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 137 2.93 0.77      3    2.93 0.79 1.27 4.8  3.53    0    -0.27 0.07
## ------------------------------------------------------------ 
## group: learning
##    vars   n mean   sd median trimmed  mad min  max range  skew kurtosis   se
## X1    1 108 3.13 0.61   3.13    3.15 0.59 1.4 4.73  3.33 -0.31     0.65 0.06
## ------------------------------------------------------------ 
## group: other
##    vars  n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 76 2.91 0.73   2.93     2.9 0.79 1.13 4.67  3.53  0.1    -0.43 0.08
## ------------------------------------------------------------ 
## group: physical
##    vars  n mean   sd median trimmed  mad  min max range skew kurtosis  se
## X1    1 44 3.23 0.67   3.17    3.24 0.44 1.53   5  3.47 0.04     0.46 0.1
## ------------------------------------------------------------ 
## group: psychiatric
##    vars   n mean   sd median trimmed  mad  min max range skew kurtosis   se
## X1    1 346 2.72 0.65   2.73    2.72 0.64 1.27 4.8  3.53 0.16     0.03 0.04
## ------------------------------------------------------------ 
## group: sensory
##    vars  n mean  sd median trimmed  mad  min max range skew kurtosis   se
## X1    1 62 3.37 0.5   3.27    3.36 0.49 2.13   5  2.87 0.36     0.64 0.06
# lastly, use a boxplot to examine your chosen continuous and categorical variables together

boxplot(d$pipwd~d$disability)

5 Check Your Assumptions

5.1 T-test Assumptions

  • IV must have two levels.
  • Data values must be independent (independent t-test only)
  • Data obtained via a random sample.
  • Dependent variable must be normally distributed.
  • Variances of the two groups are approximately equal.
# 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, disability != "chronic health") # use subset() to remove all participants from the additional level

table(d$disability, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
## chronic health       learning          other       physical    psychiatric 
##              0            108             76             44            346 
##        sensory           <NA> 
##             62              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> 
##         108          76          44         346          62           0
 d <- subset(d, disability != "learning") # use subset() to remove all participants from the additional level

table(d$disability, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##    learning       other    physical psychiatric     sensory        <NA> 
##           0          76          44         346          62           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> 
##          76          44         346          62           0
 d <- subset(d, disability != "other") # use subset() to remove all participants from the additional level

table(d$disability, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##       other    physical psychiatric     sensory        <NA> 
##           0          44         346          62           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> 
##          44         346          62           0
 d <- subset(d, disability != "sensory") # use subset() to remove all participants from the additional level

table(d$disability, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##    physical psychiatric     sensory        <NA> 
##          44         346           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> 
##          44         346           0

5.2 Testing Homogeneity of Variance with Levene’s Test

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(pipwd~disability, data =d)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   1  0.5037 0.4783
##       388

Levene’s test revealed that our data has a non-significant difference in variances between the two comparison groups, psychiatric and physical, with their positive identity.

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.

5.3 Issues with My Data

My independent variable has more than two levels. To proceed with this analysis, I will drop the chronic health, learning, other, and sensory groups 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 does not have an issue regarding homogenity in my study, as Levene’s test was non-significant. I will use Welch’s t-test instead of Student’s t-test in my analysis anyways.

6 Run a 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$pipwd~d$disability)  # t_output will now show in your Global Environment

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$pipwd by d$disability
## t = 4.8064, df = 53.887, p-value = 1.271e-05
## alternative hypothesis: true difference in means between group physical and group psychiatric is not equal to 0
## 95 percent confidence interval:
##  0.3006210 0.7309257
## sample estimates:
##    mean in group physical mean in group psychiatric 
##                  3.234848                  2.719075

8 Calculate Cohen’s d - Effect Size

# 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$pipwd~d$disability)  # d_output will now show in your Global Environment

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: 0.7856042 (medium)
## 95 percent confidence interval:
##     lower     upper 
## 0.4660987 1.1051097
## Remember to always take the ABSOLAUTE VALUE of the effect size value (i.e., it will never be negative)

10 Write Up Results

To test our hypothesis that there would be a significant difference between positive identity of people with physical or psychiatric disabilities, we used an independent samples t-test. This required us to drop our sensory, learning, chronic health, and other disability-having 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 no signs of heterogeneity (p < .47). This suggests that there isn’t an increased chance of Type I error. We used Welch’s t-test, which does not assume homogeneity of variance. Our data fits all assumptions of an independent samples t-test.

We found that people with a psychiatric disability (M = 2.72, SD = .65) reported significantly lower levels of positive identity than physical disabilities (M = 3.23, SD = .67); t(53.89) = 4.81, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of 0.79 (medium effect; Cohen, 1988).

References

Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.