2

First of all, I apologize for asking a silly question because I'm relatively new to R.

I've been trying for hours to fix this issue but I can't get it right. I've also tried to search some relevant topics on Google, but to no avail. I tried to create a function in R which can perform a Monte Carlo simulation which can't be properly handled in VBA. Here are the two functions I created:

ZoomRNG1 <- function(n,Px1,Px2,Py1,Py2)
{
  sink("DataZoomRNG1.txt")
  for(i in 1:n)
  {
    repeat
    {
      x = runif(1)
      y = runif(1)
      if(x > Px1 & x < Px2)
      {
        if(y > Py1 & y < Py2)
        {
          cat(i, x, y, "\n", sep="\t", append=TRUE);
          break
        }
      }
    }
  }
  sink()
}

and

ZoomRNG2 <- function(n,Px1,Px2,Py1,Py2)
{
  logFile = "DataZoomRNG2.txt"
  for(i in 1:n)
  {
    repeat
    {
      x = runif(1)
      y = runif(1)
      if(x > Px1 & x < Px2)
      {
        if(y > Py1 & y < Py2)
        {
          cat(i, x, y, file=logFile, append=TRUE, "\n", sep="\t");
          break
        }
      }
    }
  }
}

Both functions work fine and the plot of ZoomRNG1 function outputs (1,000 data for each random variables x and y) in Excel is shown in the figure below:

enter image description here

But the problems are

  1. Both functions run relatively slow on my machine. OS: Windows 10 Pro 64-bit; System Model: HP Pro 3330 MT; Processor: Intel Core i3-2120 CPU @ 3.30GHz (4 CPUs); Memory: 4 GB.
  2. Some of their outputs' format are broken, for example

389     0.5067888       0.5064327   
390     0.50462 0.5083072   
391     0.5040369       0.5075297   
392     0.5094  0.5064151   
393     0.5068058       0.5041317   
394     0.5073923       0.5002383   

As one can see above, the formats of line 390 and 392 are broken (some of other lines are broken, too). How does one fix this issue? Also, is there a way to make both functions run faster? Something like (maybe?) writing the outputs of the function in array/ vector first and then exporting the result to an external file like I usually do when writing the program in VBA.

Community
  • 1
  • 1

1 Answers1

2

We can simplify looped functions to:

write.table(data.frame(x = runif(n, Px1, Px2),
                       y = runif(n, Py1, Py2)),
            "DataZoomRNG1.txt", sep = "\t")

Regarding "broken format", it is not broken, it is tab separated. enter image description here

Avoid Excel copy paste, use R plot instead, see example:

n = 100
Px1 = 10
Px2 = 20
Py1 = 5
Py2 = 7

set.seed(42)
df1 <- data.frame(x = runif(n, Px1, Px2),
                  y = runif(n, Py1, Py2))

plot(df1)

As @Roland demosntrated with set.seed, forloop and runif output should be the same:

set.seed(42)
for (i in 1:5) print(runif(1))
# [1] 0.914806
# [1] 0.9370754
# [1] 0.2861395
# [1] 0.8304476
# [1] 0.6417455

set.seed(42)
runif(5)
# [1] 0.9148060 0.9370754 0.2861395 0.8304476 0.6417455
zx8754
  • 52,746
  • 12
  • 114
  • 209
  • Thanks for your answer and yes, it's really fast. But how do I ensure that the output of your way will follow the same distribution as the output of my way? Do you have a mathematical reasoning to support the claim? (+1) – Anastasiya-Romanova 秀 Aug 31 '16 at 11:03
  • 1
    @Anastasiya-Romanova秀 as Roland showed in the comments, `for loop`and `runif` on its own will give you the same result? – zx8754 Aug 31 '16 at 11:05
  • Regarding the broken format, when I tried to copy the output in Notepad and then paste it to Excel, it wasn't separated properly so I thought it's broken – Anastasiya-Romanova 秀 Aug 31 '16 at 11:05
  • Seeing from the chart, it seems there's no different but I haven't done any fitting test yet to check it. – Anastasiya-Romanova 秀 Aug 31 '16 at 11:07