Displaying 1 result from an estimated 1 matches for "pmvrs".
2012 Sep 27
2
Generating an autocorrelated binary variable
...e binary variable should be 0.5.
Below you can see the R code (I use for simplicity a diagonal matrix in rmvnorm even if it produces no correlated sample):
"sampleCop" <- function(n = 1000, rho = 0.2) {
require(splus2R)
mvrs <- rmvnorm(n + 1, mean = rep(0, 3), cov = diag(3))
pmvrs <- pnorm(mvrs, 0, 1)
var1 <- matrix(0, nrow = n + 1, ncol = 1)
var1[1] <- qbinom(pmvrs[1, 1], 1, 0.5)
if(var1[1] == 0) var1[nrow(mvrs)] <- -1
for(i in 1:(nrow(pmvrs) - 1)) {
if(pmvrs[i + 1, 1] <= rho) var1[i + 1] <- var1[i]
else var1[i + 1] <- var1[i] * (-1)
}
sample...