Skip to content
Course Notes: Experimental Design in R
Course Notes
Use this workspace to take notes, store code snippets, or build your own interactive cheatsheet! For courses that use data, the datasets will be available in the datasets folder.
# Import any packages you want to use here
library(tidyverse)
if (!require(pwr)) {
install.packages('pwr')
library(pwr)
}
library(broom)
library(ggplot2)
library(dplyr)Take Notes
Add notes here about the concepts you've learned and code cells with code you want to keep.
t.test is used for comparing two groups' mean. anova is used for comparing more than two groups
# Add your code snippets here
data('ToothGrowth')
summary(ToothGrowth)t.test(ToothGrowth['len'], alternative = 'two.sided', mu = 18)ToothGrowth |>
group_by('supp') |>
summarize(mean(len), median(len), sd(len))ggplot(ToothGrowth, aes(x = len)) +
geom_histogram(bins = 15) +
geom_vline(xintercept = mean(ToothGrowth$len), color = 'red', linewidth = 2) +
geom_vline(xintercept = median(ToothGrowth$len), color = 'blue', linewidth = 2)+
geom_vline(xintercept = quantile(ToothGrowth$len, c(0.025, 0.975)), color = 'maroon', linewidth = 2) +
annotate('text', x = mean(ToothGrowth$len)-3.5, y = 9.8, label = 'Mean', color = "red", size = 5)+
annotate('segment', x = 17, y = 9.8, xend = mean(ToothGrowth$len), yend = 9.8, arrow = arrow(length = unit(0.5, "cm")), color = 'red')+
annotate('text', x = median(ToothGrowth$len) + 3.5, y = 9.8, label = 'Median', color = "blue", size = 5) +
annotate('segment', x = 21, y = 9.8, xend = median(ToothGrowth$len), yend = 9.8, arrow = arrow(length = unit(0.5, "cm")), color = 'blue')+
annotate('text', x = quantile(ToothGrowth$len, 0.025) -2, y = 5, label = '2.5 %', color = "maroon", size = 5)+
annotate('text', x = quantile(ToothGrowth$len, 0.975) -2, y = 5, label = '97.5 %', color = "maroon", size = 5)
args(annotate)# Perform a t-test
ToothGrowth_ttest <- t.test(len ~ supp, data = ToothGrowth)
# Load broom package
library(broom)
# Tidy ToothGrowth_ttest
tidy(ToothGrowth_ttest)