Untitled

Quarto Demo

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(knitr)

Graphs

ggplot(mtcars, aes(x=mpg, y=hp, color = cyl)) +
  geom_point() + 
  theme_classic() +
  labs(x = 'Miles Per Gallon', y = 'Horse Power', color = 'Cylinders')

Table

kable(head(mtcars), digits=1)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.9 2.6 16.5 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.9 2.9 17.0 0 1 4 4
Datsun 710 22.8 4 108 93 3.9 2.3 18.6 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.1 3.2 19.4 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.1 3.4 17.0 0 0 3 2
Valiant 18.1 6 225 105 2.8 3.5 20.2 1 0 3 1

Statistics

mod <- lm(mpg ~ am + cyl * hp, data= mtcars)

summary(mod)

Call:
lm(formula = mpg ~ am + cyl * hp, data = mtcars)

Residuals:
    Min      1Q  Median      3Q     Max 
-4.2202 -1.5052 -0.2882  1.0368  5.9707 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 44.113013   6.059124   7.280 7.86e-08 ***
am           3.760837   1.199249   3.136  0.00411 ** 
cyl         -2.911001   0.943814  -3.084  0.00467 ** 
hp          -0.178592   0.060300  -2.962  0.00631 ** 
cyl:hp       0.018541   0.007691   2.411  0.02301 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 2.593 on 27 degrees of freedom
Multiple R-squared:  0.8388,    Adjusted R-squared:  0.8149 
F-statistic: 35.13 on 4 and 27 DF,  p-value: 2.451e-10
mod |> 
  broom::tidy() |> 
  kable(digits = 3)
term estimate std.error statistic p.value
(Intercept) 44.113 6.059 7.280 0.000
am 3.761 1.199 3.136 0.004
cyl -2.911 0.944 -3.084 0.005
hp -0.179 0.060 -2.962 0.006
cyl:hp 0.019 0.008 2.411 0.023

Writing some stats.

t_test <- t.test(mtcars$mpg[mtcars$am == 0],
       mtcars$mpg[mtcars$am == 1])

t_test

    Welch Two Sample t-test

data:  mtcars$mpg[mtcars$am == 0] and mtcars$mpg[mtcars$am == 1]
t = -3.7671, df = 18.332, p-value = 0.001374
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -11.280194  -3.209684
sample estimates:
mean of x mean of y 
 17.14737  24.39231 

There is a statistically significant difference in mpg between automatic and manual cars t(18.33) = -3.77, p = 0.001.