0

我正在执行将 coxph 模型拟合到 1000 次重复的模拟。下面的代码使用 for 循环将数据从一个复制品拟合到另一个复制品。是否可以一次将 coxph 拟合到所有 1000 个复制的数据集,并在使用 for 循环的情况下为每个数据集保留单独的结果?

# baseline hazard: Weibull

# N = sample size    
# lambda = scale parameter in h0()
# rho = shape parameter in h0()
# beta = fixed effect parameter
# rateC = rate parameter of the exponential distribution of C
library(survival)
simulWeib <- function(N, lambda, rho, beta, rateC)
{
  # covariate --> N Bernoulli trials
  x <- sample(x=c(0, 1), size=N, replace=TRUE, prob=c(0.5, 0.5))
  
  # Weibull latent event times
  v <- runif(n=N)
  Tlat <- (- log(v) / (lambda * exp(x * beta)))^(1 / rho)
  
  # censoring times
  C <- rexp(n=N, rate=rateC)
  
  # follow-up times and event indicators
  time <- pmin(Tlat, C)
  status <- as.numeric(Tlat <= C)
  
  # data set
  data.frame(id=1:N,
             time=time,
             status=status,
             x=x)
}
r<-1e3 #Number of replicates
dat<- replicate(r,  simulWeib(N=2000, lambda=0.1, rho=1, beta=-0.6, rateC=0.001), simplify=FALSE)

betaHat <- rep(NA, 1e3)
for(i in 1:1e3)
{
  fit <- coxph(Surv(time, status) ~ x, data=dat[[i]])
  betaHat[i] <- fit$coef
}
 betaHat
4

0 回答 0