Skip to content

Instantly share code, notes, and snippets.

@i2pi
Created April 1, 2011 17:02
Show Gist options
  • Save i2pi/898476 to your computer and use it in GitHub Desktop.
Save i2pi/898476 to your computer and use it in GitHub Desktop.
d <- read.csv('even_sample.csv')
# Fake
d$Convert <- (runif(nrow(d)) < 0.27)
sent <- aggregate(rep(1, nrow(d)), list(Date=d$Date), sum)
conv <- aggregate(d$Convert, list(Date=d$Date), sum)
by_date <- merge(sent,conv, by='Date')
colnames(by_date)[2:3] <- c('sent', 'conv')
by_date$Date <- as.Date(by_date$Date)
all_dates <- as.Date(min(by_date$Date):max(by_date$Date), origin='1970-01-01')
missing_dates <- all_dates[!(all_dates %in% by_date$Date)]
plug <- data.frame(matrix(0, nrow=length(missing_dates), ncol=3))
colnames(plug) <- colnames (by_date)
plug$Date <- missing_dates
by_date <- rbind(by_date, plug)
by_date <- by_date[order(by_date$Date),]
window <- 14
s_sent <- apply(embed(by_date$sent, window*2 + 1), 1, sum)
s_conv <- apply(embed(by_date$conv, window*2 + 1), 1, sum)
plot (s_conv / s_sent, type='l')
# Plot null hypothesis of no trend
abline(h=mean(s_conv / s_sent), col='red')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment