Answers.Body: <p>The fact that you supplied <code>GROUP</code> as a covariate, you are getting 9 coefficients as one of the ten levels is used as the reference level.</p>
  
  <p>If you would like to fit one model per group (and in this scenario you would not be correcting for <code>GROUP</code>), you should first split your data frame and fit your models on the resulting list's elements:</p>
  
  <pre><code>lapply(split(DF, DF$GROUP),
         function(x) coxph(serve_object ~ age + Smoking + Diabetes, data = DF))
  </code></pre>
  
  <p>Example with the <code>mtcars</code> data set when grouped by number of cylinders, <code>cyl</code> having values of 4, 6 or 8:</p>
  
  <pre><code>&gt; lapply(split(mtcars, mtcars<span class="math-container">$cyl),
  +        function(x) lm(disp ~ hp, data = x))
  $</span>`4`
  
  Call:
  lm(formula = disp ~ hp, data = x)
  
  Coefficients:
  (Intercept)           hp  
      59.0369       0.5579  
  
  
  $`6`
  
  Call:
  lm(formula = disp ~ hp, data = x)
  
  Coefficients:
  (Intercept)           hp  
     290.9178      -0.8799  
  
  
  $`8`
  
  Call:
  lm(formula = disp ~ hp, data = x)
  
  Coefficients:
  (Intercept)           hp  
     320.2083       0.1572  
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-28T12:29:35.047Z
Answers.Text: Can you help me in interpreting result.
  .$`Group3`
  Call:
  coxph(formula = so_ukb ~ age_at_recruitment.210220.0 + Smoking + 
      Hypertention, data = UKB_FILTRD)
  
                                  coef exp(coef) se(coef)      z      p
  age_at_recruitment.210220.0  0.06961   1.07208  0.00141  49.35 <2e-16
  Smoking.L                   -0.21220   0.80880  0.01335 -15.90 <2e-16
  Hypertention.L              -0.44221   0.64262  0.01361 -32.49 <2e-16
  
  Likelihood ratio test=5142  on 3 df, p=< 2.2e-16
  n= 448287, number of events= 11377 
     (3270 observations deleted due to missingness)
