Limited Offer Get 25% off — use code BESTW25
No AI No Plagiarism On-Time Delivery Free Revisions
Claim Now

Importing data and handling variable types

T

Data$AccountType <- as.factor(Data$AccountType) Data$MonthlyIncome <- as.integer(Data$MonthlyIncome) Data$AccountBalance <- as.numeric(Data$AccountBalance) Data$Approved <- as.factor(Data$Approved) utorial Project – Assessment 2 Total Marks: 34

Objective:

The purpose of this tutorial project is to implement and explore techniques mainly covered in weeks two and three of the Foundations of Data Science subject. This activity will focus on a dataset containing a collection of credit card applications and the subsequent credit approval decisions (positive/successful or negative/unsuccessful). The project is segmented into the following parts:

  1. Importing data and handling variable types, variable names and missing values
  2. Calculating and visualising proximity measurements
  3. Visually exploring data relationships using ggplot2()
  1. (a) Importing

The data is publicly available at:

http://archive.ics.uci.edu/ml/machine-learning-databases/credit-screening/crx.data

This dataset is interesting because there is a good mix of attributes: continuous, nominal with small numbers of values, and nominal with larger numbers of values. It contains data regarding corporate MasterCard (credit card) applications from the Commonwealth Bank during 1984. This was a time when credit approvals was done manually, and research into automation was active to improve equity and accuracy into the credit approval process. In the public source where the data is currently available, the variable names have been removed for confidentiality reasons; however, we provide the variable names below, based on the original publication:

Variable Name Comments
Gender Gender of applicant. Nominal variable with two factor levels
Age Age of applicant. Numeric variable
MonthlyExpenses Monthly house hold expenses. Numeric variable. Units of $100
MaritalStatus Marital status of applicant (Married, Single, Other). Nominal variable, three factor levels
HomeStatus Home living arrangements of applicant (Renting, Own/Buying, Living with Relatives). Nominal variable, three factor levels
Occupation Occupation category of applicant. Nominal variable with multiple factor levels
BankingInstitution Primary banking/credit union institution used by applicant. Nominal variable
YearsEmployed Number of years the applicant has worked in current or previous employment. Numeric variable
NoPriorDefault Finical judgements of defaulting on a repayment. Nominal variable with two levels
Employed Current employment status of applicant. Nominal variable, two factor levels
CreditScore Offset normalised credit rating score: summary attribute score of tabulated values corresponding to application. Numeric variable
DriversLicense If the applicant has a current drivers licence. Nominal variable, two factor levels
AccountType Type of account in primary banking institution, e.g. savings account, etc. Nominal variable
MonthlyIncome Monthly disposable income. Units of $1. Summary attribute from application. Numeric variable
AccountBalance Amount in primary account in primary banking institution. Numeric variable
Approved Approval status of application. Nominal variable, two factor levels

Import the data using the R function read.table(). Note: in the data file, missing values are recorded using the “?” character. So, in order to correctly import the missing values in R, you will need to use the input argument na.strings = “?” in the call to read.table(). Also, note that the data file does not contain the variable names, so you can use the argument header = FALSE to instruct R that the first line of the file contains data, rather than variable names.

Table 1 Enter your R code you used to import the data here. Marks (2)

Enter your answer here

(b)Variable names and types

Because the data file does not contain the variable names, we will need to explicitly set them using the R function names().

Table 2 Use this R code to add names to the dataset

names(Data) <- c(“Gender”, “Age”, “MonthlyExpenses”, “MaritalStatus”, “HomeStatus”, “Occupation”, “BankingInstitution”, “YearsEmployed”, “NoPriorDefault”, “Employed”, “CreditScore”, “DriversLicense”, “AccountType”, “MonthlyIncome”, “AccountBalance”, “Approved”)

When the data is imported using the function read.table(), the variable type is automatically assigned. Data types can also be manually assigned and may need to be re-assigned to perform some types of numerical analysis – such as re-coding a two-level factor into a numeric binary variable, for example.

Table 3 Use the following R code to manually define the variables

Data$Gender <- as.factor(Data$Gender) Data$Age <- as.numeric(Data$Age)

Data$MonthlyExpenses <- as.integer(Data$MonthlyExpenses) Data$MaritalStatus <- as.factor(Data$MaritalStatus) Data$HomeStatus <- as.factor(Data$HomeStatus) Data$Occupation <- as.factor(Data$Occupation) Data$BankingInstitution <- as.factor(Data$BankingInstitution) Data$YearsEmployed <- as.numeric(Data$YearsEmployed) Data$NoPriorDefault <- as.factor(Data$NoPriorDefault) Data$Employed <- as.factor(Data$Employed) Data$CreditScore <- as.numeric(Data$CreditScore) Data$DriversLicense <- as.factor(Data$DriversLicense)

Data$AccountType <- as.factor(Data$AccountType) Data$MonthlyIncome <- as.integer(Data$MonthlyIncome) Data$AccountBalance <- as.numeric(Data$AccountBalance) Data$Approved <- as.factor(Data$Approved)

Table 4 Variables Gender and DriversLicense are both nominal binary variables, i.e., unordered factorswithtwolevels(values).Theydon’tneedto,buttheycould,berepresentedasnumeric binaryvariables,takingvalues0and1.ThetwovaluesforGenderstandforMaleandFemale, and the two values for DriversLicense indicate whether or not the individual has a current drivers license. Discuss if these variables are better interpreted as symmetric or asymmetric for the sake of credit approval analysis, justifying and/or contextualising youranswer. Marks(4)

Enter your answer here

(c) Records with missing values

There are also a few missing values. For this project, observations with missing values need to be removed. To remove observations with missing values, use the R function na.omit().

Table 5 Use this R code to remove the records with missing values from the dataset

Data <- na.omit(Data)

Table 6 In the original data, how many missing values in total are there? Hint: use the function

summary(). How many records are removed by using the function na.omit()? Marks(1)

Enter your answer here

  1. Calculating and visualising proximity measurements

Table 7 The dataset contains variables with mixed types. Use R function daisy() from package clusterto compute a Gower dissimilarity (distance) matrix between the data records, and refer to the result as “Dist”. Enter the R code you used, including any libraries needed. Marks (2)

Enter your answer here

The R object produced from the function daisy()is called a dissimilarity object and is efficient in storing information, but is not readily visualised or easy to extract information from. To make the dissimilarity object easier to work with, we can convert it to a matrix.

Table 8 Use the R code to convert the Gower dissimilarity object into a distance matrix

Dist <- as.matrix(Dist)

Table 9 Using the new distance matrix, what is the Gower similarity measure between the 10th and the 60th observation (row)? Answer using R command(s).Marks(2)

Enter your answer here

Because there are a large number of observations/records (rows) in the dataset, it is typical to visualise the distance matrix to gain insight into data structures.

Table 10 Use the following R code to visualise the distance matrix

dim <- ncol(Dist) # used to define axis in image

image(1:dim, 1:dim, Dist, axes = FALSE, xlab=””, ylab=””, col = rainbow(100))

NOTE (Optional): Additionally, you could also reorder the rows and columns of the matrix according to their similarities before visualising, using a technique called clustering(which will be studied as part of other, more advanced subjects, namely, data mining and machine learning): Marks(1)

heatmap(Dist, Rowv=TRUE, Colv=”Rowv”, symm = TRUE)

Enter your answer hereTable 11 Insert the image(s) of the distance matrix below, then Describe the pattern you see when visualising it(them). Marks (1)

Enter your answer here

Visualising a distance matrix is one form of initially exploring the dataset. Correlation matrices between numerical data types can also be useful when exploring the data.

Table 12 Enter your R code used to calculate the Pearson and then the Spearman correlation matrices using all numericalvariables. Marks(2)

Enter your answer here

  1. Visually exploring data patterns and relationships

We may have preconceived notions of what to expect in some datasets. In credit card applications, we may hypothesise that approval would be aligned, for example, with account balance, monthly expenses, credit score and/or age.

Table 13 Use the ggplot2 library to produce box plots for AccountBalance, MonthlyExpenses, CreditScore and Age segmented by approval (variable “Approved”). Insert the R codes and resulting images into the table below. Marks(4)

Enter your answer here
Enter your answer here



Enter your answer here

Enter your answer here

Table 14 Describe the apparent patterns shown in the visualisations in Table 13. Marks (4)

Enter your answer here

Table 15 Use the ggplot2 library to produce bar plots for Employed, MaritalStatus, BankingInstitution, and NoPriorDefault, all segmented by approval (variable “Approved”). Insert the R codes and resulting images into the table below. Marks (4)

Enter your answer here
Enter your answer here


Enter your answer here
Enter your answer here

Table 16 Considering that the values “f” (False) and “t” (true) of variables Employed and NoPriorDefault mean unemployed/employed and defaulted-before/never-defaulted-before, respectively, and regardless of the meaning of the values for MaritalStatus and BankingInstitution,describeinterestingrelationships(ifany)betweenthesenominalvariables and the approval of the application by visually inspecting the bar plots in table15. Marks(4)

Enter your answer here

Apparently, the strongest influencing factor in the approval of the applications is if there has been a prior credit default. By using a contingency table, we can examine the strength of this relationship.

Table 17 Use the function table() and calculate the Simple Matching Coefficient (SMC) between NoPriorDefault and Approved, assuming that values “f” (False) and “t” (True) for

NoPriorDefault are associated with “-“ and “+” for Approved, respectively. Discuss the interpretation of the SMC in this scenario. Is Jaccard meaningful in this case? Marks(3)

Enter your answer here

Table 18 Further explore this dataset (freely), possibly using other types of plots, such as histograms and scatter plots, and try to obtain further insights and hypotheses. For instance, is there any interesting relationship between monthly disposable income (“MonthlyIncome”) and approval (“Approved”), in particular when compared to what you would in principle expect (if anything)? Share your main insights / hypotheses (no more than 1 page). NOT ASSESSED

Enter your answer here

The post Importing data and handling variable types appeared first on My Assignment Online.

Plagiarism Free Assignment Help

Expert Help With This Assignment — On Your Terms

Native UK, USA & Australia writers Deadline from 3 hours 100% Plagiarism-Free — Turnitin included Unlimited free revisions Free to submit — compare quotes
Scroll to Top