mentby.com
Blog | Jobs | Help | Signup | Login

loading

simulate from conditional distribution

Thu, 13 Sep 2012 23:39:58 -0700 Post Comments

Hi.

One of the options, how to generate such numbers, is the rejection
method. This works well, if Pr(X > c) is not too small, but may be
very bad otherwise.

For generating a single number, try the following

  library(mvtnorm)
  sigma <- rbind(c(3, 1), c(1, 2))
  cc <- 2
  while (1) {
      v <- rmvnorm(1, sigma=sigma)
      if (v[2] > cc) break
  }
  x <- v[1]

For a larger number, try something like the following

  n <- 500
  for (k in 2^(1:10)) {
      v <- rmvnorm(k*n, sigma=sigma)
      x <- v[v[, 2] > cc, 1]
      if (length(x) >= n) break
  }
  stopifnot(length(x) >= n)
  x <- x[1:n]

Hope this helps.

Petr Savicky.

Trying to use pipes in R

Wed, 12 Sep 2012 11:42:31 -0700 Post Comments

Hi.

Try the following.

  writeLines(c("uno dos tres", "cuatro cinco", "seis"), "some_file.txt")
  out <- system("wc some_file.txt", intern=TRUE)
  out

  [1] " 3  6 31 some_file.txt"

Here, out is a character variable.

Hope this helps.

Petr Savicky.

Identifying duplicate rows?

Mon, 10 Sep 2012 12:11:32 -0700 Post Comments

Hi.

Try the following.

  dfA <- cbind(State="VA", data.frame(Value=c(73, 73, 76, 76, 74, 75)))
  dfA$dups <- duplicated(dfA$Value) | duplicated(dfA$Value, fromLast=TRUE)
  dfA

    State Value  dups
  1    VA    73  TRUE
  2    VA    73  TRUE
  3    VA    76  TRUE
  4    VA    76  TRUE
  5    VA    74 FALSE
  6    VA    75 FALSE

Hope this helps.

Petr Savicky.
Group(s)
Profile Widget
Copy and paste this HTML code to your blog or website: