【问题标题】:R - trying to have the output for survdiff come out in my latex pdf file in SweaveR - 试图让 survdiff 的输出出现在我的 Sweave 的乳胶 pdf 文件中
【发布时间】:2016-07-10 13:16:16
【问题描述】:

我正在尝试使用 R、Sweave 和 Latex 将我的 survdiff 打印到 pdf 中。

我正在复制下面的所有相关代码,但这是我需要打印出来的部分:

        N Observed Expected (O-E)^2/E (O-E)^2/V
male=0 10        3     8.57     3.624      4.67
male=1 65       48    42.43     0.732      4.67

 Chisq= 4.7  on 1 degrees of freedom, p= 0.0306 

这里的代码几乎可以让我到达我想要的地方:

s.male <- survfit( Surv(futime, status)~ strata(male), shou3, conf.type="log-log")
plot(s.male, lty=c(1,3), xlab="Time", ylab="Survival Probability")
legend(30, 1.1, c("Female","Male") 
       , lty=c(1,3) )
sma <- print.data.frame(survdiff(Surv(futime, status) ~ male
         , data=shou3, rho=0))
sma2 <- print.data.frame(sma, digits = 2)

> sma2
Call:
survdiff(formula = Surv(futime, status) ~ male, data = shou3, 
    rho = 0)

        N Observed Expected (O-E)^2/E (O-E)^2/V
male=0 10        3     8.57     3.624      4.67
male=1 65       48    42.43     0.732      4.67

 Chisq= 4.7  on 1 degrees of freedom, p= 0.0306 

这就是我正在尝试但没有做任何事情的所有内容。

<<results=tex, echo=FALSE>>=
latex(print(sma), file='', landscape=TRUE, round=1)
#latex(sma2, file = " ")
#latex(print.data.frame(sma2, cdec = 2, file=''))
#malesd <- survdiff(as.formula(sma$call$formula))
#latex(print(sma2, latex=TRUE, coefs=FALSE, file = " "))
#latex(sma2, file = " ")
 #     , cdec=c(0,0,2,2,2), na.blank=TRUE, booktabs=TRUE, table.env=FALSE, center="none", file="", title="")
@

我似乎无法从对象中取出 (OE)^2/E (OE)^2/V 元素,也无法取出我想要的 Chisq 线(我试过 cbind /rbind)。我正在使用 R Studio,它是一个 .rnw 文件。

【问题讨论】:

    标签: r pdf latex sweave


    【解决方案1】:

    我有两个解决方案。两者都不是很优雅。

    这是一些示例数据。

    n <- 1000
    set.seed(731)
    age <- 50 + 12*rnorm(n)
    label(age) <- "Age"
    sex <- factor(sample(c('Male','Female'), n,
              rep=TRUE, prob=c(.6, .4)))
    cens <- 15*runif(n)
    h <- .02*exp(.04*(age-50)+.8*(sex=='Female'))
    dt <- -log(runif(n))/h
    label(dt) <- 'Follow-up Time'
    status <- ifelse(dt <= cens, 1, 0)
    futime <- pmin(dt, cens)
    # Check it out:
    survfitObj <- survfit( Surv(futime, status)~ strata(sex), conf.type="log-log")
    plot(survfitObj, lty=c(1,3), xlab="Time", ylab="Survival Probability")
    survdiffObj <- survdiff(Surv(futime, status) ~ sex, rho=0)
    

    解决方案 1:不要尝试在 Latex 中输出结果,而是使用原始输出:

    <<results=verbatim, echo=FALSE>>=
    survdiffObj
    @
    

    解决方案 2:“手工”制作表格

    <<>>=
    tabl <- data.frame(N = survdiffObj$n,
       Observed = survdiffObj$obs,
       Expected = survdiffObj$exp,
       col5 = (survdiffObj$obs - survdiffObj$exp)^2/survdiffObj$exp,
       col6 = (survdiffObj$obs - survdiffObj$exp)^2/diag(survdiffObj$var))
    latex(tabl, file = TRUE)
    @
    

    这仍然需要格式化。

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      相关资源
      最近更新 更多