Hello world! Test - italics, bold, link
To do headings “#”, subheadings “##” or “###” or…
Unordered lists: - first item - second item
Ordered lists: 1. first 2. second
We can insert an image like this:
We can add citations 1
values <- rnorm(5)
values
## [1] -0.3548660 -0.3427437 0.3367141 -0.3111891 1.4513610
eval=FALSE - do not run code (only input) echo=FALSE - do not show code (only output)
values <- rnorm(5)
values
## [1] -0.14688084 -1.32246017 0.85901799 -0.01430145 -2.90588176
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
data(ToothGrowth)
View(ToothGrowth)
Load the ‘toothGrowth’ data set using this code:
{data(ToothGrowth)}
head(ToothGrowth)
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
tail(ToothGrowth)
## len supp dose
## 55 24.8 OJ 2
## 56 30.9 OJ 2
## 57 26.4 OJ 2
## 58 27.3 OJ 2
## 59 29.4 OJ 2
## 60 23.0 OJ 2
dim(ToothGrowth)
## [1] 60 3
nrow(ToothGrowth)
## [1] 60
ncol(ToothGrowth)
## [1] 3
names(ToothGrowth)
## [1] "len" "supp" "dose"
str(ToothGrowth)
## 'data.frame': 60 obs. of 3 variables:
## $ len : num 4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
## $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
## $ dose: num 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
summary(ToothGrowth)
## len supp dose
## Min. : 4.20 OJ:30 Min. :0.500
## 1st Qu.:13.07 VC:30 1st Qu.:0.500
## Median :19.25 Median :1.000
## Mean :18.81 Mean :1.167
## 3rd Qu.:25.27 3rd Qu.:2.000
## Max. :33.90 Max. :2.000
View(ToothGrowth)
glimpse(ToothGrowth)
## Rows: 60
## Columns: 3
## $ len <dbl> 4.2, 11.5, 7.3, 5.8, 6.4, 10.0, 11.2, 11.2, 5.2, 7.0, 16.5, 16.5,…
## $ supp <fct> VC, VC, VC, VC, VC, VC, VC, VC, VC, VC, VC, VC, VC, VC, VC, VC, V…
## $ dose <dbl> 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1.0, 1.0, 1.0, …
library(skimr)
skim(ToothGrowth)
| Name | ToothGrowth |
| Number of rows | 60 |
| Number of columns | 3 |
| _______________________ | |
| Column type frequency: | |
| factor | 1 |
| numeric | 2 |
| ________________________ | |
| Group variables | None |
Variable type: factor
| skim_variable | n_missing | complete_rate | ordered | n_unique | top_counts |
|---|---|---|---|---|---|
| supp | 0 | 1 | FALSE | 2 | OJ: 30, VC: 30 |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| len | 0 | 1 | 18.81 | 7.65 | 4.2 | 13.07 | 19.25 | 25.27 | 33.9 | ▅▃▅▇▂ |
| dose | 0 | 1 | 1.17 | 0.63 | 0.5 | 0.50 | 1.00 | 2.00 | 2.0 | ▇▇▁▁▇ |
ggplot(ToothGrowth, aes(x = dose, y = len)) +
geom_point()
Only boxplot
ggplot(ToothGrowth, aes (x = factor(dose), y = len)) +
geom_boxplot()
Boxplot + Jittered Individual Points + Labels
ggplot(ToothGrowth, aes (x = factor(dose), y = len)) +
geom_boxplot() +
geom_jitter(width = 0.1, alpha = 0.6) +
labs (
x = "Dosage",
y = "Tooth Length",
title = "Tooth Length by Dosage"
)
Violin Plot
ggplot(ToothGrowth, aes (x = factor(dose), y = len)) +
geom_violin()
Violin Plot + Jittered Individual Points
ggplot(ToothGrowth, aes (x = factor(dose), y = len)) +
geom_violin() +
geom_jitter(width = 0.1, alpha = 0.6)
Linear Model (lm (response ~ predictor))
lm(len ~ dose, data = ToothGrowth)
##
## Call:
## lm(formula = len ~ dose, data = ToothGrowth)
##
## Coefficients:
## (Intercept) dose
## 7.422 9.764
As the slope is positive / >0 (in this case slope = 9.764 (9.764 increase per 1 dose unit increase), the intercept of 7.422 just says that at dose=0 that 7.422 is the starting point), then yes tooth length increases with dosage size.
ggplot(ToothGrowth, aes(x = supp, y = len)) +
geom_point()
Only boxplot
ggplot(ToothGrowth, aes (x = factor(supp), y = len)) +
geom_boxplot()
Boxplot + Jittered Individual Points + Labels
ggplot(ToothGrowth, aes (x = factor(supp), y = len)) +
geom_boxplot() +
geom_jitter(width = 0.1, alpha = 0.6) +
labs (
x = "Dosage",
y = "Tooth Length",
title = "Tooth Length by Dosage"
)
Violin Plot
ggplot(ToothGrowth, aes (x = factor(supp), y = len)) +
geom_violin()
Violin Plot + Jittered Individual Points
ggplot(ToothGrowth, aes (x = factor(supp), y = len)) +
geom_violin() +
geom_jitter(width = 0.1, alpha = 0.6)
Aggregating the group means
aggregate(len ~ supp, data=ToothGrowth, mean)
## supp len
## 1 OJ 20.66333
## 2 VC 16.96333
QQ plots - kinda wonky, seems like the data isn’t normally distributed
ggplot(ToothGrowth, aes(sample = len)) +
stat_qq() +
stat_qq_line() +
facet_wrap(~ supp)
Two-sample t-test - p=0.06. I don’t think using a t-test was the correct way to go, but having a p>0.05 anyways means that this isn’t right
t.test(len ~ supp, data = ToothGrowth)
##
## Welch Two Sample t-test
##
## data: len by supp
## t = 1.9153, df = 55.309, p-value = 0.06063
## alternative hypothesis: true difference in means between group OJ and group VC is not equal to 0
## 95 percent confidence interval:
## -0.1710156 7.5710156
## sample estimates:
## mean in group OJ mean in group VC
## 20.66333 16.96333
ggplot(ToothGrowth, aes(x = factor(dose), y = len, fill = supp)) +
geom_boxplot(position = position_dodge(width = 0.8)) +
geom_jitter(
aes(color = supp),
position = position_jitterdodge(jitter.width = 0.1, dodge.width = 0.8),
alpha = 0.6
) +
labs(
x = "Dose",
y = "Tooth length",
fill = "Supplement",
color = "Supplement",
title = "Tooth Length by Dose and Supplement"
)
footnotes↩︎