This tutorial is based on the EDA Exercises in the book, Doing Data Science, which I highly recommend.
There are 31 data sets named nyt1.csv, nyt2.csv,…, nyt31.csv, which can be downloaded from GitHub.
Each csv represents one (simulated) days worth of ads shown and clicks recorded on the New York Times homepage in May 2012. Each row in the csv represents a single user.
There are five columns: Age, Gender (0=female, 1=male), Impressions, Clicks, and Signed_In status (0=not signed in, 1=signed in).
Create a data frame called data1 from the nyt1.csv file
fileLocation <- "http://stat.columbia.edu/~rachel/datasets/nyt1.csv"
data1 <- read.csv(url(fileLocation))
# Use the section below if you would rather import the data from a local file
# fileLocation <- "~/path/for/the/file/nyt1.csv"
# data1 <- read.csv("fileLocation")
Take a look at the first 5 rows to get a feel for the data
head(data1)
## Age Gender Impressions Clicks Signed_In
## 1 36 0 3 0 1
## 2 73 1 3 0 1
## 3 30 0 3 0 1
## 4 49 1 3 0 1
## 5 47 1 11 0 1
## 6 47 0 11 1 1
You can get a overview of the structure of the data by using the str() function
str(data1) # str stands for 'structure'
## 'data.frame': 458441 obs. of 5 variables:
## $ Age : int 36 73 30 49 47 47 0 46 16 52 ...
## $ Gender : int 0 1 0 1 1 0 0 0 0 0 ...
## $ Impressions: int 3 3 3 3 11 11 7 5 3 4 ...
## $ Clicks : int 0 0 0 0 0 1 1 0 0 0 ...
## $ Signed_In : int 1 1 1 1 1 1 0 1 1 1 ...
# str() returns information about each column such as name, data type, etc.
It could also be helpful to look at a summary of the data. This gives you useful information such as the number of rows in the object, and the mean, median, and range (min-max) of each column
summary(data1)
## Age Gender Impressions Clicks
## Min. : 0.0 Min. :0.000 Min. : 0.00 Min. :0.000
## 1st Qu.: 0.0 1st Qu.:0.000 1st Qu.: 3.00 1st Qu.:0.000
## Median : 31.0 Median :0.000 Median : 5.00 Median :0.000
## Mean : 29.5 Mean :0.367 Mean : 5.01 Mean :0.093
## 3rd Qu.: 48.0 3rd Qu.:1.000 3rd Qu.: 6.00 3rd Qu.:0.000
## Max. :108.0 Max. :1.000 Max. :20.00 Max. :4.000
## Signed_In
## Min. :0.000
## 1st Qu.:0.000
## Median :1.000
## Mean :0.701
## 3rd Qu.:1.000
## Max. :1.000
Since we’re exploring, it’s helpful to visualize the distribution of some of the columns, so we can see what we’re working with and if anything looks out of the ordinary
# distribution of the Age column
hist(data1$Age, main="", xlab="Age")
range(data1$Age)
## [1] 0 108
# distribution of the Impressions column
hist(data1$Impressions, main="", xlab="# of Impressions")
range(data1$Impressions)
## [1] 0 20
# distribution of the Clicks column
hist(data1$Clicks, main="", xlab="# of Clicks")
range(data1$Clicks)
## [1] 0 4
Did you notice that there is a disproportionate number of ages recorded as zero? This could skew our analysis, so we may need to account for that later.
create a new variable named Age_Group, that groups users into age categories “<18”, “18-24”, “25-34”, “35-44”, “45-54”, “55-64”, and “65+”
1.1) Cut the ages into the desired groups and add the groups to a new column in data1 called Age_Group. The ‘cut’ function creates a factor with levels
data1$Age_Group <- cut(data1$Age, c(-Inf, 18, 24, 34, 44, 54, 64, Inf))
levels(data1$Age_Group) <- c("<18", "18-24", "25-34", "35-44", "45-54", "55-64", "65+")
# Name the levels of 'Age_Group' for readability
Take a look at the changes:
head(data1)
## Age Gender Impressions Clicks Signed_In Age_Group
## 1 36 0 3 0 1 35-44
## 2 73 1 3 0 1 65+
## 3 30 0 3 0 1 25-34
## 4 49 1 3 0 1 45-54
## 5 47 1 11 0 1 45-54
## 6 47 0 11 1 1 45-54
For a single day, plot the distributions of ‘number of impressions’ and ‘click-through-rate’ by Age_Group. (CTR = clicks/impressions).
2.1) Create a subset of data1 to exclude rows where there are no impressions (if there are no impressions, we assume there will be no clicks). Name the new object d1
d1 <- subset(data1, Impressions>0)
2.2) Add a column to d1 called CTR containing the click-through-rate
d1$CTR <- d1$Clicks/d1$Impressions
head(d1)
## Age Gender Impressions Clicks Signed_In Age_Group CTR
## 1 36 0 3 0 1 35-44 0.00000
## 2 73 1 3 0 1 65+ 0.00000
## 3 30 0 3 0 1 25-34 0.00000
## 4 49 1 3 0 1 45-54 0.00000
## 5 47 1 11 0 1 45-54 0.00000
## 6 47 0 11 1 1 45-54 0.09091
2.3) Plot the distribution of Impressions>0, grouped by Age_Group, using the ggplot2 package
library(ggplot2) # used for visualizations
ggplot(subset(d1, Impressions>0), aes(x=Impressions, fill=Age_Group))+
geom_histogram(binwidth=1)
2.4) Plot the distribution of CTR>0, grouped by Age_Group
ggplot(subset(d1, CTR>0), aes(x=CTR, fill=Age_Group))+
labs(title="Click-through rate by age group (05/01/2012)")+
geom_histogram(binwidth=.025)
(Additional sections will be added here in the future)