s-news
[Top] [All Lists]

sample function

To: s-news@wubios.wustl.edu
Subject: sample function
From: carol white <wht_crl@yahoo.com>
Date: Fri, 16 May 2008 14:16:39 -0700 (PDT)
Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:Date:From:Subject:To:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-ID; b=nKDmT8UY+Xvc56HxAIB+4cbns7fJ2BqwyukAIV7s5BipSm0LbXvt98Kyn9OMOZ8oDGoWjxaHYnEXg0LOjb/WRon0oDkAdQWOUeOO5mJMRcstI0A9Xu0TGGRXZRiJUNEoTARfEo9idCpC+4qFxZIwUz2TLAyWsXFL3rWEQk/9fp4=;
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]
    }
.....

<Prev in Thread] Current Thread [Next in Thread>
  • sample function, carol white <=