class: center, middle, inverse, title-slide .title[ # (
very
) Basic statistics in R ] .author[ ### Guilherme Dias ] --- exclude: true count: false <link href="https://fonts.googleapis.com/css?family=Roboto|Source+Sans+Pro:300,400,600|Ubuntu+Mono&subset=latin-ext" rel="stylesheet"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous"> <!-- ------------ Only edit title, subtitle & author above this ------------ --> --- name: anscombe1 # Four data sets walk into a lab 🧪 <table> <thead> <tr> <th style="text-align:center;"> data_set </th> <th style="text-align:center;"> mean_x </th> <th style="text-align:center;"> mean_y </th> <th style="text-align:center;"> sd_y </th> <th style="text-align:center;"> cor_xy </th> </tr> </thead> <tbody> <tr> <td style="text-align:center;"> Set 1 </td> <td style="text-align:center;"> 9 </td> <td style="text-align:center;"> 7.5 </td> <td style="text-align:center;"> 2.03 </td> <td style="text-align:center;"> 0.82 </td> </tr> <tr> <td style="text-align:center;"> Set 2 </td> <td style="text-align:center;"> 9 </td> <td style="text-align:center;"> 7.5 </td> <td style="text-align:center;"> 2.03 </td> <td style="text-align:center;"> 0.82 </td> </tr> <tr> <td style="text-align:center;"> Set 3 </td> <td style="text-align:center;"> 9 </td> <td style="text-align:center;"> 7.5 </td> <td style="text-align:center;"> 2.03 </td> <td style="text-align:center;"> 0.82 </td> </tr> <tr> <td style="text-align:center;"> Set 4 </td> <td style="text-align:center;"> 9 </td> <td style="text-align:center;"> 7.5 </td> <td style="text-align:center;"> 2.03 </td> <td style="text-align:center;"> 0.82 </td> </tr> </tbody> </table> <br> Same means. Same spread. Same correlation. ### ✋ Are these four data sets the same? --- name: anscombe2 class: middle # Not even close <img src="slide_r_basic_statistic_files/figure-html/anscombe.plot-1.png" width="720" style="display: block; margin: auto auto auto 0;" /> **Anscombe's quartet** (1973). Summary statistics summarise. Plots show. **Always look at your data.** --- name: today # Today 1. **Describe** one variable: centre and spread 2. **Relate** two variables: correlation 3. **Compare** two groups: the t-test <br> All with functions you already know how to call: `mean()`, `sd()`, `cor()`, `t.test()`, … .footnote[Plots here are previews. You'll learn to make them yourself on Thursday.] --- name: mean_median # Centre: mean or median? Six mice weighed. One weight was typed wrong: ``` r weight <- c(24.1, 25.3, 23.8, 26.0, 24.7, 251) # 25.1 typed as 251 mean(weight) median(weight) ``` ``` ## [1] 62.48333 ## [1] 25 ``` -- - **Mean:** the average. Pulled by extreme values. - **Median:** the middle value. Robust to outliers. .content-box-yellow[ A mean that looks odd is often a data-entry error. Look before you trust it! ] --- name: skewed # Skewed data Expression of one gene in 500 cells: most cells low, a few very high. <img src="slide_r_basic_statistic_files/figure-html/skewed-1.png" width="648" style="display: block; margin: auto auto auto 0;" /> With skewed data, the median describes a "typical" value better. Common in biology: counts, concentrations, expression. --- name: spread # Spread: range and IQR Chick weights (g) after six weeks on six different feeds (`chickwts`): ``` r w <- chickwts$weight range(w) quantile(w) # min, Q1, median, Q3, max IQR(w) # Q3 - Q1: the middle 50 % ``` ``` ## [1] 108 423 ## 0% 25% 50% 75% 100% ## 108.0 204.5 258.0 323.5 423.0 ## [1] 119 ``` -- A **boxplot** draws exactly these numbers: <img src="slide_r_basic_statistic_files/figure-html/boxplot-1.png" width="648" style="display: block; margin: auto auto auto 0;" /> --- name: sd # Spread: variance and standard deviation How far are values from the mean, on average? `$$s = \sqrt{\frac{1}{n-1}\sum_{i=1}^{n}(x_i - \bar{x})^2} \qquad \text{variance} = s^2$$` ``` r sd(w) # same unit as the data: grams var(w) # squared unit: grams² ``` ``` ## [1] 78.0737 ## [1] 6095.503 ``` -- For roughly normal data: about **68 %** of values lie within 1 SD of the mean, **95 %** within 2 SD. .footnote[R divides by n − 1 (the *sample* SD), which is what you want for data from an experiment.] --- name: by_group # Summaries per group Mean weight for each feed: ``` r tapply(chickwts$weight, chickwts$feed, mean) ``` ``` ## casein horsebean linseed meatmeal soybean sunflower ## 323.5833 160.2000 218.7500 276.9091 246.4286 328.9167 ``` -- The same with the tidyverse: ``` r library(dplyr) chickwts |> group_by(feed) |> summarise(n = n(), mean = mean(weight), sd = sd(weight)) ``` ``` ## # A tibble: 6 × 4 ## feed n mean sd ## <fct> <int> <dbl> <dbl> ## 1 casein 12 324. 64.4 ## 2 horsebean 10 160. 38.6 ## 3 linseed 12 219. 52.2 ## 4 meatmeal 11 277. 64.9 ## 5 soybean 14 246. 54.1 ## 6 sunflower 12 329. 48.8 ``` --- name: missing # Missing values Most summary functions return `NA` if any value is missing: ``` r x <- c(5.1, 4.8, NA, 5.6) mean(x) mean(x, na.rm = TRUE) sd(x, na.rm = TRUE) ``` ``` ## [1] NA ## [1] 5.166667 ## [1] 0.4041452 ``` .content-box-yellow[ Before you remove them: **how many** values are missing, and **why**? `sum(is.na(x))` ] --- name: correlation # Relate: correlation How strongly do two variables move together? From −1 to +1. <img src="slide_r_basic_statistic_files/figure-html/cor.plot-1.png" width="720" style="display: block; margin: auto auto auto 0;" /> ``` r cor(trees$Girth, trees$Volume) # thicker trees hold more wood ``` ``` ## [1] 0.9671194 ``` --- name: spearman # Pearson or Spearman? Enzyme reaction rate vs. substrate concentration (`Puromycin`): it rises, then levels off. <img src="slide_r_basic_statistic_files/figure-html/spearman.plot-1.png" width="648" style="display: block; margin: auto auto auto 0;" /> ``` r cor(pt$conc, pt$rate) # Pearson: straight-line relationship cor(pt$conc, pt$rate, method = "spearman") # Spearman: any steady up (or down) trend ``` ``` ## [1] 0.8310362 ## [1] 0.9753198 ``` **Spearman** works on ranks: robust to outliers and curves. **Pearson** assumes a straight line. --- name: cor_caveats # Correlation: three warnings 1. **Plot first.** Anscombe's four data sets all have r = 0.82. 2. **One outlier** can create or hide a strong Pearson correlation. 3. **Correlation is not causation.** Ice-cream sales and sunburns are correlated. Neither causes the other: the sun does. <br> ``` r cor(c(1, 2, 3, NA), c(2, 4, 7, 8), use = "complete.obs") # handling NAs ``` ``` ## [1] 0.9933993 ``` --- name: ttest # Compare: the t-test 🍺 Invented in 1908 by William Gosset, a chemist at the **Guinness brewery**, who published as "Student". His data are in R: extra hours of sleep with two drugs, in the same 10 patients. ``` r drug1 <- sleep$extra[sleep$group == 1] drug2 <- sleep$extra[sleep$group == 2] mean(drug2) - mean(drug1) t.test(drug2, drug1)$p.value # as if 20 different patients t.test(drug2, drug1, paired = TRUE)$p.value # the same 10 patients ``` ``` ## [1] 1.58 ## [1] 0.07939414 ## [1] 0.00283289 ``` The design matters: pairing removes patient-to-patient differences. --- name: pvalue # What a p-value is (and isn't) **p-value:** if there were **no real difference**, how likely is a difference at least this large, just by chance? - p = 0.003: very unlikely by chance alone. - p = 0.08: could easily be chance. -- **It is not:** - the probability that the result is true; - a measure of **how large** or **important** the difference is. Always report the difference itself, e.g. "1.6 h more sleep". .footnote[By convention, p < 0.05 is called "significant". Test many things and some will be "significant" by chance.] --- name: tests # Which test? | Comparing | Data roughly normal | Not normal / ranks | |:----------|:--------------------|:-------------------| | 2 groups | `t.test(x, y)` | `wilcox.test(x, y)` | | 2 groups, same subjects | `t.test(x, y, paired = TRUE)` | `wilcox.test(x, y, paired = TRUE)` | | 2 numeric variables | `cor.test(x, y)` | `cor.test(x, y, method = "spearman")` | <br> The result of a test is a **list**: ``` r res <- t.test(drug2, drug1, paired = TRUE) res$conf.int # 95 % confidence interval of the difference ``` ``` ## [1] 0.7001142 2.4598858 ## attr(,"conf.level") ## [1] 0.95 ``` --- name: recap # Recap | Question | Use | R | |:---------|:----|:--| | Typical value? | mean, or median if skewed / outliers | `mean()`, `median()` | | How variable? | SD, or IQR if skewed | `sd()`, `IQR()`, `quantile()` | | Per group? | summary for each group | `tapply()`, `group_by()` + `summarise()` | | Related? | Pearson, or Spearman for ranks/curves | `cor()`, `cor.test()` | | Different? | t-test, or Wilcoxon | `t.test()`, `wilcox.test()` | <br> **Remember:** plot first, mind the `NA`s, and report the size of an effect, not just its p-value. .footnote[More statistics: the NBIS course [Introduction to biostatistics and machine learning](https://nbisweden.github.io/workshop-mlbiostatistics/).] <!-- --------------------- Do not edit this and below --------------------- --> --- name: end_slide class: end-slide, middle count: false # See you at the next lecture! .end-text[ <p class="smaller"> <span class="small" style="line-height: 1.2;">Graphics from </span><img src="./assets/freepik.jpg" style="max-height:20px; vertical-align:middle;"><br> Created: 12-Nov-2026 • <a href="https://www.scilifelab.se/">SciLifeLab</a> • <a href="https://nbis.se/">NBIS</a> </p> ]