 MattnDo - 3 years ago 50
R Question

# Optimise code for a simple monte carlo like simulation

I run the following code that works but just take ages and I'm sure there is a way to get the same results much faster.

``````runs <- 1000
prediction <- runif(77,0,1)
n< - length(prediction)
df.all <- data.frame(Preds = rep(prediction, runs),
simno=rep(1:runs,each=n))

for (x in 1:runs) {
for (i in 1:length(df.all\$Preds)){
df.all\$rand[i] <- sample(1:100,1)
df.all\$Win[i] <- ifelse(df.all\$rand[i]<df.all\$Preds[i]*100,1,0)
}
}

df.all% >% group_by(simno) %>% summarise(Wins=sum(Win)) -> output
`````` csgillespie

This can easily be vectorise by:

• Performing a single sample operation (not the additional `replace = TRUE` argument.
• Performing a single comparison `>`

You can remove the inner for loop to get

``````for (x in 1:runs) {
df.all\$rand = sample(1:100, size = length(prediction), replace=TRUE)
df.all\$Win = df.all\$rand < df.all\$Preds*100
}
``````

You can then take it one step further and remove that loop

``````df.all\$rand = sample(1:100, n = nrow(df.all), replace=TRUE)
df.all\$Win = df.all\$rand < df.all\$Preds*100
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download