Skip to content

Instantly share code, notes, and snippets.

@seb231
Created August 1, 2016 14:23
Show Gist options
  • Save seb231/0ee7c98013c44a6ae72ae04dfb0a7bf9 to your computer and use it in GitHub Desktop.
Save seb231/0ee7c98013c44a6ae72ae04dfb0a7bf9 to your computer and use it in GitHub Desktop.
(defn assert! [p msg] (when-not (p) (throw (Exception. (timbre/info (format msg))))))
(defn jumpoffyr-method-average
"Takes in a dataset with historical data, a column name to be averaged,
a name for the average column, and years to start and end averaging for.
Returns a dataset where the column to be averaged contains the averages
for all the years of interest and is named to avg-name."
[historical-data col-to-avg avg-name start-yr-avg end-yr-avg]
(let [hist-earliest-yr (reduce min (ds/column historical-data :year))
hist-latest-yr (reduce max (ds/column historical-data :year))
;;start-yr (s/validate (s/pred #(>= % hist-earliest-yr)) start-yr-avg)
end-yr (s/validate (s/pred #(<= % hist-latest-yr)) end-yr-avg)]
(assert! #(>= start-yr-avg hist-earliest-yr)
"Start year must be more than or equal to earliest year in dataset")
(-> (i/query-dataset historical-data
{:year {:$gte start-yr-avg
:$lte end-yr}})
(ds/rename-columns {col-to-avg avg-name})
(wds/rollup :mean avg-name [:gss-code :sex :age])))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment