0

In my data, Hemoglobin_group is a categorical variable with 6 levels. When I run the below code, I can't see Hemoglobin_group levels in the output. How can I solve this problem?

fit <- coxph(Surv(Time,Status)~Hemoglobin_group, data)
fit

My output:

                      coef   exp(coef) se(coef)  z        p
    Hemoglobin_group -0.06585  0.93627  0.01874 -3.514 0.000441
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Cgdmm
  • 63
  • 1
  • 7
  • Can you post a data example? – Juan Camilo Rivera Palacio Jan 17 '21 at 15:54
  • This link, https://stackoverflow.com/questions/21367259/r-cox-hazard-model-not-including-levels-of-a-factor categorical data shown in the regression output like RUTH.CLASS4, RUTH.CLASS5, RUTH.CLASS6, but i can't show hemoglobin_group2, hemoglobin_grpup3, hemoglobin_group4, hemoglobin_group5 in my data – Cgdmm Jan 17 '21 at 16:50
  • I want to compare the levels of the categorical variables and the reference group – Cgdmm Jan 17 '21 at 16:59
  • If you want to hemoglobin_group2, hemoglobin_grpup3, hemoglobin_group4..., the hemoglobine has to be factor and with labels numbers, i.e hemoglobine_group =2,3,4..... Could you check it?. – Juan Camilo Rivera Palacio Jan 17 '21 at 18:03
  • At the very least we need to see `summary(data)` or `str(data)` (if your data has many other variables other than the ones used in your analysis you might leave them out to keep things simple) – Ben Bolker Jan 17 '21 at 18:29

2 Answers2

1
str(data)
tibble [2,021 x 21] (S3: tbl_df/tbl/data.frame)
$ status             : num [1:2021] 1 1 1 1 1 1 0 0 0 0 ...
$ id                 : num [1:2021] 1 1 1 1 1 1 2 2 2 2 ...
$ Time               : num [1:2021] 20.3 20.3 20.3 20.3 20.3 ...
$ t1                 : num [1:2021] 0 1 2 3 4 5 0 1 2 3 ...
$ t2                 : num [1:2021] 1 2 3 4 5 ...
$ sex_string         : chr [1:2021] "MALE" "MALE" "MALE" "MALE"...
$ sex                : num [1:2021] 1 1 1 1 1 1 1 1 1 1 ...
$ age                : num [1:2021] 89 89 89 89 89 89 77 77 77 77 ...
$ hemoglobin         : num [1:2021] 9.71 10.22 11.3 11.8 11.2 ...
$ Diabet             : num [1:2021] 1 1 1 1 1 1 2 2 2 2 ...
$ Hemoglobin_group   : num [1:2021] 4 4 4 4 4 4 5 5 5 5 ...
$ Kreatinin          : num [1:2021] 7.19 7.19 7.19 7.19 7.19 ...
$ fosfor             : num [1:2021] 4.14 4.14 4.14 4.14 4.14 ...
$ Kalsiyum           : num [1:2021] 8.5 8.5 8.5 8.5 8.5 ...
$ CRP                : num [1:2021] 1.33 1.33 1.33 1.33 1.33 ...
$ Albumin            : num [1:2021] 4.19 4.19 4.19 4.19 4.19 ...
$ Ferritin           : num [1:2021] 428 428 428 428 428 ...
$ months             : num [1:2021] 1 2 3 4 5 6 1 2 3 4 ...

It looks like when I write str (data). I thought I was transforming into a factor by doing the following codes in my data. I guess I couldn't transform. I did not understand?

The codes I wrote to convert to factor were as follows

sex<-as.factor(sex)
is.factor(sex)

Diabet<-as.factor(Diabet)
is.factor(Diabet)


Status<-as.factor(Status)
is.factor(Status)

months<-as.factor(months)
is.factor(months)

Hemoglobin_group<-as.factor(Hemoglobin_group)
is.factor(Hemoglobin_group)
                         

When ı run this code, R console looks like:

> sex<-as.factor(sex)
> is.factor(sex)
 [1] TRUE
> 
> Diabet<-as.factor(Diabet)
> is.factor(Diabet)
[1] TRUE
> 
> 
> Status<-as.factor(Status)
> is.factor(Status)
[1] TRUE
> 
> months<-as.factor(months)
> is.factor(months)
[1] TRUE
> 
> Hemoglobin_group<-as.factor(Hemoglobin_group)
> is.factor(Hemoglobin_group)
[1] TRUE
                    

In this case, don't the categorical variables in my data turn into factors?

Cgdmm
  • 63
  • 1
  • 7
0

Your variable Hemoglobin_group is probably considered as a numeric value. Try:

Hemoglobin_groupF <- factor(Hemoglobin_group)
fit <- coxph(Surv(Time,Status) ~ Hemoglobin_groupF, data)
fit

The reference group will the first factor. You can easily change your reference factor with the function relevel

Regis
  • 148
  • 1
  • 6
  • My Hemoglobin group factor and I use relevel function but I can't show still hemoglobin categorises in regression output – Cgdmm Jan 17 '21 at 17:50
  • 1
    I guess we can't answer without seeing more code or data. – Regis Jan 17 '21 at 18:05