Linear systems in R; solution spaces

Week 1 — Linear systems in R

Quantitative biology and integrative omics

About this lecture

In Linear systems by hand we solved small systems A\mathbf{x} = \mathbf{b} on paper. Today we hand the work to R — and discover that not every system has a tidy answer. You will be able to:

  • Build A and \mathbf{b} in R and solve with solve.
  • Recognise the three kinds of solution space: exactly one, none, or infinitely many.
  • Connect each kind to a deconvolution scenario: exactly-, over-, or under-determined.
  • Know what to reach for when A is singular or non-square: least squares, via lm (full treatment later).

Recap: the deconvolution problem

A bulk expression sample is a mixture. Known per-cell-type signatures (matrix A, one column per cell type) and the measured bulk profile (vector \mathbf{b}) give a system A\mathbf{x} = \mathbf{b}, whose solution \mathbf{x} is the fractions (or amounts) of each cell type. Today: what if the system cannot be solved exactly?

Use byrow = TRUE so the R matrix reads like the equations

# Two cell types, two marker genes
A <- matrix(c(2, 1,
              1, 3), nrow = 2, byrow = TRUE)
b <- c(8, 9)

A
#      [,1] [,2]
# [1,]    2    1
# [2,]    1    3
  • matrix with byrow = TRUE fills row by row, so the code reads like the equations on paper.
  • nrow sets the number of rows; c(...) is just the entries.
  • Default byrow = FALSE fills column by column — a classic bug.

solve(A, b) finds \mathbf{x}; check it with A %*% x

x <- solve(A, b)
x
# [1] 3 2          # 3 units tumour, 2 units immune

# Always check: does A x reproduce b?
A %*% x
#      [,1]
# [1,]    8
# [2,]    9
  • solve(A, b) returns the \mathbf{x} with A\mathbf{x} = \mathbf{b} — it runs Gaussian elimination, exactly what you did by hand in Linear systems by hand.
  • %*% is matrix multiplication. Use it to verify, just like substituting back on paper.

solve(A) alone gives the inverse

Ainv <- solve(A)        # one argument: the inverse of A
Ainv %*% b
# [1] 3 2               # same answer, x = A^{-1} b
  • solve(A, b) is preferred over solve(A) %*% b: it is faster and more numerically accurate (it never forms the inverse explicitly).
  • The inverse A^{-1} only exists when A is square and non-singular — precisely the “exactly one solution” case below.

A linear system has one, no, or infinitely many solutions

For A\mathbf{x} = \mathbf{b} there are exactly three possibilities:

  1. Exactly one solution — the system is consistent and determined.
  2. No solution — the equations contradict each other.
  3. Infinitely many solutions — the equations are consistent but leave freedom.

The next slides take each in turn and pair it with a deconvolution scenario.

Case 1 — exactly one (well-determined)

As many independent measurements as unknowns. A is square and non-singular.

Biology: #genes measured = #cell types, and the signatures are genuinely different. The deconvolution has a single answer.

A <- matrix(c(2, 1,
              1, 3), nrow = 2, byrow = TRUE)
solve(A, c(8, 9))
# [1] 3 2          # the unique solution

This is the happy case from Linear systems by hand. solve() just returns the answer.

Case 2 — no solution (over-determined)

More equations than unknowns, and they conflict. Often A is non-square (more rows than columns).

Biology: we measured more genes than cell types. With real noisy data the equations rarely agree exactly, so no \mathbf{x} fits all of them.

A <- matrix(c(1, 0,
              0, 1,
              1, 1), nrow = 3, byrow = TRUE)   # 3 eqns, 2 unknowns
b <- c(2, 3, 6)                                # but 2 + 3 = 5, not 6
solve(A, b)
# Error in solve.default(A, b) : 'a' must be a square matrix

solve() refuses: it only handles square systems.

Case 3 — infinitely many (under-determined)

Fewer independent equations than unknowns. A is singular (or has fewer rows than columns).

Biology: we measured too few genes to distinguish the cell types — many fraction combinations explain the data equally well.

A <- matrix(c(1, 1,
              2, 2), nrow = 2, byrow = TRUE)   # row 2 = 2 x row 1
b <- c(3, 6)
solve(A, b)
# Error in solve.default(...) : Lapack routine dgesv: system is exactly singular

Both equations say the same thing (x + y = 3), so any point (x, y) on that line works. solve() errors on the singular matrix.

Detecting which case you are in

dim(A)                 # rows (equations) vs cols (unknowns)
qr(A)$rank             # number of independent equations
  • dim non-square \Rightarrow over- or under-determined by shape.
  • qr$rank < #unknowns \Rightarrow the equations do not pin down every unknown: a square A is singular (some columns are combinations of others) \Rightarrow not Case 1.

A square A with full rank is exactly Case 1. For a square A, det(A) \approx 0 flags the same thing.

When there is no exact answer: least squares

For over-determined systems (Case 2) we cannot satisfy every equation, so we find the \mathbf{x} that comes closest — minimising the total squared mismatch \lVert A\mathbf{x} - \mathbf{b}\rVert^2. This is least squares.

A <- matrix(c(1, 0,
              0, 1,
              1, 1), nrow = 3, byrow = TRUE)
b <- c(2, 3, 6)

qr.solve(A, b)            # least-squares solution
# [1] 2.333333 3.333333

coef(lm(b ~ A - 1))       # same thing, via linear regression

qr.solve and lm both give the best-fit \mathbf{x}. We treat this properly later in the course.

A note for real deconvolution

Real cell-type fractions must be non-negative and usually sum to 1. Plain solve/lm know nothing about that and can return negative fractions.

  • Production tools (e.g. CIBERSORT, and Bioconductor packages) add these constraints (non-negative least squares, support-vector regression, etc.).
  • The linear-system core is the same — the constraints are extra structure on top.

We use the unconstrained version to learn the mechanics; the constrained version is the real tool.

Summary

  • Build systems in R with matrix (byrow = TRUE!) and solve with solve(A, b); check with %*%.
  • A linear system has one, none, or infinitely many solutions — matching well-, over-, and under-determined deconvolution.
  • Diagnose with dim and qr$rank.
  • Over-determined \Rightarrow least squares (qr.solve, lm); full treatment later.

Next: Vectors, dot product & projection — the geometric picture: vectors, dot products, and projection (which is why least squares works).