Vectorizing sample()

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Vectorizing sample()

Stephen Collins-6
I am simulating sickness among a group of families.  Part of the task is
to randomly draw who in the family will be sick, randomly drawing from
family ID's where Dad =1, Mom = 2, Kid1 = 3, Kid2 = 4., etc.  My census of
Dads is of the form shown below.

Dad_ID  Spouse (Y=1;N=0)                #Kids #People_Becoming_Sick
1               1                               0               1
2               0                               2               2
3               1                               0               2
4               1                               3               3
...

The end output needed is if 3 people in a family are to be sick, was it
the dad and two kids, with random family ID's =  {1,3,4}, or the mom, dad,
and one kid,  with random family ID's =  {2,1,4}, etc..  The complication
is that length of the family ID's to choose from and the associated
sampling probabilities -- changes with each family.   I could loop through
the Dads, from i in 1:nrow(census), but is there a way I could vectorize
sample() to get at the same objective?

My attempts to use the apply-based functions have dead ended.  Other ideas
to vectorize this problem are warmly welcomed.



Regards,
 
Stephen Collins, MPP | Analyst
Health & Benefits | Aon Consulting
200 East Randolph, Suite 900, Chicago, IL
Tel: 312-381-2578 | Fax: 312-381-0136
Email: [hidden email]
 
Aon Consulting selected by the readers of Business Insurance as the “Best
Employee Benefit Consulting Firm” in 2006, 2007, and 2008
 
NOTE: The information contained in this transmission, including any
attachment(s) is only for the use of the intended individual(s) or entity,
and may contain information that is privileged and confidential. If the
reader of this message is not an intended recipient, you are hereby
notified that any dissemination, distribution, disclosure, or copying of
this information is unauthorized and strictly prohibited. If you have
received this communication in error, please contact the sender
immediately by reply email and destroy all copies of the original message.
 

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Vectorizing sample()

Duncan Murdoch
On 11/7/2008 12:00 PM, Stephen Collins wrote:

> I am simulating sickness among a group of families.  Part of the task is
> to randomly draw who in the family will be sick, randomly drawing from
> family ID's where Dad =1, Mom = 2, Kid1 = 3, Kid2 = 4., etc.  My census of
> Dads is of the form shown below.
>
> Dad_ID  Spouse (Y=1;N=0)                #Kids #People_Becoming_Sick
> 1               1                               0               1
> 2               0                               2               2
> 3               1                               0               2
> 4               1                               3               3
> ...
>
> The end output needed is if 3 people in a family are to be sick, was it
> the dad and two kids, with random family ID's =  {1,3,4}, or the mom, dad,
> and one kid,  with random family ID's =  {2,1,4}, etc..  The complication
> is that length of the family ID's to choose from and the associated
> sampling probabilities -- changes with each family.   I could loop through
> the Dads, from i in 1:nrow(census), but is there a way I could vectorize
> sample() to get at the same objective?
>
> My attempts to use the apply-based functions have dead ended.  Other ideas
> to vectorize this problem are warmly welcomed.
You might want to transform runif instead of using sample().  For
example, if you want to generate M integers from 1:n_i, where n_i varies
from sample to sample, you could use

gen <- ceiling(runif(M, 0, n))

(where n is a vector of length M giving the upper limits).

Duncan Murdoch


>
>
>
> Regards,
>  
> Stephen Collins, MPP | Analyst
> Health & Benefits | Aon Consulting
> 200 East Randolph, Suite 900, Chicago, IL
> Tel: 312-381-2578 | Fax: 312-381-0136
> Email: [hidden email]
>  
> Aon Consulting selected by the readers of Business Insurance as the “Best
> Employee Benefit Consulting Firm� in 2006, 2007, and 2008
>  
> NOTE: The information contained in this transmission, including any
> attachment(s) is only for the use of the intended individual(s) or entity,
> and may contain information that is privileged and confidential. If the
> reader of this message is not an intended recipient, you are hereby
> notified that any dissemination, distribution, disclosure, or copying of
> this information is unauthorized and strictly prohibited. If you have
> received this communication in error, please contact the sender
> immediately by reply email and destroy all copies of the original message.
>  
>
> [[alternative HTML version deleted]]
>
>
>
> ------------------------------------------------------------------------
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Vectorizing sample()

Kenn Konstabel
In reply to this post by Stephen Collins-6
Hi,

I'm not quite sure I understood everything but is this something close?

d <- read.table(textConnection("Dad_ID  SpouseYN  NKids  NSick
1              1    0      1
2              0    2      2
3              1    0      2
4              1    3      3"), header=TRUE)

mapply(sample, d$NKids+d$SpouseYN+1, d$NSick)

# d$NKids+d$SpouseYN+1 is supposed to be family size

Best regards,
Kenn

On Fri, Nov 7, 2008 at 7:00 PM, Stephen Collins <[hidden email]>wrote:

> I am simulating sickness among a group of families.  Part of the task is
> to randomly draw who in the family will be sick, randomly drawing from
> family ID's where Dad =1, Mom = 2, Kid1 = 3, Kid2 = 4., etc.  My census of
> Dads is of the form shown below.
>
> Dad_ID  Spouse (Y=1;N=0)                #Kids #People_Becoming_Sick
> 1               1                               0               1
> 2               0                               2               2
> 3               1                               0               2
> 4               1                               3               3
> ...
>
> The end output needed is if 3 people in a family are to be sick, was it
> the dad and two kids, with random family ID's =  {1,3,4}, or the mom, dad,
> and one kid,  with random family ID's =  {2,1,4}, etc..  The complication
> is that length of the family ID's to choose from and the associated
> sampling probabilities -- changes with each family.   I could loop through
> the Dads, from i in 1:nrow(census), but is there a way I could vectorize
> sample() to get at the same objective?
>
> My attempts to use the apply-based functions have dead ended.  Other ideas
> to vectorize this problem are warmly welcomed.
>
>
>
> Regards,
>
> Stephen Collins, MPP | Analyst
> Health & Benefits | Aon Consulting
> 200 East Randolph, Suite 900, Chicago, IL
> Tel: 312-381-2578 | Fax: 312-381-0136
> Email: [hidden email]
>
> Aon Consulting selected by the readers of Business Insurance as the “Best
> Employee Benefit Consulting Firm† in 2006, 2007, and 2008
>
> NOTE: The information contained in this transmission, including any
> attachment(s) is only for the use of the intended individual(s) or entity,
> and may contain information that is privileged and confidential. If the
> reader of this message is not an intended recipient, you are hereby
> notified that any dissemination, distribution, disclosure, or copying of
> this information is unauthorized and strictly prohibited. If you have
> received this communication in error, please contact the sender
> immediately by reply email and destroy all copies of the original message.
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> [hidden email] 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]]


______________________________________________
[hidden email] 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.