Question 1: Linear models on simulated data

Step 1: Simulate the dataset

Problem: to judge how well a model works, I need to compare its results with the truth, but with real data the truth is unknown. Goal: create a dataset where I decide the true relationship between x and y, so I can later check whether the models recover it.

How:

  • 1.1) Make the random numbers reproducible, so the results are the same every time the code runs
    • set.seed(123): fixes the starting point of the random number generator
  • 1.2) Create the independent variable x
    • 1:20: generates the whole numbers from 1 to 20
  • 1.3) Create y from x with a known equation, y = 20 + 10*x + 5*x^2 + noise, where 20 is the intercept, 10 the slope (linear term) and 5 the curvature (quadratic term)
    • rnorm(20, 0, 200): generates 20 random values from a normal distribution with mean 0 and sd 200; this is the noise that makes the relationship statistical instead of exact
  • 1.4) Look at the shape of the data
    • plot(): draws the points of y against x
set.seed(123)                                   # 1.1
x <- 1:20                                       # 1.2
y <- 20 + 10*x + 5*x^2 + rnorm(20, 0, 200)      # 1.3
plot(x, y)                                      # 1.4

Interpretation. The points show a curved, increasing pattern: y grows faster as x increases. This curvature comes from the quadratic term (5x²) built into the simulation. The points do not fall exactly on a smooth curve because of the random noise, which makes the relationship statistical rather than exact.

What this does not tell me: the plot shows the shape of the data, but not which model describes it best or whether a model can recover the true parameters. Step 2 answers this.

Step 2: Fit the three models

Problem: the data follow a curve with noise, but I do not know which model describes them best. Goal: fit three models of increasing complexity and check which one fits best and whether it recovers the true parameters (20, 10, 5).

How:

  • 2.1) Fit a model with intercept only, which predicts the same value (the mean of y) for every x
    • lm(): fits a linear model; the formula y ~ 1 means “y explained only by a constant”
  • 2.2) Fit a model with intercept and slope, which predicts with a straight line
    • lm(y ~ x): adds x as a predictor
  • 2.3) Fit a model with intercept, slope and curvature, which predicts with a parabola
    • I(x^2): tells R to compute x² literally, because inside a formula ^ means something else
  • 2.4) Look at the results of each model
    • summary(): shows the estimated coefficients (Estimate), their uncertainty (Std. Error), their p-values, the residual standard error and the adjusted R²
model_1 <- lm(y ~ 1)                  # 2.1
model_2 <- lm(y ~ x)                  # 2.2
model_3 <- lm(y ~ x + I(x^2))         # 2.3

summary(model_1)                      # 2.4

Call:
lm(formula = y ~ 1)

Residuals:
   Min     1Q Median     3Q    Max 
-947.9 -532.4 -103.3  373.7 1284.5 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)    870.8      156.6    5.56 2.31e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 700.5 on 19 degrees of freedom
summary(model_2)                      # 2.4

Call:
lm(formula = y ~ x)

Residuals:
   Min     1Q Median     3Q    Max 
-424.4 -127.4  -12.6  151.0  374.4 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) -302.981    110.115  -2.752   0.0131 *  
x            111.791      9.192  12.162 4.07e-10 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 237 on 18 degrees of freedom
Multiple R-squared:  0.8915,    Adjusted R-squared:  0.8855 
F-statistic: 147.9 on 1 and 18 DF,  p-value: 4.068e-10
summary(model_3)                      # 2.4

Call:
lm(formula = y ~ x + I(x^2))

Residuals:
    Min      1Q  Median      3Q     Max 
-379.29 -128.02  -17.08  117.01  344.32 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)  
(Intercept)   20.770    151.083   0.137   0.8923  
x             23.495     33.135   0.709   0.4879  
I(x^2)         4.205      1.533   2.743   0.0139 *
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 203.1 on 17 degrees of freedom
Multiple R-squared:  0.9248,    Adjusted R-squared:  0.9159 
F-statistic: 104.5 on 2 and 17 DF,  p-value: 2.806e-10

Interpretation. Model 3 (y ~ x + I(x^2)) gives the best fit: it has the lowest residual standard error (203.1) and the highest adjusted R² (0.916, it explains 91.6% of the variation in y). Model 1 (y ~ 1) explains nothing (R² = 0) because it always predicts the mean of y and ignores x. Model 2 (y ~ x) fits much better (R² = 0.886), but its error is larger because a straight line cannot capture the curvature. The residual standard error of model 3 (203.1) is very close to the sd of the noise I simulated (200): the model explains everything that can be explained, and what remains is only random noise.

Model 3 also recovers the true parameters: the estimated intercept (20.8) and curvature (4.2) are close to the true values (20 and 5). The estimated slope (23.5) is further from the true value (10), but its standard error is large (33.1), so the plausible range (23.5 ± 2 × 33.1 = −42.7 to 89.7) includes the true value. With only 20 points and a large noise, the slope is estimated with low precision. The quadratic term is significant (p = 0.014), confirming that the curvature is real.

What this does not tell me: these numbers summarize each model as a whole, but they do not show where, at which values of x, each model succeeds or fails. Step 3 answers this.

Step 3: Fitted values with predict()

Problem: the summaries give one global number per model, so I cannot see where each model fails. Goal: compare, point by point, the value each model predicts with the observed value.

How:

  • 3.1) Obtain the value of y that each model predicts for each x (the fitted values)
    • predict(): applies the fitted model to the x values and returns the predicted y
  • 3.2) Put x, the observed y and the fitted values of the three models together, to compare them side by side
    • data.frame(): builds a table from several vectors of the same length
  • 3.3) Show the first rows of the table
    • head(): prints the first 6 rows
fitted_1 <- predict(model_1)                                                      # 3.1
fitted_2 <- predict(model_2)                                                      # 3.1
fitted_3 <- predict(model_3)                                                      # 3.1
fit_table <- data.frame(x, observed = y, fitted_1, fitted_2, fitted_3)            # 3.2
head(fit_table)                                                                   # 3.3
  x  observed fitted_1   fitted_2  fitted_3
1 1 -77.09513 870.8248 -191.19043  48.47006
2 2  13.96450 870.8248  -79.39935  84.57888
3 3 406.74166 870.8248   32.39172 129.09683
4 4 154.10168 870.8248  144.18279 182.02392
5 5 220.85755 870.8248  255.97386 243.36015
6 6 603.01300 870.8248  367.76494 313.10553

Interpretation. Model 1 predicts the same value for every x (870.8, the mean of y), so it ignores the relationship between x and y. Model 2 follows the general increasing trend, but at low x values it predicts negative values (for example −191.2 for x = 1, while the observed value is −77.1), because a straight line cannot bend to follow the curvature. Model 3 gives fitted values closer to the observed ones across the range of x. None of the models matches the observed values exactly, because of the random noise.

What this does not tell me: comparing two columns by eye does not measure how much each model misses at each point, or in which direction. Step 4 answers this.

Step 4: Residuals with resid()

Problem: the fitted values never match the observed ones exactly, but comparing two columns by eye is not precise. Goal: measure the error of each model at each point, the residual (observed − fitted), which is what the model fails to explain.

How:

  • 4.1) Calculate the residuals of each model
    • resid(): returns observed − fitted for each point; a positive residual means the point is above the model’s prediction, a negative one means it is below
  • 4.2) Build a table with x, the observed y, the fitted values and the residuals of the three models
    • data.frame(): builds the table from scratch, so the columns are not duplicated if the chunk runs twice
  • 4.3) Show the first rows of the table
    • head(): prints the first 6 rows
resid_1 <- resid(model_1)                                                                                     # 4.1
resid_2 <- resid(model_2)                                                                                     # 4.1
resid_3 <- resid(model_3)                                                                                     # 4.1
fit_table <- data.frame(x, observed = y, fitted_1, fitted_2, fitted_3, resid_1, resid_2, resid_3)             # 4.2

