I have a matrix of B rows and p columns to create, namely "beta_bootstrap". Now I am running a "for" loop for each c running from 1 to B. Each row at each iteration will finally store a vector of p components after some convex optimization with respect to that variable namely "v3". The optimized vector is named as "final_boot1". In some iterations, say "c-th" iteration ,after optimization, this vector returns as "NA" values for each p components.

Here comes my problem and what my intention is to execute. I want to omit that "c-th" iteration, want "c will be c-1", the "for" loop is terminated for that iteration, it will again go to start of that "for" loop, consider starting for "c-th" iteration. I entirely want to do this not to encounter any row containing "NA". Also optimization remains fruitful

How to re-run that "for" loop from "c-th" iteration again if I am terminating at "c"?

Towards that I did the following code. But later I realised "next" argument won't help my problem because it will entirely skip that "c-th" iteration and start from"c+1" afresh. So my code is flawed in that sense what I specifically require. I can't find what to do. Please help. Any notations you find difficult to understand, please let me know. Here is my code....

library(CVXR). ##for convex optimization

B=50 #bootstrap iterations to be made

beta_bootstrap = matrix(NA,B,p) # collect B copies of bootstrap estimate of lasso estimator

for(c in 1:B)
{
   G_boot = matrix(NA,1,n)
   for (j in 1:n)
   {
      G_boot[1,j]= rexp(1,1) ## extract n iid exp(1) r.v for each stage optimization in objective function.
   }

G_boot 

#perturbation quantities from known density Exp(1), n copies at each stage, repeat B times

## Next we do Bootstrap convex optimization though CVXR function

v3 = CVXR::Variable(p) 

##declaration: bootstrap estimate wrt which objective function is optimized

penalty_boot = (p_norm(v3,1))*lambda_opt ## penalty term in the objective fn through p_norm syntax.

##lambda_opt is a known scalar

zz= t(y*G_boot) ## a col vector consisting of component wise product of y and G_boot. Also * does component wise product and return a vector

l1= -sum((z%*%v3)*zz) ## %*% returns actually a scalar product

zz1= as.matrix(y-P_tilde) ## y, P_tilde we know beforehand 

l2= sum((z%*%v3)*zz1) 

zz2= t(G_boot) ## col vector G_boot

l3 = sum((logistic(z%*%v3))*zz2) ## logistic(z) is an atom for log(1+exp z) in cvxr

obj_boot = penalty_boot+l1+l2+l3   ## objective function for optimization 

prob_boot = Problem(Minimize(obj_boot)) ## minimisation of objective function

resultB = solve(prob_boot)
resultB

final_boot1 = resultB$getValue(v3)

final_boot1=as.vector(final_boot1)  ##final_boot1 giving NA values sometimes, which is my concern

##I know this if statement is flawed. Please help.

if(any(is.na(final_boot1))==TRUE)
{
   c=c-1
   next
}
final_boot2=as.matrix(final_boot1)

final_boot2

final_boot3 = t(final_boot2)

final_boot3 ## Minimizer Bootstrap estimator

beta_bootstrap[c,]= final_boot3  ## At c-th stage we store that bootstrap estimator of p components

}
beta_bootstrap ##This matrix is important to construct Bootstrap Percentile Intervals

Please help.

1

There are 1 best solutions below

0
CPB On

It isn't possible to change the for loop variable after the loop has started. You will need a different approach, maybe building on a structure like:

for (i in 1:N){
  inner <- FALSE
  while (!inner){
    [...the code you want to repeat...]
    if (good) inner <- TRUE
  }
}

From the help ?'for':

The seq in a for loop is evaluated at the start of the loop; changing it subsequently does not affect the loop. If seq has length zero the body of the loop is skipped. Otherwise the variable var is assigned in turn the value of each element of seq. You can assign to var within the body of the loop, but this will not affect the next iteration. When the loop terminates, var remains as a variable containing its latest value.

https://stackoverflow.com/a/5913329/4413615