dat1 <- data.frame(group = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2),
x = c(3, 1, 4, 2, 5, 3, 2, 3, 6, 6, 3, NA))
#--------------------------------------
# One-Sample Design
# Two-sided one-sample t-test
# population mean = 3
test.t(dat1$x, mu = 3)
# One-sided one-sample t-test
# population mean = 3, population standard deviation = 1.2
test.t(dat1$x, mu = 3, alternative = "greater")
# Two-sided one-sample t-test
# population mean = 3, convert value 3 to NA
test.t(dat1$x, mu = 3, as.na = 3)
# Two-sided one-sample t-test
# population mean = 3, print Cohen's d
test.t(dat1$x, sigma = 1.2, mu = 3, effsize = TRUE)
# Two-sided one-sample t-test
# population mean = 3, print Cohen's d with small sample correction factor
test.t(dat1$x, sigma = 1.2, mu = 3, effsize = TRUE, correct = TRUE)
# Two-sided one-sample t-test
# population mean = 3,
# do not print hypotheses and descriptive statistics
test.t(dat1$x, sigma = 1.2, mu = 3, hypo = FALSE, descript = FALSE)
# Two-sided one-sample t-test
# print descriptive statistics with 3 digits and p-value with 5 digits
test.t(dat1$x, mu = 3, digits = 3, p.digits = 5)
if (FALSE) {
# Two-sided one-sample t-test
# population mean = 3, plot results
test.t(dat1$x, mu = 3, plot = TRUE)
# Load ggplot2 package
library(ggplot2)
# Save plot, ggsave() from the ggplot2 package
ggsave("One-sample_t-test.png", dpi = 600, width = 3, height = 6)
# Two-sided one-sample t-test
# population mean = 3, extract plot
p <- test.t(dat1$x, mu = 3, output = FALSE)$plot
p
# Extract data
plotdat <- data.frame(x = test.t(dat1$x, mu = 3, output = FALSE)$data[[1]])
# Draw plot in line with the default setting of test.t()
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 = 3, linetype = 3, size = 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())
}
#--------------------------------------
# Two-Sample Design
# Two-sided two-sample t-test
test.t(x ~ group, data = dat1)
# One-sided two-sample t-test
test.t(x ~ group, data = dat1, alternative = "greater")
# Two-sided two-sample t-test
# print Cohen's d with weighted pooled SD
test.t(x ~ group, data = dat1, effsize = TRUE)
# Two-sided two-sample t-test
# print Cohen's d with unweighted pooled SD
test.t(x ~ group, data = dat1, effsize = TRUE, weighted = FALSE)
# Two-sided two-sample t-test
# print Cohen's d with weighted pooled SD and
# small sample correction factor
test.t(x ~ group, data = dat1, effsize = TRUE, correct = TRUE)
# Two-sided two-sample t-test
# print Cohen's d with SD of the reference group 1
test.t(x ~ group, data = dat1, effsize = TRUE,
ref = 1)
# Two-sided two-sample t-test
# print Cohen's d with weighted pooled SD and
# small sample correction factor
test.t(x ~ group, data = dat1, effsize = TRUE,
correct = TRUE)
# Two-sided two-sample t-test
# do not print hypotheses and descriptive statistics,
test.t(x ~ group, data = dat1, descript = FALSE, hypo = FALSE)
# Two-sided two-sample t-test
# print descriptive statistics with 3 digits and p-value with 5 digits
test.t(x ~ group, data = dat1, digits = 3, p.digits = 5)
if (FALSE) {
# Two-sided two-sample t-test
# Plot results
test.t(x ~ group, data = dat1, plot = TRUE)
# Load ggplot2 package
library(ggplot2)
# Save plot, ggsave() from the ggplot2 package
ggsave("Two-sample_t-test.png", dpi = 600, width = 4, height = 6)
# Two-sided two-sample t-test
# extract plot
p <- test.t(x ~ group, data = dat1, output = FALSE)$plot
p
# Extract data used to plot results
plotdat <- test.t(x ~ group, data = dat1, output = FALSE)$data
# Draw plot in line with the default setting of test.t()
ggplot(plotdat, aes(factor(group), x)) +
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))
}
#-----------------
group1 <- c(3, 1, 4, 2, 5, 3, 6, 7)
group2 <- c(5, 2, 4, 3, 1)
# Two-sided two-sample t-test
test.t(group1, group2)
#--------------------------------------
# Paired-Sample Design
dat2 <- data.frame(pre = c(1, 3, 2, 5, 7),
post = c(2, 2, 1, 6, 8))
# Two-sided paired-sample t-test
test.t(dat2$pre, dat2$post, paired = TRUE)
# One-sided paired-sample t-test
test.t(dat2$pre, dat2$post, paired = TRUE,
alternative = "greater")
# Two-sided paired-sample t-test
# convert value 1 to NA
test.t(dat2$pre, dat2$post, as.na = 1, paired = TRUE)
# Two-sided paired-sample t-test
# print Cohen's d based on the standard deviation of the difference scores
test.t(dat2$pre, dat2$post, paired = TRUE, effsize = TRUE)
# Two-sided paired-sample t-test
# print Cohen's d based on the standard deviation of the difference scores
# with small sample correction factor
test.t(dat2$pre, dat2$post, paired = TRUE, effsize = TRUE,
correct = TRUE)
# Two-sided paired-sample t-test
# print Cohen's d controlling for the correlation between measures
test.t(dat2$pre, dat2$post, paired = TRUE, effsize = TRUE,
weighted = FALSE)
# Two-sided paired-sample t-test
# print Cohen's d controlling for the correlation between measures
# with small sample correction factor
test.t(dat2$pre, dat2$post, paired = TRUE, effsize = TRUE,
weighted = FALSE, correct = TRUE)
# Two-sided paired-sample t-test
# print Cohen's d ignoring the correlation between measures
test.t(dat2$pre, dat2$post, paired = TRUE, effsize = TRUE,
weighted = FALSE, cor = FALSE)
# Two-sided paired-sample t-test
# do not print hypotheses and descriptive statistics
test.t(dat2$pre, dat2$post, paired = TRUE, hypo = FALSE, descript = FALSE)
# Two-sided paired-sample t-test
# population standard deviation of difference score = 1.2
# print descriptive statistics with 3 digits and p-value with 5 digits
test.t(dat2$pre, dat2$post, paired = TRUE, digits = 3,
p.digits = 5)
if (FALSE) {
# Two-sided paired-sample t-test
# Plot results
test.t(dat2$pre, dat2$post, paired = TRUE, plot = TRUE)
# Load ggplot2 package
library(ggplot2)
# Save plot, ggsave() from the ggplot2 package
ggsave("Paired-sample_t-test.png", dpi = 600, width = 3, height = 6)
# Two-sided paired-sample t-test
# Extract plot
p <- test.t(dat2$pre, dat2$post, paired = TRUE, output = FALSE)$plot
p
# Extract data used to plot results
plotdat <- data.frame(test.t(dat2$pre, dat2$post, paired = TRUE, output = FALSE)$data)
# Difference score
plotdat$diff <- plotdat$y - plotdat$x
# Draw plot in line with the default setting of test.t()
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, size = 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