Skip to content

Instantly share code, notes, and snippets.

@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 / confidence_prediction_intervals.R
Created July 12, 2016 14:07
Functions for calculating the confidence and prediction intervals of a fitted linear regression model. The linear.reg.conf.interval() function outputs a base R graphics scatterplot with a fitted linear line and confidence interval.
# Function for calculating the confidence (mean) and prediction intervals of a fitted linear regression model.
# Replicates the predict() function with arguments 'confidence' and 'prediction'
# Used in post demonstrating the confidence and prediction intervals in linear regression: http://www.aaronschlegel.com/notebook/confidence-prediction-intervals/
# Takes three arguments:
# x = vector of independent variable data points
# y = vector of dependent variable data points
# pred.x = value of x to find predicted y
conf.pred.intervals <- function(x, y, pred.x) {
@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 / games_howell.R
Last active November 29, 2017 00:07
R function for performing Games-Howell Post-Hoc Test
games.howell <- function(grp, obs) {
#Create combinations
combs <- combn(unique(grp), 2)
# Statistics that will be used throughout the calculations:
# n = sample size of each group
# groups = number of groups in data
# Mean = means of each group sample
# std = variance of each group sample
@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 / split_excel_worksheets_v2.py
Last active June 1, 2022 18:11
Alternative method to split Excel worksheet into multiple worksheets based on column name
from xlwings import Workbook, Range, Sheet
import pandas as pd
import os
# Alternative method to split an Excel worksheet into multiple sheets based on a column name.
# The script will prompt four questions to enter in the required information. The workbook will then open and
# split the prompted worksheet into separate worksheets based on the desired column name.
# To run, open the command prompt and enter the command python Split_Excel_Worksheet_v2.py
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
@aschleg
aschleg / split_excel_worksheet.py
Created April 11, 2015 02:52
Split Excel worksheet into multiple worksheets based on column name
from xlwings import Workbook, Range, Sheet
import pandas as pd
# Split Excel data in one worksheet into multiple worksheets based on column name.
# Copy this file into the same location as the Excel workbook with the worksheet you wish to split.
# Download the zip of the xlwings Github repo here: https://github.com/ZoomerAnalytics/xlwings and copy the
# xlwings.bas file from the xlwings folder. Import the xlwings.bas file into your Excel workbook by entering ALT+F11
# and then going to File, Import File and clicking on the file.
# Import the Split_Excel_Worksheet.bas file and run by going to the Developer tab on the Excel Ribbon, click Macros,
@aschleg
aschleg / Merge_Excel_Workbooks
Last active March 20, 2022 20:44
Merge first worksheet in all Excel workbooks in a folder
from xlwings import Workbook, Range
import pandas as pd
import os
import re
# Script to merge a folder containing Excel workbooks into a single workbook.
# The folder should only contain Excel workbooks and must all either be in csv, xls or xlsx format
# To run, open the command prompt and enter the command python Merge_Excel_Workbooks.py
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
@aschleg
aschleg / linear_regression_R_example
Created March 14, 2015 21:22
Quick examples of different types of regression using R. The datasets used for each are available in all R distributions.
library(caret)
ginzberg <- read.csv('D:/Google_Drive/Google_Drive/Resources/Datasets/Rdata/car/Ginzberg.csv')
ginzberg <- ginzberg[2:4]
train <- createDataPartition(y = ginzberg$depression,
p = 0.50,
list = FALSE)
training <- ginzberg[ train,]
@aschleg
aschleg / Wordpress portfolio template with Isotope and Bootstrap
Last active September 2, 2020 00:44
When creating a custom Wordpress theme with Bootstrap, it took me forever for it to play nicely with the Isotope plugin for image filtering. Wanted to share in hopes it helps others.
<!--PHP file for adding a basic template portfolio page for Wordpress themes using the Bootstrap framework.-->
<?php
/*
Template Name: Portfolio
*/
?>
<?php get_header(); ?>