English 中文(简体)
如何在“为了”住所终止后,通过在R-方案规划中检查某些“如果”条件,从同一频率中重新应用?
原标题:How to re-run from the same iteration in "for" loop after terminating it by checking some "if" condition inside that "for" loop in R- programming?

我有一张布行和专栏的矩阵图,用于制造“间_”。 现在,我为每个从1个到B条的人开了一个“路”。 每一电离层的每行都将在“v3”这一变数的轮优化之后,最终储存成形元素的病媒。 优化的病媒被称作“终端_1”。 在一些分析中,如“c-th”的蒸发,在优化之后,该病媒作为每一组成部分的“NA”数值回归。

这里涉及我的问题和我打算执行的内容。 我想指出,“c-th”的循环,希望“c will be c-1”,而“for”的循环因为循环而终止,它将再次开始“for” loop,考虑开始“c-th”循环。 我完全不想碰到任何含有“NA”的行文。 优化工作仍然富有成果

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

为此,我采用了以下法典。 但是,后来,我认识到“ext”论点无助于我的问题,因为它将完全ski笑“c-th”的活力,并从“c+1”开始。 因此,从这个意义上讲,我的准则存在缺陷,我特别要求这样做。 我找不到什么。 请提供帮助。 各位难以理解的任何消息,请让我知道。 这里是我的法典。

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. 请提供帮助。

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

请提供帮助。

问题回答

lo虫开始后不可能改变 lo状变量。 你们需要一种不同的办法,或许可以在这样一种结构的基础上再接再厉:

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

:

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





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签