如何从R中的glmnet对象中提取基线危险函数h0(t)?

时间:2017-03-17 03:13:45

标签: r glmnet cox-regression hazard

从glmnet对象

中提取基线危险函数h0(t)

我想知道时间t的危险函数> h(t,X)= h0(t)exp [Σβi* Xi]。如何从R?

中的glmnet对象中提取基线危险函数h0(t)

我所知道的是,生存包中的函数“basehaz()”只能从coxph对象中提取基线危险函数。

我还找到了一个函数glmnet.basesurv(time, event, lp, times.eval = NULL, centered = FALSE)。但是当我尝试使用此功能时,会出现错误。

  

错误:无法找到函数“glmnet.basesurv”

下面是我的代码,使用glmnet拟合cox模型并获得所选变量的系数。是否有可能从此glmnet对象获得基线危险函数h0(t)?

代码

    # Split data into training data and testing data 
    set.seed(101) 
    train_ratio = 2/3
    sample <- sample.int(nrow(x), floor(train_ratio*nrow(x)), replace = F)
    x.train <- x[sample, ]
    x.test <- x[-sample, ]
    y.train <- y[sample, ]
    y.test <- y[-sample, ]

    surv_obj <- Surv(y.train[,1],y.train[,2]) 

    # 
    my_alpha = 0.5

    fit = glmnet(x = x.train, y = surv_obj, family = "cox",alpha = my_alpha)  # fit the model with elastic net method
    plot(fit,xvar="lambda", main="cox model coefficient paths(glmnet.fit)\n\n") # Plot the paths for the fit
    fit 

    # cross validation to find out best lambda
    cv_fit = cv.glmnet(x = x.train,y = surv_obj , family = "cox",nfolds = 10,alpha = my_alpha) 

    tencrossfit <- cv_fit$glmnet.fit 
    plot(cv_fit, main="Cross-validated Deviance(10 folds cv.glmnet.fit)\n\n") 

    plot(tencrossfit, main="cox model coefficient paths(10 folds cv.glmnet.fit)\n\n")

    max(cv_fit$cvm)
    summary(cv_fit$cvm)
    cv_fit$lambda.min
    cv_fit$lambda.1se

    coef.min = coef(cv_fit, s = "lambda.1se")

    pred_min_value2 <- predict(cv_fit, s=cv_fit$lambda.min, newx=x.test,type="link")

我非常感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:0)

type="button"函数是hdnom包的一部分(在CRAN上可用),而不是glmnet本身。所以安装它,然后调用它。

答案 1 :(得分:0)

我有类似的问题,并且在安装hdnom install.packages("hdnom")之后,如果您在功能列表library(help = "hdnom")中进行检查 您会看到该函数实际上是glmnet_survcurve()。我以hdnom:::glmnet_survcurve()的身份工作,示例在这里:

S <- Surv(data$survtimed, data$outcome)
X_glm<-model.matrix(S~.,data[, c("factor1", "factor2")])
cox_model <- glmnet(X_glm, S, family="cox", alpha=1, lambda=0.2)
times = c (1,2) #for predict of survival and 
linearpredictors at times = 1 and 2
predictions = hdnom:::glmnet_survcurve(cox_model, S[,1], S[,2], X_glm, survtime = times)
predictions$p[,1] #survival probability at time 1