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
# 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="YOUR DATAFILE", header=T)
d <- read.csv(file="data/eammi2_finalREAL.csv", header=T)
We predict that females will report significantly higher sense of belonging than males from the EAMMI2 data set. (PSS-4). ##put in your own hypotheis##can use results from this in final project independent with two levels and dependant with continous ##different from one we first started with or just use this one or come up with some random levels (Note: The above hypothesis is for the lab. You will need to come up with your own hypothesis, specific to your chosen variables, for the homework.) # 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': 3075 obs. of 6 variables:
## $ gender : chr "f" "m" "m" "f" ...
## $ 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" ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ belong : num 2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
## $ moa_role : num 3 2.67 2.5 2 2.67 ...
## $ moa_maturity: num 3.67 3.33 3.67 3 3.67 ...
#this command just gives us info about the data
###VARIABLE <- as.factor(VARIABLE)
##VARIABLE <- as.factor(d$gender_rc)
d$gender <- as.factor(d$gender)
#recodeing gender variable (independant variable) as a factor
#now gender shluld say favtor w/ 3 levels
##table(VARIABLE, useNA = "always")
table(d$gender, useNA = "always")
##
## f m nb <NA>
## 2252 770 53 0
#creating a table telling us how many people are fit into each category
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
##describe(VARIABLE)
describe(d$belong)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3075 3.23 0.61 3.3 3.25 0.59 1.3 5 3.7 -0.27 -0.13 0.01
#will give you skew and kurtosis and see if they are in 2 to -2
# also use a histogram to examine your continuous variable
##hist(VARIABLE)
hist(d$belong)
#gives a histogram in bottom right window
# 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, means, skew, kortosis by group so seperates stress by gender
##describeBy(VARIABLE, group=GROUP)
describeBy(d$belong, group=d$gender)
##
## Descriptive statistics by group
## group: f
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2252 3.28 0.59 3.3 3.3 0.59 1.3 5 3.7 -0.28 -0.16 0.01
## ------------------------------------------------------------
## group: m
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 770 3.08 0.63 3.1 3.1 0.59 1.3 4.9 3.6 -0.17 -0.1 0.02
## ------------------------------------------------------------
## group: nb
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 53 3.4 0.5 3.3 3.4 0.59 2.3 4.6 2.3 0.07 -0.41 0.07
# last, use a boxplot to examine your continuous and categorical variables together
#boxplot(VARIABLE~GROUP)
#variable is DV, and group is IV
boxplot(d$belong~d$gender)
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 is not met:
This assumption was confirmed in the section above:
So we only have one assumption to test:
# subetting to drop the nb group so that our IV only has two levels
d <- subset(d, gender!= "nb")
##VARIABLE <- droplevels(VARIABLE) # using droplevels() to drop the empty factor
d$gender <- droplevels(d$gender)
table(d$gender, useNA = "always")
##
## f m <NA>
## 2252 770 0
#chcking to see if it dropped with a table
# using droplevels() to drop the empty factor
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(VARIABLE~GROUP, data = d)
leveneTest(belong~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 3.4026 0.06519 .
## 3020
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
As you can see, our data is close to significant but the variance between men and women is equal.
My independent variable (gender) 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 did not have some potential issues regarding homogeneity of variance and Levene’s test was not significant. It was a bit close though so 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$VARIABLE~d$GROUP)
t_output <- t.test(d$belong~d$gender)
t_output
##
## Welch Two Sample t-test
##
## data: d$belong by d$gender
## t = 7.7042, df = 1257.2, p-value = 2.656e-14
## alternative hypothesis: true difference in means between group f and group m is not equal to 0
## 95 percent confidence interval:
## 0.1492994 0.2513140
## sample estimates:
## mean in group f mean in group m
## 3.282904 3.082597
## once again, we use our formula to calculate cohen's d
# calcuklating effect size, use cohen's for t test
#d_output <- cohen.d(d$VARIABLE~d$GROUP)
d_output <- cohen.d(d$belong~d$gender)
d_output
##
## Cohen's d
##
## d estimate: 0.3326809 (small)
## 95 percent confidence interval:
## lower upper
## 0.2503980 0.4149638
-I hypothesised that women would have a signifantly higher sense of belonging than women and tested this useing a two-sample or independent t-test. This did mean I had to drop a level from my gender variable- nonbinary- since this requires a two group comparison. I tested the homogeneity of variance with Levene’s test and found some signs of heterogeneity (p = .065). 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.
As predicted we found that women (M = 3.28,SD =0.59 ) reported significantly higher feelings of belonging (M= 3.08, SD = 0.63); t(7.7042) = , p < 2.656e-14 (see figure 1). The effect size was calculated using Cohen’s d, with a value of 0.33 (small effect; Cohen, 1988)
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.