Answers.UserDisplayName: Aryh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-28T12:31:17.073Z
Answers.Text: I am just confused that how to get one cumulative hazard ratio by adjusting model for these covariates. It is giving HR for age, smoking, hypertention separatively.. I want to see just cumulative.
Answers.UserDisplayName: Aryh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-28T12:31:34.740Z
Answers.Text: Thank you for above lapply code, it worked so well.
Answers.UserDisplayName: Aryh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-28T12:44:33.093Z
Answers.Text: Let's take the coefficient you get for `age`: The `exp()` of this coefficient is the change in in HR when `age` changes by one unit and while all the other variables are accounted for. In that sense, it takes all the variables into account so can be said to be "cumulative", although I am not sure such terminology exists for linear models. You might want to use interaction terms if your covariates depend on each other.
Answers.UserDisplayName: haci
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-28T14:36:13.350Z
Answers.Text: Can you give an idea of how we can export the summary(coxph) into csv?
Answers.UserDisplayName: Aryh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-28T14:36:38.807Z
Answers.Text: or may be first convert it into data frame and then export df intom csv.
Answers.UserDisplayName: Aryh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-28T17:11:29.530Z
Answers.Text: You can give a try with the `broom` package. Its `tidy()` function would convert output of `lm()` and many other related functions to data frames. Would probably work with the `coxph()`'s output.
Answers.UserDisplayName: haci
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-28T12:14:06.410Z
Answers.LastActivityDate: 2020-04-28T12:28:00.133Z
Answers.LastEditDate: 2020-04-28T12:28:00.133Z
Answers.UserDisplayName: haci
Body: <p>My population is divided into 10 groups. I want to run the cox-proportional hazard model separately for each of these 10 groups so that in the end i can compare which groups has highest hazard ratio depending uopn the covariates.
  How can i do this in R? Earlier i was using this formula.</p>
  
  <pre><code>Model_1 &lt;- coxph(serve_object ~ GROUP + age+ Smoking+ Diabetes, data = DF
  summary(Model_1)
  </code></pre>
  
  <p>But the output is very confusing. it gives hazard ratio of 9 out of 10 groups. I think it dealt group as covariate. </p>
  
Tags: <r><statsmodels>
Title: How to run Cox proportional Hazard model for each group in R?
ThreadText: # How to run Cox proportional Hazard model for each group in R?
  
  (Asked by: username_0 on 2020-04-28)
  
  My population is divided into 10 groups. I want to run the cox-proportional hazard model separately for each of these 10 groups so that in the end i can compare which groups has highest hazard ratio depending uopn the covariates.
  How can i do this in R? Earlier i was using this formula.
  
  ```
  Model_1 <- coxph(serve_object ~ GROUP + age+ Smoking+ Diabetes, data = DF
  summary(Model_1)
  
  ```
  
  But the output is very confusing. it gives hazard ratio of 9 out of 10 groups. I think it dealt group as covariate.
  
  **Tags:** r, statsmodels
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-04-28. Score: 2 | Accepted answer
  
  The fact that you supplied `GROUP` as a covariate, you are getting 9 coefficients as one of the ten levels is used as the reference level.
  
  If you would like to fit one model per group (and in this scenario you would not be correcting for `GROUP`), you should first split your data frame and fit your models on the resulting list's elements:
  
  ```
  lapply(split(DF, DF$GROUP),
         function(x) coxph(serve_object ~ age + Smoking + Diabetes, data = DF))
  
  ```
  
  Example with the `mtcars` data set when grouped by number of cylinders, `cyl` having values of 4, 6 or 8:
  
  ```
  > lapply(split(mtcars, mtcars$cyl),
  +        function(x) lm(disp ~ hp, data = x))
  $`4`
  
  Call:
  lm(formula = disp ~ hp, data = x)
  
  Coefficients:
  (Intercept)           hp  
      59.0369       0.5579  
  
  $`6`
  
  Call:
  lm(formula = disp ~ hp, data = x)
  
  Coefficients:
  (Intercept)           hp  
     290.9178      -0.8799  
  
  $`8`
  
  Call:
  lm(formula = disp ~ hp, data = x)
  
  Coefficients:
  (Intercept)           hp  
     320.2083       0.1572  
  
  ```
  
  > **Comments:**
  > **username_0**: Can you help me in interpreting result. .$`Group3` Call: coxph(formula = so_ukb ~ age_at_recruitment.210220.0 + Smoking +      Hypertention, data = UKB_FILTRD)                                  coef exp(coef) se(coef)      z      p age_at_recruitment.210220.0  0.06961   1.07208  0.00141  49.35 <2e-16 Smoking.L                   -0.21220   0.80880  0.01335 -15.90 <2e-16 Hypertention.L              -0.44221   0.64262  0.01361 -32.49 <2e-16  Likelihood ratio test=5142  on 3 df, p=< 2.2e-16 n= 448287, number of events= 11377     (3270 observations deleted due to missingness)  
  > **username_0**: I am just confused that how to get one cumulative hazard ratio by adjusting model for these covariates. It is giving HR for age, smoking, hypertention separatively.. I want to see just cumulative.  
  > **username_0**: Thank you for above lapply code, it worked so well.  
  > **username_1**: Let's take the coefficient you get for `age`: The `exp()` of this coefficient is the change in in HR when `age` changes by one unit and while all the other variables are accounted for. In that sense, it takes all the variables into account so can be said to be "cumulative", although I am not sure such terminology exists for linear models. You might want to use interaction terms if your covariates depend on each other.  
  > **username_0**: Can you give an idea of how we can export the summary(coxph) into csv?  
  > **username_0**: or may be first convert it into data frame and then export df intom csv.  
  > **username_1**: You can give a try with the `broom` package. Its `tidy()` function would convert output of `lm()` and many other related functions to data frames. Would probably work with the `coxph()`'s output.  
  
  ---
  
  