#----------------------------------------------------------------------------
# One-Sample Design
# Example 1a: Two-sided one-sample t-test
# population mean = 20
test.t(mtcars$mpg, mu = 20)
# Example 1b: One-sided one-sample t-test
# population mean = 20, print Cohen's d
test.t(mtcars$mpg, mu = 20, alternative = "greater", effsize = TRUE)
# Example 1c: Two-sided one-sample t-test
# population mean = 20, plot results
test.t(mtcars$mpg, mu = 20, plot = TRUE)
# Load ggplot2 package
library(ggplot2)
if (FALSE) {
# Save plot, ggsave() from the ggplot2 package
ggsave("One-sample_t-test.png", dpi = 600, width = 3, height = 6)}
# Example 1d: Two-sided one-sample t-test
# population mean = 20, extract plot
p <- test.t(mtcars$mpg, mu = 20, output = FALSE)$plot
p
# Example 1e: Two-sided one-sample t-test
# Draw plot in line with the default setting of test.t()
# Extract data
plotdat <- data.frame(x = test.t(mtcars$mpg, mu = 20, output = FALSE)$data[[1]])
# Draw plot
ggplot(plotdat, aes(0, x)) +
geom_point(stat = "summary", fun = "mean", size = 4) +
stat_summary(fun.data = "mean_cl_normal", geom = "errorbar", width = 0.20) +
scale_x_continuous(name = NULL, limits = c(-2, 2)) +
scale_y_continuous(name = NULL) +
geom_hline(yintercept = 20, linetype = 3, linewidth = 0.8) +
labs(subtitle = "Two-Sided Confidence Interval") +
theme_bw() + theme(plot.subtitle = element_text(hjust = 0.5),
axis.text.x = element_blank(),
axis.ticks.x = element_blank())
#----------------------------------------------------------------------------
# Two-Sample Design
# Example 2a: Two-sided two-sample t-test
test.t(mpg ~ vs, data = mtcars)
# Example 2b: One-sided two-sample t-test
# print Cohen's d with weighted pooled SD
test.t(mpg ~ vs, data = mtcars, alternative = "greater", effsize = TRUE)
# Example 2c: Two-sided two-sample t-test
# Plot results
test.t(mpg ~ vs, data = mtcars, plot = TRUE)
# Load ggplot2 package
library(ggplot2)
if (FALSE) {
# Save plot, ggsave() from the ggplot2 package
ggsave("Two-sample_t-test.png", dpi = 600, width = 4, height = 6)}
# Example 2d: Two-sided two-sample t-test
# extract plot
p <- test.t(mpg ~ vs, data = mtcars, output = FALSE)$plot
p
# Example 2e: Two-sided two-sample t-test
# Draw plot in line with the default setting of test.t()
# Extract data used to plot results
plotdat <- test.t(mpg ~ vs, data = mtcars, output = FALSE)$data
# Draw plot
ggplot(plotdat, aes(factor(vs), mpg)) +
geom_point(stat = "summary", fun = "mean", size = 4) +
stat_summary(fun.data = "mean_cl_normal", geom = "errorbar", width = 0.20) +
scale_x_discrete(name = NULL) + scale_y_continuous(name = "y") +
labs(title = "", subtitle = "Two-Sided 95 Confidence Interval") +
theme_bw() + theme(plot.subtitle = element_text(hjust = 0.5))
# Example 2f: Two-sided two-sample t-test, alternative specification
test.t(c(3, 1, 4, 2, 5, 3, 6, 7), c(5, 2, 4, 3, 1))
#----------------------------------------------------------------------------
# Paired-Sample Design
# Example 3a: Two-sided paired-sample t-test
test.t(mtcars$drat, mtcars$wt, paired = TRUE)
# Example 3b: One-sided paired-sample t-test
# print Cohen's d based on the standard deviation of the difference scores
test.t(mtcars$drat, mtcars$wt, paired = TRUE, alternative = "greater",
effsize = TRUE)
# Example 3c: Two-sided paired-sample t-test
# Plot results
test.t(mtcars$drat, mtcars$wt, paired = TRUE, plot = TRUE)
# Load ggplot2 package
library(ggplot2)
if (FALSE) {
# Save plot, ggsave() from the ggplot2 package
ggsave("Paired-sample_t-test.png", dpi = 600, width = 3, height = 6)}
# Example 3d: Two-sided paired-sample t-test
# Extract plot
p <- test.t(mtcars$drat, mtcars$wt, paired = TRUE, output = FALSE)$plot
p
# Example 3e: Two-sided paired-sample t-test
# Draw plot in line with the default setting of test.t()
# Extract data used to plot results
plotdat <- data.frame(test.t(mtcars$drat, mtcars$wt, paired = TRUE, output = FALSE)$data)
# Difference score
plotdat$diff <- plotdat$y - plotdat$x
# Draw plot
ggplot(plotdat, aes(0, diff)) +
geom_point(stat = "summary", fun = "mean", size = 4) +
stat_summary(fun.data = "mean_cl_normal", geom = "errorbar", width = 0.20) +
scale_x_discrete(name = NULL) + scale_y_continuous(name = NULL) +
geom_hline(yintercept = 0, linetype = 3, linewidth = 0.8) +
labs(subtitle = "Two-Sided 95 Confidence Interval") +
theme_bw() + theme(plot.subtitle = element_text(hjust = 0.5),
axis.text.x = element_blank(),
axis.ticks.x = element_blank())
Run the code above in your browser using DataLab