Hi all. I was having some trouble with a for loop and I found the problem is the following. Does anyone have some idea why I got the following R result? Since mone is equal to 3, why mu1 only have 2 components? library(MASS)> p0 <- seq(0.1, 0.9,by=0.1) > m <- 10 > > > p0 <- p0[7] > > ## data generation > > mzero <- p0*m > mone <- m-mzero > mu1 <- rnorm(mone, 0,1) > mu <- c(rep(0,mzero), mu1) > > var <- diag(m) > zv <- mvrnorm(n, mu, var)Error in mvrnorm(n, mu, var) : incompatible arguments> mone[1] 3> mu1[1] -0.08802108 0.77904786 [[alternative HTML version deleted]]
Hi, It's FAQ 7.31 Try this: mone <- round(m-mzero) HTH, Ivan Le 2/24/2011 18:31, li li a ?crit :> Hi all. > I was having some trouble with a for loop and I found the problem is the > following. > Does anyone have some idea why I got the following R result? Since mone > is equal to 3, why > mu1 only have 2 components? > > > library(MASS) >> p0<- seq(0.1, 0.9,by=0.1) >> m<- 10 >> >> >> p0<- p0[7] >> >> ## data generation >> >> mzero<- p0*m >> mone<- m-mzero >> mu1<- rnorm(mone, 0,1) >> mu<- c(rep(0,mzero), mu1) >> >> var<- diag(m) >> zv<- mvrnorm(n, mu, var) > Error in mvrnorm(n, mu, var) : incompatible arguments >> mone > [1] 3 >> mu1 > [1] -0.08802108 0.77904786 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
You've fallen afoul of rounding error, FAQ 7.31 I believe,> rnorm(3, 0, 1)[1] -2.0903756 -0.9314351 0.1477768 but> rnorm(mone, 0, 1)[1] -0.8695359 -0.5429294 because mone is not actually three, but on my linux system a bit less.> mone == 3[1] FALSE> mone < 3[1] TRUE You could try> rnorm(round(mone), 0, 1)[1] -1.1668325 -0.1606379 -0.4542331 Sarah On Thu, Feb 24, 2011 at 12:31 PM, li li <hannah.hlx at gmail.com> wrote:> Hi all. > ? I was having some trouble with a for loop and I found the problem is the > following. > ? Does anyone have some idea why I got the following R result? Since mone > is equal to 3, why > mu1 only have 2 components? > > > library(MASS) >> p0 <- seq(0.1, 0.9,by=0.1) >> m <- 10 >> >> >> p0 <- p0[7] >> >> ## data generation >> >> mzero <- p0*m >> mone <- m-mzero >> mu1 <- rnorm(mone, 0,1) >> mu <- c(rep(0,mzero), mu1) >> >> var <- diag(m) >> zv <- mvrnorm(n, mu, var) > Error in mvrnorm(n, mu, var) : incompatible arguments >> mone > [1] 3 >> mu1 > [1] -0.08802108 ?0.77904786-- Sarah Goslee http://www.functionaldiversity.org
Notice that>mone-3[1] -8.881784e-16 on my machine so, no, mone is actually less than 3, which truncates to 2. This is an overcomplicated version of R FAQ 7.31, which is mentioned in the posting guide referenced at the end of every R-help message. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. li li <hannah.hlx@gmail.com> wrote: Hi all. I was having some trouble with a for loop and I found the problem is the following. Does anyone have some idea why I got the following R result? Since mone is equal to 3, why mu1 only have 2 components? library(MASS) > p0 <- seq(0.1, 0.9,by=0.1) > m <- 10 > > > p0 <- p0[7] > > ## data generation > > mzero <- p0*m > mone <- m-mzero > mu1 <- rnorm(mone, 0,1) > mu <- c(rep(0,mzero), mu1) > > var <- diag(m) > zv <- mvrnorm(n, mu, var) Error in mvrnorm(n, mu, var) : incompatible arguments > mone [1] 3 > mu1 [1] -0.08802108 0.77904786 [[alternative HTML version deleted]]_____________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]