D Independent Samples T-test code
Video link: https://youtu.be/0cMZYOywDqk
################################################################################
# Independent Samples T-Test
################################################################################
#Assign data set to an object
df <- sleep
#See what it looks like
head(df)
#Call the package
library(psych)
#Get the descriptives by group
describeBy(df$extra, group = df$group)
#Call ggplot
library(ggplot2)
#Generate a boxplot
ggplot(data = df, aes(x = group, y = extra)) +
geom_boxplot() +
geom_jitter(width = .2)
#SW test for trees
trees <- trees
shapiro.test(trees$Height)
#Assign to an object
SW <- shapiro.test(trees$Height)
#Call tidyverse package
library(tidyverse)
#Run the Shapiro-Wilk test
df %>% #Call our dataframe and send it on
group_by(group) %>% #Group by our grouping variable
summarise("S-W Statistic" = shapiro.test(extra)$statistic, #Give us the statistics we want, in a table
"p-value" = shapiro.test(extra)$p.value)
#Generate two separate histograms
ggplot(data = df, aes(x = extra)) +
geom_histogram() +
facet_grid(~ group)
#Both in the same plot - I don't think this is as clear, especially with these data
ggplot(data = df, aes(x = extra, fill = group)) +
geom_histogram(position = "identity", alpha = 0.4)
#Call qqplotr
library(qqplotr)
#Perform QQ plots by group
ggplot(data = df, mapping = aes(sample = extra, color = group, fill = group)) +
stat_qq_band(alpha = 0.5, conf = 0.95, bandType = "pointwise") +
stat_qq_line(identity = TRUE) +
stat_qq_point(col = "black") +
facet_wrap(~ group, scales = "free") +
labs(x = "Theoretical Quantiles", y = "Sample Quantiles") +
theme_bw()
#Perform detrended QQ plots by group
ggplot(data = df, mapping = aes(sample = extra, color = group, fill = group)) +
stat_qq_band(alpha = 0.5, conf = 0.95, bandType = "pointwise", detrend = TRUE) +
stat_qq_line(identity = TRUE, detrend = TRUE) +
stat_qq_point(col = "black", detrend = TRUE) +
facet_wrap(~ group, scales = "free") +
labs(x = "Theoretical Quantiles", y = "Sample Quantiles") +
theme_bw()
#Call the car package
library(car)
#Perform Levene's Test
LT <- leveneTest(extra ~ group, data=df, center="mean")
#Perform Brown-Forsythe test
BFT <- leveneTest(extra ~ group, data=df, center="median")
#Print both of them
print(LT)
print(BFT)
#Run the independent samples t-test
t.test(extra ~ group, data = df, var.equal = TRUE)
#Calculate cohen's d
#Set our variables first
n1 <- 10 #N
s1 <- 1.79 #SD
x1 <- 0.75 #Mean
n2 <- 10
s2 <- 2
x2 <- 2.33
#Calclate pooled variance
sp <- ((n1 - 1)*(s1^2) + (n2 - 1)*(s2^2))/(n1 + n2 -2)
#Calculate cohen's d
d_is <- abs(x1 - x2)/sqrt(sp)
#Print the value
d_is
#Call the package
library(effectsize)
#Get Cohen's d
cohens_d(df$extra ~ df$group, var.equal = TRUE)