library(psych) # for the describe() command
library(car) # for the leveneTest() command
library(effsize) # for the cohen.d() commandt-Test HW
Loading Libraries
Importing Data
# update this for the HW!!!!!
d <- read.csv(file="Data/mydata.csv", header=T)State Your Hypothesis - PART OF YOUR WRITEUP
Younger participants will have lower life satisfaction than older participants.
Check Your Assumptions
T-test Assumptions
- Data values must be independent (independent t-test only) (confirmed by data report)
- Data obtained via a random sample (confirmed by data report)
- IV must have two levels (will check below)
- Dependent variable must be normally distributed (will check below. if issues, note and proceed)
- Variances of the two groups must be approximately equal, aka ‘homogeneity of variance’. Lacking this makes our results inaccurate (will check below - this really only applies to Student’s t-test, but we’ll check it anyway)
Checking IV levels
# preview the levels and counts for your IV
table(d$age, useNA = "always")
1 between 18 and 25 2 between 26 and 35 3 between 36 and 45 4 over 45
1980 115 38 18
<NA>
0
# note that the table() output shows you exactly how the levels of your variable are rewritten. when recoding, make sure you are spelling them exactly as they appear
# to drop levels from your variable
# this subsets the data and says that any participant who is coded as 'LEVEL BAD' should be removed
# # if you don't need this for the homework, comment it out (add a # at the beginning of the line)
# to combine levels
# this says that where any participant is coded as 'LEVEL BAD' it should be replaced by 'LEVEL GOOD'
# you can repeat this as needed, changing 'LEVEL BAD' if you have multiple levels that you want to combine into a single level
# if you don't need this for the homework, comment it out (add a # at the beginning of the line)
d$age_rc[d$age == "1 between 18 and 25"] <- "younger"
d$age_rc[d$age == "2 between 26 and 35"] <- "older"
d$age_rc[d$age == "3 between 36 and 45"] <- "older"
d$age_rc[d$age == "4 over 45"] <- "older"
table(d$age_rc, useNA = "always")
older younger <NA>
171 1980 0
table(d$age, d$age_rc, useNA = "always")
older younger <NA>
1 between 18 and 25 0 1980 0
2 between 26 and 35 115 0 0
3 between 36 and 45 38 0 0
4 over 45 18 0 0
<NA> 0 0 0
# preview your changes and make sure everything is correct
table(d$age, useNA = "always")
1 between 18 and 25 2 between 26 and 35 3 between 36 and 45 4 over 45
1980 115 38 18
<NA>
0
table(d$age_rc, useNA = "always")
older younger <NA>
171 1980 0
# check your variable types
str(d)'data.frame': 2151 obs. of 8 variables:
$ gender : chr "f" "m" "m" "f" ...
$ age : chr "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
$ socmeduse: int 47 23 34 35 37 13 37 43 37 29 ...
$ swb : num 4.33 4.17 1.83 5.17 3.67 ...
$ npi : num 0.6923 0.1538 0.0769 0.0769 0.7692 ...
$ exploit : num 2 3.67 4.33 1.67 4 ...
$ efficacy : num 3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
$ age_rc : chr "younger" "younger" "younger" "younger" ...
# make sure that your IV is recognized as a factor by R!!!
d$age <- as.factor(d$age)
d$age_rc <- as.factor(d$age_rc)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(swb~age, data = d)Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 3 0.0304 0.9929
2147
This is more of a formality in our case, because we are 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 extra effort on our part!
Check Normality
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
# use it to check the skew and kurtosis of your DV
describe(d$swb) vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 2151 4.44 1.33 4.5 4.49 1.48 1 7 6 -0.35 -0.5 0.03
# 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$swb, group=d$age)
Descriptive statistics by group
group: 1 between 18 and 25
vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 1980 4.44 1.33 4.67 4.5 1.48 1 7 6 -0.38 -0.49 0.03
------------------------------------------------------------
group: 2 between 26 and 35
vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 115 4.25 1.34 4.17 4.26 1.48 1 7 6 -0.03 -0.55 0.12
------------------------------------------------------------
group: 3 between 36 and 45
vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 38 4.68 1.33 4.83 4.68 1.24 2 7 5 -0.12 -0.81 0.22
------------------------------------------------------------
group: 4 over 45
vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 18 4.46 1.45 4.33 4.48 1.36 1.67 7 5.33 0.01 -0.81 0.34
# also use a histogram to examine your continuous variable
hist(d$swb)# last, use a boxplot to examine your continuous and categorical variables together
#categorical/IV goes on the right and the continuous/DV goes on the left!!!!!
boxplot(d$swb~d$age_rc)Issues with My Data - PART OF YOUR WRITEUP
I combined participants that were over the age of 25 (e.g., combined between 26 and 35, between 36 and 45, and over 45). I also confirmed significant homogeneity of variance using Levene’s test (p = .993) and that my dependent variable is normally distributed (skew and kurtosis between -2 and +2).
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$swb~d$age_rc)View Test Output
t_output
Welch Two Sample t-test
data: d$swb by d$age_rc
t = -0.69805, df = 199.3, p-value = 0.486
alternative hypothesis: true difference in means between group older and group younger is not equal to 0
95 percent confidence interval:
-0.2872940 0.1370724
sample estimates:
mean in group older mean in group younger
4.366472 4.441582
Calculate Cohen’s d
# # once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$swb~d$age_rc)View Effect Size
- Trivial: < .2
- Small: between .2 and .5
- Medium: between .5 and .8
- Large: > .8
d_output
Cohen's d
d estimate: -0.05655349 (negligible)
95 percent confidence interval:
lower upper
-0.21287103 0.09976406
Write Up Results
I tested my hypothesis that younger participants will have lower life satisfaction than older participants. I combined participants that were over the age of 25 (e.g., combined between 26 and 35, between 36 and 45, and over 45). I also confirmed homogeneity of variance using Levene’s test (p = .993) and that my dependent variable is normally distributed (skew and kurtosis between -2 and +2). I then conducted the Welch Two-Sample T-test. Our data met all of the assumptions of a t-test, but we did not find a significant difference, t(199.3) = -0.698, p = 0.486, d = -0.057, 95% [-0.21, 0.010]. My effect size was trivial according to Cohen (1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.