library(dplyr)
library(gaussquad)
library(nleqslv)
library(MASS)
n=200
lqrule64 <- legendre.quadrature.rules(64)[[64]]
simdata <- function( beta ) {
cen=1
nstep=20
Sigmat_z <- exp(-abs(outer(1:nstep, 1:nstep, "-")) / nstep)
z <- 2*(pnorm(c(mvrnorm( 1, rep(0,20), Sigmat_z )))-0.5)
left_time_points <- (0:(nstep - 1)) / nstep
z_fun <- stepfun(left_time_points, c(0,z ))
h_fun <- function(x) { beta * z_fun(x) }
lam_fun <- function(tt) 2 * exp(h_fun(tt))
u <- runif(1)
fail_time <- nleqslv(0, function(ttt)
legendre.quadrature(lam_fun, lower = 0,upper = ttt, lqrule64) + log(u))$x
X <- min(fail_time, cen)
obs=rpois(1, 5)+1
tt= sort(runif(obs, min = 0, max = 1))
obs_times <- tt[which(tt<=cen)]
if (length(obs_times) == 0)
obs_times <- cen
covariates_obscov <-z_fun(obs_times)
return( tibble(X = X,delta = fail_time < cen,
covariates = covariates_obscov,obs_times = obs_times, censoring = cen ) )
}
beta=1
data <- replicate( n, simdata( beta ), simplify = FALSE ) %>% bind_rows(.id = "id")
trans.haz(data,n,3,3,1,s=0,n^(-0.35))
Run the code above in your browser using DataLab