[R] R function help!

Pete Brecknock Peter.Brecknock at bp.com
Tue Feb 19 00:01:58 CET 2013


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



--
View this message in context: http://r.789695.n4.nabble.com/R-function-help-tp4658998p4659001.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list