Data Import and Submission Export Tutorial

This tutorial will use R to import the data and output a sample submission.

To begin, make sure your R session has its working directory set to the same directory where your data is located. To view your current working directory, run the command getwd() in the R console. Use the R options File -> Change dir... in the RGui to set your working directory or use the setwd('insert/your/path/to/data.csv') command to set your working directory.

To double check that train.csv and test.csv are in your current working directory, the following command should return TRUE twice, as seen in the output below.

In [1]:
c('train.csv', 'test.csv') %in% list.files()
Out[1]:
  1. TRUE
  2. TRUE

Data Import

Now that we have an R session with our two data files in the working directory, read the comma separated data using the read.csv() function and view a few predictor summaries.

In [2]:
train = read.csv('train.csv')
In [3]:
head(train)
Out[3]:
RowIDCalendarYearModelYearMakeModelCat1Cat2Cat3Cat4Cat5Var5Var6Var7Var8NVCatNVVar1NVVar2NVVar3NVVar4Response
141807920052004AUAU.14BCAAA0.02168622-0.6852558-0.5912954-0.2584976F-0.2315299-0.26611684.209404-0.25141890
223262520062003RR.30BCBAA-0.3529838-0.4232408-0.62815820.05436843O-0.2315299-0.2661168-0.2723372-0.25141890
337902920062006AUAU.14BAAAA0.06926337-0.6852558-0.5912954-0.163872M-0.2315299-0.2661168-0.2723372-0.25141891
418145820072000BUBU.38FCACA0.0871048-0.1717531-0.97221140.2064257O-0.2315299-0.2661168-0.2723372-0.25141890
519243420051999BUBU.38FAACA0.0871048-0.2723481-0.97221140.290987M-0.2315299-0.2661168-0.2723372-0.25141891
644332120072005AUAU.11BCBAA0.1406291-0.1635651-0.54214490.5499219O-0.2315299-0.2661168-0.2723372-0.25141890
In [4]:
hist(train$Var4, main = "Histogram of Var4", xlab = "Var4")
In [5]:
summary(train$Var8)
Out[5]:
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
-1.48500 -0.52320 -0.24210 -0.00946  0.16210 33.90000 

Also read the test set into your R session via the read.csv() function.

In [6]:
test = read.csv('test.csv')
In [7]:
head(test)
Out[7]:
RowIDCalendarYearModelYearMakeModelCat1Cat2Cat3Cat4Cat5Var4Var5Var6Var7Var8NVCatNVVar1NVVar2NVVar3NVVar4
149116920092006ANAN.4BBBAA-0.9014148-0.7871253-0.939083-0.8124724-0.7338132J-0.23152992.021183-0.27233721.408637
266190720092001BUBU.5BCBAA1.0436610.39635630.004873184-0.824760.808638O-0.2315299-0.2661168-0.2723372-0.2514189
348945920092005YY.21BCAAA-0.7830189-0.6443939-0.8408273-0.6895963-0.8164055E-0.2315299-0.2661168-0.2723372-0.2514189
449682420092006BFBF.18BCAAA0.99291960.53314060.2493426-0.55443260.01006398O-0.2315299-0.2661168-0.2723372-0.2514189
546456720092008KK.40ECAAA-1.104379-1.179637-1.061903-0.1366537-0.616215O-0.2315299-0.2661168-0.2723372-0.2514189
630729620092007KK.40ECAAA-1.104379-1.179637-1.061903-0.2595298-0.685133L-0.2315299-0.2661168-0.2723372-0.2514189
In [8]:
plot(density(train$Var2), main = "Density of Var2")
In [9]:
dim(train)
Out[9]:
  1. 100000
  2. 32
In [10]:
dim(test)
Out[10]:
  1. 40000
  2. 31

The training and testing sets have a different number of columns. This is, of course, because the test set does not contain the response variable. The following command will tell us which column is contained in the training set and not in the testing set.

In [11]:
setdiff(names(train), names(test))
Out[11]:
"Response"

Data Export

When making a submission, the predictions need to be exported in a certain fashion. The example below will generate random uniform numbers and use them as our predictions.

In [12]:
numberOfObservationsInTestSet = nrow(test)
vectorOfPredictions = runif(numberOfObservationsInTestSet, 0, 1)
summary(vectorOfPredictions)
Out[12]:
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
0.0000013 0.2508000 0.4968000 0.4987000 0.7473000 0.9999000 
In [13]:
outputDataSet = data.frame("RowID" = test$RowID,
                           "ProbabilityOfResponse" = vectorOfPredictions)

Inspect data set before export

In [14]:
head(outputDataSet)
Out[14]:
RowIDProbabilityOfResponse
14911690.5298258
26619070.7990218
34894590.42184
44968240.7455896
54645670.1310719
63072960.8185932

The following command will output a comma separated file to the current working directory. Find your current working directory again by executing the getwd() command.

In [15]:
write.csv(outputDataSet, "submissionExample.csv", row.names = FALSE)