# Logistic regression implementation

**URL:** https://discuss.hail.is/t/logistic-regression-implementation/1691
**Category:** Hail Query & hailctl
**Created:** [September 23, 2020, 3:29am UTC](https://discuss.hail.is/t/logistic-regression-implementation/1691 "2020-09-23T03:29:34Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![fengyi](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@fengyi](https://discuss.hail.is/u/fengyi)
#### Post date: [September 23, 2020, 3:29am UTC](https://discuss.hail.is/t/logistic-regression-implementation/1691/1 "2020-09-23T03:29:35Z")

</div>

Hi,

I am new to hail and trying to implement binary trait in hail.

The set up is  
1, The phenotype is in 1, 0 format  
2, I imported plink file using hl.import\_plink and set quant\_pheno= **False** and set it to mt  
3, I imported covariate using hl.import\_table and use annotate\_cols to incorporate covariates to the mt  
4, last I did something like, gwas =hl.logistic\_regression\_rows(test=‘wald’,  
y=[mt.is\_case, mt.is\_case],  
x=mt.GT.n\_alt\_alleles()  
, covariates= [1, mt.covar.1, mt.covar.2, …])  
Question:  
1, Why is my is case in bool format?  
 ![Screen Shot 2020-09-22 at 10.24.25 PM](https://canada1.discourse-cdn.com/flex036/uploads/hail/original/1X/7faed48af53f968db11ca2030ea7cc107626f9c0.png)

2, Can you also help me to understand this error? Error summary: HailException: For logistic regression, y at index 0 must be non-constant

3, I am not sure if I am implementing logistic regression correctly, can you let me know if there is anything I should change?

Thank you!  
-Fengyi

---

<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: [September 23, 2020, 2:16pm UTC](https://discuss.hail.is/t/logistic-regression-implementation/1691/2 "2020-09-23T14:16:35Z")

</div>

Hey @fengyi, I’m sorry you’re having trouble!

In the future, if you share the exact code you ran, we can more easily help you. It is also easier to help you when you copy and paste the exact output you get (like the `is_case` column).

As to your questions:

1. You specified that your trait is not a quantitate phenotype. A binary (aka case-control) phenotype is most naturally by a Boolean value. Hail is designed to work properly with Boolean values.
2. This error message indicates that all samples in your dataset have the same value for the first element of the `y` array. In other words, all your samples have the same value for `is_case`. This is clear from the `is_case` column you shared. All of your samples either have missing data or are controls. It is incorrect to encode your phenotype as 1s and 0s in a [PLINK fam file](https://www.cog-genomics.org/plink2/formats#fam). In a PLINK fam file, 1 always indicates a control, 2 always indicates a case, and -9, 0, and non-numeric values all indicate missing data. You can fix your fam file by importing it as a quantitative phenotype and then manually converting the 1s and 0s to Booleans:

```auto
mt = hl.import_plink(...)
mt = mt.annotate_columns(is_case = hl.bool(mt.quant_pheno))
mt = mt.drop('quant_pheno')

```

1. If you have no missing data you should only specify one response variable: `y=mt.is_case` because there is only one degree of freedom: case versus not-case. If you do have missing data, then you can use `y=[mt.is_case, mt.is_case]` because you have two degrees of freedom: case versus not-case and missing versus not-missing.

---

<div class="post-metadata">

### Author: ![fengyi](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@fengyi](https://discuss.hail.is/u/fengyi)
#### Post date: [September 23, 2020, 3:50pm UTC](https://discuss.hail.is/t/logistic-regression-implementation/1691/3 "2020-09-23T15:50:48Z")

</div>

Hi @danking, thank you for your help!

I have imported it as quantitative phenotype and it seems like it automatically set 0 to -9.

when I tried mt = mt.annotate\_columns(is\_case = hl.bool(mt.quant\_pheno))

![Screen Shot 2020-09-23 at 10.49.03 AM](https://canada1.discourse-cdn.com/flex036/uploads/hail/original/1X/36455a20d8dbf0b4cdae1864ba8a4d4e81b7f647.png)

It seems like everything became true.

Do you know how to transform -9 to False and 1 to True?

Thank you!  
-Fengyi

---

<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: [September 23, 2020, 4:31pm UTC](https://discuss.hail.is/t/logistic-regression-implementation/1691/4 "2020-09-23T16:31:00Z")

</div>

You’re looking for [`case`](https://hail.is/docs/0.2/overview/expressions.html#case-statements)

```auto
mt.annotate_columns(is_case =
    hl.case()
      .when(mt.quant_pheno == -9, False)
      .when(mt.quant_pheno == 1, True)
      .or_missing())

```

---

<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: [September 23, 2020, 6:47pm UTC](https://discuss.hail.is/t/logistic-regression-implementation/1691/5 "2020-09-23T18:47:29Z")

</div>

2 posts were split to a new topic: [When running Logistic Regression Rows I get a Py4JNetworkError](https://discuss.hail.is/t/when-running-logistic-regression-rows-i-get-a-py4jnetworkerror/1704)
