Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
coding

Brian Fannin PirateGrunt

💭
coding
View GitHub Profile
View Makefile
####################
# Makefile
# Copyright Brian A. Fannin 2015
####################
RDIR = .
DATA_DIR = $(RDIR)/data
GATHER_DIR = $(DATA_DIR)/gather
GATHER_SOURCE = $(wildcard $(GATHER_DIR)/*.Rmd)
@PirateGrunt
PirateGrunt / gist:6059626
Created Jul 23, 2013
Vectors of S4 classes with non-trivial slots
View gist:6059626
movie = c("Thunderball", "Goldfinger")
rating = c(4,5)
dfJoe = data.frame(movie = movie, rating = rating)
movie = c("Manhattan", "Interiors", "Radio Days", "Bananas")
rating = c(5, 4, 3, 5)
dfBob = data.frame(movie = movie, rating = rating)
setClass("BorrowedStuff", representation(stuff = "data.frame", from="character"))
View gist:6059617
kids1 = as.array(c("Bob", "Joe"))
kids2 = as.array(c("Steve", "Beth", "Kim"))
setClass("person", representation(name="character", age="numeric", children = "list"))
setMethod(
f = "[",
signature="person",
definition=function(x,i,j,...,drop=TRUE){
initialize(x, name=x@name[i], age = x@age[i], children = list(x@children[i]))
@PirateGrunt
PirateGrunt / AnotherOLS.R
Created Jul 9, 2013
Another view of ordinary regression
View AnotherOLS.R
# Get a set of "error" terms
set.seed(1234)
N = 100
E = rnorm(N, mean = 0, sd = 2)
lnLike = function(x, mu, sigma)
{
n = length(x)
lnLike = -n / 2 * log(2*pi)
lnLike = lnLike - n/2 * log(sigma ^2)
View gist:5922110
sourceFiles = "https://raw.github.com/PirateGrunt/MRMR/master/R/NAIC.R"
dummy = lapply(paste0(myDirectory, sourceFiles), source)
rm(myDirectory, sourceFiles, dummy)
dfAuto = GetNAICData(dataSetName = "comAuto_pos.csv")
dfWC = GetNAICData(dataSetName = "wkcomp_pos.csv")
dfGL = GetNAICData(dataSetName = "othliab_pos.csv")
dfProd = GetNAICData(dataSetName = "prodliab_pos.csv")
@PirateGrunt
PirateGrunt / gist:5767596
Created Jun 12, 2013
Example of how the dollar sign reference in a dataframe doesn't require the whole name.
View gist:5767596
myData = data.frame(State = c("NY","NY", "TX", "TX")
, Premium = c(100,200,150,75)
, Loss = c(80,175,80,80))
myData
moreData = data.frame(Premium = myData$P, Loss = myData$L)
moreData
rm(moreData)
colNames = c("Premium", "Loss")
@PirateGrunt
PirateGrunt / gist:5529844
Created May 7, 2013
Examples of how to get ggplot and lm style function calls.
View gist:5529844
someFunction = function(y, data = NULL)
{
arguments <- as.list(match.call())
y = eval(arguments$y, data)
sum(y)
}
myData = data.frame(A = c(1,2,3), B = c(10,9,8))
someFunction(A, data=myData)
@PirateGrunt
PirateGrunt / gist:5152111
Created Mar 13, 2013
Test calculation of start and end period udfs.
View gist:5152111
DECLARE @TestDate datetime
SET @TestDate = CAST('08-15-2000' AS datetime)
SELECT dbo.udfBeginningOfYear(@TestDate) AS BeginningOfYear
, dbo.udfEndOfYear(@TestDate) AS EndOfYear
, dbo.udfBeginningOfSemi(@TestDate) AS BeginningOfSemi
, dbo.udfEndOfSemi(@TestDate) AS EndOfSemi
, dbo.udfBeginningOfQuarter(@TestDate) AS BeginningOfQuarter
, dbo.udfEndOfQuarter(@TestDate) AS EndOfQuarter
@PirateGrunt
PirateGrunt / gist:5152102
Created Mar 13, 2013
T/SQL to generate starting and ending dates for annual, semi-annual and quarterly periods.
View gist:5152102
/* udfBeginningOfYear **********************************************************/
IF OBJECT_ID (N'udfBeginningOfYear') IS NOT NULL
DROP FUNCTION udfBeginningOfYear
GO
CREATE FUNCTION udfBeginningOfYear (@DateIn datetime)
RETURNS datetime
AS
BEGIN
View titanic_lm.R
#File created 1/31/13
#contains R code to
#-read in Kaggle Competition Titanic Data csv file
#-create a simple logistic regression model
#-make predictions on training and test data
#-write out test predictions to csv file
#
#Replace the <your path here> with the full path to your copy of train and test csv files.
###################################################################################