Hi,
I use sample function in a for loop to get a new random indexing but I don't
know why, I get the same indexes in different iterations of for.
I have a script that uses for loop calling crossval function (the same
definition as crossval in bootstrap package) defined in another script. Both
scripts are loaded into R by source function. When I reach
o <- sample (1:n) where n is the length of a vector, for ex 10
print(o)
I always get the same indexes at the same position
3 7 5 6 10 9 2 1 8 4
note that i don't change the value of o anywhere in the program. only, at a
new iteration, o gets initialized.
but when I use sample(1:n) in R environement, random sampling works perfectly.
Best
carol
-------------------------------------------
for (i in 1:5)
new.lst = crossval(data, y, s, 3)
crossval <- function (x, y, s, ngroup)
{
call <- match.call()
x <- as.matrix(x)
n <- length(y)
if (ngroup < 2) {
stop("ngroup should be greater than or equal to 2")
}
if (ngroup > n) {
stop("ngroup should be less than or equal to the number of
observations")
}
if (ngroup == n) {
groups <- 1:n
leave.out <- 1
}
if (ngroup < n) {
leave.out <- trunc(n/ngroup)
o <- sample(1:n)
print(o)
groups <- vector("list", ngroup)
for (j in 1:(ngroup - 1)) {
jj <- (1 + (j - 1) * leave.out)
groups[[j]] <- (o[jj:(jj + leave.out - 1)])
}
groups[[ngroup]] <- o[(1 + (ngroup - 1) * leave.out):n]
}
.....
[[alternative HTML version deleted]]