Skip to content

Instantly share code, notes, and snippets.

@aschleg
aschleg / austin_animal_center_dataset_analysis_functions.py
Created March 29, 2018 01:30
Functions used in series of analyses on the Austin Animal Center's animal intakes and outcomes datasets.
import requests
import numpy as np
import pandas as pd
from six.moves.urllib.error import HTTPError
def get_soda_api_data(endpoint, count=1000, offset=0, return_df=True):
params = {'$limit': count, '$offset': offset}
results = []
@aschleg
aschleg / download_soda_api_data.py
Last active March 12, 2018 17:30
Simple script for extracting Socrata Open Data Access (SODA) datasets.
"""
Simple script for extracting Socrata Open Data Access (SODA) datasets. Compatible with 3+, though one can easily make it 2.7
compatible by changing the `from urllib.error import HTTPError` import to `from urllib2 import HTTPError`
Parameters
----------
endpoint : string
SODA API endpoint of the dataset.
count : int, default 1000
@aschleg
aschleg / lagrangian_interpolation.R
Created June 24, 2017 15:22
R function for performing Lagrangian polynomial interpolation
# Function for performing Lagrangian polynomial interpolation https://en.wikipedia.org/wiki/Lagrange_polynomial.
# Requires the package rSymPy https://cran.r-project.org/web/packages/rSymPy/index.html.
# Parameters:
# x: x values of interpolating points
# y: values corresponding to x values
# Returns:
# Simplified interpolated polynomial that passes through the given x and y points
lagrange.poly <- function(x, y) {
@aschleg
aschleg / nevilles_algorithm.R
Created June 24, 2017 15:06
R function for performing Neville's Algorithm for approximating an interpolated polynomial at a given point
# Performs Neville's Algorithm (https://en.wikipedia.org/wiki/Neville%27s_algorithm) for
# interpolating a polynomial given a set of x and y values.
# Parameters:
# x: x values of interpolating points
# y: values corresponding to x values
# x0: desired point to approximate interpolated polynomial
# Returns:
# List containing approximated value of interpolated polynomial and matrix representing
# the intermediate values of Q
@aschleg
aschleg / matrix_trace.R
Created October 7, 2016 13:54
Simple function for computing the trace of a square matrix. Should replicate the output of sum(diag(matrix)).
# Simple function for computing the trace of a matrix. Should replicate the output of sum(diag(matrix)).
# Function requires a square n x n matrix as input.
# Used in post demonstrating the trace of a matrix here: http://wp.me/p4aZEo-5Nb
trace <- function(A) {
n <- dim(A)[1] # get dimension of matrix
# Check that the matrix is square
if (n != dim(A)[2]) {
@aschleg
aschleg / bisection_method.R
Created September 23, 2016 16:18
Sample function for evaluating a function root using the bisection method.
# Sample function for evaluating a function root using the bisection method
# Inputs required are a function and the starting points to calculate the midpoint
# Function used in post demonstrating the bisection method of root-finding here: http://wp.me/p4aZEo-5MU
bisection <- function(f, a, b, n = 1000, tol = 1e-7) {
# If the signs of the function at the evaluated points, a and b, stop the function and return message.
if (!(f(a) < 0) && (f(b) > 0)) {
stop('signs of f(a) and f(b) differ')
} else if ((f(a) > 0) && (f(b) < 0)) {
stop('signs of f(a) and f(b) differ')
@aschleg
aschleg / secant_method.R
Created September 7, 2016 18:04
Simple function for performing the secant method of root finding
# Simple function for performing the secant method of root finding.
# Function takes three arguments:
# f: the function in question
# x0: first starting value
# x1: second starting value
# Function used in post on demonstrating the secant method here: http://wp.me/p4aZEo-5MH
secant.method <- function(f, x0, x1, tol = 1e-9, n = 500) {
if (is.function(f) == FALSE) {
stop('f is not a function')
# Sample function demonstrating how the Newton-Raphson method for root-finding is performed.
# Requires the numDeriv package for finding the derivative. One could replace this with the limit using a
# small enough h value.
# Requires three inputs:
# f: univariate function
# a: lower bound (also represents starting value for method)
# b: upper bound
# Newton-Raphson method for finding roots was examined in post here: http://wp.me/p4aZEo-5My
newton.raphson <- function(f, a, b, tol = 1e-5, n = 1000) {
@aschleg
aschleg / working_hotelling_bonferroni_intervals.R
Last active August 15, 2016 17:07
Example function for calculating Working-Hotelling and Bonferroni confidence intervals at a 95% confidence level
# Example function for calculating Working-Hotelling and Bonferroni confidence intervals at a 95% level.
# Function takes two arguments:
# x: predictor variable. Must be a vector.
# y: response variable. Must be a vector.
# Plots are built using ggplot2
# gridExtra package, https://cran.r-project.org/web/packages/gridExtra/index.html, is used to plot multiple ggplots
# Function used to demonstrate how to construct simultaneous confidence intervals in post here: http://wp.me/p4aZEo-5Mg
working.hotelling.bonferroni.intervals <- function(x, y) {
@aschleg
aschleg / matrix_inverse.R
Created August 10, 2016 18:32
Sample R function demonstrating how a 2x2 or 3x3 matrix inverse is computed.
# Sample function demonstrating how a 2x2 or 3x3 matrix inverse is computed.
# Requires an object with no more than 3 columns and an equal number of rows that can be coerced into a matrix.
# Used in post on computing the inverse of a matrix if one exists: http://wp.me/p4aZEo-5Ln
matrix.inverse <- function(mat) {
A <- as.matrix(mat)
# If there are more than four columns in the supplied matrix, stop routine
if ((ncol(A) >= 4) | (nrow(A) >= 4)) {