Beruflich Dokumente
Kultur Dokumente
R
# Bret Larget
# January 23, 2009
getN = function(x,tt=1) {
if(sum(x)<tt)
return(NA)
if(x[1]>tt)
return(0)
else
return(max(which(cumsum(x)<tt)))
}
# EXAMPLE:
# Create an initial vector of time intervals
# Notice that the event times are 0.1, 0.7, 1.2, 1.5, 2.9
# > x = c(0.1,0.6,0.5,0.3,1.4)
#
# Call getN() on this vector; by default, the time is 1
# In the output, the [1] indicates that the row begins with the first element
# There are exactly two event times before the time 1
# > getN(x)
# [1] 2
#
# Now call getN() with the second argument equal to 2
# Notice that there are four events before time 2
# > getN(x,2)
# [1] 4
#
# Now call getN() for time 5.
# Since all events occur before time 5, we do not know of the next event would be before or after time 5.
# Hence, the program returns NA for a missing value.
# > getN(x,5)
# [1] NA
#
# The cumsum() function computes the cumulative sum of a vector; here the event times.
# > cumsum(x)
# [1] 0.1 0.7 1.2 1.5 2.9
################################################################################
# simPP() --- function to simulate a Poisson process many times and return counts
# arguments:
# ntrials = the number of times to repeat the Poisson process simulation
# tt = the length of the time interval
# lambda = the rate parameter
# return value:
# a vector of counts, one for each trial, of the number of events before time tt
#
################################################################################
converted by Web2PDFConvert.com
simPP = function(ntrials,tt=1,lambda=2) {
maxN = round(qpois(0.99999,tt*lambda))+10 # large enough so that the chance of needing a larger sample is tiny
x = matrix(rexp(ntrials*maxN,lambda),ntrials,maxN)
return(apply(x,1,getN,tt=tt))
}
# How the code works:
# The general idea is to create a matrix of iid random exponential random variables
# so that each row corresponds to a single realization of a Poisson process.
# Enough random variable are generated so that it is very improbable that the sum in any row will be below the
target time tt.
# So, x is an ntrials by maxN matrix.
# The function apply() is used to apply a function to either the rows or columns of a matrix.
# The command apply(x,1,getN,tt=tt) instructs R to take the matrix x
# and apply to each row (1 stands for the first dimension of the two-dimensional array, (row,column))
# the function getN. The additional argument tt=tt is passed on to getN().
# Here, the left tt matches the name of an argument in getN() and the right tt matches the name
# of an object already defined in simPP(), here as one of its arguments.
# So, this code applies getN() to each row of x and returns the answer as a vector of length ntrials,
# the number of rows of x.
converted by Web2PDFConvert.com