Hi R-fellows,

I am trying to simulate a multivariate correlated sample via the Gaussian 
copula method. One variable is a binary variable, that should be 
autocorrelated. The autocorrelation should be rho = 0.2. Furthermore, the 
overall probability to get either outcome of the 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 <- matrix(0, nrow = n, ncol = 4)
        sample[, 1] <- var1[1:nrow(var1) - 1]
        sample[, 2] <- var1[2:nrow(var1)]
        sample[, 3] <- qnorm(pmvrs[1:nrow(var1) - 1, 2], 0, 1, 1, 0)
        sample[, 4] <- qnorm(pmvrs[1:nrow(var1) - 1, 3], 0, 1, 1, 0)
        
        sample
        
}

Now, the code is fine, everything compiles. But when I compute the 
autocorrelation of the binary variable, it is not 0.2, but 0.6. Does anyone 
know why this happens? 

Best Regards

Simon


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to