# Linear regression explanation

**URL:** <https://discuss.hail.is/t/linear-regression-explanation/3451>\
**Category:** Hail Query & hailctl\
**Created:** [June 26, 2023, 10:33am UTC](https://discuss.hail.is/t/linear-regression-explanation/3451 "2023-06-26T10:33:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ag14774](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/ag14774/32/933_2.png) [@ag14774](https://discuss.hail.is/u/ag14774)\
**Post date:** [June 26, 2023, 10:33am UTC](https://discuss.hail.is/t/linear-regression-explanation/3451/1 "2023-06-26T10:33:07Z")

</div>

Hi all,

I was wondering, how is linear regression in Hail implemented? Could someone point me to where the actual computation of weights happen? Most importantly, _how_ are covariates treated? Are they simply treated as just another component in the linear equation or do they get any special treatment (e.g. is a separate model fitted on just the covariates first?). This is not entirely clear in the documentation.

Thanks  
Andreas

---

<div class="post-metadata">

**Author:** ![patrick-schultz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/patrick-schultz/32/265_2.png) [@patrick-schultz](https://discuss.hail.is/u/patrick-schultz)\
**Post date:** [June 26, 2023, 1:45pm UTC](https://discuss.hail.is/t/linear-regression-explanation/3451/2 "2023-06-26T13:45:40Z")

</div>

Hi Andreas,

I assume you’re referring to `linear_regression_rows`? Unfortunately the implementation is a bit hard to read. Mathematically, the covariates don’t get any special treatment, they’re just another variable in the model alongside the genotype. However, instead of independently performing a multivariate regression on every row, we take advantage of the fact that the covariates are constant across rows, and do something like fitting a separate model once before performing the per-row regressions. But again, this is just an optimization, and is mathematically equivalent to fitting a standard linear model independently per row.

Does that answer your question?

Best.  
Patrick

---

<div class="post-metadata">

**Author:** ![ag14774](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/ag14774/32/933_2.png) [@ag14774](https://discuss.hail.is/u/ag14774)\
**Post date:** [June 26, 2023, 1:56pm UTC](https://discuss.hail.is/t/linear-regression-explanation/3451/3 "2023-06-26T13:56:18Z")

</div>

Could you clarify what does “row” refer to here? If we assume a data matrix X where each row is a patient and each column is variant/feature, why are covariates constant? If for example I perform PCA and get scores to use as covariates, wouldn’t the first, second, third,etc PC score be different from one patient to the other?

---

<div class="post-metadata">

**Author:** ![patrick-schultz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/patrick-schultz/32/265_2.png) [@patrick-schultz](https://discuss.hail.is/u/patrick-schultz)\
**Post date:** [June 26, 2023, 2:05pm UTC](https://discuss.hail.is/t/linear-regression-explanation/3451/4 "2023-06-26T14:05:03Z")

</div>

I was referring to the [`linear_regression_rows`](https://hail.is/docs/0.2/methods/stats.html#hail.methods.linear_regression_rows) method, which performs a linear regression per row of a matrix table. This is most often used where each row is a variant, and each column is a sample/patient. This has historically been the standard representation of genetic data in hail, because a matrix table is partitioned/distributed across rows, and there have typically been many more variants than samples (though that is becoming less true). In this case, covariates (which in `linear_regression_rows` must be column fields) are constant across rows/variants/features.

---

<div class="post-metadata">

**Author:** ![ag14774](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/ag14774/32/933_2.png) [@ag14774](https://discuss.hail.is/u/ag14774)\
**Post date:** [June 26, 2023, 3:37pm UTC](https://discuss.hail.is/t/linear-regression-explanation/3451/5 "2023-06-26T15:37:31Z")

</div>

Thank you! What if there is correlation between the covariates and some of the remaining variables. Wouldn’t fitting a separate model with only the covariates “break” this collinearity? Is it mathematically guaranteed that the model is fitted in such a way such that if you were to fit a single model to all of the variables + covariates together, you would end up with the same weights/betas for all variables and all covariates?

---

<div class="post-metadata">

**Author:** ![patrick-schultz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/patrick-schultz/32/265_2.png) [@patrick-schultz](https://discuss.hail.is/u/patrick-schultz)\
**Post date:** [June 26, 2023, 4:12pm UTC](https://discuss.hail.is/t/linear-regression-explanation/3451/6 "2023-06-26T16:12:44Z")

</div>

Yes, it is mathematically guaranteed that what we compute is the same thing you would get by fitting a single model to all variables and covariates together.

---

<div class="post-metadata">

**Author:** ![danking](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/danking/32/43_2.png) [@danking](https://discuss.hail.is/u/danking)\
**Post date:** [June 26, 2023, 8:23pm UTC](https://discuss.hail.is/t/linear-regression-explanation/3451/7 "2023-06-26T20:23:19Z")

</div>

Jon Bloom described some of this in sections 1-3 of [this arxiv pre-print](https://arxiv.org/pdf/1901.09531.pdf)

---

<div class="post-metadata">

**Author:** ![ag14774](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/ag14774/32/933_2.png) [@ag14774](https://discuss.hail.is/u/ag14774)\
**Post date:** [June 27, 2023, 7:47am UTC](https://discuss.hail.is/t/linear-regression-explanation/3451/8 "2023-06-27T07:47:33Z")

</div>

Thank you both! That answers my question
