Git Product home page Git Product logo

Comments (7)

tylcole avatar tylcole commented on July 29, 2024 1

Hello,
Just wanted to say that I had that same issue, and it happened because I was using ordered factors instead of normal factors. Unordering them solved the problem.

from finalfit.

ewenharrison avatar ewenharrison commented on July 29, 2024

from finalfit.

ewenharrison avatar ewenharrison commented on July 29, 2024

Hi,
On RStudio Server running on Ubuntu, I can't replicate the problem. The appearance is as intended. Code block below.
Which system are you using this on?
You can debug by running the code at the bottom of the block. fit_id from summary_factorlist() should have factor levels in same format as they appear in coxphmulti().
Please post back results.
Many thanks again.
Ewen

> library(finalfit)
> library(dplyr)
> 
> # Define variables
> explanatory = c("agege65","nodesge3","adhere.factor")
> dependent = "Surv(time, status)"
> 
> # Without unicode
> data(colon_s)
> colon_s %<>% 
+ 	mutate(agege65= factor(if_else(age>=65,">= 65 years","< 65 years")),
+ 				 nodesge3 = factor(if_else(nodes>=3,">=3","< 3"))
+ 	)
> 
> colon_s %>%
+ 	finalfit(dependent, explanatory) -> t6
> knitr::kable(t6, row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"))


|Dependent: Surv(time, status) |            |          HR (univariable)|        HR (multivariable)|
|:-----------------------------|:-----------|-------------------------:|-------------------------:|
|agege65                       |< 65 years  |                         -|                         -|
|                              |>= 65 years | 1.20 (1.00-1.45, p=0.056)| 1.18 (0.98-1.43, p=0.087)|
|nodesge3                      |< 3         |                         -|                         -|
|                              |>=3         | 2.23 (1.84-2.70, p<0.001)| 2.25 (1.85-2.72, p<0.001)|
|Adherence                     |No          |                         -|                         -|
|                              |Yes         | 1.37 (1.07-1.75, p=0.011)| 1.38 (1.08-1.77, p=0.011)|
> 
> # With unicode
> data(colon_s)
> colon_s %<>% 
+ 	mutate(agege65= factor(if_else(age>=65,"\u2265 65 years","< 65 years")),
+ 				 nodesge3 = factor(if_else(nodes>=3,"\u2265 3","< 3"))
+ 	)
> 
> colon_s %>%
+ 	finalfit(dependent, explanatory) -> t6
> knitr::kable(t6, row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"))


|Dependent: Surv(time, status) |           |          HR (univariable)|        HR (multivariable)|
|:-----------------------------|:----------|-------------------------:|-------------------------:|
|agege65                       |< 65 years |                         -|                         -|
|                              |≥ 65 years | 1.20 (1.00-1.45, p=0.056)| 1.18 (0.98-1.43, p=0.087)|
|nodesge3                      |< 3        |                         -|                         -|
|                              |≥ 3        | 2.23 (1.84-2.70, p<0.001)| 2.25 (1.85-2.72, p<0.001)|
|Adherence                     |No         |                         -|                         -|
|                              |Yes        | 1.37 (1.07-1.75, p=0.011)| 1.38 (1.08-1.77, p=0.011)|
> 
> # Debug
> colon_s %>%
+ 	summary_factorlist(dependent, explanatory, fit_id=TRUE) -> t7
> knitr::kable(t7, row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"))


|label     |levels     |         all| index|            fit_id|
|:---------|:----------|-----------:|-----:|-----------------:|
|agege65   |< 65 years | 567 (100.0)|     1| agege65< 65 years|
|          |≥ 65 years | 362 (100.0)|     2| agege65≥ 65 years|
|nodesge3  |< 3        | 470 (100.0)|     3|       nodesge3< 3|
|          |≥ 3        | 441 (100.0)|     4|       nodesge3≥ 3|
|Adherence |No         | 794 (100.0)|     5|   adhere.factorNo|
|          |Yes        | 135 (100.0)|     6|  adhere.factorYes|
> 
> colon_s %>%
+ 	coxphmulti(dependent, explanatory) %>% 
+ 	fit2df() -> t8
> knitr::kable(t8, row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"))


|explanatory       |HR                        |
|:-----------------|:-------------------------|
|agege65≥ 65 years |1.18 (0.98-1.43, p=0.087) |
|nodesge3≥ 3       |2.25 (1.85-2.72, p<0.001) |
|adhere.factorYes  |1.38 (1.08-1.77, p=0.011) |
> 
> # fit_id and explanatory columns should contain terms with the same formatting, as this is what is used to match. 

from finalfit.

ygstats avatar ygstats commented on July 29, 2024

Thank you for your kind reply. Here is my sessionInfo() from R:

> sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Please see below for the way the table is output:

With unicode

      data(colon_s)
   colon_s %<>% 
    	mutate(agege65= factor(if_else(age>=65,"\u2265 65 years","< 65 years")),
              				 nodesge3 = factor(if_else(nodes>=3,"\u2265 3","< 3"))
              	)
  
   colon_s %>%
    	finalfit(dependent, explanatory) -> t6
   knitr::kable(t6, row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"))
     
 |Dependent: Surv(time, status) |           |          HR (univariable)|        HR (multivariable)|
 |:-----------------------------|:----------|-------------------------:|-------------------------:|
 |agege65                       |< 65 years |                         -|                         -|
 |                              |≥ 65 years |                         -|                         -|
 |nodesge3                      |< 3        |                         -|                         -|
 |                              |≥ 3        |                         -|                         -|
 |Adherence                     |No         |                         -|                         -|
 |                              |Yes        | 1.37 (1.07-1.75, p=0.011)| 1.38 (1.08-1.77, p=0.011)|
 |NA                            |NA         | 1.20 (1.00-1.45, p=0.056)| 1.18 (0.98-1.43, p=0.087)|
 |NA                            |NA         | 2.23 (1.84-2.70, p<0.001)| 2.25 (1.85-2.72, p<0.001)|

Debug

      colon_s %>%
     	summary_factorlist(dependent, explanatory, fit_id=TRUE) -> t7
   knitr::kable(t7, row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"))
       
 |label     |levels     |         all| index|            fit_id|
 |:---------|:----------|-----------:|-----:|-----------------:|
 |agege65   |< 65 years | 567 (100.0)|     1| agege65< 65 years|
 |          |≥ 65 years | 362 (100.0)|     2| agege65≥ 65 years|
 |nodesge3  |< 3        | 470 (100.0)|     3|       nodesge3< 3|
 |          |≥ 3        | 441 (100.0)|     4|       nodesge3≥ 3|
 |Adherence |No         | 794 (100.0)|     5|   adhere.factorNo|
 |          |Yes        | 135 (100.0)|     6|  adhere.factorYes|
   colon_s %>%
     	coxphmulti(dependent, explanatory) %>% 
     	fit2df() -> t8
   knitr::kable(t8, row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"))
     
 |explanatory       |HR                        |
 |:-----------------|:-------------------------|
 |agege65= 65 years |1.18 (0.98-1.43, p=0.087) |
 |nodesge3= 3       |2.25 (1.85-2.72, p<0.001) |
 |adhere.factorYes  |1.38 (1.08-1.77, p=0.011) |

It appears that the explanatory and fit_id columns are not matching. I wonder if this is an issue specific to Windows OS.

Thank you again for making this incredibly useful package easily available.

from finalfit.

ewenharrison avatar ewenharrison commented on July 29, 2024

Thanks for this. Yes, the unicode is not showing in the library(survival) output on Windows. I don't have access to experiment.

Can you check that it doesn't work with native library(survival) functions.

> # Check factor levels not correct in `library(survival)` output
> colon_s %<>% 
+ 	mutate(agege65= factor(if_else(age>=65,"\u2265 65 years","< 65 years")),
+ 				 nodesge3 = factor(if_else(nodes>=3,"\u2265 3","< 3"))
+ 	)
> 
> library(survival)
> coxph(Surv(time, status) ~ agege65 + nodesge3 + adhere.factor, data=colon_s)
Call:
coxph(formula = Surv(time, status) ~ agege65 + nodesge3 + adhere.factor, 
    data = colon_s)

                    coef exp(coef) se(coef)    z      p
agege65≥ 65 years 0.1669    1.1816   0.0975 1.71  0.087
nodesge3≥ 3       0.8089    2.2454   0.0982 8.24 <2e-16
adhere.factorYes  0.3238    1.3824   0.1268 2.55  0.011

Likelihood ratio test=78.96  on 3 df, p=<2e-16
n= 911, number of events= 441 
   (18 observations deleted due to missingness)
>
> # This works for me as expected

If it doesn't work, check if it works with glm().

> # Check if works with `glm()`
> glm(mort_5yr ~  agege65 + nodesge3 + adhere.factor, dat=colon_s, family = binomial)

Call:  glm(formula = mort_5yr ~ agege65 + nodesge3 + adhere.factor, 
    family = binomial, data = colon_s)

Coefficients:
      (Intercept)  agege65≥ 65 years        nodesge3≥ 3   adhere.factorYes  
          -1.0209             0.2837             1.1715             0.4729  

Degrees of Freedom: 896 Total (i.e. Null);  893 Residual
  (32 observations deleted due to missingness)
Null Deviance:	    1230 
Residual Deviance: 1151 	AIC: 1159
>
> # This also works for me. 

Does this workaround work for you?

> # Does this work around work?
> data(colon_s)
> colon_s %<>% 
+ 	mutate(agege65= factor(if_else(age>=65,">= 65 years","< 65 years")),
+ 				 nodesge3 = factor(if_else(nodes>=3,">=3","< 3"))
+ 	)
> 
> colon_s %>%
+ 	finalfit(dependent, explanatory) -> t6
> t6
  Dependent: Surv(time, status)                      HR (univariable)        HR (multivariable)
3                       agege65  < 65 years                         -                         -
4                               >= 65 years 1.20 (1.00-1.45, p=0.056) 1.18 (0.98-1.43, p=0.087)
5                      nodesge3         < 3                         -                         -
6                                       >=3 2.23 (1.84-2.70, p<0.001) 2.25 (1.85-2.72, p<0.001)
1                     Adherence          No                         -                         -
2                                       Yes 1.37 (1.07-1.75, p=0.011) 1.38 (1.08-1.77, p=0.011)
> t6[,2] = gsub(">=", "\u2265", t6[,2])
> t6
  Dependent: Surv(time, status)                     HR (univariable)        HR (multivariable)
3                       agege65 < 65 years                         -                         -
4                               ≥ 65 years 1.20 (1.00-1.45, p=0.056) 1.18 (0.98-1.43, p=0.087)
5                      nodesge3        < 3                         -                         -
6                                       ≥3 2.23 (1.84-2.70, p<0.001) 2.25 (1.85-2.72, p<0.001)
1                     Adherence         No                         -                         -
2                                      Yes 1.37 (1.07-1.75, p=0.011) 1.38 (1.08-1.77, p=0.011)

from finalfit.

ygstats avatar ygstats commented on July 29, 2024

Thank you for the prompt and detailed reply. On my windows machine, unicode values do not appear correctly when using both survival() and glm() packages. I appreciate your suggestion to re-check this.

In addition, the fix you provide is quite nifty but does not solve this specific problem. As you can see from the results (I sent before and also shown below), my output table t6 has a different dimension as the estimates are added directly at the bottom (specifically, please note that in the example we are using, my output table t6 has 2 more rows when compared to your table t6).



data(colon_s)
   colon_s %<>% 
    	mutate(agege65= factor(if_else(age>=65,"\u2265 65 years","< 65 years")),
              				 nodesge3 = factor(if_else(nodes>=3,"\u2265 3","< 3"))
              	)
  
explanatory = c("agege65","nodesge3","adhere.factor")
dependent = "Surv(time, status)"


   colon_s %>%
    	finalfit(dependent, explanatory) -> t6

Dependent: Surv(time, status) HR (univariable) HR (multivariable)
agege65 < 65 years - -
≥ 65 years - -
nodesge3 < 3 - -
≥ 3 - -
Adherence No - -
Yes 1.37 (1.07-1.75, p=0.011) 1.38 (1.08-1.77, p=0.011)
NA NA 1.20 (1.00-1.45, p=0.056) 1.18 (0.98-1.43, p=0.087)
NA NA 2.23 (1.84-2.70, p<0.001) 2.25 (1.85-2.72, p<0.001)

So, using gsub() will not sove the problem. For now, I have decided to omit unicode based expression from my code. I will try to play around with this and let you know if I come up with a potential solution. Thank you for your assistance.

from finalfit.

ewenharrison avatar ewenharrison commented on July 29, 2024

Thank you for that.
Have a look at the workaround again.
Run with non-unicode using finalfit, then apply gsub() to the finalfit results to add whatever unicode you wish.
Best wishes.

from finalfit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.