Today the lab focuses on data outliers, data preparation, and data modeling. This lab requires the use of Microsoft Excel, R, and ERDplus.
Make sure to download the folder titled ‘bsad_lab02’ zip folder and extract the folder to unzip it. Next, we must set this folder as the working directory. The way to do this is to open R Studio, go to ‘Session’, scroll down to ‘Set Working Directory’, and click ‘To Source File Location’. Now, follow the directions to complete the lab.
First, we must calculate the mean, standard deviation, maximum, and minimum for the Age column using R.
In R, we must read in the file again, extract the column and find the values that are asked for.
#Read File
mydata = read.csv(file = "data/creditrisk.csv")
#Name the extracted variable
age = mydata$Age
age
[1] 23 32 38 36 31 25 26 27 25 43 32 34 26 44 46 39
[17] 25 31 47 23 22 26 19 27 39 26 50 34 23 23 46 35
[33] 28 25 36 41 54 43 33 34 39 34 30 48 19 29 26 42
[49] 30 38 21 32 35 46 37 39 29 27 22 40 44 26 57 34
[65] 47 46 38 52 34 29 39 36 25 59 27 24 25 29 64 31
[81] 38 41 22 29 32 34 23 21 49 31 40 28 21 30 37 33
[97] 22 23 21 40 36 29 37 26 37 39 27 19 45 34 43 33
[113] 35 31 25 27 56 45 53 23 25 63 34 24 35 30 29 20
[129] 49 56 48 24 22 30 39 20 22 24 41 32 23 41 52 65
[145] 26 33 56 24 20 35 34 38 34 37 31 67 26 24 21 46
[161] 22 20 60 25 21 22 29 56 32 30 27 37 39 26 32 31
[177] 35 31 64 27 31 32 73 30 42 45 27 22 26 33 27 24
[193] 21 28 51 21 27 40 34 34 63 33 28 24 18 63 37 38
[209] 22 31 31 42 24 65 22 22 51 23 30 32 21 52 22 41
[225] 53 54 37 27 25 34 29 27 32 29 32 28 25 51 44 33
[241] 22 35 30 27 23 19 44 41 35 25 28 34 31 32 41 22
[257] 28 21 41 36 26 37 35 34 43 30 31 43 24 26 45 26
[273] 48 43 59 55 29 32 53 33 56 33 46 54 22 35 46 29
[289] 22 53 42 20 35 33 26 46 28 38 35 47 23 56 28 48
[305] 27 23 24 22 42 25 47 32 25 36 25 26 53 66 23 25
[321] 24 31 57 44 42 27 51 23 31 22 35 37 35 30 49 32
[337] 52 45 23 41 28 30 50 58 36 23 48 37 20 23 39 34
[353] 22 21 38 36 26 47 38 27 25 28 67 25 34 38 41 26
[369] 35 45 32 47 24 30 33 23 21 36 30 52 27 44 26 24
[385] 24 50 31 38 28 48 56 31 28 26 25 41 24 29 25 35
[401] 35 26 29 59 34 41 35 27 41 30 27 65 19 33 24 40
[417] 62 33 29 22 35 30 28 28 44
#Calculate the average age below. Refer to Worksheet 1 for the correct command.
mean(age)
[1] 34.39765
#Calculate standard deviation of age below. Refer to Worksheet 1 for the correct command.
sd_age = sd(age)
sd_age
[1] 11.04513
#Calculate the maximum of age below. The command to find the maximum is max(variable) where variable is the extracted variable.
max(age)
[1] 73
#Calculate the minimum of age below. The command to find the minimum is min(variable) where variable is the extracted variable.
min(age)
[1] 18
Next, use the formula from class to detect any outliers. An outlier is value that “lies outside” most of the other values in a set of data. A common way to estimate the upper and lower threshold is to take the mean (+ or -) 3 * standard deviation. Try using this formula to find the upper and lower limit for age.
#Use the formula above to calculate the upper and lower threshold
lower = mean_age - (3)* sd_age
Error: object 'mean_age' not found
A method to find the upper and lower thresholds discussed in introductory statistics courses involves finding the interquartile range. Follow along below to see how we first calculate the interquartile range..
quantile(age)
iqr = upperq - lowerq
The formula below calculates the threshold. The threshold is the boundaries that determine if a value is an outlier. If the value falls above the upper threshold or below the lower threshold, it is an outlier.
Below is the upper threshold:
upperthreshold = (iqr * 1.5) + upperq
Error: object 'iqr' not found
Below is the lower threshold:
lowerthreshold = lowerq - (iqr * 1.5)
Error: object 'lowerq' not found
Are there any outliers? How many? It can also be useful to visualize the data using a box and whisker plot. The boxplot below supports the IQR we found of 15 and upper and lower threshold.
Next, we must read the ‘creditriskorg.csv’ file into R. This is the original dataset and contains missing values.
mydata = read.csv(file = "data/creditriskorg.csv")
head(mydata)
#tail(mydata)
We observe that the column names are shifted down below. So, we must make sure to use the command skip and set the header to true.
mydata = read.csv("data/creditriskorg.csv",skip = 1)
head(mydata)
To calculate the mean for Checking in R, follow Worksheet 1. Extract the Checking column first and then find the average using the function built in R. What happens when we try to use the function?
checking = mydata$Checking
class(checking)
[1] "factor"
str(checking)
Factor w/ 168 levels " $- "," $1,257.00 ",..: 1 1 1 105 163 41 1 1 101 164 ...
mean(checking)
argument is not numeric or logical: returning NA
[1] NA
checking[1:6]
[1] $- $- $- $638.00
[5] $963.00 $2,827.00
168 Levels: $- $1,257.00 ... $996.00
To resolve the error, we must remove understand where it is coming from. There are missing values in the csv file, which is quite common as most datasets are not perfect. Additionally, there are commas within the excel spreadsheet, and R does not recognize that ‘1,234’ is equivalent to ‘1234’. Lastly, there are ‘$’ symbols throughout the file which is not a numerica symbol either.
The sub function replaces these symbols with something else. So, in order to remove the comma in the number “1,234”, we must substitute it with just an empty space.
As shown on the worksheet, type and copy the exact commands to find the mean with the NA values removed.
clean = checking[1:6]
clean
[1] $- $- $- $638.00
[5] $963.00 $2,827.00
168 Levels: $- $1,257.00 ... $996.00
#substitute comma with blank in all of checking
clean = sub(",","",checking[1:6])
clean
[1] " $- " " $- " " $- "
[4] " $638.00 " " $963.00 " " $2827.00 "
#substitute dollar sign with blank in all of checking
clean = sub("\\$","",checking[1:6])
class(clean)
[1] "character"
#numeric convert
clean = as.numeric(clean)
NAs introduced by coercion
class(clean)
[1] "numeric"
#mean with NA removed
clean
[1] NA NA NA 638 963 NA
JUST DO WITH CHECKING NOT JUST 1:10 What are some other ways to clean this data in R? How about in Excel?
Now, we will look at Chicago taxi data. Go and explore the interactive dashboard and read the description of the data.
Chicago Taxi Dashboard: https://data.cityofchicago.org/Transportation/Taxi-Trips-Dashboard/spcw-brbq
Chicago Taxi Data Description: http://digital.cityofchicago.org/index.php/chicago-taxi-data-released/
Open in RStudio the csv file is located in the data folder, note the size of the file, the number of columns and of rows here. Use the functions learned in lab00 and lab01 to describe the data, identify unique entities, fields and summarize.
Define a relational business logic for the column field ‘Trip Seconds’.
Using https://erdplus.com/#/standalone draw a star schema using the following three tables: