pbreheny / ncvreg

Regularization paths for SCAD- and MCP-penalized regression models
http://pbreheny.github.io/ncvreg
41 stars 28 forks source link

loss function of cox model #42

Closed EQUIWDH closed 1 year ago

EQUIWDH commented 1 year ago

Hello, I am wondering if fit$loss returned by ncvsurv represents only the cox -2*logL calculating by estImated $\beta$ or the -2logL + penalty form ,thanks!

EQUIWDH commented 1 year ago

Sorry for disturbing, I am using the loss function structure provided by loss.R which is

loss<- function(x,beta,y ,total=TRUE) {
  eta = x%*%beta
  ind <- order(y[,1])
  d <- as.double(y[ind,2])
  if (is.matrix(eta)) {
    eta <- eta[ind, , drop=FALSE]
    r <- apply(eta, 2, function(x) rev(cumsum(rev(exp(x)))))
  } else {
    eta <- eta[ind]
    r <- rev(cumsum(rev(exp(eta))))
  }
  if (total) {
    return(-2*(crossprod(d, eta) - crossprod(d, log(r))))
  } else { 
    return(-2*(eta[d==1,] - log(r)[d==1,]))
  }
}

compareloss(fit$X,fit$beta,train$y) and fit$loss, I find they don't correspond as the picture showing

EQUIWDH commented 1 year ago

test