Created
January 27, 2014 18:45
-
-
Save jdavidson/8654897 to your computer and use it in GitHub Desktop.
Quick and dirty analysis of iPhone Sales and Google Trends
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
require(reshape2) | |
library(dplyr) | |
library(lubridate) | |
library(scales) | |
options(scipen=999) | |
options(stringsAsFactors = FALSE) | |
sales <- read.csv("quarterly-iphone-sales.csv") | |
ggplot(sales, aes(x=quarter, y=sales)) + geom_point() | |
queries <- read.csv("iphone-ipad.csv") | |
queries$date <- mdy(as.character(queries$date)) | |
queries$quarter <- paste(year(queries$date), quarters(queries$date)) | |
queries.agg <- queries %.% group_by(quarter) %.% summarise(queries=mean(iphone)) | |
queries.agg.join <- left_join(queries.agg, sales) | |
sales <- inner_join(sales, queries.agg) | |
sales$norm_sales <- scale(sales$sales) | |
sales$norm_queries <- scale(sales$queries) | |
sales.pt <- melt(sales[, c("quarter", "norm_sales", "norm_queries")], "quarter") | |
plot <- ggplot(filter(sales.pt, quarter > "2008 Q1"), aes(x=gsub(" ", "\n", quarter), y=value, color=variable)) + geom_point() + ggtitle("Normalized Apple iPhone Sales and Google Search Query Index") + xlab("Quarter") + ylab("") + scale_color_discrete(breaks=c("norm_sales", "norm_queries"), labels=c("Sales", "Queries"), name="Normalized\nVariable") + theme(legend.position=c(.9,.2),legend.key = element_rect(fill="white"), legend.background = element_rect(fill="white"), axis.title.y=element_blank()) | |
ggsave("norm-sales-queries.png", plot, width=640 / 72, height=400 / 72, dpi=72) | |
model <- glm(sales ~ queries, data=sales) | |
queries.agg.join$pred_sales <- predict(model, queries.agg.join) | |
plot <- ggplot(filter(queries.agg.join, quarter > "2008 Q1", quarter < "2014 Q1"), aes(x=gsub(" ", "\n", quarter), y=sales)) + geom_bar(stat="identity") + geom_bar(stat="identity", data=filter(queries.agg.join, quarter == "2013 Q4"), aes(x=gsub(" ", "\n", quarter), y=pred_sales), fill="red") + ggtitle("Apple iPhone Sales (in Millions of Units)") + xlab("Quarter") + theme(axis.title.y=element_blank(), axis.title.x=element_blank()) | |
ggsave("pred-sales.png", plot, width=640 / 72, height=400 / 72, dpi=72) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment