Skip to content

Instantly share code, notes, and snippets.

@ScottPJones
Created May 10, 2016 23:45
Show Gist options
  • Select an option

  • Save ScottPJones/942f00a3a7b29f9504f6f868667dba29 to your computer and use it in GitHub Desktop.

Select an option

Save ScottPJones/942f00a3a7b29f9504f6f868667dba29 to your computer and use it in GitHub Desktop.
"""
@file ScienceFair.jl
@author Alex Matthew Jones-Martin
@copyright 2016 Gandalf Software, Inc.
@brief My science fair project
"""
module ScienceFair
end
# This is a plotting package that will let me display
# my results in different ways
using Plots
pyplot()
# This lets me read a file with data separated by commas
using CSV
using DataStreams
# Open the file with the data I collected
myfile = CSV.Source(joinpath(homedir(), "sciencefair.csv"))
# Load the data from the open file
mydata = Data.stream!(myfile, Data.Table)
# Get the number of rows of data that I collected
num_rows = myfile.schema.rows
# Make vectors with all of the data from my file
vec_name = Vector{String}(num_rows)
vec_digits = Vector{Int}(num_rows)
vec_age = Vector{Int}(num_rows)
for i = 1:num_rows
vec_name[i] = mydata[i, 1].value
vec_digits[i] = mydata[i, 2].value
vec_age[i] = mydata[i, 3].value
end
# Find all the different ages that data was collected for, sorted
# Julia makes this easy, with a function that returns a vector of only the unique entries
# in a vector, and then has a function that will return a vector in sorted order
ages = sort(unique(vec_age))
# Get the number of unique ages
num_ages = length(ages)
# Create a vector that stores all of the results for a particular age
digits_by_age = Vector{Vector{Int}}(num_ages)
for i = 1:num_ages
# Start off with an empty vector
digits_by_age[i] = Vector{Int}()
end
# For each unique age, store all results
for i = 1:num_ages
age = ages[i]
for j = 1:num_rows
if age == vec_age[j]
digits = vec_digits[j]
# Add these results to the previous results for this age
push!(digits_by_age[i], digits)
end
end
end
# Calculate different results, such as mean (average), median, and range, for each unique age
mean_by_age = Vector{Float64}(num_ages)
median_by_age = Vector{Float64}(num_ages)
low_range = Vector{Int}(num_ages)
high_range = Vector{Int}(num_ages)
for i = 1:num_ages
digits = digits_by_age[i]
mean_by_age[i] = mean(digits)
median_by_age[i] = median(digits)
low_range[i] = minimum(digits)
high_range[i] = maximum(digits)
end
# Plot data (with help by Tom Breloff, a friend of my dad)
plot(ages, mean_by_age, lab="Mean", m=(10, :diamond), err=(mean_by_age - low_range, high_range-mean_by_age), xlab = "Age", ylab="Correct Digits")
plot(ages, median_by_age, lab="Median", m=(10, :square))
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment