R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

# Titanic Data Analysis
# Lazarus O Egwurube    T no= T00797302


# 1. 
library(readr)
titanic3 <- read_csv("C:/Users/lazarus/Downloads/titanic3.csv")
## Rows: 1309 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): pclass, name, sex, ticket, cabin, embarked, boat, home.dest
## dbl (6): survived, age, sibsp, parch, fare, body
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(titanic3)

# 2. 
t3<-titanic3
View(t3)

# 3. 
titanic<-t3[,c("survived", "embarked", "age", "sex", "sibsp", "parch", "fare")]

# 4. 
summary(titanic)
##     survived       embarked              age            sex           
##  Min.   :0.000   Length:1309        Min.   : 0.17   Length:1309       
##  1st Qu.:0.000   Class :character   1st Qu.:21.00   Class :character  
##  Median :0.000   Mode  :character   Median :28.00   Mode  :character  
##  Mean   :0.382                      Mean   :29.88                     
##  3rd Qu.:1.000                      3rd Qu.:39.00                     
##  Max.   :1.000                      Max.   :80.00                     
##                                     NA's   :263                       
##      sibsp            parch            fare        
##  Min.   :0.0000   Min.   :0.000   Min.   :  0.000  
##  1st Qu.:0.0000   1st Qu.:0.000   1st Qu.:  7.896  
##  Median :0.0000   Median :0.000   Median : 14.454  
##  Mean   :0.4989   Mean   :0.385   Mean   : 33.295  
##  3rd Qu.:1.0000   3rd Qu.:0.000   3rd Qu.: 31.275  
##  Max.   :8.0000   Max.   :9.000   Max.   :512.329  
##                                   NA's   :1
# 5. 
prop.table(table(titanic$survived))
## 
##        0        1 
## 0.618029 0.381971
# 6. 
titanic<-na.omit(titanic)

# 7.
titanic$survived<-factor(titanic$survived)
titanic$embarked<-factor(titanic$embarked)
titanic$sex<-factor(titanic$sex)

str(titanic)
## tibble [1,043 × 7] (S3: tbl_df/tbl/data.frame)
##  $ survived: Factor w/ 2 levels "0","1": 2 2 1 1 1 2 2 1 2 1 ...
##  $ embarked: Factor w/ 3 levels "C","Q","S": 3 3 3 3 3 3 3 3 3 1 ...
##  $ age     : num [1:1043] 29 0.92 2 30 25 48 63 39 53 71 ...
##  $ sex     : Factor w/ 2 levels "female","male": 1 2 1 2 1 2 1 2 1 2 ...
##  $ sibsp   : num [1:1043] 0 1 1 1 1 0 1 0 2 0 ...
##  $ parch   : num [1:1043] 0 2 2 2 2 0 0 0 0 0 ...
##  $ fare    : num [1:1043] 211 152 152 152 152 ...
##  - attr(*, "na.action")= 'omit' Named int [1:266] 16 38 41 47 60 70 71 75 81 107 ...
##   ..- attr(*, "names")= chr [1:266] "16" "38" "41" "47" ...
# 8. 
cor_matrix <- cor(titanic[, sapply(titanic, is.numeric)])

cor_matrix
##              age      sibsp      parch      fare
## age    1.0000000 -0.2423449 -0.1493106 0.1772057
## sibsp -0.2423449  1.0000000  0.3739597 0.1421305
## parch -0.1493106  0.3739597  1.0000000 0.2176495
## fare   0.1772057  0.1421305  0.2176495 1.0000000
# 9. 
pairs(~survived + sex + embarked, data = titanic)

# 10. 
set.seed(1000)
indexes <- sample(1:nrow(titanic), size = 0.8 * nrow(titanic))
train_data <- titanic[indexes, ]
test_data <- titanic[-indexes, ]

# 11. 
train_data <- titanic[1:834, ]
test_data <- titanic[835:1043, ]

# 12. 
library(rpart)
## Warning: package 'rpart' was built under R version 4.3.3
str(titanic)
## tibble [1,043 × 7] (S3: tbl_df/tbl/data.frame)
##  $ survived: Factor w/ 2 levels "0","1": 2 2 1 1 1 2 2 1 2 1 ...
##  $ embarked: Factor w/ 3 levels "C","Q","S": 3 3 3 3 3 3 3 3 3 1 ...
##  $ age     : num [1:1043] 29 0.92 2 30 25 48 63 39 53 71 ...
##  $ sex     : Factor w/ 2 levels "female","male": 1 2 1 2 1 2 1 2 1 2 ...
##  $ sibsp   : num [1:1043] 0 1 1 1 1 0 1 0 2 0 ...
##  $ parch   : num [1:1043] 0 2 2 2 2 0 0 0 0 0 ...
##  $ fare    : num [1:1043] 211 152 152 152 152 ...
##  - attr(*, "na.action")= 'omit' Named int [1:266] 16 38 41 47 60 70 71 75 81 107 ...
##   ..- attr(*, "names")= chr [1:266] "16" "38" "41" "47" ...
# 13.
fit <- rpart(survived ~ sex + age + sibsp + parch + fare + embarked, data = train_data, method="class")

# 14. 
png("regression_tree.png")
plot(fit)
text(fit, use.n=TRUE)
dev.off()
## png 
##   2
#15.
library(rpart.plot)
## Warning: package 'rpart.plot' was built under R version 4.3.3
library(RColorBrewer)
library(rattle)
## Warning: package 'rattle' was built under R version 4.3.3
## Loading required package: tibble
## Loading required package: bitops
## Rattle: A free graphical interface for data science with R.
## Version 5.5.1 Copyright (c) 2006-2021 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
#16.
fancyRpartPlot(fit)

#17.
#sex

#18
predictions <- predict(fit, test_data, type = "class")

#19.
Results <- data.frame(PassengerSex = test_data$sex, survived = predictions)

#20.
write.csv(Results, file = "Titanictree.csv", row.names = FALSE)

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.