Skip to content

Instantly share code, notes, and snippets.

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,
*.pyc
@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, matt@baggott.net
#
# Accompanying slides at:
# http://www.slideshare.net/mattbagg/baggott-predict-customerinrpart1#
#
@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 / 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
## matt@baggott.net
## 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 / 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
## matt@baggott.net
## Dec 22, 2012
# inspired by
# http://stackoverflow.com/questions/2076370/most-underused-data-visualization
library(lubridate)
library(circular)
@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
## matt@baggott.net
## Dec 16, 2012
# install.packages("ggsubplot")
library(ggplot2)
library(ggsubplot)
library(maps)
#Setup
rm(list = ls(all = TRUE)) #CLEAR WORKSPACE
#Directory
setwd("~/Overfitting")
#Load Required Packages
library('caTools')
library('caret')
library('glmnet')
@mattbaggott
mattbaggott / demo_timezone.R
Last active October 14, 2015 00:18
Short demo on successfully working with timezones in R
##
## Sample code to teach about timezones in R
## matt@baggott.net
## Dec 13, 2012
## Time zones are important to understand when working with dates
## because the most common date class for R, POSIX, is actually a date/time
## class. (POSIX = "Portable Operating System Interface,
## an IEEE standard)
##
@mattbaggott
mattbaggott / demo_distline.R
Created December 13, 2012 18:30
demonstration of plotting distributions overlapping lineplots in R
##
## demonstration of distributions overlapping on lineplots
## matt@baggott.net
## Dec 11, 2012
##
## in response to: http://stats.stackexchange.com/questions/45591/r-plot-time-indexed-densities/45614#45614
library(ggplot2) # for plotting
library(lubridate) # for getting year from dates in dataset
library(plyr) # for getting annual mean easily