I've been taking a online course in R programming. The long term hope with learning R is that I'll be more competitive in the job market, in comparison to my peers. But, in the short term I've been learning a lot. And one of the homework assignments was to test the law of large numbers with a program I coded. I was successful so I thought I'd share my results.

But first:

What is the law of large numbers?

The law of large numbers is a fairly simple idea. When you take a sample of a large group, there will be two means. The sample mean, gathered from the survey/sample, and the true mean for the population. As you increase the sample size your mean gets more accurate or it approaches the true population mean.

Or in other words, adding more people/data into your sample increases the chances that your result will be accurate (assuming the people are randomly selected).



Formula - Picture from Wikipedia

But the challenge I was given was testing this idea in R.

The way I accomplished it was by using two variables, a for loop, and a if statement.

I used the following code:

N <- 10

counter <- 0

for(x in rnorm(N)){

if(x > -1 & x < 1){

counter <- counter + 1}

}

answer <- counter / N

answer

Then all you have to do is change the variable N to increase or decrease it in size.

Here are the results for various N variables. The smaller the value of N, the greater the variation. Try it in your own Rstudio.

N is 10, answer is 0.5

N is 100, answer is 0.63

N is 1000, answer is 0.704

N is 10000, answer is 0.684

N is 1000000, answer is 0.683169

N is 10000000, answer is 0.682751

So as we can see the population mean is 0.682

We can continue to increase the value of N but I think we see the point here. It was a fun little program to write!