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)
We predict that individuals with at least 1 sibling will report significantly more perceived social support than individuals with no siblings, as measured by the Multidimensional Scale of Perceived Social Support.
# 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': 3165 obs. of 6 variables:
## $ sibling : chr "at least one sibling" "at least one sibling" "at least one sibling" "at least one sibling" ...
## $ marriage5: chr "are currently divorced from one another" "are currently married to one another" "are currently married to one another" "are currently married to one another" ...
## $ 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 ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ idea : num 3.75 3.88 3.75 3.75 3.5 ...
d$sibling<- as.factor(d$sibling)
table(d$sibling, useNA = "always")
##
## at least one sibling only child <NA>
## 2861 304 0
# you can use the describe() command on an entire dataframe (d) or just on a single variable (d$pss)
describe(d$support)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3165 5.53 1.14 5.75 5.66 0.99 0 7 7 -1.12 1.49 0.02
# also use a histogram to examine your continuous variable
hist(d$support)
# 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$support, 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 2861 5.52 1.14 5.75 5.64 0.99 0 7 7 -1.09 1.4 0.02
## ------------------------------------------------------------
## group: only child
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 304 5.66 1.11 5.92 5.79 0.99 1 7 6 -1.34 2.49 0.06
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$support~d$sibling)
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(d$support~d$sibling, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.8511 0.3563
## 3163
As you can see, our data is 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 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 non-binary 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.
My data also has some potential issues regarding homogeneity of variance. Although Levene’s test was not significant, it was 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 nb group
# 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$support~d$sibling)
t_output
##
## Welch Two Sample t-test
##
## data: d$support by d$sibling
## t = -2.0524, df = 374.51, p-value = 0.04082
## 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.268783892 -0.005758004
## sample estimates:
## mean in group at least one sibling mean in group only child
## 5.519253 5.656524
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$support~d$sibling)
d_output
##
## Cohen's d
##
## d estimate: -0.1209866 (negligible)
## 95 percent confidence interval:
## lower upper
## -0.239302608 -0.002670656
To test our hypothesis that individuals with at least 1 sibling in our sample would report significantly more perceived social support than only children, we used an two-sample or independent t-test. This test is limited to a two-group comparison, dropping a third variable was not necessary. We tested the homogeneity of variance with Levene’s test and found some signs of heterogeneity (p = .085). This suggests that there is an increased chance of Type I error. To correct 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.
Unlike we predicted, we found that individuals with at least 1 sibling (M = 5.52, SD = 1.14) did not report significantly higher stress than only children (M = 5.66, SD = 1.11); t(374.51) = -2.05, p = .04 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of -.12 (small effect; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.