Created
May 30, 2018 14:08
-
-
Save genkiFurukawa/844a61ecd0303afd62b2b62490cc46a5 to your computer and use it in GitHub Desktop.
Generate sample data for SCRW-model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(MASS) | |
set.seed(17) | |
# | |
x1 <- c(0, 0) | |
Sigma <- matrix(c(1, 0.7, 0.7, 1), | |
ncol=2, | |
nrow=2) | |
x2 <- mvrnorm(1, x1, Sigma) | |
r <- c(0.8, 0.3) | |
# 0.03rad = | |
# -1.8rad = -103deg | |
theta <- c(0.03, -1.8) | |
a <- c(0.8, 0.3) | |
bmode <- rep(NA, 99) | |
bmode[1] <- 0 | |
# | |
x <- matrix(NA, ncol=2, nrow=100) | |
x[1,] <- x1 | |
x[2,] <- x2 | |
for (i in 2:99){ | |
# 二項分布乱数rbinom | |
bmode[i] <- rbinom(1, 1, 1-a[bmode[i-1]+1]) | |
TM <- matrix(c(cos(theta[bmode[i]+1]), | |
sin(theta[bmode[i]+1]), | |
-sin(theta[bmode[i]+1]), | |
cos(theta[bmode[i]+1])), | |
ncol=2,nrow=2) | |
# 多変量正規乱数mvnorm | |
x[i+1,] <- mvrnorm(1, x[i,]+r[bmode[i]+1] * TM %*% (x[i,]-x[i-1,]), Sigma) | |
} | |
y <- x + matrix(rnorm(200), ncol=2, nrow=100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment