Strange behavior using set.seed several times

I came up with a weird result when doing my homework in R, can someone explain to me what is going on?

The instructions told me that seed 1 will contain the sequence.

First I set the seed (1) twice

set.seed(1)
x <- rnorm(100, mean = 0, sd = 1)
set.seed(1)
epsilon <- rnorm(100, mean = 0, sd = 0.25)
y <- 0.5 * x + epsilon -1
plot(x,y,main = "Scatter plot between X and Y", xlab = "X", ylab = "Y")

I get a scatter plot as follows: Plot with two sets of seeds

After I use only one given seed, the code:

set.seed(1)
x <- rnorm(100, mean = 0, sd = 1)
epsilon <- rnorm(100, mean = 0, sd = 0.25)
y <- 0.5 * x + epsilon -1
plot(x,y,main = "Scatter plot between X and Y", xlab = "X", ylab = "Y")

The plot has become reasonable: A plot with one set of seeds

Can someone explain to me why the two results are different from each other by adding an extra "set.seed (1)"?

+4
source share
1

Set.seed() , . , , , , . :

set.seed(1234)
runif(3)
[1] 0.1137034 0.6222994 0.6092747

set.seed(1234)
runif(3)
[1] 0.1137034 0.6222994 0.6092747

set.seed(12345)
runif(3)
[1] 0.7209039 0.8757732 0.7609823

, , .seed(x) , . ( . . ). , , ,

y <- 0.5 * x + epsilon -1

y <- 0.5 * x + x -1

.

y <- 1.5 * x -1

.

, set.seed(x) , script.


: " Epsilon sd, x, , , ?"

. ~N(mean,sd) :

, sd, , , sd . sd. :

set.seed(1)
rnorm(4, mean = 0, sd = 1)
[1] -0.6264538  0.1836433 -0.8356286  1.5952808
set.seed(1)
rnorm(4, mean = 0, sd = 0.25)
[1] -0.15661345  0.04591083 -0.20890715  0.39882020

, , , 0.25 , .

, , epsilon 0.25 * x, y <- 0.75 * x - 1, - .

+12

Source: https://habr.com/ru/post/1682308/


All Articles