vendredi 22 juin 2018

How to break a repeat loop when its output falls within the 95% confidence interval of the previous 3 outputs?

I have been trying to write a repeat function that breaks whenever its output equilibrates, meaning that it stops after a specific number of iterations, after which the function returns values that fall within the 95% confidence interval of the last 3 values returned by that function. This is example data:

a <- c(1, 5)
a.2 <- c(9, 18)
rbind(a, a.2)
b <- c(1, 0.5)
c <- c(1, 0)

This is the function I am trying out. Every computation of the values is addded to a matrix with two columns. I am using the last three rows to work out a 95% confidence intrval for the two different values. If the values fall within this interval, the function is supposed to break.

repeat {
abc <- sum(a * b) + c
for(alpha in abc[alpha]) {
if (abc[alpha] > 0) {
new.abc[alpha] <- (1-b)a - 0.2(b)
} else {
new.abc[alpha] <- (b-1)a - 0.2(b)   
}
}
all.abc <<- matrix(ncol = 2, dimnames = list(c(),c("abc.1", "abc.2")
all.abc <<- rbind(all.abc, new.abc)
s <- apply(all.abc[(length(abc[,1]) - 2):length(abc[,3]),], 2, sd)
n <- apply(all.abc[(length(abc[,1]) - 2):length(abc[,3]),], 2, length)
error <- qnorm(0.975)*s/sqrt(n)
for(j in length(all.abc[j,])) {
    for(i in length(all.abc[,i])) {
        if((all.abc[j,i] < all.abc[j,i] + error * all.abc[j,i]) &
          (all.abc[j,i] > all.abc[j,i] - error * all.abc[j,i])) {
                                   break    
                           } 
                }
        }
}

However, this does not seem to work. I suppose the values of the last rows are not readily available during the execution of the function. I hope I have been clear. Tell me if I need to clarify anything. Thanks!

Aucun commentaire:

Enregistrer un commentaire