seldak
seldak

Reputation: 291

Generating random numbers while maintaining average

I'm trying to model traffic captured every one second throughout the course of a day. I have an average traffic value for every hour and want to create a random vector to model that average traffic.

I tried the rpois to model three hours with averages 20,10 and 30 percent as follows:

r<-vector()   
lambda <- c(20,10,30)
for (i in 1:length(lambda)) {
    r <- append( r, rpois(3600,lambda[i]) ) # 3600 = number of seconds in 1 hour 
}
plot(r, col='red', type='l')

By examining the plot, there is a sudden change in the transition between hours, which makes the data not very realistic. My question is there a way in R to smooth the values between transitions, yet maintain the averages?

Upvotes: 1

Views: 139

Answers (1)

LPH
LPH

Reputation: 1295

i would guess no - because the values orientate themself on the average values == if the average value "jumps", the random value "jump";

but of course you can "smooth" the values between the transitions manually with desired "Bridge-Average-Valuse":

r<-vector()   
lambda <- c(20,15,10,21,30)
for (i in 1:length(lambda)){

    if (lambda[i]%%10>0) {
        r <- append( r, rpois(100,lambda[i]) ) 
    } else {
            r <- append( r, rpois(3200,lambda[i]) )
    }
   }
plot(r, col='red', type='l')

Upvotes: 2

Related Questions