簡體   English   中英

你如何操縱R的cuminc函數的輸出

[英]How do you manipulate the output from R's cuminc function

例如,在控制台中,

> x=cuminc(ftime=c(1,2,3,4,5,6,5,4),c(1,0,0,1,1,0,0,0),0)
> x
Estimates and Variances:
$`est`
        1     2     3   4         5
0 1 0.125 0.125 0.125 0.3 0.5333333

$var
       1        2        3        4      5
0 1 0.015625 0.015625 0.015625 0.040625 0.0725

我如何獲得'est'數據?

如果我們檢查str(x) ,它是一個嵌套列表

library(cmprsk)
str(x)
#List of 1
# $ 0 1:List of 3
#  ..$ time: num [1:8] 0 1 1 4 4 5 5 6
#  ..$ est : num [1:8] 0 0 0.125 0.125 0.3 ...
#  ..$ var : num [1:8] 0 0 0.0156 0.0156 0.0406 ...
# - attr(*, "class")= chr "cuminc"

[[然后使用$[[提取'est'元素)提取list

x[[1]]$est
#[1] 0.0000000 0.0000000 0.1250000 0.1250000 0.3000000 0.3000000 0.5333333 0.5333333

如果我們想獲得與打印估算中相同的輸出

d1 <- data.frame(x[[1]][1:2])
library(dplyr)
library(tidyr)
d1 %>% 
  distinct(est, .keep_all = TRUE) %>% 
  filter(time > 0) %>%
  complete(time = full_seq(c(min(time), max(time)), 1)) %>% 
  fill(est) %>% 
  pull(est)
#[1] 0.1250000 0.1250000 0.1250000 0.3000000 0.5333333

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM