Guest User

Untitled

a guest
Feb 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. library(glmnet)
  2.  
  3. n=500
  4.  
  5. p=30
  6.  
  7. nzc=trunc(p/10)
  8.  
  9. x=matrix(rnorm(n*p),n,p)
  10.  
  11. beta=rnorm(nzc)
  12.  
  13. fx=x[,seq(nzc)]%*%beta/3
  14.  
  15. hx=exp(fx)
  16.  
  17. ty=rexp(n,hx)
  18.  
  19. tcens=rbinom(n=n,prob=.3,size=1)
  20.  
  21. y=cbind(time=ty,status=1-tcens)
  22.  
  23. NFOLD = 5
  24.  
  25. foldid=sample(rep(seq(NFOLD),length=n))
  26.  
  27. fit1_cv = cv.glmnet(x,y,family="cox",foldid=foldid, thresh = 1e-16)
  28.  
  29. fit1_cv$lambda.min
  30.  
  31. library(penalized)
  32.  
  33. cv.penal <- optL1(Surv(y[, 'time'], y[, 'status']), penalized = x, fold = foldid)
  34.  
  35. cv.penal$lambda
  36.  
  37. cv.penal$fullfit@penalized
  38.  
  39. fit1_cv$cvm
  40.  
  41. whichLambda <- which( fit1_cv$lambda ==
  42.  
  43. fit1_cv$lambda.min )
  44.  
  45. fit1_cv$lambda.min, thresh = 1e-16)
  46.  
  47. res <- cbind(CV.GLMNET =
  48.  
  49. fit1_cv$
  50. glmnet.fit
  51.  
  52. $
  53. beta [, whichLambda],
  54.  
  55. 'GLMNET.NAIF' = as.numeric(fit$beta),
  56.  
  57. 'penalized' = cv.penal$fullfit@penalized)
  58.  
  59. res
Add Comment
Please, Sign In to add comment