```{r }
library(readr) T3 <- read_csv(“https://goo.gl/At238b”) View(T3)
titanic <- data.frame(T3\(survived, T3\)sex,T3\(age, T3\)sibsp, T3\(parch, T3\)fare,T3$embarked) titanic View(titanic)
feature_name<- c(‘survived’, ‘sex’, ‘age’, ‘sibsp’, ‘parch’, ‘embarked’, ‘fare’) colnames(titanic)<- feature_name View(titanic)
#Remove NAs if any
any(is.na(titanic)) titanic_na<-na.omit(titanic) any(is.na(titanic_na))
#Perform a statistical analysis of the titanic dataset.
summary(titanic) sapply(titanic_na, sd) summary(titanic_na\(survived) summary(titanic_na\)age) summary(titanic_na\(sibsp) summary(titanic_na\)fare) summary(titanic_na$parch)
```{r }
#Survived is the dependent variable, find its proportion in the dataset.
table(titanic_na$survived)
prop.table(table(titanic_na$survived))
str(titanic_na$survived)
#Make Survived, embarked, and sex as factors
titanic$survived <- as.factor(titanic$survived)
titanic$embarked <- as.factor(titanic$embarked)
titanic$sex <- as.factor(titanic$sex)
#Find the correlation matrix between survival and the other features.
cor(titanic_na$survived, titanic_na[, c("embarked", "sibsp", "parch")])
#Plot survival with other features to see if any correlation exists.
plot(titanic$survived~titanic$sex)
```{r } #Set a seed to 1000 and use it to split the dataset into 80% training, 20% testing.
set.seed(1000)
train_index <- sample(1:nrow(titanic_na), 0.8 * nrow(titanic_na))
train <- titanic_na[train_index, ]
test<- titanic_na[-train_index, ]
#Build your training (till index 1046) and test (till index 1308)datasets
titanic_train <- titanic_na[1:834,] titanic_test <- titanic_na[835:1043,]
#Download the rpart for “Recursive Partitioning and RegressionTrees” if it is not in your R base and bring it in yourenvironment. library(rpart)
#Train your learner on the training dataset and save your model in a variable fitfit <- rpart(Survived ~ Sex + Age + SibSp + Parch + Fare + Embarked,data=train, method=“class”)
fit <- rpart(survived ~ sex + age + sibsp + parch + fare + embarked, data = titanic_train, method = “class”)
fit
```{r }
install.packages(c("rattle", "rpart.plot", "RColorBrewer"))
library(rpart.plot)
library(rattle)
library(RColorBrewer)
fancyRpartPlot(fit)
#Examine the tree you obtained. What is the most “important”feature over which the tree first split? Do you agree with theTitanic slogan “ Women and children first!”
#The most important feature which the tree first split would be the "sex" feature. I do agree with with the slogan "Women and children first!"
#Use the predict function with your model fit to makepredictions on the test dataset and save it in a variablePrediction:Prediction <- predict(fit, test, type = "class")
Prediction <- predict(fit, titanic_test, type = "class")
Prediction
#Save the results in a data frame Results that will have twocolumns 1: PassengerSex =test$Sex, and 2: Survived = Prediction
Results <- data.frame(PassengerSex = titanic_test$sex, Survived = Prediction)
Results
#Save your data frame in a .csv file by usingwrite.csv(Results, file = "Titanicdtree.csv", row.names = FALSE)
write.csv(Results, file = "Titanicdtree.csv", row.names = FALSE)