Skip to content

Instantly share code, notes, and snippets.

@fauxvalue
fauxvalue / mines_2pp.r
Created June 30, 2019 13:47
Aus Fed election two party preferred and mines
# packages
require(data.table)
require(leaflet)
require(readxl)
twoppReps <- as.data.table(read.csv("https://tallyroom.aec.gov.au/Downloads/HouseTppByPollingPlaceDownload-24310.csv", skip = 1L))
pollingPlaces <- as.data.table(read.csv("https://tallyroom.aec.gov.au/Downloads/GeneralPollingPlacesDownload-24310.csv", skip = 1))
operatingMines <- as.data.table(read_excel("C:/Users/mcoog/Dropbox/blogs/operating_mines.xls"))
# inner join to add lat and longtitude data
@fauxvalue
fauxvalue / fed2019_nsw_boothmap.r
Last active June 30, 2019 01:55
Plot NSW AEC data on leaflet map
# how to draw a map -- and some geospatial points -- using R
require(data.table)
require(leaflet)
# get the data
pollingPlaces <- as.data.table(read.csv("https://tallyroom.aec.gov.au/Downloads/GeneralPollingPlacesDownload-24310.csv", skip = 1))
nswReps <- as.data.table(read.csv("https://tallyroom.aec.gov.au/Downloads/HouseStateFirstPrefsByPollingPlaceDownload-24310-NSW.csv", skip = 1))
# inner join to add lat and longtitude data
nswReps[pollingPlaces[, list(PollingPlaceID, Latitude, Longitude)],
@fauxvalue
fauxvalue / optimize.r
Last active March 31, 2019 11:11
example of R's optimize function: weighting us 2x10 curve to have zero correlation to usdjpy
library(fredr)
require(xts)
# note you'll need an API key
fredr_set_key("123YourFREDAPIKey321")
# helper function to convert tib to xts
xtsT <- function(TIB) xts(TIB$value, TIB$date)
# helper function to remove rows that have NA values
#include<RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
//[[Rcpp::export]]
arma::colvec getCoef(const arma::vec & X, const arma::vec & Y) {
// this function takes two numeric vectors
// checks to make sure the dimensions match
// returns the coefficients from a bivariate regression
// TODO: add an intercept switch ... noInt