Generation of samples from a two-Gaussian mixture in r (code given in MATLAB)

I am trying to create (in d) the equivalent of the following MATLAB function, which will generate n samples from a mixture of N (m1, (s1) ^ 2) and N (m2, (s2) ^ 2) with a fraction of alpha from the first Gaussian.

I have a start, but the results are noticeably different between MATLAB and R (i.e. MATLAB results give random values ​​+ -8, but the R version never even gives + -5). Please help me figure out what's wrong here. Thank: -)

For example: A plot of 1000 samples from a mixture of N (0.1) and N (0.36) with 95% of samples from the first Gauss. Normalize samples to indicate zero and standard deviation.

MATLAB

function

function y = gaussmix(n,m1,m2,s1,s2,alpha)
y = zeros(n,1);
U = rand(n,1);
I = (U < alpha)
y = I.*(randn(n,1)*s1+m1) + (1-I).*(randn(n,1)*s2 + m2);

implementation

P = gaussmix(1000,0,0,1,6,.95)
P = (P-mean(P))/std(P)
plot(P)
axis([0 1000 -15 15])
hist(P)
axis([-15 15 0 1000])

summary chart

plot of randomly generated samples from two Gaussian distributions in MATLAB

final story

histogram of randomly generated samples from two Gaussian distributions in MATLAB

R

yn <- rbinom(1000, 1, .95)
s <- rnorm(1000, 0 + 0*yn, 1 + 36*yn)
sn <- (s-mean(s))/sd(s)
plot(sn, xlim=range(0,1000), ylim=range(-15,15))
hist(sn, xlim=range(-15,15), ylim=range(0,1000))

summary chart

plot of randomly generated samples from two Gaussian distributions in R

final story

histogram of randomly generated samples from two Gaussian distributions in R

Thanks as always!

Decision

gaussmix <- function(nsim,mean_1,mean_2,std_1,std_2,alpha){
   U <- runif(nsim)
   I <- as.numeric(U<alpha)
   y <- I*rnorm(nsim,mean=mean_1,sd=std_1)+
       (1-I)*rnorm(nsim,mean=mean_2,sd=std_2)
   return(y)
}

z1 <- gaussmix(1000,0,0,1,6,0.95)
z1_standardized <- (z1-mean(z1))/sqrt(var(z1))
z2 <- gaussmix(1000,0,3,1,1,0.80)
z2_standardized <- (z2-mean(z2))/sqrt(var(z2))
z3 <- rlnorm(1000)
z3_standardized <- (z3-mean(z3))/sqrt(var(z3))

par(mfrow=c(2,3))
hist(z1_standardized,xlim=c(-10,10),ylim=c(0,500),
   main="Histogram of 95% of N(0,1) and 5% of N(0,36)",
   col="blue",xlab=" ")
hist(z2_standardized,xlim=c(-10,10),ylim=c(0,500),
   main="Histogram of 80% of N(0,1) and 10% of N(3,1)",
   col="blue",xlab=" ")
hist(z3_standardized,xlim=c(-10,10),ylim=c(0,500),
   main="Histogram of samples of LN(0,1)",col="blue",xlab=" ")
##
plot(z1_standardized,type='l',
   main="1000 samples from a mixture N(0,1) and N(0,36)",
   col="blue",xlab="Samples",ylab="Mean",ylim=c(-10,10))
plot(z2_standardized,type='l',
   main="1000 samples from a mixture N(0,1) and N(3,1)",
   col="blue",xlab="Samples",ylab="Mean",ylim=c(-10,10))
plot(z3_standardized,type='l',
  main="1000 samples from LN(0,1)",
   col="blue",xlab="Samples",ylab="Mean",ylim=c(-10,10))
+5
4

, ... (1) R- 1 37. (2) prob rbinom() , . , , sd 37, 5% - sd 1, sd 1, 5% - sd 6 ( 36,6) ...

( , , , , ...)

(, , ) Matlab gaussmix ( , runif(n)<alpha , rbinom(n,size=1,prob=alpha))

gaussmix <- function(n,m1,m2,s1,s2,alpha) {
    I <- runif(n)<alpha
    rnorm(n,mean=ifelse(I,m1,m2),sd=ifelse(I,s1,s2))
}
set.seed(1001)
s <- gaussmix(1000,0,0,1,6,0.95)
+6

, , mclust . . ?mclust::sim. :

require(mclust)
simdata = sim(modelName = "V",
              parameters = list(pro = c(0.95, 0.05),
                                mean = c(0, 0),
                                variance = list(modelName = "V", 
                                                d = 1, 
                                                G = 2,
                                                sigmasq = c(0, 36))),
              n = 1000)
plot(scale(simdata[,2]), type = "h")
+2

:

dmultiNorm <- function(x,means,sds,weights)
{
  if (length(means)!=length(sds)) stop("Length of means must be equal to length of standard deviations")
  N <- length(x)
  n <- length(means)
  if (missing(weights))
  {
    weights <- rep(1,n)  
  }
  if (length(weights)!=n) stop ("Length of weights not equal to length of means and sds")
  weights <- weights/sum(weights)
  dens <- numeric(N)
  for (i in 1:n)
  {
    dens <- dens + weights[i] * dnorm(x,means[i],sds[i])
  }
  return(dens)
}

rmultiNorm <- function(N,means,sds,weights,scale=TRUE)
{
  if (length(means)!=length(sds)) stop("Length of means must be equal to length of standard deviations")
  n <- length(means)
  if (missing(weights))
  {
    weights <- rep(1,n)  
  }
  if (length(weights)!=n) stop ("Length of weights not equal to length of means and sds")

  Res <- numeric(N)
  for (i in 1:N)
  {
    s <- sample(1:n,1,prob=weights)
    Res[i] <- rnorm(1,means[s],sds[s])  
  }
  return(Res)
}

means, , sds deviatians weights, . ?

+1

:

": 1000 N (0,1) N (0,36) 95% . ."

 plot(multG <- c( rnorm(950), rnorm(50, 0, 36))[sample(1000)] , type="h")
 scmulG <- scale(multG)
 summary(scmulG)
 #-----------    
   V1          
 Min.   :-9.01845  
 1st Qu.:-0.06544  
 Median : 0.03841  
 Mean   : 0.00000  
 3rd Qu.: 0.13940  
 Max.   :12.33107  

enter image description here

+1

All Articles