Created
July 27, 2014 06:51
-
-
Save anonymous/f3431bd846bafb32f8b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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