Skip to content

Instantly share code, notes, and snippets.

@HughParsonage
HughParsonage / stata_summary.R
Last active February 21, 2018 14:00 — forked from leeper/stata_summary.R
Stata-style regression summaries
stata_summary <-
function(
x,
...
) {
# summarize
mod <- x
x <- summary(x)
# find outcome variable
@HughParsonage
HughParsonage / price2income-vs-housingper1000-by-Country.R
Last active February 19, 2018 11:57
Price 2 income vs Housing per capita (The Satisfaction of Cowgill)
library(rsdmx)
library(data.table)
library(hutils)
library(magrittr)
library(ggplot2)
library(ggrepel)
ISO3166 <- Census2016.spec:::ISO3166 # hughparsonage/Census2016.spec
to_name <- function(a3) {
ISO3166[["name"]][match(a3, ISO3166[["alpha_3"]])]
library(data.table)
library(magrittr)
library(grattan)
library(SampleFile1415)
library(taxstats)
library(hutils)
library(scales)
tax2014 <- copy(sample_file_1415) %>% apply_super_caps_and_div293()
@HughParsonage
HughParsonage / CGT_tax_expenditure_1415
Created January 18, 2018 05:39
CGT tax expenditure
library(magrittr)
library(grattan)
library(data.table)
library(SampleFile1415)
as.data.table(sample_file_1415) %>%
.[, tax_noCGT := Taxable_Income + Net_CG_amt] %>%
.[, tax := income_tax(Taxable_Income, "2014-15", .dots.ATO = sample_file_1415)] %>%
.[, tax_new := income_tax(tax_noCGT, "2014-15", .dots.ATO = sample_file_1415)] %>%
.[, .(tot = round(50 * sum(tax_new - tax) / 1e9, 1))]
#' Quickly count the number of files in a directory
number_of_files <- function(dir, filetype = "*.*") {
if (.Platform$OS.type == "windows" &&
nzchar(Sys.which("powershell"))) {
current_wd <- getwd()
on.exit(setwd(current_wd))
setwd(dir)
result <- system2("powershell",
@HughParsonage
HughParsonage / ghostscript-frontpage-compress.R
Created November 5, 2017 00:39
For compressing frontpages
paste("gswin64 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer",
"-dNOPAUSE -dQUIET -dBATCH", "-sOutputFile=output.pdf",
"report-cover-energy-nem-latex.pdf")
parse2 <- function(x) {
# x <- "ab{c{f}}d{e}f{g}h{ij{{k}}}lm"
x_split <- unlist(strsplit(x, "", fixed = TRUE))
tex_group <- cumsum(x_split == "{") - cumsum(x_split == "}")
max_tex_group <- max(tex_group)
out <- setDT(list(char = x_split, tex_group = tex_group))
out[, I := .I]
@HughParsonage
HughParsonage / employment-QLD
Last active August 27, 2017 14:05
SA4 employment figures by QLD region
library(grattanCharts) # devtools::install_github('hughparsonage/grattanCharts')
library(dplyr)
library(httr)
library(tidyr)
library(readxl)
library(scales)
library(ggplot2)
library(data.table)
library(lubridate)
library(hutils)
@HughParsonage
HughParsonage / HILDA-household-disp-Gini.R
Created July 25, 2017 14:30
Use HILDA to calculate Gini by household
library(data.table)
library(hildaData)
library(magrittr)
library(grattanCharts)
library(ggplot2)
#' @source acid package (but it's chock full of unnecessary dependencies)
weighted_gini <- function (x, w = NULL) {
if (is.null(w))
w <- rep(1, length(x))
formalCoverage <- function(fn) {
formalz <<- formals(fn)
bod <<- as.character(body(fn))
formal_present <- function(formal) {
formal_dots_escaped <<- gsub(".", "\\.", formal, fixed = TRUE)
any(grepl(pattern = paste0("(?:\\b|\\s)", formal_dots_escaped, "(?:\\b|\\s)"),
x = bod,
perl = TRUE))