English 中文(简体)
Simulate Poisson的配送,使用反向的民防部队方法,与不同的拉姆布达斯
原标题:Simulate Poisson distribution with different lambdas using inverse CDF method

我必须产生波松回归,因为我想使用反常的民防部队方法。 下面是,即生成数据的员额。 但是,这是以反常 c法为基础的。 该员额遵循法典

>   #sample size
> n <- 10
>   #regression coefficients
> beta0 <- 1
> beta1 <- 0.2
>   #generate covariate values
> x <- runif(n=n, min=0, max=1.5)
>   #compute mu s
> mu <- exp(beta0 + beta1 * x)
>   #generate Y-values
> y <- rpois(n=n, lambda=mu)
>   #data set
> data <- data.frame(y=y, x=x)
> data

I do not want to use the r built in function rpois. I have found this post which does utilize the method only for one lambda. Now, how can I generate Poisson distribution with inverse cdf method for a variety of lambda? N.B. This useful post provides code for variety of sample sizes, I do not want that.

问题回答

The answer here is elegant and fast, but if you don t care about efficiency then

my_rpois <- function(n, lambda) {
   qpois(runif(n), lambda)
}

(lambda) 能够成为病媒

• 在你的联系职位上,修改我的答复,以掌握费率的矢量:

rpois2 <- function(n, lambda) {
  u <- runif(n)
  i <- 0L
  k <- integer(n)
  p <- exp(-lambda)
  f <- p
  idx <- 1:n
  
  while (length(idx)) {
    bln <- u[idx] < f[idx]
    k[idx[bln]] <- i
    idx <- idx[!bln]
    p[idx] <- lambda[idx]*p[idx]/(i <- i + 1L)
    f[idx] <- f[idx] + p[idx]
  }
  
  k
}

测试:

n <- 10
beta0 <- 1
beta1 <- 0.2
x <- runif(n=n, min=0, max=1.5)
mu <- exp(beta0 + beta1 * x)
system.time({y <- replicate(1e5, rpois2(n, mu))})
#>    user  system elapsed 
#>    1.87    0.01    1.89
system.time({z <- replicate(1e5, rpois(n, mu))})
#>    user  system elapsed 
#>    0.35    0.06    0.40
data.frame(
  mean.y = rowMeans(y),
  mean.z = rowMeans(z),
  var.y = apply(y, 1, var),
  var.z = apply(z, 1, var)
)
#>     mean.y  mean.z    var.y    var.z
#> 1  2.82232 2.81855 2.826438 2.814754
#> 2  2.89834 2.91290 2.905394 2.914763
#> 3  3.41615 3.41951 3.416323 3.418016
#> 4  3.47183 3.47874 3.473361 3.460303
#> 5  3.26286 3.25541 3.261857 3.244148
#> 6  3.28565 3.28160 3.269067 3.288014
#> 7  3.49654 3.48724 3.485543 3.484132
#> 8  3.59513 3.60039 3.596586 3.604258
#> 9  3.39864 3.38836 3.378420 3.388850
#> 10 2.89590 2.89425 2.887852 2.901856




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签