Estimate the tuning parameter in Ridge logistic regression

I need to know how can we estimate the tuning parameter in penalized likelihood?

I write my own code but there is a mistake I could not find it.!! Could you please help, I do my best to make the code clear..

Given the likelihood that

$l(\beta)= \sum_i y_i log (\pi_i)+(1-y_i)log (1-\pi) -1/2 \lambda \sum_j {\beta_j}^2$

where $\pi_i= logit^{-1} (x\beta$) and $y_i$ belong to (0,1)

I need to chose the lambda using cross validation ; my question is that

• I need to include the Code B inside Code A. then I can run the code.
• is the code correct or no. Is there any way to make it faster.\

Code A : The Cross Validation to choose $\lambda$

   PE=c()
lambda=c(0.005,0.001,0.1,0.5,0.2)

for(j in 1:length(lambda)){
print(lambda[j])
tem.PE=c()
for(i in 1:2014){
cv.y=y[-i]
cv.x=x[-i,]
tem.lambda=lambda[j]
## I need to put the function /WNRfit/  here to calculate the Coefficients -Betas-