I noticed that if another pseudo-random number generator is used when generating the pseudo-random sequence, the seed sequence is placed. My question is, is there anything to be done? Can you somehow provide the initial seed sequence? Let me show you an example:
A simple loop that prints a pseudo-random number taken from the normal distribution:
set.seed(145)
for (i in 1:10){
print(rnorm(1,0,1))
}
Which gives the following conclusion:
[1] 0.6869129
[1] 1.066363
[1] 0.5367006
[1] 1.906029
[1] 1.06316
[1] 1.370344
[1] 0.5277918
[1] 0.4030967
[1] 1.167752
[1] 0.7926794
Next, we introduce a pseudo-random thread from a uniform distribution if the iterator is five.
set.seed(145)
for (i in 1:10){
print(rnorm(1,0,1))
if (i == 5){
print(runif(1,0,1))
}
}
Which gives the following conclusion (at the next output, the star marks a pseudo-random draw from the uniform distribution):
[1] 0.6869129
[1] 1.066363
[1] 0.5367006
[1] 1.906029
[1] 1.06316
[1] 0.9147102*
[1] -1.508828
[1] -0.03101992
[1] -1.091504
[1] 0.2442405
[1] -0.6103299
, , set.seed(145), :
[1] 0.6869129
[1] 1.066363
[1] 0.5367006
[1] 1.906029
[1] 1.06316
[1] 0.9147102*
[1] 1.370344
[1] 0.5277918
[1] 0.4030967
[1] 1.167752
[1] 0.7926794
, .
EDIT:
Rui Barradas, , . rnorm for, if-statement, Rui. , , , - , , , , ( AR-1).
tt <- rnorm(500,0,1)*10
test1 <- function(y, x0=1, n,qsigma = 3, alpha = 5, beta = 20, limit = 0.30){
t <- length(y)
gama <- (alpha + beta)/2
x <- matrix(0,n,t)
x[, 1] <- rep(x0,n)
for(s in 2:t) {
x[, s] <-pmax(alpha*(x[,s-1]<=gama) +beta*(x[,s-1]>gama)+rnorm(n,0,qsigma),1)
if (s==250) {
current <- .GlobalEnv$.Random.seed
resamp <- sample(n, n, replace = TRUE)
x[,s] <- x[resamp,s]
.GlobalEnv$.Random.seed <- current
}
}
list(x = x)
}
test3 <- function(y, x0=1, n,qsigma = 3, alpha = 5, beta = 20, limit = 0.30) {
t <- length(y)
gama <- (alpha + beta)/2
x <- matrix(0,n,t)
x[, 1] <- rep(x0,n)
e_4 <- matrix(rnorm(n * (t), 0, qsigma),n, (t))
for(s in 2:t) {
x[, s] <-pmax(alpha*(x[,s-1]<=gama) +beta*(x[,s-1]>gama)+e_4[,(s-1)],1)
if (s==250) {resamp <-sample(n, n, replace = TRUE)
x[,s] <- x[resamp,s]
}
}
list(x = x, pp = e_4)
}
set.seed(123)
dej11 <- test3(y = tt, n = 5000)$x
set.seed(123)
dej21 <- test1(y = tt, n = 5000)$x
all.equal(dej11,dej21)
, True , , 1,186448.