Skip to content

Instantly share code, notes, and snippets.

#Setup
rm(list = ls(all = TRUE)) #CLEAR WORKSPACE
#Directory
setwd("~/Overfitting")
#Load Required Packages
library('caTools')
library('caret')
library('glmnet')
@mattbaggott
mattbaggott / demo_ggsubplot.R
Created December 16, 2012 19:25
ggsubplot allows embedding of one ggplot in another. Variants of this are commonly requested, although often separate plots are better. Package is quite slow. ggsubplot uses but does not include code for map_afghanistan and map_americas layers, which are included here.
##
## Vignette code for ggsubplot with needed map_afghanistan and map_americas layers
## [email protected]
## Dec 16, 2012
# install.packages("ggsubplot")
library(ggplot2)
library(ggsubplot)
library(maps)
@mattbaggott
mattbaggott / demo_circular_day_plots.R
Last active July 9, 2021 15:37
Sample code to demonstrate some ways of making circular time-of-day plots in R (i.e. polar plots with 24 major hourly units)
##
## Sample code to demonstrate circular time plots in R
## [email protected]
## Dec 22, 2012
# inspired by
# http://stackoverflow.com/questions/2076370/most-underused-data-visualization
library(lubridate)
library(circular)
@mattbaggott
mattbaggott / timetoevent.R
Created December 29, 2012 20:36
Example code for time-to-event analysis in R, as in whether repeated ad viewings lead to a sale
##
## Example code for time-to-event analysis in R
## [email protected]
## Dec 28, 2012
##
## joineR package: analyzing longitudinal data where the response
## from each person is a time-sequence of repeated measurements
## and we are interested in a possibly censored time-to-event outcome
##
## example: repeated ad viewings leading to a sale
@mattbaggott
mattbaggott / ggsurvival.R
Last active December 18, 2016 23:20
Functions to make ggplot KM survival / cumulative incidence plot from survfit() models ( library(survival) )
#
# Functions to make ggplot KM survivor curves made with survfit() in library(survival)
#
# code written by Ramon Saccilotto
# and included in his ggplot2 tutorial
# 2010-12-08
# define custom function to create a survival data.frame
createSurvivalFrame <- function(f.survfit){
# initialise frame variable
@mattbaggott
mattbaggott / predicting_customer_behav_1.R
Last active September 15, 2020 22:16
Uses the BTYD package and Pareto/NBD model to predict customer behavior in R Slides are at: http://www.slideshare.net/mattbagg/baggott-predict-customerinrpart1#
#
# PREDICTING LONG TERM CUSTOMER VALUE WITH BTYD PACKAGE
# Pareto/NBD (negative binomial distribution) modeling of
# repeat-buying behavior in a noncontractual setting
#
# Matthew Baggott, [email protected]
#
# Accompanying slides at:
# http://www.slideshare.net/mattbagg/baggott-predict-customerinrpart1#
#
*.pyc
library(ggplot2)
library(ggmap)
# blue bottle coffee shops in SF area
latitude <- c(37.782375,37.795933,37.776327,37.762033,37.795966,37.785936)
longitude <- c(-122.407567,-122.273128,-122.42328,-122.411603,-122.394025,-122.400761)
df2 <- data.frame(longitude=longitude,latitude=latitude)
# df to make map title on the water
dflabels <- data.frame(longitude=-122.678,