12

When I try to run the following code for 10000 iterations I get the following error.Error in rep(G1[, 2], G1[, 3]) : invalid 'times' argument. So don't know how to change the code to fix that error. Basically just want to create time series for the generator performance using the equation for Time to fail and time to repair for 8736 hours in the year so that I have the time series in hours when the generator is operating in when is not. The starting conditions is that the generator is operating on the first hour. For sure there is a more elegant solution for simulating this I'm just not able to find it. Any comment or help will be appreciated.

MTTF<-2940 # MEDIUM TIME TO FAIL(hours)
MTTR<-60 # MEDIUM TIME TO REPAIR (hours)
TTF<--MTTF*log(runif(100))# equation for Time to fail 
TTR<--MTTR*log(runif(100))# equation for Time to repair
mix<-rep(0,length(TTF)+length(TTR))
sw<-rep(0,length(TTF)+length(TTR))
for(i in 1:length(TTF)){
mix[2*i-1]<-TTF[i]
sw[2*i-1]<-1
mix[2*i]<-TTR[i]
}
cmix<-cumsum(mix)
ccmix<-cbind(cmix[1:which(cmix>8736)],sw[1:which(cmix>8736)])
ccmix[dim(ccmix)[1],1]<-8736


G1<-round(ccmix)
# transform binary values
G1[G1 == 1] <- 12 # is the capacity of the generator

G1 <- cbind(G1, c(G1[1,1], diff(G1[,1])))
a1 <- rep(G1[,2], G1[,3]) ## GENERATING 8736 Values

So the desired output are 8736 values of 12 when is ON and 0 when is OFF

kelamahim
  • 577
  • 1
  • 4
  • 21
  • Maybe there is a better approach for generating time series from a available data – kelamahim Feb 18 '16 at 10:21
  • I don't get what you're trying to achieve, can you create an example of desired output from short input ? – Tensibai Feb 18 '16 at 10:29
  • `1:which(cmix > 8736)` <- This is strange as which will return a vector and 1:c(1,2,3) will only be 1:1 ...sorry but I don't get the goal of all this and I'm pretty sure your error come from an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Your code show an attempt to solve a problem, but the problem itself is unclear. – Tensibai Feb 18 '16 at 10:43
  • For sure there is a better approach for generating hourly data from the generator using the above equations. Just want to have 8736 hourly values on the generation so that I know the generator is operating for 4555 hours then not operating for 160 hours then again operating and so on.. – kelamahim Feb 18 '16 at 10:49
  • and then simulating this for 1000 times – kelamahim Feb 18 '16 at 10:50
  • Ok, I got it after rereading your question again (problem is in fact stated, but down the line of the rep problem :p) – Tensibai Feb 18 '16 at 10:50
  • 1
    There may be a better way to do this, but with Adela's answer you can fix the script so that it works. If you want someone to look at your code extensively (to see if you can write it more efficiently), try our sister website [Code Review](http://codereview.stackexchange.com/). – slamballais Feb 18 '16 at 11:18

1 Answers1

13

Check what G1[,3] is. Error can be caused by negative values in times argument

Adela
  • 1,757
  • 19
  • 37
  • 1
    there are no negative values – kelamahim Feb 18 '16 at 10:43
  • Well, I run your code and another warning occurred. I would probably use `cmix[which(cmix>8736)]` instead of `cmix[1:which(cmix>8736)]`. However there is the negative value in `G1[,3]` (the last one) – Adela Feb 18 '16 at 10:49
  • 1
    Maybe my whole approach is wrong. Will check the code – kelamahim Feb 18 '16 at 10:53
  • 2
    @Adela: Nice job, it took me a while to replicate it, but the problem is pretty straightforward and is indeed caused by negative values. `G1[G1==1] <- 12` checks all of `G1` for values of `1`. If the first value of the left column is `1` and the second value of the left column is below `12`, it will therefore produce a negative difference. One way to solve that is `G1[,2][G1[,2] == 1] <- 12`. – slamballais Feb 18 '16 at 11:04