Skip to main content

Posts

Showing posts with the label r

Titanic: A case study for predictive analysis on R (Final)

Our previous attempt to accurately predict whether a passenger is likely to survive, a competition from Kaggle.com . We used some statistics and machine learning models to classify the passengers. In our final part, we will push our limits using advanced machine learning models, including Random Forests, Neural Networks, Support Vector Machines and other algorithms, and see how long we can torture our data before it confesses. Let's resume from where we left. We are applying an implementation of Random forest method of classification. Shortly, this model grows many decision trees and then uses a voting system to decide which trees to pick. This way, the common issue with decision trees,  over fitting  is mitigated (learn more here ). > library(randomForest) > formula <- as.factor(Survived) ~ Sex + Pclass + FareGroup + SibSp + Parch + Embarked + HasCabin + AgePredicted + AgeGroup  > set.seed(seed) > rf_fit <- randomForest(formula, data=dataset[...

Titanic: A case study for predictive analysis on R (Part 4)

Working with titanic data set picked from Kaggle.com's competition, we predicted the passenger survivals with 79.426% accuracy in our previous attempt . This time, we will try to learn the missing values instead of setting trying mean or median. Let's start with Age. Looking at the available data, we can hypothetically correlate Age with attributes like Title, Sex, Fare and HasCabin. Also note that we previous created variable AgePredicted ; we will use it here to identify which records were filled previously. > age_train <- dataset[dataset$AgePredicted == 0, c("Age","Title","Sex","Fare","HasCabin")] >   age_test <- dataset[dataset$AgePredicted == 1, c("Title","Sex","Fare","HasCabin")] >   formula <- Age ~ Title + Sex + Fare + HasCabin >   rp_fit <- rpart(formula, data=age_train, method="class") >   PredAge <- predict(rp_fit, newdata=age_tes...

Titanic: A case study for predictive analysis on R (Part 3)

In our previous attempt , we applied some machine learning techniques to our data and predicted the values for target variable using AgeGroup, Sex, Pclass and Embarked attributes. Now, we will further explore other attributes and see how much information we can extract. This time, instead of keeping test set apart, we will merge it into the training data set. This will enable us to collect complete range of values for each attribute, in case there are some missing outs in training set: > dataset$Dataset <- 'train' >   testset$Dataset <- 'test' >   testset$Survived <- 0 >   dataset <- rbind(dataset, testset[,c(1,13,2:12)]) This may look a strange way to merge two data sets, but here's some explanation. The first line adds a column Survived to testset, so that both the dataset and testset have identical columns. The next two lines add another column to identify whether a record is from training set or test set. The last line merge...

Titanic: A case study for predictive analysis on R (Part 2)

Previously , we successfully classified passengers as "will survive" or "will not survive" with 76.5% accuracy using Gender only. We will now extend our experiment further and include other attributes in order to improve our accuracy rate. Let's resume.. Pre-processing Real data is never in ideal form. It always needs some pre-processing. We will fill missing values, extract some extra information from available variables and convert continuous valued fields into discrete valued. First, let's have a look at how Age variable is distributed. We will use a parameter useNA="always" in table function > table(dataset$Age, useNA="always") We see 177 missing values (NA's). We will fill them with mean value of  Age  as a straight forward solution. The commands below store TRUE/FALSE values in a vector bad by checking whether the value of age is available for each record or not, then stores the middle value of age where it is ...

Titanic: A case study for predictive analysis on R (Part 1)

Kaggle.com is a popular community of data scientists, which holds various competitions of data science. The article performs predictive analysis on a benchmark case study -- Titanic, picked from Kaggle.com -- in-depth. The case study is a classification problem, where the objective is to determine which class does an instance of data belong to. This can also be called prediction problem, because we are predicting class of a record based on its attributes. Note: This tutorial requires some basic R programming background. If you haven't yet gotten yourself acquainted with R, maybe this is the right time. Codeacademy's tutorial is my personal recommendation. We will be using RStudio here,  the most used IDE for 'R' language. It is free and open-source, you can download it  here . Dataset: RMS Titanic was a British cruise that sank on its course in the North Atlantic Ocean on its maiden voyage. 1502 people, out of 2224 on board lost their lives in this disaster. D...