head(fit_table)                       # 4.3
  x  observed fitted_1   fitted_2  fitted_3   resid_1    resid_2    resid_3
1 1 -77.09513 870.8248 -191.19043  48.47006 -947.9199 114.095296 -125.56519
2 2  13.96450 870.8248  -79.39935  84.57888 -856.8603  93.363856  -70.61437
3 3 406.74166 870.8248   32.39172 129.09683 -464.0831 374.349944  277.64483
4 4 154.10168 870.8248  144.18279 182.02392 -716.7231   9.918887  -27.92224
5 5 220.85755 870.8248  255.97386 243.36015 -649.9672 -35.116316  -22.50261
6 6 603.01300 870.8248  367.76494 313.10553 -267.8118 235.248062  289.90747

Interpretation. Model 1 has by far the largest residuals (for example −947.9 at x = 1), and in the first rows they are all negative: the flat line (the mean of y) lies above every low-x point. A systematic sign pattern like this means the model is missing part of the relationship. The residuals of models 2 and 3 are much smaller and similar in size in these first rows.

What this does not tell me: six rows are not enough to tell models 2 and 3 apart, or to see whether the residuals follow a pattern across all values of x. Step 5 answers this.

Step 5: Plot fitted values and residuals

Problem: the tables only show a few points, and models 2 and 3 look similar there. Goal: plot all 20 points to see which model follows the data best, and whether the residuals show a pattern or are just random noise.

How:

  • 5.1) Draw the observed data and, on top, the line of fitted values of each model
    • plot(): draws the observed points; pch = 19 makes them solid circles
    • lines(): draws a line through the fitted values; col sets the color and lwd the line width
    • legend(): adds a box that identifies each model by its color
  • 5.2) Draw the residuals of each model against x, in three panels side by side
    • par(mfrow = c(1, 3)): splits the plot window into 1 row and 3 columns
    • abline(h = 0, lty = 2): draws a dashed horizontal line at 0; in a good model the residuals are scattered randomly around it, with no pattern
    • par(mfrow = c(1, 1)): resets the window to one plot
plot(x, y, pch = 19, col = "grey50",
     main = "Observed data and fitted models")                    # 5.1
lines(x, fitted_1, col = "blue", lwd = 2)                         # 5.1
lines(x, fitted_2, col = "red", lwd = 2)                          # 5.1
lines(x, fitted_3, col = "darkgreen", lwd = 2)                    # 5.1
legend("topleft", legend = c("y ~ 1", "y ~ x", "y ~ x + I(x^2)"),
       col = c("blue", "red", "darkgreen"), lwd = 2)              # 5.1

par(mfrow = c(1, 3))                                              # 5.2
plot(x, resid_1, pch = 19, main = "Residuals y ~ 1")              # 5.2
abline(h = 0, lty = 2)                                            # 5.2
plot(x, resid_2, pch = 19, main = "Residuals y ~ x")              # 5.2
abline(h = 0, lty = 2)                                            # 5.2
plot(x, resid_3, pch = 19, main = "Residuals y ~ x + I(x^2)")     # 5.2
abline(h = 0, lty = 2)                                            # 5.2

par(mfrow = c(1, 1))                                              # 5.2

Interpretation. In the first plot, model 1 (flat line) does not follow the data at all. Model 2 (straight line) follows the general trend, but it lies below the points at the beginning and at the end, and above them in the middle, because it cannot bend with the curvature. Model 3 (parabola) follows the curve along the whole range of x.

The residual plots confirm this. The residuals cannot be close to zero, because the noise I simulated has sd = 200, so even the correct model misses by about ±200. What matters is whether they show a pattern. Model 1 shows a clear increasing trend: it ignores x completely. Model 2 shows a U-shaped pattern (mostly positive at low and high x, mostly negative in the middle), the signature of the curvature it misses. Model 3 shows residuals scattered randomly around zero with no visible pattern, which means it captures the systematic part of the relationship and only random noise remains.

What this does not tell me: all these results come from one noise level (sd = 200), so I do not know how much of the remaining error, and of the precision of the estimates, depends on the noise. Step 6 answers this.

Step 6: Change the noise

Problem: all the results so far depend on one noise level (sd = 200), so I do not know how much of what I saw is caused by the noise. Goal: repeat the simulation with a smaller noise (sd = 20) and a larger noise (sd = 1000), keeping everything else the same, and see how the fit of model 3 and its ability to recover the true parameters change.

How:

  • 6.1) Simulate y again with small noise (sd = 20) and with large noise (sd = 1000)
    • set.seed(123) before each rnorm(): gives the same random pattern, only scaled to a different size, so the only difference between datasets is the amount of noise
  • 6.2) Fit model 3 to each new dataset
    • lm(y_low ~ x + I(x^2)) and lm(y_high ~ x + I(x^2)): the same correct model as before
  • 6.3) Compare the estimated coefficients, standard errors, residual standard error and R²
    • summary(): shows these values for each model
  • 6.4) Plot the three datasets (sd = 20, 200, 1000) side by side, each with its fitted curve
    • predict(): gives the fitted values of each new model, drawn with lines()
set.seed(123)                                                     # 6.1
y_low  <- 20 + 10*x + 5*x^2 + rnorm(20, 0, 20)                    # 6.1
set.seed(123)                                                     # 6.1
y_high <- 20 + 10*x + 5*x^2 + rnorm(20, 0, 1000)                  # 6.1

model3_low  <- lm(y_low  ~ x + I(x^2))                            # 6.2
model3_high <- lm(y_high ~ x + I(x^2))                            # 6.2

summary(model3_low)                                               # 6.3

Call:
lm(formula = y_low ~ x + I(x^2))

Residuals:
    Min      1Q  Median      3Q     Max 
-37.929 -12.802  -1.708  11.701  34.432 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  20.0770    15.1083   1.329  0.20145    
x            11.3495     3.3135   3.425  0.00323 ** 
I(x^2)        4.9205     0.1533  32.104  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 20.31 on 17 degrees of freedom
Multiple R-squared:  0.9992,    Adjusted R-squared:  0.9991 
F-statistic: 1.112e+04 on 2 and 17 DF,  p-value: < 2.2e-16
summary(model3_high)                                              # 6.3

Call:
lm(formula = y_high ~ x + I(x^2))

Residuals:
     Min       1Q   Median       3Q      Max 
-1896.43  -640.11   -85.41   585.04  1721.60 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)   23.852    755.417   0.032    0.975
x             77.476    165.675   0.468    0.646
I(x^2)         1.023      7.663   0.133    0.895

Residual standard error: 1015 on 17 degrees of freedom
Multiple R-squared:  0.2714,    Adjusted R-squared:  0.1857 
F-statistic: 3.167 on 2 and 17 DF,  p-value: 0.06775
par(mfrow = c(1, 3))                                              # 6.4
plot(x, y_low, pch = 19, col = "grey50", main = "Noise sd = 20")  # 6.4
lines(x, predict(model3_low), col = "darkgreen", lwd = 2)         # 6.4
plot(x, y, pch = 19, col = "grey50", main = "Noise sd = 200")     # 6.4
lines(x, fitted_3, col = "darkgreen", lwd = 2)                    # 6.4
plot(x, y_high, pch = 19, col = "grey50",
     main = "Noise sd = 1000")                                    # 6.4
lines(x, predict(model3_high), col = "darkgreen", lwd = 2)        # 6.4

par(mfrow = c(1, 1))                                              # 6.4

Interpretation. The amount of noise controls how well the model can fit the data and recover the truth. In all three cases the residual standard error of model 3 is close to the sd of the noise (20.3, 203.1 and about 1015), which confirms that the remaining error is only the random noise I built in. With small noise (sd = 20) the points follow an almost perfect parabola, R² is 0.999, and the estimates are very close to the true values (20.1, 11.3 and 4.92 vs 20, 10 and 5), with small standard errors. With the original noise (sd = 200) the estimates are less precise but the true values are still inside their plausible ranges. With large noise (sd = 1000) the curvature is no longer visible in the plot, the estimated quadratic term drops to about 1 and is no longer significant: even though model 3 is the correct model, 20 noisy points do not contain enough information to detect the curvature.

What this does not tell me: here I knew the true relationship because I built it. With real biological data the truth is unknown, the relationship may not be a simple curve, and the values may need normalization. Questions 2 to 5 apply the same tools to a real gene expression dataset.

Conclusion

  • Model 3 (y ~ x + I(x^2)) is the best model: it has the lowest residual standard error (203.1) and the highest adjusted R² (0.916), and its residuals show no pattern.
  • Model 1 (y ~ 1) explains nothing: it always predicts the mean of y (R² = 0), and its residuals show a clear increasing trend.
  • Model 2 (y ~ x) captures the trend but misses the curvature: its residuals show a U-shaped pattern, the signature of the quadratic term it leaves out.
  • Model 3 recovers the true parameters (20, 10, 5): all true values fall inside the plausible range of the estimates (Estimate ± 2 × Std. Error), although the slope is estimated with low precision.
  • The residual standard error of the correct model matches the noise: about 20, 200 and 1000 for noise sd = 20, 200 and 1000. A model cannot explain random noise, so this is the smallest error possible.
  • The amount of noise controls what we can learn from the data: with little noise the parameters are recovered almost exactly; with a lot of noise (sd = 1000) the curvature is no longer detected, even though the model is correct.
  • A linear model can describe a curve: y ~ x + I(x^2) is still a linear model, because it is linear in its coefficients, even if it is not linear in x.

Question 2: Gene-by-gene linear models on real data

Step 1: Explore the sample metadata

Problem: to fit a linear model for each gene, I need a continuous variable measured in the same samples (for example age or BMI), but I do not know yet which variables this dataset contains. Goal: download the sample information of GSE226151 and find a continuous variable to use as the dependent variable.

How:

  • 1.1) Load the package that connects R with GEO
    • library(GEOquery): loads the functions to download data from GEO
  • 1.2) Download the study information from GEO
    • getGEO("GSE226151", GSEMatrix = TRUE): downloads the study; it returns a list, with one element per platform
  • 1.3) Extract the sample metadata (one row per sample, one column per characteristic)
    • gse[[1]]: takes the first element of the list
    • pData(): extracts the sample table
  • 1.4) List the names of the columns, to look for a continuous variable
    • colnames(): shows the column names; the sample characteristics usually end in :ch1
  • 1.5) Look at the values of the most relevant columns
    • meta[, c(...)]: selects only the columns listed inside c()
    • head(): shows the first 6 rows
  • 1.6) Look at all the samples: how many there are, which groups they belong to, and the range of age and BMI
    • nrow(): counts the number of samples (rows)
    • table(): counts how many samples there are in each disease state
    • as.numeric(): converts the text values into numbers
    • summary(): shows the minimum, quartiles, mean and maximum
library(GEOquery)                                                                     # 1.1
gse  <- getGEO("GSE226151", GSEMatrix = TRUE)                                         # 1.2
meta <- pData(gse[[1]])                                                               # 1.3
colnames(meta)                                                                        # 1.4
 [1] "title"                   "geo_accession"          
 [3] "status"                  "submission_date"        
 [5] "last_update_date"        "type"                   
 [7] "channel_count"           "source_name_ch1"        
 [9] "organism_ch1"            "characteristics_ch1"    
[11] "characteristics_ch1.1"   "characteristics_ch1.2"  
[13] "characteristics_ch1.3"   "characteristics_ch1.4"  
[15] "growth_protocol_ch1"     "molecule_ch1"           
[17] "extract_protocol_ch1"    "extract_protocol_ch1.1" 
[19] "taxid_ch1"               "data_processing"        
[21] "data_processing.1"       "data_processing.2"      
[23] "data_processing.3"       "data_processing.4"      
[25] "data_processing.5"       "platform_id"            
[27] "contact_name"            "contact_email"          
[29] "contact_phone"           "contact_laboratory"     
[31] "contact_department"      "contact_institute"      
[33] "contact_address"         "contact_city"           
[35] "contact_zip/postal_code" "contact_country"        
[37] "data_row_count"          "instrument_model"       
[39] "library_selection"       "library_source"         
[41] "library_strategy"        "relation"               
[43] "relation.1"              "supplementary_file_1"   
[45] "age:ch1"                 "bmi:ch1"                
[47] "disease state:ch1"       "Sex:ch1"                
[49] "tissue:ch1"             
head(meta[, c("age:ch1", "bmi:ch1", "disease state:ch1", "Sex:ch1", "tissue:ch1")])   # 1.5
           age:ch1 bmi:ch1 disease state:ch1 Sex:ch1      tissue:ch1
GSM7065818      56   23.33      healthy aged  female Skeletal muscle
GSM7065819      65   24.97      healthy aged  female Skeletal muscle
GSM7065820      67   26.27      healthy aged  female Skeletal muscle
GSM7065821      65   22.86      healthy aged  female Skeletal muscle
GSM7065822      57   23.52      healthy aged  female Skeletal muscle
GSM7065823      72   28.89      healthy aged  female Skeletal muscle
nrow(meta)                                                                            # 1.6
[1] 60
table(meta[, "disease state:ch1"])                                                    # 1.6

  healthy aged pre-sarcopenia     sarcopenia 
            20             20             20 
summary(as.numeric(meta[, "age:ch1"]))                                                # 1.6
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  53.00   65.00   69.50   69.13   74.00   88.00 
summary(as.numeric(meta[, "bmi:ch1"]))                                                # 1.6
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  15.31   21.51   23.52   23.88   26.32   33.22 

Interpretation.

  • Samples: GSE226151 contains 60 skeletal muscle samples from older adults.
  • Groups: three groups of 20 samples each: healthy aged, pre-sarcopenia and sarcopenia.
  • Continuous variables available: age (53 to 88 years, mean 69.1) and BMI (15.3 to 33.2, mean 23.9), with no missing values.
  • Chosen variable: age. Sarcopenia is the age-related loss of muscle mass and function, so genes whose muscle expression is associated with age are biologically meaningful.
  • Data type: age and BMI are stored as text (<chr>), so they must be converted to numbers with as.numeric() before modeling.

What this does not tell me: I know which variable to use, but I still need the gene expression values for these 60 samples, matched to the same samples as the age values. Step 2 answers this.

Step 2: Download the gene expression data

Problem: I chose age as the continuous variable, but I still need the expression of every gene in the same 60 samples. Goal: download the gene expression table of GSE226151 and check that its samples match the samples of the metadata.

How:

  • 2.1) Load the package to read large tables quickly
    • library(data.table): loads fread(), a fast function to read tables, also from the internet
  • 2.2) Download the raw count table that NCBI generates for this dataset (the same file that GEODataDownload() downloads internally)
    • url: the web address of the file; it contains the GSE number of the dataset
    • fread(url): downloads and reads the table
    • as.matrix(..., rownames = "GeneID"): converts it into a matrix and uses the GeneID column as row names; each row is a gene, each column is a sample, each value is the number of reads (counts) of that gene in that sample
  • 2.3) Check the size of the table
    • dim(): returns the number of rows (genes) and columns (samples)
  • 2.4) Look at a small corner of the table
    • counts[1:5, 1:5]: shows the first 5 genes and the first 5 samples
  • 2.5) Check that the samples are in the same order in the count table and in the metadata
    • colnames(counts): sample names of the count table
    • rownames(meta): sample names of the metadata
    • all(... == ...): returns TRUE only if every name matches in the same position
  • 2.6) Keep only the samples present in both tables, in the same order
    • setdiff(A, B): returns the names that are in A but not in B (the samples without counts)
    • intersect(A, B): returns the names that are in both A and B
    • counts[, common]: keeps only those columns (samples) of the count table
    • meta[common, ]: keeps only those rows (samples) of the metadata, in the same order
    • all(... == ...): checks again that the names match one by one
