Monte Carlo Simulation in R
Before running things …
Wait for WEBR STATUS to be “Ready!”, before running the code. You can definitely modify the code, so experiment away!
You will be running code which enables you to carry out Monte Carlo simulations. These simulations are really ways for you to imagine hypothetical data or fake data in order to shed light on the procedures you see in practice.
Learn bits of R
Gain a sense of what these commands are doing. Don’t offload this to a generative AI. Figure it out. Play around with the commands because you can edit the commands presented and run them interactively.
Let us put everything together. Run the script below some number of times to get a feel for what is happening. What are the emerging patterns? What stays the same? What changes?
In terms of R: What are the crucial parts? Which are the bells and whistles?
Explore further
- Focus on
x <- rbinom(10, 1, 0.5). - Change \(n\) from 10 to 1000, while holding everything else constant. Run the previous code a few times. Emerging patterns?
- Change \(p\) from 0.5 to 0.05, while holding everything else constant. Run the previous code a few times. Emerging patterns?
- Change \(n\) from 10 to 1000 and \(p\) from 0.5 to 0.05, while holding everything else constant. Run the previous code a few times. Emerging patterns?
- Because we run this code repeatedly, it might be good to turn it into something like a “module” where you have user inputs and the main output is the plot. Maybe you can explore this aspect – do a dive into creating functions in R. Refer to Lesson 16 of fasteR.
Connections to the things you might be doing soon
What is the line
z <- cumsum(x)/(1:length(x))actually doing? Can you articulate it?The pictures you have been generating are a testament to the fact that a certain type of variation exists in “nature”. This is called sampling variation.
Let \(n\) be the number of observations. For \(t=1,\ldots, n\), define \(X_t\) be the \(t\)th observation of a characteristic measured by \(X\).
- In the R code, \(X\) takes on two values: 0 or 1.
- So, we have \(X_1,X_2,\ldots,X_n\).
- What does \(X_1+X_2+\ldots+X_n\) represent?
- After dividing the sum by \(n\), what do we obtain?
Re-express the final result in the previous item by collecting all the “zeros” together and all the “ones” together. Use words here to express the mathematical expressions. What do you notice?
More Monte Carlo simulations
Behavior of the sample mean
What happens when you increase \(n\) further by a factor of 4, holding everything else constant?
Now change 0.5 to 0.05. What do you notice?
Behavior of regression coefficients
You will be exploring what lm() does in an artificial setting.
Randomly select a word from the sentence “I SEE THE MOUSE”.
- Record how many letters and how many E’s there are in the chosen word.
- Return the word to the sentence.
Repeat \(n\) times.
Run a regression of the number of letters on the number of E’s.
Repeat all previous steps \(10^4\) times.
What happens when you increase \(n\) further by a factor of 4, holding everything else constant?