如何获取survdiff返回的p值

yhived7q  于 2023-01-15  发布在  其他
关注(0)|答案(3)|浏览(183)

我用的是R生存包,survdiff函数。我想知道如何从返回值中得到p值。

> diff = survdiff(Surv(Time, Censored) ~ Treatment+Gender, data = dat)
> diff
Call:
survdiff(formula = Surv(Time, Censored) ~ Treatment + Gender, 
    data = dat)

                            N Observed Expected (O-E)^2/E (O-E)^2/V
Treatment=Control, Gender=M 2        1     1.65  0.255876  0.360905
Treatment=Control, Gender=F 7        3     2.72  0.027970  0.046119
Treatment=IND, Gender=M     5        2     2.03  0.000365  0.000519
Treatment=IND, Gender=F     6        2     1.60  0.100494  0.139041

 Chisq= 0.5  on 3 degrees of freedom, p= 0.924

我想用一些函数得到p值0.924,谢谢。

iq3niunx

iq3niunx1#

p值不存储在survdiff类中,因此必须在输出时动态计算。要重现p值,可以使用chisq分布函数:“pchisq”

diff = survdiff(Surv(Time, Censored) ~ Treatment+Gender, data = dat)
pchisq(diff$chisq, length(diff$n)-1, lower.tail = FALSE)
fquxozlt

fquxozlt2#

函数print.survdiff中显示p值的代码为:

cat("\n Chisq=", format(round(x$chisq, 1)), " on", df, 
            "degrees of freedom, p=", format(signif(1 - pchisq(x$chisq, 
                df), digits)), "\n")

引导它的代码:

if (is.matrix(x$obs)) {
            otmp <- apply(x$obs, 1, sum)
            etmp <- apply(x$exp, 1, sum)
        }         else {
            otmp <- x$obs
            etmp <- x$exp
        }
        df <- (sum(1 * (etmp > 0))) - 1

并且参数列表中的“digits”设置为3,因此使用surv.diff帮助页面上的示例:

x <- survdiff(Surv(time, status) ~ pat.karno + strata(inst), data=lung) 
cat( "p=", format(signif(1 - pchisq(x$chisq, 
                 df), digits)) )
#p= 0.00326

回应评论:在该示例中,第二码块简化为:

df <- with(x,    (sum(1 * (apply(x$exp, 1, sum) > 0))) - 1 )
> df
[1] 7
ncgqoxb0

ncgqoxb03#

使用broom包中的glance()函数,可以非常容易地获得p.value。

diff = survdiff(Surv(Time, Censored) ~ Treatment+Gender, data = dat)

broom::glance(diff)$p.value

相关问题