[R] Generation of binomial numbers using a loop

Phil Spector spector at stat.berkeley.edu
Wed Jun 23 23:18:07 CEST 2010


Sarah -
    If you're willing to forgo the loop,

  res = unlist(mapply(rbinom,frequency,no_trials,prob))
  res[res == 0] = 1

will give you what you want.

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu


On Wed, 23 Jun 2010, Sarah Sanchez wrote:

> Dea'R' helpers
>
> I have following data -
>
> prob = c(0.1, 0.2, 0.3, 0.4, 0.5)
> frequency = c(100, 75, 45, 30, 25)
> no_trials = c(10, 8, 6, 4, 2)
>
> freq1 = rbinom(frequency[1], no_trials[1], prob[1])
> freq2 = rbinom(frequency[2], no_trials[2], prob[2])
> freq3 = rbinom(frequency[3], no_trials[3], prob[3])
> freq4 = rbinom(frequency[4], no_trials[4], prob[4])
> freq5 = rbinom(frequency[5], no_trials[5], prob[5])
>
> total_frequency = c(freq1, freq2, freq3, freq4, freq5)
> total_frequency = as.numeric(lapply(total_frequency, function(x){replace(x, x == 0, 1)}))
>
> This helps me to generate (sum(frequency) = 275) non zero binomial numbers as
>
> total_frequency
>
> ? [1] 1 1 1 2 1 1 1 2 1 1 1 1 1 1 3 2 1 1 1 1 1 1 1 1 1 3 1 1 2 1 3 1 1 1 3 1 1 1 1 1 1 4 3 1 1 1 2 1 1 1 2 3 2 1 1 1 1 1 1
> 1
>
> ?[61] 2 1 1 1 1 1 1 1 1 1 2 1 1 2 1 1 1 1 1 2 3 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 3 2 3 1 3 1 2 3 1 1 1 1 2 1 3 1 2 1 1 2
>
> [121] 3 1 2 1 3 3 3 1 5 2 3 2 1 3 2 1 2 1 1 1 3 1 4 1 1 3 3 1 1 3 1 2 3 2 1 2 5 1 2 2 1 1 1 1 2 2 2 3 1 1 3 1 1 1 1 3 1 3 1 2
>
> [181] 1 2 1 2 1 2 1 2 2 4 2 2 1 4 1 1 3 2 4 1 1 3 1 3 1 1 1 2 2 1 3 1 1 4 1 1 3 2 1 3 3 1 1 2 1 1 2 3 3 1 1 2 3 2 1 1 1 2 1 1
>
> [241] 1 3 2 1 1 2 3 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 1 1 1
>
>
> ## My Problem
>
> However I am not sure whether I will be dealing with only "5" numbers. So I can have 'n' no.s of frequencies, no of trials and even probabilities. Thus I need to generate these numbers using a loop. Assuming I am once again dealing with above data only, I have tried to modify my R code as given below.
>
> n = length(frequency)
>
> freq = NULL
>
> for (i in 1:n)
> {
> freq[i] = rbinom(frequency[i], no_trials[i],
> prob[i])
> }
>
> I get following errors -
>
> Warning messages:
> 1: In freq[i] = rbinom(frequency[i], no_trials[i], prob[i]) :
> ? number of items to replace is not a multiple of replacement length
> 2: In freq[i] = rbinom(frequency[i], no_trials[i], prob[i]) :
> ? number of items to replace is not a multiple of replacement length
> 3: In freq[i] = rbinom(frequency[i], no_trials[i], prob[i]) :
> ? number of items to replace is not a multiple of replacement length
> 4: In freq[i] = rbinom(frequency[i], no_trials[i], prob[i]) :
> ? number of items to replace is not a multiple of replacement length
> 5: In freq[i] = rbinom(frequency[i], no_trials[i], prob[i]) :
> ? number of items to replace is not a multiple of replacement length
>
>
> I understand I have written a stupid code. I even tried to define freq in a matrix form as
>
> freq <- matrix(data=0, nrow=n, ncol=1) but the result is
> same.
>
> Please guide me as how I should be writing the required loop in order to take care of variable size of frequency input.
>
> Thanking you in advance for the guidance.
>
> Sarah
>
>
>
>
> 	[[alternative HTML version deleted]]
>
>



More information about the R-help mailing list