title: R : function to generate a mixture distribution
body: <p>I need to generate samples from a mixed distribution </p>
  
  <ul>
  <li><p>40% samples come from Gaussian(mean=2,sd=8)</p></li>
  <li><p>20% samples come from Cauchy(location=25,scale=2)</p></li>
  <li><p>40% samples come from Gaussian(mean = 10, sd=6) </p></li>
  </ul>
  
  <p>To do this, i wrote the following function : </p>
  
  <pre><code>dmix &lt;- function(x){
  prob &lt;- (0.4 * dnorm(x,mean=2,sd=8)) + (0.2 * dcauchy(x,location=25,scale=2)) + (0.4 * dnorm(x,mean=10,sd=6))
  return (prob)
  }
  </code></pre>
  
  <p>And then tested with:</p>
  
  <pre><code>foo = seq(-5,5,by = 0.01)
  vector = NULL
  for (i in 1:1000){
  vector[i] &lt;- dmix(foo[i])
  }
  hist(vector)
  </code></pre>
  
  <p>I'm getting a histogram like this (which I know is wrong) - 
  <img src="https://i.stack.imgur.com/cRJd1.png" alt="histogram of the supposed distribution"></p>
  
  <p>What am I doing wrong? Can anyone give some pointers please?</p>