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.
On 30-08-2013, at 09:44, Jonsson <amen.alyaari at Bordeaux.inra.fr> wrote:> 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 >I tried your code and got this error message: Error in model.frame.default(formula = x ~ y, drop.unused.levels = TRUE) : variable lengths differ (found for 'y') Calls: lm -> eval -> eval -> <Anonymous> -> model.frame.default And looking at your code: x is a vector, x_e is a scalar and in the while loop you are assigning x_e to x so x is then a scalar. Berend