[R] R function help!

Rolf Turner rolf.turner at xtra.co.nz
Tue Feb 19 03:54:45 CET 2013


Pete:  Please don't do people's homework for them!
At most, give them a ***hint*** only.

     cheers,

         Rolf Turner

On 02/19/2013 12:01 PM, Pete Brecknock wrote:
> simonj16 wrote
>> Consider an urn that contains 10 tickets, labelled: 1,1,1,1,2,5,5,10,10,10
>>
>> I want to draw with replacement n=40 tickets. I am interested in the sum,
>> Y, of the 40 ticket values that I draw
>>
>> Write an R function named urn.model that simulates this experiement. What
>> I have below is not working.
>>
>> flip.n = function(p,n) {
>> 	return(runif(n,0,1) < p)
>> }
>> ticket.ns<-c(1,1,1,1,2,5,5,10,10,10)
>> urn.model = function(ticket.ns) {
>> 	draws.per.sim = 1
>> 	prob = .1
>> 	urn.results = rep(-1, ticket.ns)
>> 	for (i in 1:ticket.ns) {
>> 		draws = flip.n(prob,draws.per.sim)
>> 		num =sum(draws,ticket.ns)
>> 		urn.results[i] = num
>> 	}
>> 	return(urn.results)	
>> }
>> urn.25.samples =urn.model(25)
>>
>> urn.25.samples
>>
>> Follow up question:
>>
>> Use urn.model to generate a sample y={y1,...,y25) of n=25 observed sums.
> Any good?
>
> ticket.ns<-c(1,1,1,1,2,5,5,10,10,10)
>
> draw=NULL
> for (i in 1:25){
>    draw[i] <- sum(sample(ticket.ns,40,replace=TRUE))
> }
>
> print(draw)
>
> HTH
>
> Pete



More information about the R-help mailing list