1 Loading Libraries

library(psych) # for the describe() command
library(car) # for the leveneTest() command
## Warning: package 'car' was built under R version 4.3.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.3.3
## 
## Attaching package: 'car'
## The following object is masked from 'package:psych':
## 
##     logit
library(effsize) # for the cohen.d() command
## Warning: package 'effsize' was built under R version 4.3.3
## 
## Attaching package: 'effsize'
## The following object is masked from 'package:psych':
## 
##     cohen.d

2 Importing Data

# for the homework: 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/eammi2_data_final.csv", header=T)

3 State Your Hypothesis

We predict that individuals with siblings will report significantly more mindfulness than individuals without siblings, as measured by the mindful attention awareness scale.

4 Check Your Variables

# 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':    3161 obs. of  7 variables:
##  $ ResponseId: chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ gender    : chr  "f" "m" "m" "f" ...
##  $ sibling   : chr  "at least one sibling" "at least one sibling" "at least one sibling" "at least one sibling" ...
##  $ mindful   : num  2.4 1.8 2.2 2.2 3.2 ...
##  $ efficacy  : num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
##  $ support   : num  6 6.75 5.17 5.58 6 ...
##  $ stress    : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
d$sibling <- as.factor(d$sibling)

table(d$sibling, useNA = "always")
## 
## at least one sibling           only child                 <NA> 
##                 2858                  303                    0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
describe(d$mindful)
##    vars    n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 3161 3.71 0.84   3.73    3.71 0.79 1.13   6  4.87 -0.06    -0.14 0.01
# also use a histogram to examine your continuous variable
hist(d$mindful)

# 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$mindful, group=d$sibling)
## 
##  Descriptive statistics by group 
## group: at least one sibling
##    vars    n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 2858  3.7 0.84   3.67     3.7 0.79 1.13   6  4.87 -0.06    -0.13 0.02
## ------------------------------------------------------------ 
## group: only child
##    vars   n mean   sd median trimmed  mad  min  max range  skew kurtosis   se
## X1    1 303 3.83 0.88   3.87    3.84 0.89 1.53 5.87  4.33 -0.17    -0.16 0.05
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$mindful~d$sibling)

5 Check Your Assumptions

5.1 T-test Assumptions

  • IV must be a categorical variable with two levels
  • Data values must be independent (in this case, that the collection of one variable isn’t based on the response to another variable)
  • Data obtained via a random sample
  • Dependent variable must be normally distributed
  • Variances of the two groups are approximately equal

Some of these we can check in R, while others are down to our research design. These assumptions are confirmed by our research design, so we don’t have to do anything now:

  • Data values must be independent (in this case, that the collection of one variable isn’t based on the response to another variable) – confirmed by the data report that details how the data was collected
  • Data obtained via a random sample – confirmed by the data report that details how the data was collected

This assumption is not met:

  • IV must be a categorical variable with two levels – participants could answer M, F, or NB for their gender, so there are three levels, not two

This assumption was confirmed in the section above:

  • Dependent variable must be normally distributed – we checked skew and kurtosis above. For our class, even if your variables don’t meet the assumption we’ll proceed anyway, just make a note of it.

So we only have one assumption to test:

  • Variances of the two groups are approximately equal – AKA homogeneity of variance. Tested below!

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
# 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(mindful~sibling, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value Pr(>F)
## group    1  1.3009 0.2541
##       3159

As you can see, our data isn’t very close to 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 checking the homogeneity of our variance, even if we already have a solution for any potential problems.

5.3 Issues with My Data

My data also has some potential issues regarding statistical significance. The p-value of Levene’s test is around .25 which is not very close to the aim of p<0.05.There are also issues regarding the homogeneity of variance due to this low p-value.

6 Run a T-test

# 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$mindful~d$sibling)

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$mindful by d$sibling
## t = -2.4181, df = 361.81, p-value = 0.01609
## alternative hypothesis: true difference in means between group at least one sibling and group only child is not equal to 0
## 95 percent confidence interval:
##  -0.23298125 -0.02399506
## sample estimates:
## mean in group at least one sibling           mean in group only child 
##                           3.696594                           3.825083

8 Calculate Cohen’s d

# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$mindful~d$sibling)

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: -0.1526714 (negligible)
## 95 percent confidence interval:
##       lower       upper 
## -0.27119197 -0.03415092

10 Write Up Results

To test our hypothesis that individuals with siblings in our sample would report significantly more mindfulness than individuals without siblings, we used an two-sample or independent t-test. We tested the homogeneity of variance with Levene’s test and found few signs of heterogeneity (p = .2541). This suggests that there is a decreased chance of Type I error. To double-check for this possible issue, we use Welch’s t-test, which does not assume homogeneity of variance. Our data met all other assumptions of a t-test.

As predicted, we found that individuals with siblings (M = 3.70, SD = .84) reported lower mindfulness than individuals without siblings (M = 3.83, SD = .88); t(361.81) = -2.4181, p = .0161 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of -0.153 (negligible effect; Cohen, 1988).

References

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