Dear R-helpers,
I would like to generate a binary random variable within a stratum's
stratum. Here is a simple example.
## x is the first level strata index, here I have 3 strata.
x=c(rep(1,5), rep(2,5), rep(3,5))
## within x, there is a second strata indexed by t=0 and t=1
Hi Carrie,
Here are two options:
# Option 1
d - data.frame(x, t)
y - with(d, ifelse(t == 0, rbinom(2, 1, 0.2), rbinom(3, 1, 0.8)))
y
# Option 2 -- more general case, e.g. you do not know
# how many 0's and 1's you have within each strata
spd - with(d, split(d, x))
do.call(c, lapply(spd,
Thanks! Jorge
Just one more question I don't get it even after checking help
For option, why just using with(d,...), ifelse works on stratum indexed by x
automatically ?
Since in with, we didn't specify the stratum is indexed by x, what if you
have another categorical variable in the data ?
Thanks
Hi Jorge,
I found a problem.
I just want to check if the answer is random, I change the code as follows:
d - data.frame(x, t)
y - with(d, ifelse(t == 0, rbinom(2, 1, 0.5), rnorm(3)))
cbind(x, t,y)
x t y
[1,] 1 0 0.000
[2,] 1 0 0.000
[3,] 1 1 0.8920037
[4,] 1 1
Hi Carrie,
It works just fine in this case because you have the same number of 0's and
1's within each strata. If that would not be the case, option 1 would not
work. That's why I provided you a second option.
Best,
Jorge
On Thu, Jun 3, 2010 at 7:24 PM, Carrie Li wrote:
Thanks! Jorge
Just
Yes, in my case here, each strata has the same number of 0's and 1's. But I
want the y to be randomly generated within each strata, so y should have
some difference across the strata. (at least for the rnorm part we would see
much clear randomness)
(I hope what I am asking here is clear to you. )
Hi Carrie,
Try running the following:
# function to create y using binomials and normals
# -- this function is based on option 2
makey - function(d){
spd - with(d, split(d, x))
do.call(c, lapply(spd, function(comp)
with(comp, ifelse(t == 0, rbinom(sum(t==0), 1, 0.2),
7 matches
Mail list logo