Help to understand set.seed(547) in R
0
0
Entering edit mode
2.5 years ago
Hien • 0

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 • 874 views
ADD COMMENT
1
Entering edit mode

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" 
ADD REPLY
0
Entering edit mode

Thank you for your help! I get the idea.

ADD REPLY

Login before adding your answer.

Traffic: 1830 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6