[R-es] Error in while (diff > 1e-08)

Proyecto R-UCA r-uc@ @end|ng |rom uc@@e@
Dom Feb 2 17:07:10 CET 2025


Buenas, Javier:

No he ejecutado el código, pero alguna vez he tenido un problema similar, los motivos que conozco pueden ser dos:
1. diff no es un número debido a que es, por ejemplo, un vector.
2. diff es NaN o NA, en estos casos diff > 1e-08 da NA que no se puede convertir a lógico. Si diff es NA ni siquiera diff == diff es true.

Una opción es usar testar si diff es NA con la función is.na y actuar en consecuencia, o bien, usar isTRUE(diff > 1e-08) que devolverá TRUE
si la comparación se puede hacer y se verifica la desigualdas y FALSE en cualquier otro caso.

Un saludo.

El dom, 02-02-2025 a las 11:09 -0300, Javier Marcuzzi escribió:
> Estimados
> 
> Copio y pego un código, del cuál al ejecutar podrán reproducir el error.
> 
> La primer parte es una función, cuándo a mano coloco números próximos al resultado no hay problema, pero claro, ¿si no tengo ni idea del
> resultado que deseo calcular?
> 
> Entonces genero 22 valores, como así también dos versiones para w (donde una de estas es todo 1)
> 
> Hasta ahí no hay problema, pueden correr el código que comparto.
> 
> El problema es al correr el código, en distinto número de interacciones aparece el error:
> #Error in while (diff > 1e-08) { : missing value where TRUE/FALSE needed
> 
> Pero en la descripción de la función este error no está, y comienza a andar hasta varios ciclos donde aparece y corta.
> 
> ¿Alguna idea? 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)
>   }
> 
> 
> # 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]]
> 
> _______________________________________________
> R-help-es mailing list
> R-help-es using r-project.org
> https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help-es__;!!D9dNQwwGXtA!TOZaT--0zH_nd14RlIt-Go-icTdS7d8Ekckz3eeBdfAm2mnDhKfXJqFM4SOAf-ZcTVxaMM3nTJVCTK8sMnOVx4E$
>  



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