This file contains 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
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. |
This file contains 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
# 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)) |
This file contains 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 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) { |
This file contains 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)) { |
This file contains 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 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 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 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 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 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 |
OlderNewer