[英]In R, how do I plot a Weibull probability density function for right censored data using package fitdistrplus?
[英]Generating Right Censored Copula Data using R
我正在使用以下代碼來生成正確的審查生存數據。 但是,每當我運行此代碼時,都會出現錯誤,並且我無法打印數據框,因為R表示“沒有找到'dat'對象”。
我不知道這是什么問題,請幫我解決這個問題!
代碼如下:
library(methods)
library(copula)
library(muhaz)
library(foreach)
library(survival)
library (MASS)
tau = 0.2
nsample = 100
err_dist = "Normal"
generate.dat <- function(tau,nsample,err_dist)
{
C <- rexp(nsample, rate = 0.8) # Censoring
cc <- claytonCopula(copClayton@iTau(tau))
u <- rCopula(nsample, cc)
e <- qnorm(u,mean=0,sd=1) # Error term
b1 <- 0.5 ; b2 <- 0.2 # using the old betas
beta <- matrix(c(b1, 0, 0, b2), 2) # as Beta got to be 2x2
RHO <- 0 # Indpendence
MU1 <- 0; S1 <- 1 # Std. Normal
MU2 <- 0; S2 <- 1 # Std. Normal
MU <- c(MU1, MU2) # mean vector
SIGMA <- matrix (c(S1^2, S1*S2*RHO, S1*S2*RHO, S2^2), 2) # Var-Covar matrix
Z <- mvrnorm(nsample, mu=MU, Sigma=SIGMA)
T <- exp(-Z%*%beta + e)
T1 <- T[,1]
T2 <- T[,2]
x1 <- pmin(T1,C)
x2 <- pmin(T2,C);
d1 <- ifelse(T1<=C, 1, 0)
d2 <- ifelse(T2<=C, 1, 0)
dat <- data.frame(x1, x2, d1, d2)
return(dat)
print(dat)
}
我運行了您的代碼,沒有任何錯誤。 您是否正在運行這樣的功能?
dat = generate.dat(tau, nsample, err_dist)
如果您希望函數每次都打印dat
,則將print
語句放在return
之前(通常, return
語句應始終是函數中的最后一件事)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.