R loop until condition is met

Ma Ov

Working on a project in R, I got stuck somewhere. Those are my data:


r <- 0.001
a <- 0.386
b <- 0.799

yp <- b*df$x+a
sse <- ((df$y-yp)^2)*0.5
a <- a-r*sum(-(df$y-yp))
b <- b-r*sum(-(df$y-yp)*df$x)

How can I find the number of iterations until SSE error is 0.0001?

Allan Cameron

You could do a while loop that stores the sum of squared errors in a vector for each iteration. The loop stops when the sum of squares stops falling, at which point we should have an accurate a and b, and the number of iterations is given by the length of the sse vector.

df <- data.frame(x=c(1.0,2.1,2.2,3.5,5.3,5.4,6.5,7.4),
                 y=c(4,4,4,5,5,6,8,8))

r <- 0.001
a <- 0.386
b <- 0.799

yp  <- b * df$x + a
sse <- sum(((df$y - yp)^2))

while(TRUE)
{
  a <- a - r * sum(-(df$y - yp))
  b <- b - r * sum(-(df$y - yp) * df$x)
  yp <- b * df$x + a
  sse <- c(sse, sum((df$y - yp)^2))
  if(tail(sse, 1) >= sse[length(sse) - 1]) break
}

To see the results, we can do:

cat('iterations: ', length(sse), '\n Intercept = ', a, '\n Slope = ', b, '\n')
#> iterations:  9152 
#>  Intercept =  2.691678 
#>  Slope =  0.6726519

And we can see that our results are correct (to the first six decimal places) by running an lm:

coef(lm(y~x, df))
#> (Intercept)           x 
#>   2.6916789   0.6726517

Created on 2022-03-15 by the reprex package (v2.0.1)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Store variable with multiple condition in for loop until condition is met in R

Continuos loop until condition is not met

Loop in JavaScript until a condition is met

Execute the loop until the condition met

R sample until a condition is met

Trying to loop an API call until condition is met

Loop until condition is met or time has elapsed

Repeat loop step until a condition is met

Counting rows until a condition is met in R - NAs before the condition is met

Do Until Loop Condition Met - but Loop continues - Powershell

NodeJS - While loop until certain condition is met or certain time is passed

How do I stay in a for loop's element until a condition is met?

How to constantly re-loop through code until a condition is met?

Loop over and count columns in a data frame until condition is met

R: Stopping a Loop When a Condition is Met

In R-studio, how would go about repeating a for-loop using the same [i] until a specific condition is met?

Looping in a data frame in R until a certain condition is met

I need help in an if else statement in R until a certain condition is met

R loop until condition matched, then go on

Execute ansible include_tasks until a certain condition is met (kind of while until loop)

How to make my loop check the if statement until the condition is met / basically until the button appears on screen?

Awaitatility, polling until condition is met

Do action until condition is met

Collapse Cells until a condition is met

Javascript looping until condition met

Repeat prompt until condition is met

Finding row until condition is met

Is there a way to nest a loop in JavaScript until a condition is met? Like a while loop but nested?

While loop if condition is not met