#----------------------------------------------------------------------------
# One-Sample Design
# Example 1a: Two-sided one-sample z-test
# population mean = 20, population standard deviation = 6
test.z(mtcars$mpg, sigma = 6, mu = 20)
# Example 1b: One-sided one-sample z-test
# population mean = 20, population standard deviation = 6
# print Cohen's d
test.z(mtcars$mpg, sigma = 6, mu = 20, alternative = "greater", effsize = TRUE)
# Example 1c: Two-sided one-sample z-test
# population mean = 20, population standard deviation = 6
# plot results
test.z(mtcars$mpg, sigma = 6, mu = 20, plot = TRUE)
# Load ggplot2 package
library(ggplot2)
if (FALSE) {
# Save plot, ggsave() from the ggplot2 package
ggsave("One-sample_z-test.png", dpi = 600, width = 3, height = 6)}
# Example 1d: Two-sided one-sample z-test
# population mean = 20, population standard deviation = 6
# extract plot and results
p <- test.z(mtcars$mpg, sigma = 6, mu = 20, output = FALSE)$plot
p
# Example 1e: Two-sided one-sample z-test
# Draw plot in line with the default setting of test.z()
# Extract data
plotdat <- data.frame(test.z(mtcars$mpg, sigma = 6, mu = 20, output = FALSE)$data[[1]])
# Extract results
result <- test.z(mtcars$mpg, sigma = 6, mu = 20, output = FALSE)$result
# Draw plot
ggplot(plotdat, aes(0, x)) +
geom_point(data = result, aes(x = 0L, m), size = 4) +
geom_errorbar(data = result, aes(x = 0L, y = m, ymin = m.low, ymax = m.upp),
width = 0.2) +
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 z-test
# population standard deviation (SD) = 62, equal SD assumption
test.z(mpg ~ vs, sigma = 6, data = mtcars)
# Example 2b: Two-sided two-sample z-test
# population standard deviation (SD) = 4 and 6, unequal SD assumption
test.z(mpg ~ vs, sigma = c(4, 6), data = mtcars)
# Example 2c: One-sided two-sample z-test
# population standard deviation (SD) = 1.2, equal SD assumption
# print Cohen's d
test.z(mpg ~ vs, sigma = c(4, 6), data = mtcars, alternative = "greater",
effsize = TRUE)
# Example 2d: Two-sided two-sample z-test
# population standard deviation (SD) = 1.2, equal SD assumption
# plot results
test.z(mpg ~ vs, sigma = 6, data = mtcars, plot = TRUE)
# Load ggplot2 package
library(ggplot2)
if (FALSE) {
# Save plot, ggsave() from the ggplot2 package
ggsave("Two-sample_z-test.png", dpi = 600, width = 4, height = 6)}
# Example 2e: Two-sided two-sample z-test
# population standard deviation (SD) = 1.2, equal SD assumption
# extract plot
p <- test.z(mpg ~ vs, sigma = 6, data = mtcars, output = FALSE)$plot
p
# Example 2f: Two-sided two-sample z-test
# population standard deviation (SD) = 1.2, equal SD assumption
test.z(c(3, 1, 4, 2, 5, 3, 6, 7), c(5, 2, 4, 3, 1), sigma = 1.2)
#----------------------------------------------------------------------------
# Paired-Sample Design
# Example 3a: Two-sided paired-sample z-test, alternative specification
# population standard deviation of difference score = 1.2
test.z(mtcars$drat, mtcars$wt, sigma = 1.2, paired = TRUE)
# Example 3b: One-sided paired-sample z-test
# population standard deviation of difference score = 1.2
# print Cohen's d
test.z(mtcars$drat, mtcars$wt, sigma = 1.2, paired = TRUE,
alternative = "greater", effsize = TRUE)
# Example 3c: Two-sided paired-sample z-test
# population standard deviation of difference score = 1.2
# plot results
test.z(mtcars$drat, mtcars$wt, sigma = 1.2, paired = TRUE, plot = TRUE)
# Load ggplot2 package
library(ggplot2)
if (FALSE) {
# Save plot, ggsave() from the ggplot2 package
ggsave("Paired-sample_z-test.png", dpi = 600, width = 3, height = 6)}
# Example 3d: Two-sided paired-sample z-test
# population standard deviation of difference score = 1.2
# extract plot
p <- test.z(mtcars$drat, mtcars$wt, sigma = 1.2, paired = TRUE, output = FALSE)$plot
p
# Example 1e: Two-sided paired-sample z-test
# Draw plot in line with the default setting of test.z()
# Extract data
plotdat <- data.frame(test.z(mtcars$drat, mtcars$wt, sigma = 1.2, paired = TRUE,
output = FALSE)$data)
# Difference score
plotdat$diff <- plotdat$y - plotdat$x
# Extract results
result <- test.z(mtcars$drat, mtcars$wt, sigma = 1.2, paired = TRUE,
output = FALSE)$result
# Draw plot
ggplot(plotdat, aes(0, diff)) +
geom_point(data = result, aes(x = 0, m.diff), size = 4) +
geom_errorbar(data = result,
aes(x = 0L, y = m.diff, ymin = m.low, ymax = m.upp), width = 0.2) +
scale_x_continuous(name = NULL, limits = c(-2, 2)) +
scale_y_continuous(name = "y") +
geom_hline(yintercept = 0, 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())
Run the code above in your browser using DataLab