Skip to content

Instantly share code, notes, and snippets.

@aschleg
aschleg / logistic_regression
Last active August 29, 2015 14:19
Simple R script for performing logistic regression on binary categorical variables
library(readr)
library(caret)
# Enter csv file path. If the data is in xls or xlsx, you will need to use read.table or the read_table function
# in the readr package
data <- read_csv("")
attach(data)
summary(data)
#Enter column name with categorical variables you want to model and predict between the empty quotations.
@aschleg
aschleg / simple_linear_regression.R
Last active July 6, 2016 21:03
Quick function for performing linear regression with one predictor variable
# Quick function for performing linear regression with only one predictor variable.
# Takes two arguments, x and y. Variables must have same length.
# Output will be similar to output of lm() with one predictor.
# Gist created as part of post on linear regression: http://www.aaronschlegel.com/notebook/simple-linear-regression-models-r/
simple.linear.coef <- function(x, y) {
# Find length of x to get sample size. Assuming x and y have the same sample size.
n <- length(x)
if (n != length(y))
@aschleg
aschleg / multiple_regression_two_predictors.R
Created August 4, 2016 19:56
Example function for fitting a multiple regression model with two predictor variables
# Simple function for fitting a regression line with two predictor variables.
# Function takes two arguments:
# x must be a matrix or dataframe with only two columns (variables)
# y must be a vector containing the response values of the same length
# Output is similar to the lm() function
# Function used as a demonstration in post on multiple regression here: http://wp.me/p4aZEo-5Lh
two.predictor.regression <- function(x, y) {
y <- as.matrix(y)
if (ncol(y) > 1) {
@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)) {
@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) {
# 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 / 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')
@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 / 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 / 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