R vs. Julia in the for loop/sampling distribution exercise

In a previous post I gave a simple exercise to illustrate the sampling distribution. I figured it would be of interest to some to show how this exercise could be completed in R, or the language which I now use more frequently, Julia.

Here it is in R:

# Sampling Distribution Exercise in R
# Justin Angevaare
# June 18, 2015

# Generate 10000 samples from a normal distribution
population = rnorm(10000, 10, 10)

# Create an empty vector of length 10000 to store sample means
samplemeans = numeric(10000)

# Run the loop
for(i in 1:10000)
{
samplemeans[i] = mean(sample(population, 100))
}

# Create a histogram
hist(samplemeans)

# Calculate means and standard deviations
mean(samplemeans)
mean(population)
sd(samplemeans)
sd(population)

And in Julia!

# Sampling Distribution Exercise in Julia
# Justin Angevaare
# June 18, 2015

# Use the Distributions package for the generation of random variates
# Use the Gadfly package for visualization
using Distributions, Gadfly

# Generate 10000 samples from a normal distribution
population = rand(Normal(10, 10), 10000)

# Create a zero length Float64 vector
samplemeans = Float64[]

# Run the loop
for i = 1:10000
push!(samplemeans, mean(sample(population, 100)))
end

# Create a histogram
plot(x=samplemeans, Geom.histogram)

# Calculate means and standard deviations
mean(samplemeans)
mean(population)
std(samplemeans)
std(population)

Note the following differences:

R has special functions to generate samples from different distributions, or to calculate densities, quantiles from different distributions. In Julia you define the distribution, and then may do various things to that distribution (such as generate random samples).

In Julia, additional numbers can be easily added to a vector, so it is sufficient to define a vector of length zero. The push! function adds a single value, whereas append! can join several values to an existing vector. The ! function name syntax indicates the function modifies it’s argument.

for loop syntax is simpler in Julia vs. R. No brackets are required. A loop is ended in Julia with the word end, where as in R, curly braces indicate the scope of a loop.

Plotting functionality is provided by several Julia packages. The one utilized in this example, Gadfly, is very similar to R’s ggplot2 (both in coding the plots, and how the plots appear)

Performance issues aside, which do you prefer?

Leave a Reply