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.0000000
 [2,] 1 0 0.0000000
 [3,] 1 1 0.8920037
 [4,] 1 1 1.6695435
 [5,] 1 1 0.8289429
 [6,] 2 0 0.0000000
 [7,] 2 0 0.0000000
 [8,] 2 1 0.8289429
 [9,] 2 1 0.8920037
[10,] 2 1 1.6695435
[11,] 3 0 0.0000000
[12,] 3 0 0.0000000
[13,] 3 1 1.6695435
[14,] 3 1 0.8289429
[15,] 3 1 0.8920037

So, notice that the normal random variable only takes 3 values, and same as
the binary part, it's all 0 0 1 1 1 for each stratum. Is there way to make
them complete randomly different over the strata ?

Thank you again!
Carrie



On Thu, Jun 3, 2010 at 7:24 PM, Carrie Li <carrieands...@gmail.com> wrote:

> 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 again!
>
>
>
>
> On Thu, Jun 3, 2010 at 12:21 PM, Jorge Ivan Velez <
> jorgeivanve...@gmail.com> wrote:
>
>> 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, function(comp)
>>                 with(comp, ifelse(t == 0, rbinom(sum(t==0), 1, 0.2),
>> rbinom(sum(t!=0), 1, 0.8)))))
>>
>> HTH,
>> Jorge
>>
>>
>> On Thu, Jun 3, 2010 at 11:49 AM, Carrie Li <> wrote:
>>
>>>  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
>>> t=rep(c(0,0,1,1,1),3)
>>>
>>>
>>> ## and within strata i and t=0 and t=1, I generate the random binomial
>>> variable respectively, and save in y
>>> y=rep(NA, length(x))
>>> for (i in 1:3)
>>> {
>>> y[(x==i)&(t==0)]=rbinom(2, 1, 0.2)
>>> y[(x==i)&(t==1)]=rbinom(3, 1, 0.8)
>>> }
>>>
>>>
>>> My question: is there any way to avoid the for loop, since I have the
>>> first
>>> level strata has thousands of strata. (Within each x stratum, the t only
>>> has
>>> 2 levels, 0 and 1 )
>>>
>>> Thanks for any help!
>>>
>>> Carrie
>>>
>>>        [[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]]

______________________________________________
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.

Reply via email to