This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)) { |
NewerOlder