Data exploration example using used car data

getwd()
## [1] "/cloud/project"

I uploaded the file to the cloud “/cloud/project” and therefore I can access it directly from here. Assign the file to the variable usedcars

usedcars <- read.csv("usedcars.csv", stringsAsFactors = FALSE)

Get structure of used car data

str(usedcars)
## 'data.frame':    150 obs. of  6 variables:
##  $ year        : int  2011 2011 2011 2011 2012 2010 2011 2010 2011 2010 ...
##  $ model       : chr  "SEL" "SEL" "SEL" "SEL" ...
##  $ price       : int  21992 20995 19995 17809 17500 17495 17000 16995 16995 16995 ...
##  $ mileage     : int  7413 10926 7351 11613 8367 25125 27393 21026 32655 36116 ...
##  $ color       : chr  "Yellow" "Gray" "Silver" "Gray" ...
##  $ transmission: chr  "AUTO" "AUTO" "AUTO" "AUTO" ...

Exploring numeric variables Summarize numeric variables

# summarize numeric variables
summary(usedcars$year)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    2000    2008    2009    2009    2010    2012
summary(usedcars$price)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    3800   10995   13592   12962   14904   21992
summary(usedcars$mileage)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    4867   27200   36385   44261   55124  151479
summary(usedcars[c("price", "mileage")])
##      price          mileage      
##  Min.   : 3800   Min.   :  4867  
##  1st Qu.:10995   1st Qu.: 27200  
##  Median :13592   Median : 36385  
##  Mean   :12962   Mean   : 44261  
##  3rd Qu.:14904   3rd Qu.: 55124  
##  Max.   :21992   Max.   :151479

Calculate the mean income

# calculate the mean income
(36000 + 44000 + 56000) / 3
## [1] 45333.33
mean(c(36000, 44000, 56000))
## [1] 45333.33

Calculate the median income

# the median income
median(c(36000, 44000, 56000))
## [1] 44000

Calculate the min/max of used car prices

# the min/max of used car prices
range(usedcars$price)
## [1]  3800 21992

Calculate the min/max of used car mileage

range(usedcars$mileage)
## [1]   4867 151479

Calculate the difference of the range

# the difference of the range
diff(range(usedcars$price))
## [1] 18192

Calculate IQR for used car prices

# IQR for used car prices
IQR(usedcars$price)
## [1] 3909.5

Use quantile to calculate five-number summary (price)

# use quantile to calculate five-number summary
quantile(usedcars$price)
##      0%     25%     50%     75%    100% 
##  3800.0 10995.0 13591.5 14904.5 21992.0

Use quantile to calculate five-number summary (mileage)

quantile(usedcars$mileage)
##        0%       25%       50%       75%      100% 
##   4867.00  27200.25  36385.00  55124.50 151479.00

Calculate the 99th percentile

# the 99th percentile
quantile(usedcars$price, probs = c(0.01, 0.99))
##       1%      99% 
##  5428.69 20505.00

Quintiles of price (from 0 to 1)

# quintiles
quantile(usedcars$price, seq(from = 0, to = 1, by = 0.20))
##      0%     20%     40%     60%     80%    100% 
##  3800.0 10759.4 12993.8 13992.0 14999.0 21992.0

Display boxplot of used car prices and mileage

# boxplot of used car prices and mileage
boxplot(usedcars$price, main="Boxplot of Used Car Prices",
      ylab="Price ($)")

boxplot(usedcars$mileage, main="Boxplot of Used Car Mileage",
      ylab="Odometer (mi.)")

Histograms of used car prices and mileage

# histograms of used car prices and mileage
hist(usedcars$price, main = "Histogram of Used Car Prices",
     xlab = "Price ($)")

hist(usedcars$mileage, main = "Histogram of Used Car Mileage",
     xlab = "Odometer (mi.)")

Variance and standard deviation of the used car data Variance of price

# variance and standard deviation of the used car data
var(usedcars$price)
## [1] 9749892

Standard deviation of price

sd(usedcars$price)
## [1] 3122.482

Variance of mileage

var(usedcars$mileage)
## [1] 728033954

Standard deviation of mileage

sd(usedcars$mileage)
## [1] 26982.1

Exploring numeric variables One-way tables for the used car data Year as columns

# one-way tables for the used car data
table(usedcars$year)
## 
## 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 
##    3    1    1    1    3    2    6   11   14   42   49   16    1

Model as columns

table(usedcars$model)
## 
##  SE SEL SES 
##  78  23  49

Color as columns

table(usedcars$color)
## 
##  Black   Blue   Gold   Gray  Green    Red Silver  White Yellow 
##     35     17      1     16      5     25     32     16      3

Compute table proportions

# compute table proportions
model_table <- table(usedcars$model)
prop.table(model_table)
## 
##        SE       SEL       SES 
## 0.5200000 0.1533333 0.3266667

Round the data

# round the data
color_table <- table(usedcars$color)
color_pct <- prop.table(color_table) * 100
round(color_pct, digits = 1)
## 
##  Black   Blue   Gold   Gray  Green    Red Silver  White Yellow 
##   23.3   11.3    0.7   10.7    3.3   16.7   21.3   10.7    2.0

Exploring relationships between variables

# scatterplot of price vs. mileage
plot(x = usedcars$mileage, y = usedcars$price,
     main = "Scatterplot of Price vs. Mileage",
     xlab = "Used Car Odometer (mi.)",
     ylab = "Used Car Price ($)")

New variable indicating conservative colors

# new variable indicating conservative colors
usedcars$conservative <-
  usedcars$color %in% c("Black", "Gray", "Silver", "White")
# checking our variable
table(usedcars$conservative)
## 
## FALSE  TRUE 
##    51    99
install.packages("gmodels")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.5'
## (as 'lib' is unspecified)

Crosstab of conservative by model

# Crosstab of conservative by model
library(gmodels)
CrossTable(x = usedcars$model, y = usedcars$conservative)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  150 
## 
##  
##                | usedcars$conservative 
## usedcars$model |     FALSE |      TRUE | Row Total | 
## ---------------|-----------|-----------|-----------|
##             SE |        27 |        51 |        78 | 
##                |     0.009 |     0.004 |           | 
##                |     0.346 |     0.654 |     0.520 | 
##                |     0.529 |     0.515 |           | 
##                |     0.180 |     0.340 |           | 
## ---------------|-----------|-----------|-----------|
##            SEL |         7 |        16 |        23 | 
##                |     0.086 |     0.044 |           | 
##                |     0.304 |     0.696 |     0.153 | 
##                |     0.137 |     0.162 |           | 
##                |     0.047 |     0.107 |           | 
## ---------------|-----------|-----------|-----------|
##            SES |        17 |        32 |        49 | 
##                |     0.007 |     0.004 |           | 
##                |     0.347 |     0.653 |     0.327 | 
##                |     0.333 |     0.323 |           | 
##                |     0.113 |     0.213 |           | 
## ---------------|-----------|-----------|-----------|
##   Column Total |        51 |        99 |       150 | 
##                |     0.340 |     0.660 |           | 
## ---------------|-----------|-----------|-----------|
## 
##