This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Help to understand set.seed(547) in R

Hello everyone,

I have a question related to set.seed() functions in R environment. The question is as below:

set.seed(547) # **If this is commented, every run gives you different p-value. why?**
gene=800 # Notice that we pick another gene

NUMPERMS=1e4
permstats=c() # Need to initialize

for(i in 1:NUMPERMS) {
  permsample = sample(1:38, size=27, replace=FALSE)
  data1 = golub[gene, permsample]
  data2 = golub[gene, -permsample]
  permstats[i] = t.test(data1, data2)$statistic
}

## Actual t-statistic for gene at position 800
ts = t.test(golub[800, 1:27], golub[800, 28:38])$statistic
ts

hist(permstats)
abline(v=ts, col='red')

## Calculating the p-value
p_value = mean(ts < abs(permstats))
p_value

What is the function of set.seed(547) in here? Normally, to remain the consistent, I use set.seed(111) for running.

p-value r

it's a random number (seed) and 547 has no special meaning there. Using the same seed helps in reproducing the results of a code. Since the code uses sample function, following is an example, how random sampling can be reproduced with using seed:

> set.seed(100)
> sample(letters,4)
[1] "j" "w" "f" "p"
> sample(letters,4)
[1] "s" "y" "n" "l"
> set.seed(100)
> sample(letters,4)
[1] "j" "w" "f" "p"
> set.seed(547)
> sample(letters,4)
[1] "l" "e" "v" "i"
> sample(letters,4)
[1] "h" "e" "s" "j"
> set.seed(547)
> sample(letters,4)
[1] "l" "e" "v" "i" 

Thank you for your help! I get the idea.

0 answers

No answers yet.

Log in to answer this question.