I have some trouble writing out a monte carlo simulation properly.
I assign variables for the process before the steps.
The first step is assigning variable j = j + 1
Second step i draw three random numbers from a U(0,1) dist. u1, u2 and u3.
Third step is a long if formula. It has to be a function of j, V(j), so as j increases it should record a new output. The result in this step is what is used in the end to create the final result.
Fourth step is another if forumla: if(j = n) j = 0 && s = s +1 && a long formula
Fifth step is another if formula if s < S then loop to step 3
How do I create this in an efficient manner? Some of the if formulas are rather long.
You shouldn't generate 3 random numbers in each loop of the simulation.
Let's say you want 2000 simulations. Then generate a 2000*3 matrix of random numbers as your first step, and in each loop/sapply/lapply of your monte carlo use MATRIX[i,] to slice off 3 new random numbers. It should be faster that way.
Why don't you write the loop yourself then submit it for critique?
I don't understand if(u[,i]<delta) for i=1,2,3. u[,i] is a vector of 100 numbers and delta is 1 number. What does it mean to say that 100 numbers is less than 1 number?
If you mean to say that every number in the vector is less than delta, well that's fine, but you have to tell R this properly.
This is why you're getting the error message that you're getting.
if(sum(u[,1]>delta)==0)DO SOMETHING CONDITIONAL UPON EVERY SINGLE VALUE IN u[,1] BEING LESS THAN DELTA.
Will give you what you want. To see what this is doing, type in u[,1]<delta. Then, sum just counts how many TRUEs there are.
By the way I think you want u[1,] and u[2,], because you want 3 numbers (a row) not 100 (a column).
EDIT based on re-reading your OP:
I suspect that what you want is to test individual numbers in these if statements. Do a for loop or a sapply over 1 to 100, and use u[i,1], u[i,2], u[i,3] (i'th row, where i is in 1 to 100, and the 1st 2nd or 3rd number in that row).
As far as I can see this is the setup I have used for the if formulas. I think the problem occurs because I need to include the second if statement inside the first if statement? I keep getting an '}' unexpected, or 'else' unexpected error in R.
Is my problem in the if formulas setup?
Also you're still trying to test whether a bunch of numbers (e.g. u[,1]) is bigger or smaller than a single number (e.g. alpha), which as I've said doesn't make any sense. The "<" and ">" symbols test for 1 number. They can be used for vectors but not in the way you're doing it, and that's a bit too advanced for you.
Also here's a tip. Don't try to get your whole loop to work first off. Set j = 1 and see if it runs with that. And run tiny subsets of your loop for j=1 to identify exactly where the problem area is. If you find that there's an error but you don't break up the block of code into tiny bits and test each bit, then your approach to bug-testing is uber inefficient.