Using Cox regression I'm trying to find the difference in churnrate for different demographic properties for a dataset with millions of records. The data is similar to below:

user   zip      time  churn
1      422      12    0
2      421      244   0   
3      421      13    0
4      411      431   1

I'm doing like this:

(load data into data frame df)
df$survival <- Surv(df$time, df$churn == 1)
results <- coxph(survival ~ zip, data = df)

And getting expected results for each zip-value (ignore the coeffients, it's just dummy data):

> results
            coef exp(coef)  se(coef)     z       p
(zip411 should have been here)
zip421   3.70460  40.63375   0.70774  5.23 1.7e-07
zip422   3.71651  41.12044   0.70765  5.25 1.5e-07

However the first zip-code (sorted lowest value) is always missing, no matter what data I input or subset - the first derived group is always gone.

I can solve it by creating a column for each zip-code with a binary value and then specifying each like below. But there must be better ways to do it.

results <- coxph(survival ~ zip411 + zip421 + zip422, data=df)

Thank you!

EDIT: Changed question to better reflect what I'm actually asking. Thanks for clarification.

  • $\begingroup$ This doesn't have anything to do with R or Cox modeling, it is just a function of the way categorical variables are coded. $\endgroup$ – gung Apr 10 '16 at 15:32
  • 1
    $\begingroup$ You're right, I'm changing the question to better reflect what I'm asking. $\endgroup$ – Nikko Apr 10 '16 at 17:27

It seems that R has treated "zip411" as the reference group. So it does not appear in the results table (as the reference group's Hazard ratio is already fixed to 1).

Note that you can change the reference group of a categorical variable by using the relevel function. For example:

var2 <- relevel(var1, ref = "zip422")

By default, R uses the lexicographical order to choose the reference category.

  • 1
    $\begingroup$ Thanks for you answer, I'm as you might have guessed a newbie when it comes to R. Could you please elaborate with code how I can use relevel function to see the whole result? $\endgroup$ – Nikko Apr 10 '16 at 15:54
  • 1
    $\begingroup$ as @gung points out in the comment above, and as this answer says, it is just how R treats the reference group. The whole result is implicitly available to you: the reference group has coefficient equal to zero. $\endgroup$ – Erosennin Apr 10 '16 at 16:06
  • $\begingroup$ So in this case, "zip411" have automatically been choosen as a reference group and the other coefficients are relative to that but how can I use the GLOBAL mean as reference group? I thought that was the default behavior? Thanks a lot for your clarification. $\endgroup$ – Nikko Apr 10 '16 at 16:39
  • 1
    $\begingroup$ I found this as a way to change default behavior: options(contrasts=c(unordered="contr.sum", ordered="contr.poly")) here: stackoverflow.com/questions/21367259/… But still unable to see the whole result. $\endgroup$ – Nikko Apr 10 '16 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.