Using package::function inside formula in R gives different results (specifically survival::strata inside coxph)

huangapple go评论199阅读模式
英文:

Using package::function inside formula in R gives different results (specifically survival::strata inside coxph)

问题

I believed the following two formula calls should be equivalent.

  1. install.packages("survival")
  2. library(survival)

Model 1

  1. coxph(Surv(futime, fustat) ~ age + strata(rx), data=ovarian)
  2. Call:
  3. coxph(formula = Surv(futime, fustat) ~ age + strata(rx), data = ovarian)
  4. coef exp(coef) se(coef) z p
  5. age 0.13735 1.14723 0.04741 2.897 0.00376
  6. Likelihood ratio test=12.69 on 1 df, p=0.0003678
  7. n= 26, number of events= 12

Model 2

  1. coxph(Surv(futime, fustat) ~ age + survival::strata(rx), data=ovarian)
  2. Call:
  3. coxph(formula = Surv(futime, fustat) ~ age + survival::strata(rx),
  4. data = ovarian)
  5. coef exp(coef) se(coef) z p
  6. age 0.14733 1.15873 0.04615 3.193 0.00141
  7. survival::strata(rx)rx=2 -0.80397 0.44755 0.63205 -1.272 0.20337
  8. Likelihood ratio test=15.89 on 2 df, p=0.0003551
  9. n= 26, number of events= 12

However, they are not. The second model, which references 'strata' specifically using the "::" operator, treats it like a fixed effect variable in the model, rather than fitting the model separately within levels of 'strata'. I do not know if this is:

A) An issue with using 'package::function' type calls within a formula in R, or
B) An issue with the survival package and namespaces/dependencies

I want to use the call to Model 2, as I am building a package and don't want to have to attach the entire survival package (which is large) when loading mine.

Any help on this would be much appreciated.

英文:

I believed the following two formula calls should be equivalent.

  1. install.packages("survival")
  2. library(survival)

Model 1

  1. coxph(Surv(futime, fustat) ~ age + strata(rx), data=ovarian)
  2. Call:
  3. coxph(formula = Surv(futime, fustat) ~ age + strata(rx), data = ovarian)
  4. coef exp(coef) se(coef) z p
  5. age 0.13735 1.14723 0.04741 2.897 0.00376
  6. Likelihood ratio test=12.69 on 1 df, p=0.0003678
  7. n= 26, number of events= 12

Model 2

  1. coxph(Surv(futime, fustat) ~ age + survival::strata(rx), data=ovarian)
  2. Call:
  3. coxph(formula = Surv(futime, fustat) ~ age + survival::strata(rx),
  4. data = ovarian)
  5. coef exp(coef) se(coef) z p
  6. age 0.14733 1.15873 0.04615 3.193 0.00141
  7. survival::strata(rx)rx=2 -0.80397 0.44755 0.63205 -1.272 0.20337
  8. Likelihood ratio test=15.89 on 2 df, p=0.0003551
  9. n= 26, number of events= 12

However they are not. The second model, which references 'strata' specifically using the "::" operator, treats it like a fixed effect variable in the model, rather than fitting the model separately within levels of 'strata'. I do not know if this is:

A) An issue with using 'package::function' type calls within a formula in R, or
B) An issue with the survival package and namespaces/dependencies

I want to use the call to Model 2, as I am building a package and don't want to have to attach the entire survival package (which is large) when loading mine.

Any help on this would be much appreciated.

答案1

得分: 3

In coxph(),这里有这些行:

  1. special <- c("strata", "tt", "frailty", "ridge", "pspline")
  2. tform$formula <- if (missing(data))
  3. terms(formula, special)
  4. else terms(formula, special, data = data)

因此,在第一种情况下,coxph 正确检测到了 strata,但在第二种情况下没有。我们可以使用您的示例来验证这一点:

  1. library(survival)
  2. formula1 <- Surv(futime, fustat) ~ age + strata(rx)
  3. formula2 <- Surv(futime, fustat) ~ age + survival::strata(rx)
  4. special <- c("strata", "tt", "frailty", "ridge", "pspline")
  5. data <- ovarian
  6. t1 <- terms(formula1, special, data = data)
  7. t2 <- terms(formula2, special, data = data)
  8. attr(t1, "specials")$strata
  9. #> [1] 3
  10. attr(t2, "specials")$strata
  11. #> NULL

虽然我不了解 coxph() 的具体细节,但我会说这种差异后来导致了结果的不同。

因此,您应该避免在公式中使用前缀 survival::。如果由于某种原因,您必须使用这个前缀,可以使用以下技巧:

  1. library(survival)
  2. # 在这里
  3. strata <- survival::strata
  4. coxph(Surv(futime, fustat) ~ age + strata(rx), data=ovarian)
  5. #> Call:
  6. #> coxph(formula = Surv(futime, fustat) ~ age + strata(rx), data = ovarian)
  7. #>
  8. #> coef exp(coef) se(coef) z p
  9. #> age 0.13735 1.14723 0.04741 2.897 0.00376
  10. #>
  11. #> Likelihood ratio test=12.69 on 1 df, p=0.0003678
  12. #> n= 26, number of events= 12
英文:

In coxph(), there are these lines:

  1. special <- c("strata", "tt", "frailty", "ridge", "pspline")
  2. tform$formula <- if (missing(data))
  3. terms(formula, special)
  4. else terms(formula, special, data = data)

Therefore, in the first case, coxph correctly detects the strata but not in the second case. We can see this using your example:

  1. library(survival)
  2. formula1 <- Surv(futime, fustat) ~ age + strata(rx)
  3. formula2 <- Surv(futime, fustat) ~ age + survival::strata(rx)
  4. special <- c("strata", "tt", "frailty", "ridge", "pspline")
  5. data <- ovarian
  6. t1 <- terms(formula1, special, data = data)
  7. t2 <- terms(formula2, special, data = data)
  8. attr(t1, "specials")$strata
  9. #> [1] 3
  10. attr(t2, "specials")$strata
  11. #> NULL

While I don't know the specifics of coxph(), I would say that this difference later leads to the differences in results.


Therefore, you should avoid using the prefix survival:: in the formula. If for some reason, you have to use this prefix, you can use the following trick:

  1. library(survival)
  2. # here
  3. strata <- survival::strata
  4. coxph(Surv(futime, fustat) ~ age + strata(rx), data=ovarian)
  5. #> Call:
  6. #> coxph(formula = Surv(futime, fustat) ~ age + strata(rx), data = ovarian)
  7. #>
  8. #> coef exp(coef) se(coef) z p
  9. #> age 0.13735 1.14723 0.04741 2.897 0.00376
  10. #>
  11. #> Likelihood ratio test=12.69 on 1 df, p=0.0003678
  12. #> n= 26, number of events= 12

huangapple
  • 本文由 发表于 2023年5月11日 17:01:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定