Skip to content
Prev 314712 / 398502 Next

Iterative loop using "repeat"

On 01/05/2013 10:32 PM, mary wrote:
Hi mary,
There are a few different ways to iterate this. One is recursion, where 
the function calls itself until the desired precision is obtained:

robustm<-function(x,z) {
  # not a good idea to use the function name for the return value
  eigenx<-eigen(x)
  d<-madmatrix(z)
  eigenc<-eigen$vectors
  q<-d%*%eigenc
  invQ<-matrix.inverse(q)
  sZ<-mdefpos(x,invQ)
  madZ<-madmatrix(sZ)
  S_X<-q%*%(madsZ)^2%*%t(q)
  # insert your test for the difference of matrix elements here
  keep_going<-???
  # if the test does not succeed (no difference <= 0.001)
  if(keep_going) xz<-robustm(S_X,sZ)
  else xz<-list(finalx<-S_X,finalz<-sZ)
  return(xz)
}

Jim