library(data.table)                                                                                     # 2.1

url <- paste0("https://www.ncbi.nlm.nih.gov/geo/download/?format=file&type=rnaseq_counts",
              "&acc=GSE226151&file=GSE226151_raw_counts_GRCh38.p13_NCBI.tsv.gz")                        # 2.2
counts <- as.matrix(fread(url, header = TRUE), rownames = "GeneID")                                     # 2.2

dim(counts)                                                                                             # 2.3
[1] 39376    58
counts[1:5, 1:5]                                                                                        # 2.4
          GSM7065818 GSM7065819 GSM7065820 GSM7065821 GSM7065822
100287102         11          2          0          2          2
653635          1065        452        293        415        224
102466751         18          2         16         12          7
107985730          0          0          0          2          2
100302278          0          0          0          1          0
all(colnames(counts) == rownames(meta))                                                                 # 2.5
Warning in colnames(counts) == rownames(meta): longer object length is not a
multiple of shorter object length
[1] FALSE
setdiff(rownames(meta), colnames(counts))                                                               # 2.6
[1] "GSM7065856" "GSM7065870"
common <- intersect(colnames(counts), rownames(meta))                                                   # 2.6
counts <- counts[, common]                                                                              # 2.6
meta   <- meta[common, ]                                                                                # 2.6
dim(counts)                                                                                             # 2.6
[1] 39376    58
all(colnames(counts) == rownames(meta))                                                                 # 2.6
[1] TRUE

Interpretation.

  • Expression table: the raw count table contains 39,376 genes (rows) and 58 samples (columns); each value is the number of sequencing reads of a gene in a sample.
  • Missing samples: two samples of the metadata (GSM7065856 and GSM7065870) have no counts in the NCBI table, so the first check returned FALSE: the two tables did not have the same number of samples.
  • Matching: after keeping only the samples present in both tables, the count table and the metadata contain the same 58 samples in the same order (TRUE). This is essential: each age value must correspond to the expression values of the same person.
  • Raw values: these are raw counts, not normalized.

What this does not tell me: many of the 39,376 genes may have very few or zero reads, and age is still stored as text. Before fitting thousands of models, I need to prepare both. Step 3 answers this.

Step 3: Prepare the data

Problem: age is stored as text, and many of the 39,376 genes have almost no reads, so a model for them would carry no information. Goal: convert age into numbers and keep only the genes with enough reads to be analyzed.

How:

  • 3.1) Convert age from text to numbers
    • as.numeric(): converts text values like “56” into the number 56
    • summary(): checks the range and that there are no missing values
  • 3.2) Find the genes with enough reads: at least 10 reads in at least 20 samples (the same rule used inside GEODataDownload())
    • counts >= 10: returns a TRUE/FALSE table, TRUE where a gene has 10 or more reads in a sample
    • rowSums(): counts, for each gene, how many samples are TRUE
    • >= 20: TRUE for the genes that reach the minimum in 20 or more samples
    • sum(keep): counts how many genes pass the filter
  • 3.3) Keep only those genes
    • counts[keep, ]: keeps the rows (genes) where keep is TRUE
    • dim(): checks the new size of the table
age <- as.numeric(meta[, "age:ch1"])                # 3.1
summary(age)                                        # 3.1
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  53.00   65.00   69.00   68.78   73.00   88.00 
keep <- rowSums(counts >= 10) >= 20                 # 3.2
sum(keep)                                           # 3.2
[1] 16738
counts_f <- counts[keep, ]                          # 3.3
dim(counts_f)                                       # 3.3
[1] 16738    58

Interpretation.

  • Age: after conversion to numbers, age ranges from 53 to 88 years (median 69, mean 68.8) across the 58 samples, with no missing values, so it can be used as the dependent variable in lm().
  • Gene filter: 16,738 of the 39,376 genes (42.5%) have at least 10 reads in at least 20 samples. The other 22,638 genes were removed because they have too few reads to carry reliable information.
  • Why filter: genes with almost no reads add thousands of uninformative tests, which increases the computing time and the number of false positives without adding biological information.

What this does not tell me: the data are ready, but I still do not know which genes are associated with age. Step 4 fits one linear model per gene to answer this.

Step 4: Fit one linear model per gene

Problem: I have 16,738 genes and I do not know which ones are associated with age. Goal: fit a linear model for every gene, with age as the dependent variable and the gene’s expression as the independent variable, and keep the p-value of each model to measure how strong the association is.

How:

  • 4.1) Choose how many genes to analyze: first a test with 100 genes (to check that the code works, as suggested in the hint), then all of them
    • n_genes: the number of genes the loop will go through
  • 4.2) Prepare an empty vector to store one p-value per gene
    • numeric(n_genes): creates a vector of zeros with one position per gene
    • names(): labels each position with the gene ID, so I know which p-value belongs to which gene
  • 4.3) Repeat for each gene: fit the model and store its p-value
    • for (i in 1:n_genes) { }: repeats the code inside the braces once for each gene; i is the number of the current gene
    • counts_f[i, ]: the expression of gene i in the 58 samples (row i of the table)
    • lm(age ~ expression): fits the linear model, age explained by the expression of the gene
    • anova(fit)[1, "Pr(>F)"]: takes the p-value of the gene effect from the ANOVA table (row 1, column “Pr(>F)”)
  • 4.4) Look at the first p-values and count how many are below 0.05
    • head(): shows the first 6 values
    • sum(p_values < 0.05): counts how many genes are significant at the 0.05 level
n_genes <- nrow(counts_f)                               # 4.1

p_values <- numeric(n_genes)                            # 4.2
names(p_values) <- rownames(counts_f)[1:n_genes]        # 4.2

for (i in 1:n_genes) {                                  # 4.3
  expression  <- counts_f[i, ]                          # 4.3
  fit         <- lm(age ~ expression)                   # 4.3
  p_values[i] <- anova(fit)[1, "Pr(>F)"]                # 4.3
}

head(p_values)                                          # 4.4
   653635 102466751 100996442    729737 102723897 112268260 
0.5597498 0.8428116 0.7975149 0.4966472 0.8483896 0.7831087 
sum(p_values < 0.05)                                    # 4.4
[1] 691

Interpretation.

  • Test run: with the first 100 genes the loop worked correctly and only 1 gene had p < 0.05.
  • All genes: 691 of the 16,738 genes (4.1%) have p < 0.05 for the association between their expression and age.
  • Comparison with chance: if no gene were associated with age, about 5% of the tests (≈ 837 genes) would be below 0.05 just by chance. Finding fewer than that means the raw data do not show a global age signal stronger than random noise, and many of these 691 genes are probably false positives.
  • Possible reason: raw counts also depend on the sequencing depth of each sample (total number of reads), which is unrelated to age and adds noise. This will be tested in Question 4 after normalization.

What this does not tell me: a single count of p-values below 0.05 does not show how the p-values are distributed across all genes. Step 5 plots this distribution.

Step 5: Distribution of the p-values

Problem: the number of genes with p < 0.05 does not show the whole picture across 16,738 tests. Goal: plot the distribution of all the p-values and compare it with what is expected if no gene were associated with age (a flat distribution).

How:

  • 5.1) Draw a histogram of the p-values
    • hist(): counts how many p-values fall in each interval and draws one bar per interval
    • breaks = 20: splits the range 0–1 into 20 intervals of width 0.05
  • 5.2) Draw the height expected if all genes were null (no association with age)
    • length(p_values) / 20: the number of genes divided by the number of bars
    • abline(h = ...): draws a horizontal line at that height; col = "red" and lty = 2 make it a red dashed line
