0

This is a random data set being generated here for understanding and plotting a hierarchical cluster in R. I need to know the logic behind why the difference in the calls to rnorm for the x and y axis of the plot. Why y<-rnorm(12, mean=rep(c(1,2,1) when I would have expected mean=rep(c(1,2,3). Perhaps just the literal translation would help me.

set.seed(1234); par(mar=c(0,0,0,0)) ## par sets parameter mar (sets margin)  
x<-rnorm(12, mean=rep(1:3,each=4),sd=0.2) ## repeat the vector 3 times
y<-rnorm(12, mean=rep(c(1,2,1),each=4),sd=0.2) ## ?????
plot(x,y,col="blue",pch=19,cex=2)
text(x+0.05,y+0.05,label=as.character(1:12))

Any help appreciated!

  • 1
    Your specifying two different means so why would they be the same? plus they are random variables so the values generated wouldn't be the same for x or y anyway. However, your questions is not that clearly stated. –  Feb 11 '13 at 19:59
  • At the moment the question appears incoherent. What meaning are we supposed to attach to : "would have expected mean=rep(c(1,2,3)". That not interpretable R code. – IRTFM Feb 11 '13 at 20:14

1 Answers1

1

If you run your code, you get graphical output that looks something like this:

clusters

You can see that there are three clusters at three distinct mean x values (1, 2 and 3) but only two distinct y values (1 and 2, then 1 again). That's because the code for the y values has mean=rep(c(1,2,1),each=4). i.e. the rnorm function is generating twelve random y values, the first four of which have a mean of 1, the second four of which have a mean of 2 and the third four of which have a mean of 1.

Simon
  • 10,679
  • 1
  • 30
  • 44
  • What does this mean? "mean=rep(c(1,2,1)" – IRTFM Feb 11 '13 at 20:14
  • @DWin: I agree that my answer wasn't very clear so I've explained a bit further. – Simon Feb 11 '13 at 20:18
  • The answer to the question [Calling rnorm with a vector of means](http://stackoverflow.com/questions/3510619/calling-rnorm-with-a-vector-of-means) might also be useful. – Simon Feb 11 '13 at 21:49