Skip to content

Iterative loop using "repeat"

8 messages · Mario Bourgoin, mary, Jim Lemon

#
Hi,
I'm Marianna 
I'm trying to apply the command "repeat" to my matrix but the repeat process
doesn't work as I would.

In particular I would like to apply the function robustm () _that I have
created_ to my two matrices,  if the difference between the two matrices is
less than 0.001, R give me back the last matrix.
The code thus created allows me to repeat the process only on the first two
matrices:

function(x) {
for(i in 1:10)
repeat {
b<-robustm(S_X)
b2<-robustm(b)
if(abs(b2[i,i]-b[i,i])<=0.001)
break
}
print(b2)}

in particular S_X is my matrix number1 (already defined, dim 10*10), b2 is
my matrix n.2, if the difference isn't less than my value, I would like
reiterate the process using b2 instead S_X and so on...

(actually serves to me that if the difference is not less than 0.001 the
process starts again taking the last array that has identified as output (in
this case b2) and the remarket in the process creating b3, b4, etc. up to
find the minimum difference between the two matrices and returns me the
last.)

Sorry if I am not so clear, thanks
Mary



--
View this message in context: http://r.789695.n4.nabble.com/Iterative-loop-using-repeat-tp4654625.html
Sent from the R help mailing list archive at Nabble.com.
#
On 01/04/2013 10:13 PM, mary wrote:
Hi Mary,
In the above, you are testing whether there is at least one absolute 
difference less than or equal to 0.001 in the diagonals of two matrices. 
One matrix is S_X after applying the "robustm" function and the other is 
after a second iteration of "robustm" on the first matrix. So it seems 
that "robustm" is a function that hopefully converges on some final value.

First, remember that applying "robustm" to S_X in your repeat loop 
doesn't change S_X. Each time you repeat, you will get the same result. 
What you may want to do is this:

iterate.robustm<-function(x) {
  b<-robustm(x)
  finido<-FALSE
  while(!finido) {
   b2<-robustm(b)
   for(i in 1:10)
    if(abs(b2[i,i]-b[i,i])<=0.001) finido<-TRUE
   b<-b2
  }
  return(b2)
}

iterate.robustm(S_X)

This iterates "robustm" until at least one diagonal difference is less 
than or equal to 0.001. Hopefully that function will always converge.

Jim
#
Thanks for the help,

For Mario: you're absolutely right in fact the process never stopped!!!

in particular I'm trying to do a converge process; 

the robustm() function is:
function (x,z)          
1#  {eigen<-eigen(x)
2# d<-madmatrix(z)                                                                
##I have created this function to calculate "mad" of a whole matrix
3# eigenc<-eigen$vectors
4# q<-d%*%eigenc
5# invQ<-matrix.inverse(q)
6# sZ<-mdefpos(z,invQ)                                     ##this function
serves me to define positive my new matrix Z, it's  X%*%invQ
7# madZ<-madmatrix(sZ)
8# S_X<-q%*%(madsZ)^2%*%t(q)
return(S_X)
}

 reviewing this function I realized that it can not be applied in an
iterative manner in the next step because every time a new matrix z should
be fed back into the process and then calculate a new scatter matrix; in the
code above :
(x) is a scatter matrix, Z is a matrix (n*p) of original data that I have
used to obtain a scatter matrix...
in line 6# I need this new matrix sZ because it will be my new (z) if I
reiterate the process on the new scatter matrix S_X, in fact my function
robustm() ask me the scatter matrix and the data matrix so...
how I can do this...and the reiterate the process so that the matrix
converges?? 
(Thanks to Jim: your reply has focused my problem!!!)



--
View this message in context: http://r.789695.n4.nabble.com/Iterative-loop-using-repeat-tp4654625p4654719.html
Sent from the R help mailing list archive at Nabble.com.
#
Thanks for the help:your reply has focused my problem

in particular I'm trying to do a converge process; 

the robustm() function is: 
function (x,z)           
1#  {eigen<-eigen(x) 
2# d<-madmatrix(z)                                                                
##I have created this function to calculate "mad" of a whole matrix 
3# eigenc<-eigen$vectors 
4# q<-d%*%eigenc 
5# invQ<-matrix.inverse(q) 
6# sZ<-mdefpos(z,invQ)                                     ##this function
serves me to define positive my new matrix Z, it's  X%*%invQ 
7# madZ<-madmatrix(sZ) 
8# S_X<-q%*%(madsZ)^2%*%t(q) 
return(S_X) 
} 

 reviewing this function I realized that it can not be applied in an
iterative manner in the next step because every time a new matrix z should
be fed back into the process and then calculate a new scatter matrix; in the
code above : 
(x) is a scatter matrix, Z is a matrix (n*p) of original data that I have
used to obtain a scatter matrix... 
in line 6# I need this new matrix sZ because it will be my new (z) if I
reiterate the process on the new scatter matrix S_X, in fact my function
robustm() ask me the scatter matrix and the data matrix so... 
how I can do this...and the reiterate the process so that the matrix
converges?? 




--
View this message in context: http://r.789695.n4.nabble.com/Iterative-loop-using-repeat-tp4654625p4654725.html
Sent from the R help mailing list archive at Nabble.com.
#
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
#
Hi Jim,
The last question:
I changed my function robustm ()  to have as output the scatter matrix and
the data to re-enter into the process and I used as suggested you  "while
do", and seems to work without errors.

iterate.robustm<-function(x,z) { 
b<-robustm(x,z)                                     #in robustm() the output
is a list with [[1]]=scatter matrix  [[2]]=my new data
bS<-b[[1]] 
finido<-FALSE 
while(!finido) {      
 b2<-robustm(b[[1]],b[[2]])
bS2<-b2[[1]]
   for(i in 1:10) 
for(j in 1:10)
    if(abs(bS2[i,j]-bS[i,j])<=0.001) finido<-TRUE 
   b<-b2
  } 
  return(b) 
}

But my question now is what is the difference between the recursive process
"while do" and suggested earlier with "if else"?

Thank you for all precious tips suggested me
Mary



--
View this message in context: http://r.789695.n4.nabble.com/Iterative-loop-using-repeat-tp4654625p4654777.html
Sent from the R help mailing list archive at Nabble.com.