Endless run time for-loop in R - r

I have recently created this code and I am struggling with my run time for my for-loops. If I try to run this code, R is seems to take forever and I am not sure where the mistake is:
Graph <- rbind(c(0,0.8,0,0.2,0.1),
c(0,0,0.7,0.6,0.1),
c(0,0,0,0,0.9),
c(0,0,0,0,0.2),
c(0,0,0,0,0))
AmountNodes<-5
Method<-"unweighted"
InfectedNodes<-c(1)
## Function Amount Excluded
SIR_algorithm<-function(Graph, AmountNodes, Method){
ResultMatrix <- rep(0, AmountNodes)
as.data.frame(ResultMatrix)
if (strcmp(Method,"unweighted")){
Graph <- sign(Graph)}
for (i in 1:AmountNodes){
InfectedNodes <- rep(AmountNodes, 0)
ExcludedNodes <- rep(AmountNodes, 0)
InfectedNodes <-c(1)} # Initial Infection, k=Columns, j=Rows
while(sum(InfectedNodes) > 0){
InfectedNodes_Reflection <- InfectedNodes
for (j in 1:nrow(Graph)){
if (Graph[j] == 1){
for (k in 1:ncol(Graph)){
if ((Graph[k,j] > 0 && (InfectedNodes[k][1]) == 0 && (ExcludedNodes[k][1]) == 0)){
RandomValue <- runif(1, max=1, min=0)
if (RandomValue < (Graph[k,j])){
InfectedNodes_Reflection[k] <- k == 1
} #End If-Function
} #End If-Function
} #End For-Function k
} #End If-Function
}} #End For-Function j
for (j in 1:AmountNodes){
if (InfectedNodes[j] == 1){
InfectedNodes[j] <- c(0)
InfectedNodes_Reflection[j] <- c(0)
ExcludedNodes[j] <- c(1)}} #End If-Function and for-function
InfectedNodes <- InfectedNodes_Reflection
ResultMatrix[i] <- ExcludedNodes
length(ResultMatrix) #check length for ResultMatrix
length(ExcludedNodes) #check length for ExcludedNodes
} #End While-Function
#End For-Function i
AmountExcluded <- sum(ResultMatrix)/AmountNodes
#Damage Values<- ResultMatrix * Damage Potential
} #End Function_total
SIR_algorithm(Graph=Graph,AmountNodes=5,Method="unweighted")
Can someone help me please?

The problem is located at the following lines:
...
while(sum(InfectedNodes) > 0){
print(sum(InfectedNodes))
InfectedNodes_Reflection <- InfectedNodes
...
sum(InfectedNodes) is constant and equals to 1. So the loop is infinite.

Related

R next prime number Inclusive of start

I have the below existing code in R. The code prints the next immediate prime number. I want to consider inclusive of starting number
np <- function(x){
if (x==1L | x==2L) {return(2L)}
else {
temp <- x+1
test <- 2:x
while( any( (temp %% test) == 0 ) ){
temp <- temp+1
}
temp
} }
Eg.. np(7) returns 11. But expected output is 7.
Try the code below (following a similar idea in the answer here)
np <- function(x) {
p <- x
repeat {
if (p %in% c(2, 3) | all(p %% ceiling(sqrt(2:p)) != 0)) {
return(p)
}
p <- p + 1
}
}
and you will see
> np(2)
[1] 2
> np(3)
[1] 3
> np(4)
[1] 5
> np(5)
[1] 5
> np(7)
[1] 7
Maybe it's stupid but does this do the trip?
np <- function(x){
if (x==1L | x==2L) {return(2L)}
else {
x= x-1
temp <- x+1
test <- 2:x
while( any( (temp %% test) == 0 ) ){
temp <- temp+1
}
temp
} }
You are testing numbers above x only by calling temp <- x+1. Here is a version that should work with minimal changes to your code:
np <- function(x){
if (x==1L | x==2L) {return(2L)}
else {
temp <- x
test <- 2:(x - 1)
while( any( (temp %% test) == 0 ) ){
temp <- temp+1
}
temp
} }

R - for loop error: "Unexpected '}' in '}'"

here is the code for a simulation I'm trying to run:
n_draws <- 1000
black <- rep(0, n_draws)
hispanic <- rep(0, n_draws)
asian <- rep(0, n_draws)
white <- rep(0, n_draws)
cutoff <- c(0.05,0.1,0.25,1)
draws <- runif(n_draws,0,1)
for (i in draws){
if (draws[i] < cutoff[1]){
black[i] <- 1
} else if ((draws[i] >= cutoff[1]) & (draws[i] < cutoff[2])){
hispanic[i] <- 1
} else if ((draws[i] >= cutoff[2]) & (draws[i] < cutoff[3]){
asian[i] <- 1
} else {
white[i] <- 1
}
}
Basically, I want to add a 1 to the corresponding list, conditional on where that number falls in the range (0,1). I'm not sure why this is giving an error. Suggestions?
You're just missing a closing bracket just after cutoff[3], also used seq_along in my example as it's a bit nicer
for (i in seq_along(draws)){
if (draws[i] < cutoff[1]){
black[i] <- 1
} else if ((draws[i] >= cutoff[1]) & (draws[i] < cutoff[2])){
hispanic[i] <- 1
} else if ((draws[i] >= cutoff[2]) & (draws[i] < cutoff[3])){
asian[i] <- 1
} else {
white[i] <- 1
}
}

R Raster - Create layer with conditionals looping through multiple layers

I am working with a time-series raster brick. The brick has 365 layers representing a value for each day of the year.
I want to create a new layer in which each cell holds the number of day of year in which a certain condition is met.
My current approach is the following (APHRO being the raster brick), but returns the error message below:
enter code here
r <- raster(ncol=40, nrow=20)
r[] <- rnorm(n=ncell(r))
APHRO <- brick(x=c(r, r*2, r))
NewLayer <- calc(APHRO, fun=FindOnsetDate(APHRO))
Returning this error:
Error in .local(x, ...) : not a valid subset
And the function being parsed:
FindOnsetDate <- function (s) {
x=0
repeat {
x+1
if(s[[x]] >= 20 | s[[x]] + s[[x+1]] >= 20 & ChkFalseOnset() == FALSE)
{break}
}
return(x);
}
With the function for the 3rd condition being:
ChkFalseOnset <- function (x) {
for (i in 0:13){
if (sum(APHRO[[x+i:x+i+7]]) >= 5)
{return(FALSE); break}
return(TRUE)
}
}
Thank you in advance!!!!
And please let me know if I should provide more information - tried to keep it parsimonious.
The problem is that your function is no good:
FindOnsetDate <- function (s) {
x=0
repeat {
x+1
if(s[[x]] >= 20 | s[[x]] + s[[x+1]] >= 20)
{break}
}
return(x);
}
FindOnsetDate(1:100)
#Error in s[[x]] :
# attempt to select less than one element in get1index <real>
Perhaps something like this:
FindOnsetDate <- function (s) {
j <- s + c(s[-1], 0)
sum(j > 20 | s > 20)
# if all values are positive, just do sum(j > 20)
}
FindOnsetDate(1:20)
#10
This works now:
r <- calc(APHRO, FindOnsetDate)
I would suggest a basic two-step process. With a 365-days example:
set.seed(123)
r <- raster(ncol=40, nrow=20)
r_list <- list()
for(i in 1:365){
r_list[[i]] <- setValues(r,rnorm(n=ncell(r),mean = 10,sd = 5))
}
APHRO <- brick(r_list)
Use a basic logic test for each iteration:
r_list2 <- list()
for(i in 1:365){
if(i != 365){
r_list2[[i]] <- APHRO[[i]] >= 20 | APHRO[[i]] + APHRO[[i+1]] >= 20
}else{
r_list2[[i]] <- APHRO[[i]] >= 20
}
}
Compute sum by year:
NewLayer <- calc(brick(r_list2), fun=sum)
plot(NewLayer)

switch statement help in R

I've got the following code in R:
func.time <- function(n){
times <- c()
for(i in 1:n){
r <- 1 #x is the room the mouse is in
X <- 0 #time, starting at 0
while(r != 5){
if(r == 1){
r <- sample(c(2,3),1) }
else if(r == 2){
r <- sample(c(1,3), 1) }
else if(r == 3){
r <- sample(c(1,2,4,5), 1) }
else if (r == 4){
r <- sample(c(3,5), 1) }
X <- X + 1
}
times <- c(X, times)
}
mean(times)
}
func.time(10000)
It works fine, but I've been told that using switch() can speed it up seeing as I've got so many if else statements but I can't seem to get it to work, any help is appreciated in advance.
Edit
I've tried this:
func.time <- function(n) {
times <- c()
for(i in 1:n) {
r <- 1 #x is the room the mouse is in
X <- 0 #time, starting at 0
while(r != 5) {
switch(r, "1" = sample(c(2,3), 1),
"2" = sample(c(1,3), 1),
"3" = sample(c(1,2,4,5), 1),
"4" = sample(c(3,5)))
X <- X + 1
}
times <- c(X, times)
}
mean(times)
}
func.time(10000)
But it was a basic attempt, I'm not sure I've understood the switch() method properly.
I though Dominic's assessment was very useful but when I went to examine the edit it was being held up on what I thought was an incorrect basis. So I decided to just fix the code. When usign a numeric argument to the EXPR parameter you do not use the item=value formalism but rather just put in the expressions:
func.time <- function(n){times <- c()
for(i in 1:n){; r <- 1; X <- 0
while(r != 5){
r <- switch(r,
sample(c(2,3), 1) , # r=1
sample(c(1,3), 1) , # r=2
sample(c(1,2,4,5), 1), #r=3
sample(c(3,5), 1) ) # r=4
X <- X + 1 }
times <- c(X, times) }
mean(times) }
func.time(1000)
#[1] 7.999
For another example of how to use switch with a numeric argument to EXPR, consider my answer to this question: R switch statement with varying outputs throwing error

how to skip and break a loop in R

I am trying write a function that generates simulated data but if the simulated data does not meet the condition, I need to skip it and if it does meet the condition, then I will apply the function summary.
I would like to loop it until I find 10 valid datasets and then stop. (I actually have to do this until it reaches 10000). Here is the code. The code sort of works except it does not stop. I think I probably placed the next and break function in the wrong place. I hope someone could help me write this together.
Another way I could approach this is to generate all the valid data first and then apply the function find_MLE (summary) to the final list.
Edit: I put break inside repeat. I edit the code to make it reproducible. Still the code keeps generating data and does not break.
here is a reproducible version
validData <- function(GM, GSD,sampleSize, p) {
count=0
for (i in 1:n) {
repeat {
lod <- quantile(rlnorm(1000000, log(GM), log(GSD)), p = p)
X_before <- rlnorm(sampleSize, log(GM), log(GSD))
Xs <- ifelse(X_before <= lod, lod, X_before)
delta <- ifelse(X_before <= lod, 1, 0)
pct_cens <- sum(delta)/length(delta)
print(pct_cens)
if (pct_cens == 0 & pct_cens ==1) next
else {
sumStats <- summary(Xs)
Med <- sumStats[3]
Ave <- sumStats[4]
}
count<- count+1
if (count == 10) break
}}
return(c(pct_cens, Med, Ave))
}
validData(GM=1,GSD=2,sampleSize=10,p=0.1)
Thanks for your help. I was able to write a function without using break function! I posted it here in case other people might find it helpful.
dset <- function (GM, GSD, n, p) {
Mean <- array()
Median <- array()
count = 0
while(count < 10) {
lod <- quantile(rlnorm(1000000, log(GM), log(GSD)), p = p)
X_before <- rlnorm(n, log(GM), log(GSD))
Xs <- ifelse(X_before <= lod, lod, X_before)
delta <- ifelse(X_before <= lod, 1, 0)
pct_cens <- sum(delta)/length(delta)
print(pct_cens)
if (pct_cens == 0 | pct_cens == 1 ) next
else {count <- count +1
if (pct_cens > 0 & pct_cens < 1) {
sumStats <- summary(Xs)
Median[count] <- sumStats[3]
Mean [count]<- sumStats[4]
print(list(pct_cens=pct_cens,Xs=Xs, delta=delta, Median=Median,Mean=Mean))
}
}
}
return(data.frame( Mean=Mean, Median=Median)) }
Since your code isn't replicable, I cannot fully test and debug your code, but here is what I think it would look like without being able to replicate with an MLE function. This is roughly how I would set it up. But check out the documentation/Google on break, next, for/while loops related to R when testing your code.
validData <- function(GM, GSD,Size, p) {
for (i in 1:20) {
count <- 1
repeat {
lod <- quantile(rlnorm(1000000, log(GM), log(GSD)), p = p)
X_before <- rlnorm(Size, log(GM), log(GSD))
Xs <- ifelse(X_before <= lod, lod, X_before)
delta <- ifelse(X_before <= lod, 1, 0)
pct_cens <- sum(delta)/length(delta)
if (pct_cens == 0 & pct_cens ==1)
function() #your foo goes here
else {
mles <- find_MLE(c(0,0), Xs, delta)
GM_est <- mles[1]
GSD_est <- mles[2]
AM_est <- exp(log(GM_est) + 1 )
SD_est<- sqrt((AM_est)^2*exp(log(GSD_est)^2))
D95th_est <- GM_est*(GSD_est^1.645)
} }
return(c(GM_est,GSD_est,AM_est,SD_est,D95th_est))
count<- count+1
if (count == 10) break
}}
To skip to the outer loop based on a condition, simply use break()
Here's a simple example where the inner loop will try to run 10 times, but a condition will usually be met which prevents it
# OUTER LOOP
for(i in 1:2) {
print(paste("Outer loop iteration", i))
# INNER LOOP (will run max 10 times)
for(j in 1:10) {
print(paste("Inner loop iteration", j))
if (runif(1) > 0.4) { # Randomly break the inner loop
print(paste("Breaking inner loop", j))
break()
}
}
}
If you want to skip to the outer loop when there's an error (rather than based on a condition), see here

Resources