[R] problem in while loop?

Jonsson amen.alyaari at Bordeaux.inra.fr
Fri Aug 30 09:44:51 CEST 2013


I have three datasets that I want to compute the errors between them using
linear regression.for this, I want to iterate to reach certain criteria for
the calibration. if changes become smaller than eps the iteration is
successful, hence stop and write parameters into cal:eps=0.00001 if number
of iterations is > itermax the iteration failed, hence stop and fill cal
with missing value itermax=400

So I tried this code:

                  x= c(5,2,4,2,1)
                  y= c(5,3,4,6,9)
                  z= c(5,8,4,7,3)
              itermax=400
get initial calibration parameters, here we assume that:x is the reference
dataset offset x_a=0, slope x_b=1 the other two datasets y, z are
"calibrated" to x using a simple linear regression

             res=lm(x~y)
               y_a=coef(res)[1] ; y_b=coef(res)[2]
              res1=lm(x~z)
              z_a=coef(res1)[1] ; z_b=coef(res1)[2]
              y_t = y/y_b - y_a/y_b  # "calibrate" y
              z_t = z/z_b - z_a/z_b  #"calibrate" z
              x_e = sqrt(mean((x-y_t)*(x-z_t)))#calculate error of x
               iter <- 0
           while(((x_e-x) > 0.00001)&& (iter < itermax)) { 
              iter <- 0  ##start iteration
                   x = x_e 
                   res=lm(x~y)
                   y_a=coef(res)[1] ; y_b=coef(res)[2]
                   res1=lm(x~z)
                   z_a=coef(res1)[1] ; z_b=coef(res1)[2]
                   y_t = y/y_b - y_a/y_b  # "calibrate" y
                   z_t = z/z_b - z_a/z_b  #"calibrate" z
                   x_e = sqrt(mean((x-y_t)*(x-z_t)))
                    iter <- iter + 1 # increase iteration counter
                    } 
But I got the same result for X_e before and after the loop:

                          > x_e
                         [1] 6.454089



--
View this message in context: http://r.789695.n4.nabble.com/problem-in-while-loop-tp4674962.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list