生存中的 R 公式
R formula in survfit
我在公式、环境和 survfit()
方面遇到问题。
lm()
一切正常,但 survfit()
失败。
一般问题陈述:
我正在对一些数据拟合一系列公式。所以,我称
将公式作为变量传递的建模函数。之后,
我想使用拟合对象中的公式。
(以我幼稚的观点,麻烦来自survfit not
记录环境。)
详细示例
lm()
中的预期行为:
library("plyr")
preds <- c("wt", "qsec")
f <- function() {
lm(mpg ~ wt, data = mtcars)
}
fits <- alply(preds, 1, function(pred)
{
modform <- reformulate(pred, response = "mpg")
lm(modform, data = mtcars)
})
fits[[1]]$call$formula
##modform
formula(fits[[1]])
## mpg ~ wt
## <environment: 0x1419d1a0>
即使 fits[[1]]$call$formula
解析为 modform
我也可以
仍然得到 formula(fits[[1]])
.
的原始公式
但是 survfit()
的事情失败了:
library("plyr")
library("survival")
preds <- c("resid.ds", "rx", "ecog.ps")
fits <-
alply(preds, 1, function(pred)
{
modform <- paste("Surv(futime, fustat)", pred, sep = " ~ ")
modform <- as.formula(modform)
print(modform)
fit <- survfit(modform, data = ovarian)
})
fits[[1]]$call$formula
## modform
formula(fits[[1]])
## Error in eval(expr, envir, enclos) : object 'modform' not found
此处(与 lm-fits 相反),formula(fits[[1]])
不
工作!
所以,我的具体问题是:如何取回使用的公式
适合 fits[[1]]
?
问题是,当 x$formula
为 NULL
时,对于 lm
对象,存在获取公式的备份计划; survfit
个对象
不存在
library("plyr")
library("survival")
preds <- c("wt", "qsec")
f <- function() lm(mpg ~ wt, data = mtcars)
fits <- alply(preds, 1, function(pred) {
modform <- reformulate(pred, response = "mpg")
lm(modform, data = mtcars)
})
fits[[1]]$formula
# NULL
可以使用 formula(fits[[1]])
提取公式,它使用 formula
泛型。 formula
的 lm
S3 方法是
stats:::formula.lm
# function (x, ...)
# {
# form <- x$formula
# if (!is.null(form)) {
# form <- formula(x$terms)
# environment(form) <- environment(x$formula)
# form
# }
# else formula(x$terms)
# }
所以当 fits[[1]]$formula
returns NULL
时,forumla.lm
在对象中查找 terms
属性并找到公式
fits[[1]]$terms
survfit
对象没有 x$formula
或 x$terms
,所以 formula(x)
给出错误
preds <- c("resid.ds", "rx", "ecog.ps")
fits <- alply(preds, 1, function(pred) {
modform <- paste("Surv(futime, fustat)", pred, sep = " ~ ")
modform <- as.formula(modform)
fit <- survfit(modform, data = ovarian)
})
fits[[1]]$formula
# NULL
formula(fits[[1]]) ## error
formula(fits[[1]]$terms)
# list()
您可以通过将公式插入调用并对其求值来解决此问题
modform <- as.formula(paste("Surv(futime, fustat)", 'rx', sep = " ~ "))
substitute(survfit(modform, data = ovarian), list(modform = modform))
# survfit(Surv(futime, fustat) ~ rx, data = ovarian)
eval(substitute(survfit(modform, data = ovarian), list(modform = modform)))
# Surv(futime, fustat) ~ rx
# Call: survfit(formula = Surv(futime, fustat) ~ rx, data = ovarian)
#
# n events median 0.95LCL 0.95UCL
# rx=1 13 7 638 268 NA
# rx=2 13 5 NA 475 NA
或手动将公式输入 x$call$formula
fit <- survfit(modform, data = ovarian)
fit$call$formula
# modform
fit$call$formula <- modform
fit$call$formula
# Surv(futime, fustat) ~ rx
fit
# Call: survfit(formula = Surv(futime, fustat) ~ rx, data = ovarian)
#
# n events median 0.95LCL 0.95UCL
# rx=1 13 7 638 268 NA
# rx=2 13 5 NA 475 NA
我在公式、环境和 survfit()
方面遇到问题。
lm()
一切正常,但 survfit()
失败。
一般问题陈述:
我正在对一些数据拟合一系列公式。所以,我称 将公式作为变量传递的建模函数。之后, 我想使用拟合对象中的公式。
(以我幼稚的观点,麻烦来自survfit not 记录环境。)
详细示例
lm()
中的预期行为:
library("plyr")
preds <- c("wt", "qsec")
f <- function() {
lm(mpg ~ wt, data = mtcars)
}
fits <- alply(preds, 1, function(pred)
{
modform <- reformulate(pred, response = "mpg")
lm(modform, data = mtcars)
})
fits[[1]]$call$formula
##modform
formula(fits[[1]])
## mpg ~ wt
## <environment: 0x1419d1a0>
即使 fits[[1]]$call$formula
解析为 modform
我也可以
仍然得到 formula(fits[[1]])
.
但是 survfit()
的事情失败了:
library("plyr")
library("survival")
preds <- c("resid.ds", "rx", "ecog.ps")
fits <-
alply(preds, 1, function(pred)
{
modform <- paste("Surv(futime, fustat)", pred, sep = " ~ ")
modform <- as.formula(modform)
print(modform)
fit <- survfit(modform, data = ovarian)
})
fits[[1]]$call$formula
## modform
formula(fits[[1]])
## Error in eval(expr, envir, enclos) : object 'modform' not found
此处(与 lm-fits 相反),formula(fits[[1]])
不
工作!
所以,我的具体问题是:如何取回使用的公式
适合 fits[[1]]
?
问题是,当 x$formula
为 NULL
时,对于 lm
对象,存在获取公式的备份计划; survfit
个对象
library("plyr")
library("survival")
preds <- c("wt", "qsec")
f <- function() lm(mpg ~ wt, data = mtcars)
fits <- alply(preds, 1, function(pred) {
modform <- reformulate(pred, response = "mpg")
lm(modform, data = mtcars)
})
fits[[1]]$formula
# NULL
可以使用 formula(fits[[1]])
提取公式,它使用 formula
泛型。 formula
的 lm
S3 方法是
stats:::formula.lm
# function (x, ...)
# {
# form <- x$formula
# if (!is.null(form)) {
# form <- formula(x$terms)
# environment(form) <- environment(x$formula)
# form
# }
# else formula(x$terms)
# }
所以当 fits[[1]]$formula
returns NULL
时,forumla.lm
在对象中查找 terms
属性并找到公式
fits[[1]]$terms
survfit
对象没有 x$formula
或 x$terms
,所以 formula(x)
给出错误
preds <- c("resid.ds", "rx", "ecog.ps")
fits <- alply(preds, 1, function(pred) {
modform <- paste("Surv(futime, fustat)", pred, sep = " ~ ")
modform <- as.formula(modform)
fit <- survfit(modform, data = ovarian)
})
fits[[1]]$formula
# NULL
formula(fits[[1]]) ## error
formula(fits[[1]]$terms)
# list()
您可以通过将公式插入调用并对其求值来解决此问题
modform <- as.formula(paste("Surv(futime, fustat)", 'rx', sep = " ~ "))
substitute(survfit(modform, data = ovarian), list(modform = modform))
# survfit(Surv(futime, fustat) ~ rx, data = ovarian)
eval(substitute(survfit(modform, data = ovarian), list(modform = modform)))
# Surv(futime, fustat) ~ rx
# Call: survfit(formula = Surv(futime, fustat) ~ rx, data = ovarian)
#
# n events median 0.95LCL 0.95UCL
# rx=1 13 7 638 268 NA
# rx=2 13 5 NA 475 NA
或手动将公式输入 x$call$formula
fit <- survfit(modform, data = ovarian)
fit$call$formula
# modform
fit$call$formula <- modform
fit$call$formula
# Surv(futime, fustat) ~ rx
fit
# Call: survfit(formula = Surv(futime, fustat) ~ rx, data = ovarian)
#
# n events median 0.95LCL 0.95UCL
# rx=1 13 7 638 268 NA
# rx=2 13 5 NA 475 NA