hist(p_values, breaks = 20, col = "grey80",
     main = "Distribution of p-values (age ~ expression)",
     xlab = "p-value")                                          # 5.1
abline(h = length(p_values) / 20, col = "red", lty = 2)         # 5.2

Interpretation.

  • Flat region: between 0.4 and 1 the bars are close to the red line, the height expected if no gene were associated with age.
  • No peak near zero: the first bar (p < 0.05) is the lowest of all (about 690 genes, below the expected 837). A real age effect would produce a peak right next to zero, so there is no clear evidence of genes associated with age in the raw data.
  • Bump between 0.05 and 0.35: there are more moderate p-values than expected and fewer very small ones. This shape is not a biological signal; it suggests the p-values are not well calibrated because the model does not fit the raw counts well.
  • Likely cause: raw counts are very skewed, with extreme values and large differences in sequencing depth between samples. These violate the assumptions of the linear model (for example, normally distributed residuals), so its p-values cannot be taken at face value. Normalization (Question 4) and the residual check (Question 5) will test this.

What this does not tell me: the histogram describes all genes together, but not what the models of individual genes look like. Step 6 looks at the two extreme cases: the gene with the most and the gene with the least significant effect.

Step 6: The most and the least significant genes

Problem: the histogram shows all genes together, but not what an individual model looks like. Goal: study in detail the two extreme cases, the gene with the most significant effect on age (smallest p-value) and the gene with the least significant effect (largest p-value), with their model summaries, ANOVA tables and diagnostic plots.

How:

  • 6.1) Find the two extreme genes
    • which.min() / which.max(): return the position of the smallest / largest p-value
    • names(): returns the gene ID at that position
  • 6.2) Find the gene names (symbols), to interpret them biologically
    • fread(): downloads the NCBI annotation table, which links each GeneID with its symbol and description
    • annot[GeneID %in% ..., ]: keeps only the rows of the two genes
  • 6.3) Fit the model of each gene again
    • counts_f[gene, ]: the expression of that gene in the 58 samples
    • lm(age ~ expression): the same model used in the loop
  • 6.4) Show the model summaries and the ANOVA tables
    • summary(): coefficients, p-values and R²
    • anova(): the ANOVA table with the F test of the gene effect
  • 6.5) Plot age against the expression of each gene, with the fitted line
    • plot(): draws the points; abline(fit): draws the fitted straight line
  • 6.6) Draw the diagnostic plots of each model
    • par(mfrow = c(2, 2)): splits the window into 2 rows and 2 columns
    • plot(fit): draws the 4 standard diagnostic plots of a linear model
best_gene  <- names(which.min(p_values))                                  # 6.1
worst_gene <- names(which.max(p_values))                                  # 6.1
p_values[c(best_gene, worst_gene)]                                        # 6.1
        7008       131474 
0.0007595959 0.9999518246 
library(org.Hs.eg.db)                                                     # 6.2
AnnotationDbi::select(org.Hs.eg.db, keys = c(best_gene, worst_gene),
                      columns = c("SYMBOL", "GENENAME"),
                      keytype = "ENTREZID")                               # 6.2
  ENTREZID SYMBOL                                                GENENAME
1     7008    TEF        TEF transcription factor, PAR bZIP family member
2   131474 CHCHD4 coiled-coil-helix-coiled-coil-helix domain containing 4
best_expr  <- counts_f[best_gene, ]                                       # 6.3
worst_expr <- counts_f[worst_gene, ]                                      # 6.3
fit_best   <- lm(age ~ best_expr)                                         # 6.3
fit_worst  <- lm(age ~ worst_expr)                                        # 6.3

summary(fit_best)                                                         # 6.4

Call:
lm(formula = age ~ best_expr)

Residuals:
     Min       1Q   Median       3Q      Max 
-15.2374  -5.0998   0.8196   4.8223  16.5361 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 76.293443   2.310805  33.016  < 2e-16 ***
best_expr   -0.010146   0.002848  -3.562  0.00076 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 7.169 on 56 degrees of freedom
Multiple R-squared:  0.1847,    Adjusted R-squared:  0.1702 
F-statistic: 12.69 on 1 and 56 DF,  p-value: 0.0007596
anova(fit_best)                                                           # 6.4
Analysis of Variance Table

Response: age
          Df  Sum Sq Mean Sq F value    Pr(>F)    
best_expr  1  652.12  652.12  12.689 0.0007596 ***
Residuals 56 2877.97   51.39                      
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(fit_worst)                                                        # 6.4

Call:
lm(formula = age ~ worst_expr)

Residuals:
     Min       1Q   Median       3Q      Max 
-15.7760  -3.7759   0.2241   4.2241  19.2242 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 6.878e+01  4.769e+00   14.42   <2e-16 ***
worst_expr  1.121e-06  1.848e-02    0.00        1    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 7.94 on 56 degrees of freedom
Multiple R-squared:  6.568e-11, Adjusted R-squared:  -0.01786 
F-statistic: 3.678e-09 on 1 and 56 DF,  p-value: 1
anova(fit_worst)                                                          # 6.4
Analysis of Variance Table

Response: age
           Df Sum Sq Mean Sq F value Pr(>F)
worst_expr  1    0.0   0.000       0      1
Residuals  56 3530.1  63.037               
par(mfrow = c(1, 2))                                                      # 6.5
plot(best_expr, age, pch = 19, xlab = "Expression (raw counts)",
     main = "Most significant gene")                                      # 6.5
abline(fit_best, col = "red", lwd = 2)                                    # 6.5
plot(worst_expr, age, pch = 19, xlab = "Expression (raw counts)",
     main = "Least significant gene")                                     # 6.5
abline(fit_worst, col = "red", lwd = 2)                                   # 6.5

par(mfrow = c(2, 2))                                                      # 6.6
plot(fit_best)                                                            # 6.6

plot(fit_worst)                                                           # 6.6

par(mfrow = c(1, 1))                                                      # 6.6

Interpretation.

  • The two extreme genes: the most significant gene is TEF (TEF transcription factor, PAR bZIP family member; p = 0.00076) and the least significant is CHCHD4 (coiled-coil-helix-coiled-coil-helix domain containing 4; p close to 1).
  • Each point is one sample: in the scatter plots, each of the 58 points is one person, with their age and the expression of the gene in raw counts (number of reads).
  • TEF: the points show a negative trend: people with higher TEF expression tend to be younger. The slope is −0.0101, about 1 year less for every 100 additional reads. The association is weak: the points are widely scattered around the line, TEF explains only 18.5% of the variation in age (R² = 0.185), and the model misses by about ±7 years (residual standard error = 7.2).
  • CHCHD4: the expression is similar at all ages and the fitted line is flat; the gene gives no information about age.
  • Association is not causation: the model is age ~ expression, so it describes age, not sarcopenia, and it does not show that TEF protects against aging. It only shows that older people in this dataset tend to express less TEF.
  • Diagnostic plots (TEF): the residuals are spread around zero without a clear pattern, the Q-Q plot follows the line (residuals are approximately normal), and no point lies beyond the Cook’s distance lines, so no single person drives the model.
  • Multiple testing: with 16,738 tests, about 13 genes (16,738 × 0.00076) would reach a p-value this small by chance alone, even if no gene were related to age. So TEF could be a false positive, which agrees with the histogram of Step 5 (no peak near zero).
  • Possible confounder: TEF is part of the circadian clock, so its expression changes with the time of day. If older and younger participants had their biopsies at different times, the association between TEF and age could come from the time of sampling instead of age. The metadata do not include this information, so it cannot be ruled out.

Conclusion

  • What I did: using muscle samples from 58 older adults in a sarcopenia study (GSE226151), I tested all 16,738 expressed genes to find which ones are associated with age, fitting one linear model per gene.
  • Main result: the most significant gene was TEF, whose expression decreases as age increases, while CHCHD4 showed no relationship with age at all.
  • The evidence is weak: TEF explains only 18.5% of the variation in age, and the distribution of p-values shows no more significant genes than expected by chance (no peak near zero). With so many tests, a gene as significant as TEF could appear by chance alone, so TEF may be a false positive.
  • Raw data limit the conclusions: I worked with raw counts, which differ between samples for technical reasons (sequencing depth) and are very skewed. Normalizing the data (Question 4) is needed before drawing more reliable conclusions.
  • Possible confounder: TEF belongs to the circadian clock, so the time of day of each biopsy could affect its expression and create or hide an association with age.

Question 3: Linear vs non-linear fits for TEF

Step 1: Fit LOESS and GAM and plot them with the linear fit

Problem: in Question 2 I assumed a straight-line relationship between TEF expression and age, but the true relationship could be curved. Goal: fit two flexible, non-linear models (LOESS and GAM) to the same data and plot them together with the linear fit, to see whether they reveal a shape the straight line missed.

How:

  • 1.1) Fit a LOESS curve: many small local regressions joined into a smooth curve
    • loess(age ~ best_expr, span = 0.75): each local fit uses the 75% of points closest to it
  • 1.2) Fit a GAM: a smooth curve whose flexibility is chosen from the data
    • library(mgcv): loads the package for GAMs (it comes with R)
    • gam(age ~ s(best_expr), method = "REML"): s() asks for a smooth function of expression; REML chooses how much it can bend
    • summary(): shows the edf of the smooth term (edf ≈ 1 means a straight line; higher values mean curvature)
  • 1.3) Plot the data with the three fits on the same graph
    • order(best_expr): sorts the samples by expression, so the curves are drawn from left to right
    • abline(fit_best): draws the linear fit from Question 2
    • lines(): draws the LOESS and GAM fitted values
    • legend(): identifies each fit by its color
fit_loess <- loess(age ~ best_expr, span = 0.75)                                   # 1.1

library(mgcv)                                                                      # 1.2
fit_gam <- gam(age ~ s(best_expr), method = "REML")                                # 1.2
summary(fit_gam)                                                                   # 1.2

Family: gaussian 
Link function: identity 

Formula:
age ~ s(best_expr)

Parametric coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  68.7759     0.9413   73.06   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Approximate significance of smooth terms:
             edf Ref.df     F p-value    
s(best_expr)   1      1 12.69 0.00076 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

R-sq.(adj) =   0.17   Deviance explained = 18.5%
-REML = 193.83  Scale est. = 51.392    n = 58
ord <- order(best_expr)                                                            # 1.3
plot(best_expr, age, pch = 19, col = "grey50",
     xlab = "TEF expression (raw counts)", ylab = "Age",
     main = "TEF: linear, LOESS and GAM fits")                                     # 1.3
abline(fit_best, col = "red", lwd = 2, lty = 2)                                    # 1.3
lines(best_expr[ord], fitted(fit_loess)[ord], col = "orange", lwd = 2)             # 1.3
lines(best_expr[ord], fitted(fit_gam)[ord], col = "steelblue", lwd = 2)            # 1.3
legend("topright", legend = c("Linear", "LOESS", "GAM"),
       col = c("red", "orange", "steelblue"), lwd = 2, lty = c(2, 1, 1))           # 1.3

Interpretation.

  • GAM = straight line: the GAM curve lies exactly on top of the linear fit. Its edf is 1, which means that, even though it was free to bend, the GAM chose a straight line.
  • Same result as the linear model: the GAM gives the same p-value (0.00076) and explains the same proportion of the variation in age (deviance explained = 18.5%, equal to the R² of the linear model).
  • LOESS bends where there are few points: at low expression (about 270–550 reads) there are only a few people, some aged 63–65 and some aged 85–88. Because LOESS fits local lines with the nearest points, each of these people has a large influence there, so the curve follows individual points. This bend is most likely noise, not a real biological pattern.

What this does not tell me: the plot suggests the three fits describe the same trend, but I need a number that balances fit and complexity to decide which model is best. Step 2 answers this with AIC.

Step 2: Compare the three models with AIC

Problem: the plot suggests that the three fits are similar, but I need a number to decide which model is best. Goal: compare the linear model, LOESS and GAM with AIC, which rewards a good fit but penalizes complexity (lower AIC = better; a difference larger than 2 is usually considered meaningful).

How:

  • 2.1) Calculate the residual sum of squares (RSS) of each model: how much it misses in total
    • resid(): the residuals of each model
    • sum(... ^ 2): squares them and adds them up
  • 2.2) Get the complexity of each model (its number of parameters, or the equivalent)
    • linear model: 2 parameters (intercept and slope)
    • fit_loess$enp: the equivalent number of parameters of LOESS
    • sum(fit_gam$edf): the effective degrees of freedom of the GAM (intercept + smooth term)
  • 2.3) Calculate the AIC of the three models with the same formula, because AIC() does not work for LOESS
    • n * log(RSS / n) + 2 * (complexity + 1): the AIC for models with normal errors; the +1 counts the residual variance. This formula differs from R’s AIC() by a constant, so only the differences between models matter
    • data.frame(): puts the results of the three models in one table
n <- length(age)                                                                  # 2.1
rss <- c(sum(resid(fit_best)^2),
         sum(resid(fit_loess)^2),
         sum(resid(fit_gam)^2))                                                   # 2.1

complexity <- c(2, fit_loess$enp, sum(fit_gam$edf))                               # 2.2

aic_table <- data.frame(model      = c("Linear", "LOESS", "GAM"),
                        complexity = complexity,
                        RSS        = rss,
                        AIC        = n * log(rss / n) + 2 * (complexity + 1))     # 2.3
aic_table                                                                         # 2.3
   model complexity      RSS      AIC
1 Linear   2.000000 2877.965 232.4550
2  LOESS   4.838372 2725.068 234.9655
3    GAM   2.000023 2877.964 232.4550

Interpretation.

  • LOESS fits slightly better but is not worth it: it has the lowest RSS (2725 vs 2878), but it uses almost 5 parameters instead of 2, so its AIC is about 2.5 points worse (234.97 vs 232.46). The small gain in fit does not justify the extra complexity.
  • Linear and GAM tie: both have the same complexity (2), the same RSS and the same AIC (232.46), because the GAM reduced to a straight line.
  • Choice: I choose the linear model, because it gives the same result as the GAM and is simpler (parsimony).

Conclusion

  • The non-linear fits did not reveal anything new: LOESS and GAM simply reproduced the same decreasing trend that the straight line had already shown.
  • Evidence: the GAM chose a straight line (edf = 1) with the same p-value and the same 18.5% explained; the AIC of the linear model and the GAM are equal, and LOESS is worse.
  • The bend of LOESS is noise: it appears only where there are few samples, driven by individual people.
  • The linear model is adequate for the relationship between TEF and age. The main limitation is not the shape of the line, but the weakness of the association and the use of raw data.

Question 4: Effect of normalization

Step 1: Normalize the counts

Problem: raw counts depend on the sequencing depth of each sample (how many reads the machine produced in total), which is a technical difference unrelated to age. A sample with twice as many reads shows about twice the counts for every gene. Goal: remove these technical differences with DESeq2 normalization, and put the data on a log scale, where every doubling counts as one unit.

How:

  • 1.1) Build a DESeq2 object with the filtered counts and the age of each sample
    • library(DESeq2): loads the package
    • DESeqDataSetFromMatrix(): combines the count matrix (countData) and the sample information (colData); row.names = colnames(counts_f) links each age to its sample; design = ~ 1 because no group comparison is needed here
  • 1.2) Estimate one size factor per sample: how much bigger or smaller its sequencing depth is compared with a typical sample
    • estimateSizeFactors(): computes the size factors
    • sizeFactors(): shows them (1 = typical depth; 2 = twice as many reads)
  • 1.3) Obtain the normalized counts: each count divided by the size factor of its sample
    • counts(dds, normalized = TRUE): returns the normalized counts
  • 1.4) Transform raw and normalized counts to the log2 scale
    • log2(... + 1): every doubling becomes +1; the +1 avoids log2(0), which is −infinity
library(DESeq2)                                                                                       # 1.1
dds <- DESeqDataSetFromMatrix(countData = counts_f,
                              colData   = data.frame(age = age, row.names = colnames(counts_f)),
                              design    = ~ 1)                                                        # 1.1

dds <- estimateSizeFactors(dds)                                                                       # 1.2
summary(sizeFactors(dds))                                                                             # 1.2
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.7409  0.9135  0.9804  1.0183  1.1164  1.6636 
norm_counts <- counts(dds, normalized = TRUE)                                                         # 1.3

log_raw  <- log2(counts_f + 1)                                                                        # 1.4
log_norm <- log2(norm_counts + 1)                                                                     # 1.4

Interpretation.

  • Size factors: they range from 0.74 to 1.66 (median 0.98). The sample with the highest sequencing depth has about 2.25 times more reads than the sample with the lowest depth (1.66 / 0.74).
  • What this means: a gene expressed at exactly the same level in those two samples would show about 2.25 times more raw counts in one of them, only for technical reasons.
  • Why it matters: if samples with higher depth happened to be from older (or younger) people, the raw-count model would find an association between expression and age that does not exist (a false positive), or the technical noise could hide a real one.
  • Normalization: dividing each count by the size factor of its sample removes this technical difference; the log2 scale then makes every doubling count as one unit.

Step 2: Compare the data before and after normalization

Problem: I need to see whether the samples really differed before normalization and whether normalization made them comparable. Goal: plot the distribution of the expression values of every sample before and after normalization.

How:

  • 2.1) Draw one boxplot per sample, before and after normalization, side by side
    • par(mfrow = c(1, 2)): two plots in one row
    • boxplot(): one box per column (sample); the thick line inside each box is the median
    • outline = FALSE: hides the extreme points, to see the boxes better
    • names = FALSE: hides the 58 sample names, which would not fit
  • 2.2) Plot the size factors, to see how different the sequencing depth was between samples
    • barplot(): one bar per sample; abline(h = 1) marks a typical depth
par(mfrow = c(1, 2))                                                                  # 2.1
boxplot(log_raw, outline = FALSE, names = FALSE, col = "grey80",
        main = "Before normalization", ylab = "log2(counts + 1)")                     # 2.1
boxplot(log_norm, outline = FALSE, names = FALSE, col = "lightblue",
        main = "After normalization", ylab = "log2(normalized counts + 1)")           # 2.1

par(mfrow = c(1, 1))                                                                  # 2.1

barplot(sizeFactors(dds), names.arg = FALSE, col = "grey60",
        main = "Size factor of each sample", ylab = "Size factor")                    # 2.2
abline(h = 1, col = "red", lty = 2)                                                   # 2.2

Interpretation.

  • Before normalization: the medians of the boxes form an irregular, zigzag line: some samples have systematically higher or lower values than others for all genes.
  • Size factors explain it: the bar plot shows that some samples have a sequencing depth above 1 (more reads than a typical sample) and others below 1. Samples with more reads have higher boxes, for purely technical reasons.
  • After normalization: the medians are aligned at almost the same height, so the technical differences in sequencing depth have been removed and the samples are now comparable.
  • Extra plot: the size factor bar plot supports this conclusion, because it shows directly the technical differences that the normalization corrected.

Step 3: Repeat the gene-by-gene analysis on the normalized data

Problem: the results of Question 2 were obtained with raw counts, which include technical noise. Goal: repeat exactly the same analysis (one linear model per gene, age ~ expression, p-value from ANOVA) on the normalized log2 data.

How:

  • 3.1) Prepare an empty vector for the new p-values, one per gene
    • numeric() and names(): as in Question 2
  • 3.2) Fit one model per gene and store its p-value
    • log_norm[i, ]: the normalized expression of gene i in the 58 samples
    • lm(age ~ expression) and anova(fit)[1, "Pr(>F)"]: the same model and p-value as in Question 2
  • 3.3) Count the significant genes and compare with Question 2
    • sum(p_values_norm < 0.05): number of genes with p < 0.05 (raw data: 691; expected by chance: about 837)
p_values_norm <- numeric(nrow(log_norm))                                 # 3.1
names(p_values_norm) <- rownames(log_norm)                               # 3.1

for (i in 1:nrow(log_norm)) {                                            # 3.2
  expression       <- log_norm[i, ]                                      # 3.2
  fit              <- lm(age ~ expression)                               # 3.2
  p_values_norm[i] <- anova(fit)[1, "Pr(>F)"]                            # 3.2
}

sum(p_values_norm < 0.05)                                                # 3.3
[1] 561

Interpretation.

  • Fewer significant genes: after normalization, 561 genes have p < 0.05, compared with 691 with raw counts.
  • False positives removed: about 130 genes were significant only with raw counts. Their apparent association with age was most likely caused by technical differences in sequencing depth, not by biology, so normalization removed these false positives.
  • Still no strong age signal: 561 is still below the ≈ 837 genes expected by chance alone (5% of 16,738), so even with normalized data there is no evidence of a widespread association between gene expression and age in this dataset.

Step 4: Compare raw and normalized results

Problem: a count of significant genes is not enough to decide which representation, raw or normalized, gives more trustworthy results. Goal: compare the distribution of p-values, the p-values of each gene, and the most significant gene in both analyses.

How:

  • 4.1) Plot the p-value histograms of both analyses side by side, with the height expected by chance
    • hist() and abline(h = ...): as in Question 2
  • 4.2) Compare the p-value of every gene in both analyses
    • -log10(): transforms p-values so that smaller p-values become larger numbers (p = 0.01 → 2, p = 0.001 → 3)
    • plot(): one point per gene; abline(0, 1) is the line where both analyses agree
  • 4.3) Find the most significant gene in the normalized analysis and compare it with TEF
    • which.min() and names(): the gene with the smallest p-value
    • AnnotationDbi::select(): its symbol and name
    • p_values_norm["7008"]: the p-value of TEF after normalization
par(mfrow = c(1, 2))                                                     # 4.1
hist(p_values, breaks = 20, col = "grey80",
     main = "Raw counts", xlab = "p-value")                              # 4.1
abline(h = length(p_values) / 20, col = "red", lty = 2)                  # 4.1
hist(p_values_norm, breaks = 20, col = "lightblue",
     main = "Normalized log2", xlab = "p-value")                         # 4.1
abline(h = length(p_values_norm) / 20, col = "red", lty = 2)             # 4.1

par(mfrow = c(1, 1))                                                     # 4.1

plot(-log10(p_values), -log10(p_values_norm), pch = 20, col = "grey40",
     xlab = "-log10(p) raw counts", ylab = "-log10(p) normalized",
     main = "p-value of each gene: raw vs normalized")                   # 4.2
abline(0, 1, col = "red", lty = 2)                                       # 4.2

best_gene_norm <- names(which.min(p_values_norm))                        # 4.3
p_values_norm[best_gene_norm]                                            # 4.3
  100652739 
0.001098593 
AnnotationDbi::select(org.Hs.eg.db, keys = best_gene_norm,
                      columns = c("SYMBOL", "GENENAME"),
                      keytype = "ENTREZID")                              # 4.3
   ENTREZID     SYMBOL               GENENAME
