Skip to content

Instantly share code, notes, and snippets.

Created July 27, 2014 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f3431bd846bafb32f8b0 to your computer and use it in GitHub Desktop.
Save anonymous/f3431bd846bafb32f8b0 to your computer and use it in GitHub Desktop.
## allocate 'events' events among 'times' times
## what is the maximum number in an interval of width 'scanwidth'?
## return table based on N replications
scanFixedTotal<-function(times, events, scanwidth,N){
values<-replicate(N,{
x<-numeric(times)
for(i in sample(times,events)){
x[i]<-x[i]+1
}
max(rowSums(embed(x,scanwidth)))
}
)
table(values)
}
## simulate events at rate 'events/times' for 'times' times
## what is the maximum number in an interval of width 'scanwidth'?
## return table based on N replications
scanRandomTotal<-function(times, events, scanwidth,N){
values<-replicate(N,{
x<-rpois(times,events/times)
max(rowSums(embed(x,scanwidth)))
}
)
table(values)
}
##example: 91 fatal crashes involve 18+ people in 10 years
scanRandomTotal(3650,91,8,10000)
scanFixedTotal(3650,91,8,10000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment