[R-es] Código reproducible Error in while (diff > 1e-08) { : missing value where TRUE/FALSE needed

Javier Marcuzzi j@v|er@ruben@m@rcuzz| @end|ng |rom gm@||@com
Dom Feb 2 16:17:54 CET 2025


Estimados

Hace unos minutos envié una consulta, pero el código para reproducir el error estaba sin los datos, yo los tenía en memoria y no me di cuenta, ahora envío el el código con los datos, la función y la pregunta, todo reproducible.

Pregunta original : Error in while (diff > 1e-08) { : missing value where TRUE/FALSE needed

Gracias Javier Marcuzzi

## Given the MME, iteratively solve for the solutions by Jocobi Over Relaxation.

## Arguments
## LHS: left hand side of MME
## RHS: right hand side of MME
## inits: a vecotr of initial values for the solutions
## w: a vector of relaxation factor
## disp: a logical value. If true, show the solutions at each iteration

## Note: When 'w' is all one, usual Jacobi method will be performed.
jor <-
  function(LHS, RHS, inits, w, disp) {
    D <- diag(diag(LHS))
    R <- LHS
    diag(R) <- 0
    w <- diag(w)
    Dinv <- solve(D) %*% w
    x <- matrix(inits)
    I <- diag(1, dim(w)[1])

    diff <- 1
    i <- 0
    while (diff > 10E-9) {
      i <- i + 1
      newx <- (I - w) %*% x + Dinv %*% (RHS - R %*% x)
      if (disp == TRUE) {
        cat("\n")
        cat("iteration ", i, "\n")
        print(x)
      }
      diff <- (sum((newx - x)^2)) / sum(newx^2)
      x <- newx
    }

    cat("\n")
    cat("Final solutions after", i, "th iteration")
    return(x)
  }

# datos
datosLHS <- c(
    3, 0, 0, 0, 3, 0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.000000, 0.000000, 0.000000, 1.000000, 1.000000, 0.000000, 1.000000, 0.000000,
    0, 2, 0, 0, 2, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 1.000000,
    0, 0, 3, 0, 0, 3, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 1.000000, 1.000000, 0.000000,
    0, 0, 0, 2, 0, 2, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000,
    3, 2, 0, 0, 5, 0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.000000, 0.000000, 0.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    0, 0, 3, 2, 0, 5, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.000000, 0.000000, 0.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
    0, 0, 0, 0, 0, 0, 3.5, 0.7, 0.0, -1.4, 0.7, -1.4, 0.7, -1.4, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
    0, 0, 0, 0, 0, 0, 0.7, 2.8, 0.7, -1.4, -1.4, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
    0, 0, 0, 0, 0, 0, 0.0, 0.7, 2.8, 0.7, -1.4, 0.0, -1.4, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
    1, 0, 1, 0, 1, 1, -1.4, -1.4, 0.7, 5.5, 0.0, 0.0, -1.4, 0.0, 0.000000, 0.000000, 0.000000, 2.000000, 0.000000, 0.000000, 0.000000, 0.000000,
    1, 0, 0, 1, 1, 1, 0.7, -1.4, -1.4, 0.0, 5.5, -1.4, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 2.000000, 0.000000, 0.000000, 0.000000,
    0, 1, 1, 0, 1, 1, -1.4, 0.0, 0.0, 0.0, -1.4, 4.8, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 2.000000, 0.000000, 0.000000,
    1, 0, 1, 0, 1, 1, 0.7, 0.0, -1.4, -1.4, 0.0, 0.0, 5.5, -1.4, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 2.000000, 0.000000,
    0, 1, 0, 1, 1, 1, -1.4, 0.0, 0.0, 0.0, 0.0, 0.0, -1.4, 4.8, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 2.000000,
    0, 0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.333333, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
    0, 0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.000000, 2.333333, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
    0, 0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 2.333333, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
    1, 0, 1, 0, 1, 1, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 4.333333, 0.000000, 0.000000, 0.000000, 0.000000,
    1, 0, 0, 1, 1, 1, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 4.333333, 0.000000, 0.000000, 0.000000,
    0, 1, 1, 0, 1, 1, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 4.333333, 0.000000, 0.000000,
    1, 0, 1, 0, 1, 1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 4.333333, 0.000000,
    0, 1, 0, 1, 1, 1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 4.333333
)
LHS <- matrix(data = datosLHS, nrow = 22, ncol = 22) 
LHS

datosRHS <- c(531, 445, 720, 500, 976, 1220, 0, 0, 0, 481, 350, 350, 430, 585, 0, 0, 0, 481, 350, 350, 430, 585)
RHS <- matrix(data = datosRHS, nrow = 22, ncol = 1)
RHS

# genero 22 números aleatorios
aleatorios <- sample(0:1,
  size = 22,
  replace = TRUE, prob = NULL
)
inits <- aleatorios

w <- c(rep(0.8, 22))
w1 <- c(rep(1, 22))

# -----------------------------------------
# primer prueba - uso w
jor(LHS, RHS, inits, w, disp = TRUE)
#iteration  1010
#Error in while (diff > 1e-08) { : missing value where TRUE/FALSE needed


# -----------------------------------------
# segunda prueba - uso w1
# iteration  499
#Error in while (diff > 1e-08) { : missing value where TRUE/FALSE needed 
jor(LHS, RHS, inits, w1, disp = TRUE)
	[[alternative HTML version deleted]]



Más información sobre la lista de distribución R-help-es