1 100652739 RAET1E-AS1 RAET1E antisense RNA 1
p_values_norm["7008"]                                                    # 4.3
      7008 
0.01284454 

Interpretation.

  • Histograms: after normalization, the bump between 0.05 and 0.35 disappears and the bars from 0.05 to 1 lie on the red line, so the p-values are better calibrated. However, the first bar (p < 0.05) is still low in both cases: no gene stands out, and there is no peak of genes associated with age.
  • p-value of each gene: the points form a wide cloud instead of following the red line, so the p-value of the same gene changes a lot between raw and normalized data. Much of the apparent signal in the raw data depended on technical noise.
  • TEF loses strength: its p-value goes from 0.00076 (raw) to 0.0128 (normalized), about 17 times larger. With 16,738 tests, a p-value of 0.0128 is not remarkable, so the association between TEF and age found in Question 2 was largely driven by differences in sequencing depth.
  • New top gene: the most significant gene after normalization is RAET1E-AS1 (RAET1E antisense RNA 1), a different gene from Question 2. The top gene changes depending on how the data are processed, which is another sign that neither result is robust.

Conclusion

  • Normalization was needed: the sequencing depth of the samples differed by up to 2.25 times, and the boxplots show that normalization made the samples comparable.
  • Fewer false positives: the number of genes with p < 0.05 dropped from 691 (raw) to 561 (normalized), and the p-value distribution became better calibrated.
  • No real age signal: in both analyses, the number of significant genes is below what chance alone would produce (≈ 837), and no gene stands out. TEF, the top gene of Question 2, lost most of its significance after normalization.
  • More trustworthy representation: the normalized data, because they remove technical differences that are unrelated to age and that created false associations in the raw data.

Question 5: Fitted values, residuals and normality

Step 1: Fit the final model and compare fitted and observed values

Problem: the p-value of a linear model is only trustworthy if the model fits the data reasonably well. Goal: take the most significant gene from the analysis I judge most trustworthy (normalized data, Question 4), fit its model, and compare the ages it predicts with the real ages.

How:

  • 1.1) Choose the gene and fit its model
    • log_norm[best_gene_norm, ]: the normalized expression of the most significant gene of Question 4
    • lm(age ~ final_expr): the linear model
  • 1.2) Obtain the fitted values (the age predicted by the model for each person)
    • predict(): as in Question 1
  • 1.3) Plot the observed age against the fitted age
    • abline(0, 1): the line where predicted = observed; a perfect model would put every point on it
final_expr <- log_norm[best_gene_norm, ]                                 # 1.1
fit_final  <- lm(age ~ final_expr)                                       # 1.1
summary(fit_final)                                                       # 1.1

Call:
lm(formula = age ~ final_expr)

Residuals:
    Min      1Q  Median      3Q     Max 
-15.036  -4.806  -1.331   5.021  17.206 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   50.517      5.388   9.375 4.49e-13 ***
final_expr     4.526      1.315   3.442   0.0011 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 7.213 on 56 degrees of freedom
Multiple R-squared:  0.1746,    Adjusted R-squared:  0.1599 
F-statistic: 11.85 on 1 and 56 DF,  p-value: 0.001099
fitted_final <- predict(fit_final)                                       # 1.2

plot(fitted_final, age, pch = 19, col = "grey40",
     xlab = "Fitted age", ylab = "Observed age",
     main = "Observed vs fitted age")                                    # 1.3
abline(0, 1, col = "red", lty = 2)                                       # 1.3

Interpretation.

  • Gene and model: I used RAET1E-AS1, the most significant gene in the normalized analysis (Question 4), which I consider the most trustworthy representation. Its slope is +4.53: when its expression doubles (one log2 unit), age increases by about 4.5 years (p = 0.0011).
  • Observed vs fitted: the points form a wide cloud around the diagonal line instead of lying on it; many people are 10 to 15 years away from their predicted age.
  • Predictions stay near the middle: most fitted ages are between 65 and 73, while the real ages range from 53 to 88. The model cannot separate younger from older people well.
  • Weak fit: this agrees with the low R² (0.175, only 17.5% of the variation in age explained) and the residual standard error of 7.2 years.

Step 2: Plot the residuals

Problem: the plot of observed vs fitted values shows the overall fit, but a linear model also assumes that its errors are random and normally distributed. Goal: obtain the residuals and look at their shape.

How:

  • 2.1) Obtain the residuals (observed age − fitted age)
    • resid(): as in Question 1
  • 2.2) Plot the residuals in three ways
    • plot(fitted, residuals) with abline(h = 0): residuals should form a random cloud around 0
    • hist(): the residuals should look like a bell
    • qqnorm() and qqline(): if the residuals are normal, the points follow the line
resid_final <- resid(fit_final)                                                     # 2.1

par(mfrow = c(1, 3))                                                                # 2.2
plot(fitted_final, resid_final, pch = 19, col = "grey40",
     xlab = "Fitted age", ylab = "Residual", main = "Residuals vs fitted")          # 2.2
abline(h = 0, col = "red", lty = 2)                                                 # 2.2
hist(resid_final, breaks = 10, col = "grey80",
     main = "Histogram of residuals", xlab = "Residual")                            # 2.2
qqnorm(resid_final, pch = 19, col = "grey40")                                       # 2.2
qqline(resid_final, col = "red")                                                    # 2.2

par(mfrow = c(1, 1))                                                                # 2.2

Interpretation.

  • Residuals vs fitted: the residuals range from about −15 to +15 years. Positive and negative residuals are mixed with no visible pattern (no U shape, no trend), so the straight line does not miss any systematic shape. The points are concentrated between fitted ages of 65 and 75 because the model predicts similar ages for most people.
  • Histogram: the residuals have a bell shape centered at zero, as expected under normality.
  • Q-Q plot: the points lie very close to the red line along the whole range, including the extremes, which also indicates approximately normal residuals.

Step 3: Test the normality of the residuals

Problem: judging normality by eye is subjective. Goal: test it formally with the Shapiro-Wilk test (H0: the residuals are normally distributed; p < 0.05 means they are not normal).

How:

  • 3.1) Run the Shapiro-Wilk test on the residuals
    • shapiro.test(): returns the W statistic and the p-value
shapiro.test(resid_final)                                                # 3.1

    Shapiro-Wilk normality test

data:  resid_final
W = 0.9879, p-value = 0.8312

Interpretation.

  • Shapiro-Wilk test: W = 0.988, p = 0.83. Since p > 0.05, there is no evidence that the residuals depart from a normal distribution. This does not prove normality, but the residuals are compatible with it, which agrees with the histogram and the Q-Q plot.
  • Can the p-value be taken at face value? For this single model, yes: the normality assumption holds and the residuals show no pattern, so the p-value of the gene effect (p = 0.0011) is technically valid.
  • But multiple testing matters: this gene was selected as the best of 16,738 tests. If no gene were related to age, about 18 genes (16,738 × 0.0011) would reach a p-value this small by chance alone. So a valid p-value does not make this gene a convincing finding.

Conclusion

  • The model is well specified: for RAET1E-AS1, the residuals are scattered randomly around zero, have a bell shape, follow the Q-Q line, and pass the Shapiro-Wilk test (p = 0.83), so the linear model’s assumptions are met.
  • But the fit is weak: the gene explains only 17.5% of the variation in age, predictions stay between 65 and 73 years while real ages range from 53 to 88, and the typical error is about ±7 years.
  • The finding is not convincing: checking the assumptions confirms that the p-value is valid, but because the gene was the best of 16,738 tests, a p-value of 0.0011 is expected by chance for about 18 genes. A correction for multiple testing (Week 6) would be needed before claiming that this gene is associated with age.
  • Overall: in this dataset of 58 older adults, I found no robust evidence that the expression of any single muscle gene is associated with age.