Statistical Learning

RaukR 2026 • Data Science With R

Dania Machlab

18-Aug-2026

Outline

  • Overview: R, Bioconductor, statistical learning
  • Simple linear regression
  • Multiple linear regression
  • Shrinkage methods
  • Stability selection
  • Applications

R and statistics

  • S (for Statistics) is a statistical programming language developed at Bell Labs in 1976.
    • “to turn ideas into software, quickly and faithfully” – John Chambers
  • Bell labs: Unix, C, C++, Information Theory
    • 11 Nobel prizes and 5 Turing awards
  • R released in 1993 by Ross Ihaka and Robert Gentleman as the free and open-source alternative (initially part of GNU project) using ideas and syntax from S.
    • R 1.0 released in 2000.
  • R is the language of choice for academic statisticians and new developments and methods in statistics are often released as R packages first.

R and bioinformatics - the Bioconductor project


  • “The mission of the Bioconductor project is to develop, support, and disseminate free open source software that facilitates rigorous and reproducible analysis of data from current and emerging biological assays. We are dedicated to building a diverse, collaborative, and welcoming community of developers and data scientists.”

  • Current release (2.23):
    • 2,418 software packages
    • 928 AnnotationData packages
    • 436 ExperimentData packages
    • 28 Workflows
    • 8 books
BiocManager::install(
    "Biostrings", 
    version = "3.23")
renv::init(bioconductor = "3.23")

EuroBioc 2026 Turku, Finland (June 3-5)

25 years of Bioconductor

Conferences

  • BioC – Aug 10-12 in Seattle
  • BiocAsia – next one in Melbourne, Australia Nov 19-20, 2026
  • EuroBioC – next one in Basel, Switzerland Sept 8-10, 2027 (pre-conf 6-7)

Image 1 Caroline Uhler

Image 2 Davide Risso

Image 3 Mireia Ramos-Rodríguez

Image 4 Luca Giorgetti

Image 5 Daria Lazic

  • Zulip channel open to everyone

R and statistics

Statistical methods are developed to be used in the real world!


  • William Sealy Gosset worked as a chemist and brewer at Guinness Brewery in Dublin in the early 1900s.
  • Used pen name Student because Guinness prohibited employees from publishing research under their own names.
  • Guiness wanted to improve barley varieties and beer quality.
  • Experiments with small sample sizes. The population variance is unknown and must be estimated.
  • Student’s t-distribution and test of statistical significance.

Early examples of learning something about the world

  • Eratosthenes: ancient Greek philosopher, scholar, mathematician, geographer, poet, astronomer and music theorist.
  • Estimating the circumference of the earth in 240 BC.
  • Assumptions:
    • earth is round
    • sun is so far the rays coming from there are parallel
    • distance between Alexandria and Syene is known
  • \(\frac{7.2^\circ}{360^\circ}=\frac{ditance \ between \ Alexandria \ and \ Aswan}{Earth's \ circumference}\)
  • His estimate: approximately 40,338 km (25,065 mi)
  • Today’s number (around the equator): 40,075.017 km (24,901.461 mi)

Data science today

  • Nowadays, skill set:
    • programming
    • statistics
    • data visualization
    • creativity
    • data exploration and going hunting for “problems”
    • understanding the data and data generation process
    • formulating a question and then finding the right method to answer it
    • communication in an interdisciplinary world

Hypothesis generation

  • Formulate a hypothesis -> collect data -> test
  • Collect data -> explore -> generate hypothesis
  • Explore -> new data -> confirm
  • Exploration vs confirmation

Statistical learning

  • “Statistical learning refers to a set of tools for making sense of complex datasets” – An Introduction to Statistical Learning by James, Witten, Hastie and Tibshirani

  • Many of these methods were developed long before we had computers, and they are incredibly useful today.

  • No single methods will perform well or be appropriate for all data set. It is therefore important to understand how these tools work, when to use them and how to interpret the results.

  • We are drowning in information and starving for knowledge” – Rutherford D. Roger as quoted in Elements of Statistical Learning

Supervised vs unsupervised learning

  • Suppose we have measurements for \(p\) variables \(x_1, x_2 ... x_p\) and their associated variable \(y\) and wish to say something about how \(x_1, x_2 ... x_p\) explain or relate to \(y\). An example is prediction or classification.

  • \(y \sim f(x)\)

  • Suppose we only have measurements for variables \(x_1, x_2 ... x_p\) and wish to learn the relationships between them. An example method is clustering.

Prediction

  • Training data vs test data

  • Trade off between prediction accuracy and model interpretability

  • Training data: to estimate the model parameters and tune the model.
  • Test data: evaluate the model.
  • Do you think we can mix both or use the training data to test?
  • Our aim here is to use the model on new data and not to explain the data we already have.
  • Overfitting – Should we be skeptical when someone claims 100% prediction accuracy with their model?

Inference

  • Which \(x\) variables are associated with the variable \(y\)?
  • Can we learn or day something about this relationship?
  • Suppose we saw the data below, can we use a model to learn the nature of the relationship between the number of Nobel prizes and chocolate consumption?

Choosing the right model


  • Do we have prior information about the data?
  • Data generation process?
  • Expected distributions (e.g. count data)?
  • Missing values, missing at random?
  • Do we have enough data points?
  • Are we trying to learn something about the relationship between our variables or to have a model that we can use to predict outcomes for future data sets?

Linear models


\(y = f(x) + \epsilon\)

  • \(y\) is often called the response or dependent variable

  • \(x\) is often called the predictor or independent variable

  • \(\epsilon\) is the random error term

  • Statistical learning refers to a set of approaches for estimating \(f\)

  • Simple linear model: \(y = \beta_0 + \beta_1x + \epsilon\)

  • By the 1970s many methods for learning from the data were available. The vast majority of them were linear methods because fitting non-linear relationships was computationally difficult at the time.

Why care about linear approaches?

  • Having a good understanding of linear models is essential for understanding non-linear ones. Many of the latter are generalizations of the linear methods.

  • Powerful in their interpretability and in their flexibility

  • If we are interested in inference then retaining interpretability is key

Simple linear regression

  • learn the association between a single predictor \(x\) and a quantitative response \(y\).

  • assumes an approximately linear relationship between \(x\) and \(y\).

\[y = \beta_0 + \beta_1x + \epsilon\]

  • The error term \(\epsilon\) captures the noise or lack of fit to the linear equation.




  • What do \(\beta_0\) and \(\beta_1\) capture?
  • How do we find the best fit?

Least squares

  • \(y = \beta_0 + \beta_1x + \epsilon\)
  • \(\hat{y_i}=\beta_0 + \beta_1x_i\)
  • \(e_i=y_i-\hat{y_i}\)
  • The residual sum of squares (RSS) is defined as \(RSS=\sum_{i=1}^Ne_i^2\)

  • We want to choose beta estimates which minimize the RSS. This is the least squares method.

  • Least squares was developed at the beginning of the 19th century (earliest form of linear regression), with applications in astronomy.

Can you think of another way?

  • Given \(x\), how far is the prediction off \(y\)?
  • What line lies closest to the cloud of points regardless of which axis is \(x\) or \(y\)
  • Which would you pick?
  • Least squares: residuals are capturing unexplained variance and prediction error.

Multiple linear regression

  • \(y = \beta_0 + \beta_1x_1 + \beta_2x_2 + ... + \beta_px_p + \epsilon\)
  • \(y = \beta.X + \epsilon\)
  • Assumptions:
    • given x and y values, estimate the coefficients
    • linear relationship
    • constant variance of residuals (homoscedasticity)
    • \(\epsilon \sim N(0, \sigma^2)\)
    • independence of observations
    • no multicollinearity among predictors
    • n >> p

Spot the linear model

\[y = \beta_0 + \beta_1x_1 + \beta_2x_2 + \beta_3x_4x_5 + \epsilon\]

\[y = \beta_0 + \beta_1x_1 + \beta_2x_2^2 + \beta_3\sqrt{x_3} + \epsilon\]

\[log2(y) = \beta_0 + \beta_1x_1 + \beta_2x_2^2 + \beta_3\sqrt{x_3} + \epsilon\]

\[y = \beta_0 e^{\beta_1x} + \epsilon\]

\[y = \beta_0 + \beta_1e^{x} + \epsilon\]

\[y = \beta_0 + sin(\beta_1x) + \epsilon\]

Multiple linear regression

## predictor matrix
X <- model.matrix(~ TV + radio + newspaper, 
                  data = advertisingData)
head(X)
  (Intercept)    TV radio newspaper
1           1 230.1  37.8      69.2
2           1  44.5  39.3      45.1
3           1  17.2  45.9      69.3
4           1 151.5  41.3      58.5
5           1 180.8  10.8      58.4
6           1   8.7  48.9      75.0
## linear fit
fit1 <- lm(sales ~ TV + radio + newspaper, data = advertisingData)
fit2 <- lm(sales ~ X + 0, data = advertisingData)
fit1

Call:
lm(formula = sales ~ TV + radio + newspaper, data = advertisingData)

Coefficients:
(Intercept)           TV        radio    newspaper  
   2.938889     0.045765     0.188530    -0.001037  
fit2

Call:
lm(formula = sales ~ X + 0, data = advertisingData)

Coefficients:
X(Intercept)           XTV        Xradio    Xnewspaper  
    2.938889      0.045765      0.188530     -0.001037  

How do we interpret the coefficient values?

fit1

Call:
lm(formula = sales ~ TV + radio + newspaper, data = advertisingData)

Coefficients:
(Intercept)           TV        radio    newspaper  
   2.938889     0.045765     0.188530    -0.001037  



\(y = \beta_0 + \beta_1x_1 + \beta_2x_2 + ... + \beta_px_p + \epsilon\)

Categorical variables

  • How would you represent them?
  • Categorical variables with \(l\) levels can be represented as \(l-1\) so called dummy variables consisting of columns of 1s and 0s. We will see this more in the lab session.
head(Credit)
   Income Limit Rating Cards Age Education Own Student Married Region Balance
1  14.891  3606    283     2  34        11  No      No     Yes  South     333
2 106.025  6645    483     3  82        15 Yes     Yes     Yes   West     903
3 104.593  7075    514     4  71        11  No      No      No   West     580
4 148.924  9504    681     3  36        11 Yes      No      No   West     964
5  55.882  4897    357     2  68        16  No      No     Yes  South     331
6  80.180  8047    569     4  77        10  No      No      No  South    1151
X <- model.matrix(~ Own, 
                  data = Credit)
head(X)
  (Intercept) OwnYes
1           1      0
2           1      1
3           1      0
4           1      1
5           1      0
6           1      0
X <- model.matrix(~ Region, 
                  data = Credit)
head(X)
  (Intercept) RegionSouth RegionWest
1           1           1          0
2           1           0          1
3           1           0          1
4           1           0          1
5           1           1          0
6           1           1          0

Interaction terms

  • Beta coefficients per predictor say something about the effect of that predictor on \(y\) when all others are fixed. Can you think of a way to have a coefficient capturing the simultaneous effect of two predictors holidng the rest fixed in our predictor matrix?
  • Interaction terms allow us to capture more complex relationships in the model reflecting the joint effect of two predictors. This is represented in the product of the two predictors.

X <- model.matrix(~ TV + radio + newspaper + TV:radio, 
                  data = advertisingData)
head(X)
  (Intercept)    TV radio newspaper TV:radio
1           1 230.1  37.8      69.2  8697.78
2           1  44.5  39.3      45.1  1748.85
3           1  17.2  45.9      69.3   789.48
4           1 151.5  41.3      58.5  6256.95
5           1 180.8  10.8      58.4  1952.64
6           1   8.7  48.9      75.0   425.43
X <- model.matrix(~ TV*radio + newspaper, 
                  data = advertisingData)
head(X)
  (Intercept)    TV radio newspaper TV:radio
1           1 230.1  37.8      69.2  8697.78
2           1  44.5  39.3      45.1  1748.85
3           1  17.2  45.9      69.3   789.48
4           1 151.5  41.3      58.5  6256.95
5           1 180.8  10.8      58.4  1952.64
6           1   8.7  48.9      75.0   425.43

Evaluating the quality of the linear fit

  • Residual standard error \(RSE=\sqrt{\frac{1}{N-p-1}RSS}\)
  • Roughly represents the average amount of deviation from the fitted line.
  • The size of the RSE value will reflect how poorly the model fits the data.
  • Measured in units of \(y\).
  • \(R^2=\frac{TSS-RSS}{TSS}\) where \(TSS=\sum(y_i-\bar{y})^2\).
  • RSS can be thought of as measuring the variability that is left unexplained after doing the regression.
  • The \(R^2\) value thus reflects the proportion of variability in \(y\) that can be explained using \(x\) by performing the regression.

Correlation is not causation

Time for the lab





Please follow sections 1 to 5 in the lab. Read carefully and try to answer the sections marked as Task.

Outliers

  • What is an outlier?
  • Why do they arise?
  • What do you do with them?
  • Can the linear fit be impacted by outliers and when?
  • What can you say about the fit and residuals?

Robust regression

\(RSS=\sum_{i=1}^Ne_i^2\)

Data varies

  • We draw samples from a population and sample size matters. Is it representative?
  • How strong is the signal. How easily is it detected and how much variance does it explain in the data?
  • Something to be said for good quality data sets.

The bias-variance trade-off

  • Suppose we have our estimated beta coefficients. Do you think we would get the same values if we did a fit on another set of observations?
  • What happens to the beta coefficients of predictors which are highly correlated but don’t explain the response \(y\)?

\[y = \beta_0 + \beta_1x_1 + \beta_2x_2 + \beta_3x_3 + \beta_4x_4 + \beta_5x_5 + \epsilon\]

  • Out goal is get good and robust estimates of the coefficients that we can interpret.
  • Shrinkage methods try to overcome these issues by imposing a penalty on the size of the coeficients.
  • variance: the amount by which \(\hat{f}(x)\) would change if we estimate it in a different data set.
  • bias: the error introduced by approximating a real life problem.
  • trade-off: beta estimates will vary less at the expense of being slightly more biased.

Shrinkage methods: ridge

\[ \hat{\beta}=\min\limits_{\beta}\{\sum_{i=1}^N (y_i - \beta_0 - \sum_{j=1}^px_{ij}\beta_j)^2 + \lambda \sum_{j=1}^p\beta_j^2 \} \]

  • What is happening to the total sum in the above equation?
  • Where are we penalizing more harshly?
  • What \(\lambda\) would you choose?

k-fold cross validation

  • For each \(\lambda\), train on k-1 folds and test on kth fold.
  • Compute prediction error and average the k prediction errors.
  • Repeat for each \(\lambda\).

glmnet

## prepare to fit
y <- Credit$Balance
X <- model.matrix(Balance ~ 0 + Income + Limit + Rating + 
                      Cards + Age + Education, 
                  data = Credit)

## fit
fit <- glmnet(x = X, y = y, alpha = 0)
fitCV <- cv.glmnet(x = X, y = y, alpha = 0)

## "best" lambda
fitCV$lambda.min
fitCV$lambda.1se
[1] 39.65627
[1] 63.144
plot(fit)

plot(fitCV)

Shrinkage methods: lasso

\[ \hat{\beta}=\min\limits_{\beta}\{\frac{1}{2}\sum_{i=1}^N (y_i - \beta_0 - \sum_{j=1}^px_{ij}\beta_j)^2 + \lambda \sum_{j=1}^p|\beta_j| \} \]

## fit
fit <- glmnet(x = X, y = y, alpha = 1)
fitCV <- cv.glmnet(x = X, y = y, alpha = 1)

## "best" lambda
fitCV$lambda.min
fitCV$lambda.1se
[1] 0.7784687
[1] 15.28166
coef(fitCV, s = "lambda.1se")
7 x 1 sparse Matrix of class "dgCMatrix"
               lambda.1se
(Intercept) -407.24322564
Income        -5.60086451
Limit          0.07417868
Rating         2.37681123
Cards          .         
Age           -0.25837116
Education      .         
plot(fit)

plot(fitCV)

Variable selection with the lasso

  • When would we want to make use of such a method?
  • What do you think the method does when 2 predictors \(x_1\) and \(x_2\) are highly correlated to each other?

  • Is this problematic?

  • Collinearity within the predictor matrix:

Illustration of the bias-variance trade-off

Shrinkage methods: elastic net

\[ \lambda \sum_{j=1}^p(\alpha|\beta_j| + (1-\alpha)\beta_j^2) \]

  • A compromise between the ridge and lasso.
  • \(\alpha\) can be between 0 and 1.
  • Choice of \(\alpha\) is not so easy and depends on how noisy the data is as well.
fitCV <- cv.glmnet(x = X, y = y, alpha = 0.5)

So far

  • We understand how linear models work.
  • We understand the limitations they can suffer from.
  • Shrinkage methods can be used to estimate more robust coefficients or to select meaningful variables.
  • Collinearity in the predictor matrix is a known challenge.
  • But can we do a little better with reasonably correlated predictors and co-select them?
  • What if we have more predictors than obervations?
  • What if our data set has a very low signal to noise ratio?
  • Enter stability selection.

Stability selection

  • Can be used when p > N.
  • We speak of selection probability values for predictors instead of beta coefficient values.
  • How is this calculated with lasso stability selection. If predictor \(x_1\) has a non-zero beta coefficient 80 times and we subset 100 times to do a regression what is its selection probability? Do you think this is high?

\[ \hat{\beta}=\min\limits_{\beta} (||y-X\beta||_2^2 + \lambda \sum_{j=1}^p\frac{|\beta_j|}{W_j} ) \]

  • Randomized lasso stability selection.
  • Weakness term is between 0 and 1.
  • What do you think the effect of the weakness parameter is?
  • The randomized lasso changes the penalty λ to a randomly chosen value in the range [λ, λ/weakness].

Stability paths

Selecting true predictors

  • Regularization with stability selection is rather conservative in practice.
  • We need to specify 2 of: probability cutoff, upper bound on PFER, number of selected variables q
  • Not slecting a predictor is also a result and telling. Can you think of an example where this could happen?

What about correlated predictors?

  • What did we say about correlated predictors?

Strengths of stability selection

  • p >> n situations (flexibility to for example add many more interaction terms in predictor matrix)
  • Reasonably correlated predictor matrix.
  • Consistent variable selection (robust) - important in noisy data sets.
  • Conservative, no selection is a result.
  • Parameters for error control.
  • Less dependent on the particular \(\lambda\) choice.


  • Overall questions on the linear models and concepts we have explored so far before we move on to some interesting uses and applications?

Loess

# example data set

fit1 <- loess(y ~ x)

fit2 <- loess(y ~ x, span = 0.9)

fit3 <- loess(y ~ x, span = 0.5)

Correcting the trend with limma’s normalizeCyclicLoess()

Experimental design and batch correction


\[y_{observed} = \beta_0 + \beta_{biology}x_{biology} + \beta_{batch}x_{batch} + \epsilon\]



\[y_{corrected} = \beta_0 + \beta_{biology}x_{biology} + \epsilon\]

or

\[y_{corrected} = y_{observed} - \beta_{batch}x_{batch}\]

Gaussian graphical models (GGMs)

  • Model statistical relationships between variables using graphs.
  • Special algebraic and geometric properties.
  • Covariance matrix: covariance between each pair of variables.
  • The goal is to estimate the precision matrix (inverse covariance matrix ) which shows the edges in the graph.

Estimating the (sparse) graph

  • Fit lasso on each variable, using all others as predictors.
  • Lambda controls sparsity.
  • Draw edge with AND/OR rule.
  • Edges in the graph reflect meaningful relationships. A missing edge between \(x_1\) and \(x_2\) means those two variables are conditionally independent given all other variables.
  • Example: gene x cell count matrix. We want to learn the gene regulatory network. If necessary, we need to transform the data to look Gaussian to meet the needed assumptions. glmnet allows you to specify other distribution families that better match the response.

And much more we haven’t covered here

  • Mixed effects models.
  • Multivariate regression.
  • Logistic regression.
  • … to mention a few.

Resources

  • An Introduction to Statistical Learning with applications in R – Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani

  • https://www.statlearning.com


Concluding Remarks

The quiet statisticians have changed our world; not by discovering new facts or technical developments, but by changing the ways that we reason, experiment and form our opinions.

– Ian Hacking



Many of us are drawn to the field of statistics because we are natural-born skeptics. Through our statistical training, we become more skeptical still. Our experience—both personal and professional—has taught us that if something seems too good to be true, then it probably is. We seek the simplest possible answer to every question, and no simpler. After all, statistics is hard. The key is to enjoy the challenge.

– Daniela Witten & Rob Tibshirani

Thank you!

Questions?

2026 • SciLifeLabNBISRaukR