Home > database >  Moving the function to the right does not work
Moving the function to the right does not work

Time:12-17

I made Gaussian mixture model, it works fine, but the function is not moving to the right. In the graph it shows as if the function did not move anything. I incorporated a for loop for the mu:

for(i in 1:n){
  for (j in 1:5){
    mu1 = c(1 j,2)
    mu2 = c(6 j,7) 
    .
    .
    .
     }
     }

but it doesn't work. I try to obtain a graphic as it appears in the image: that it moves at the same time as the previous thing is saved, covering more and more space

library(MASS)


n = 1000
Sigma = matrix(c(1, 0.5, 0.5, 1), 2, 2)
rho = 0.5

# Gaussian mixture model
# X=(X1,X2) ~ rho * N(mu1, Sigma)   (1-rho) * N(mu2, Sigma)
X = cbind(rep(0, n), rep(0, n))
for(i in 1:n){
  for (j in 1:5){
    mu1 = c(1 j,2)
    mu2 = c(6 j,7)
    u = runif(1, min = 0, max = 1)
    if(u < rho){
      X[i,] = mvrnorm(1, mu = mu1, Sigma = Sigma)
    }else{
      X[i,] = mvrnorm(1, mu = mu2, Sigma = Sigma)
    }
  }
}

plot(X[,1], X[,2], xlab = 'X1', ylab = 'X2')

Image

CodePudding user response:

I've adjusted your code to save all 5000 rows:

n = 1000  
m = 5
Sigma = matrix(c(1, 0.5, 0.5, 1), 2, 2)
rho = 0.5

library(MASS)
# Gaussian mixture model
# X=(X1,X2) ~ rho * N(mu1, Sigma)   (1-rho) * N(mu2, Sigma)
X = cbind(rep(0, n * m), rep(0, n * m))
for(i in 1:n){
  for (j in 1:m){
    mu1 = c(1 j,2)
    mu2 = c(6 j,7)
    u = runif(1, min = 0, max = 1)
    if(u < rho){
      X[(i - 1) * m   j,] = mvrnorm(1, mu = mu1, Sigma = Sigma)
    }else{
      X[(i - 1) * m   j,] = mvrnorm(1, mu = mu2, Sigma = Sigma)
    }
  }
}

plot(X[,1], X[,2], xlab = 'X1', ylab = 'X2')

enter image description here

  • Related