
medcoeffit <- function(dfi, penalty = "lasso", alpha = 1, family = "binomial") {
  
  p <- ncol(dfi$data) - 1
  n <- nrow(dfi$data)
  
  beta <- matrix(nrow = dfi$m, ncol = p+1)
  
  for(i in 1:dfi$m) {
    df_c <- mice::complete(dfi,i)
    
    y <- as.numeric(as.character(df_c[,p+1]))
    X <- df_c[,-(p+1)]
    
    fit <- ncvreg::cv.ncvreg(X,  y, family = family, penalty = penalty, alpha = alpha)
    beta[i,] <- coef(fit, which = which.min((fit$cve)))
  }
  
  beta_hat <- apply(beta, 2, median)
  beta_hat
}
