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
# 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/realdata_final.csv", header=T)
We predict that participants with siblings will report significantly higher levels of needing to belong than participants without.
# 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': 1620 obs. of 7 variables:
## $ ResponseId: chr "R_12G7bIqN2wB2N65" "R_2CfdmFw1NTliv4e" "R_24kJPxVOxMshN3Q" "R_3JmBijdH1z82QU2" ...
## $ gender : chr "m" "f" "f" "f" ...
## $ sibling : chr "at least one sibling" "only child" "at least one sibling" "at least one sibling" ...
## $ pipwd : num 2.33 3.53 3 3 3.13 ...
## $ mindful : num 2.2 3 3.4 4.6 3.87 ...
## $ belong : num 3.6 3.6 2.9 4.1 3.1 2.4 3 4.1 2.8 3.3 ...
## $ support : num 5.17 6.08 6 6 6 ...
d$sibling <- as.factor(d$sibling)
table(d$sibling, useNA = "always")
##
## at least one sibling only child <NA>
## 1473 147 0
# you can use the describe() command on an entire datafrom (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 1620 3.23 0.6 3.2 3.24 0.59 1.3 4.8 3.5 -0.22 -0.15 0.01
# 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$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 1473 3.22 0.6 3.2 3.24 0.59 1.3 4.8 3.5 -0.23 -0.15 0.02
## ------------------------------------------------------------
## group: only child
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 147 3.3 0.64 3.2 3.31 0.59 1.6 4.6 3 -0.16 -0.23 0.05
# last, use a boxplot to examine your continuous and categorical variables together dv/con~iv/
boxplot(d$belong~d$sibling)
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:
This assumption was confirmed in the section above:
So we only have one assumption to 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(belong~sibling, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.9692 0.325
## 1618
As you can see, our data is not 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.
My data has some potential issues regarding homogeneity of variance. Levene’s test was not significant. To accommodate any potential heterogeneity of variance, I will use Welch’s t-test instead of Student’s 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$belong~d$sibling)
t_output
##
## Welch Two Sample t-test
##
## data: d$belong by d$sibling
## t = -1.3872, df = 172.26, p-value = 0.1672
## 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.18536637 0.03235641
## sample estimates:
## mean in group at least one sibling mean in group only child
## 3.220774 3.297279
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$belong~d$sibling)
d_output
##
## Cohen's d
##
## d estimate: -0.1271461 (negligible)
## 95 percent confidence interval:
## lower upper
## -0.29685920 0.04256697
To test our hypothesis that children with at least one sibling in our sample would report significantly higher need to belong than children without siblings, we used an two-sample or independent t-test. We tested the homogeneity of variance with Levene’s test and found some signs of homogeneity (p = 0.17). This suggests that there is an increased chance of Type II 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.
Compared to what was predicted, children with at least one sibling (M = 3.22, SD = 0.6) reported at an almost equal rate as children without siblings (M = 3.30, SD = 0.64); t(172.26) = 5.43, p = 0.17 (see figure 1). The effect size was calculated using Cohen’s d, with a value of -0.13 (small effect; Cohen 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.