-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThe Train_R_project.Rmd
More file actions
205 lines (151 loc) · 4.42 KB
/
The Train_R_project.Rmd
File metadata and controls
205 lines (151 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
---
title: "R_project.Rmd"
output: html_document
date: "2024-03-20"
---
```
```
# The Train Project
```
Note:
The train dataset in the Spaceship Titanic records 8693 passengers as a row number with 14 column objects.
Passenger is specified with a unique number. The Homeplanet is the residence of the passenger and the Destination is the place where the passenger is moved. CryoSleep is for the passenger to be elected for suspended animation during the voyage. The Cabin is the number where the passenger stays.The passager is also age-specified and allowed to pay for VIP and bills for Room service, Food court, shopping mall, Spy, and VRDesk. The dataset also records the name of the passenger and whether the passenger is transported to the destination.
Analyzing the data encompasses four steps. Reading the data, cleaning the data, analyzing and visualizing using R libraries and functions.
```
```{r}
#setwd("/Users/ruth/Downloads/train.csv")
df_train <- read.csv("~/Downloads/train_data(in).csv")
View(df_train)
```
```{r}~/Desktop/train.csv}
```
```{r}
df_train
```
# Cleaning Data: Filtering the missing data using filter function.
- <div>
```{r}
df_train %>%
select(HomePlanet, CryoSleep, Cabin, Destination, Age, VIP, RoomService, FoodCourt, ShoppingMall, Spa,VRDeck, Name,Transported)%>%
filter(!complete.cases(.))%>%
View()
#As part of cleaning data, we first identify the missing data and removing duplicates #and invalid data. In the case of train data, there is no duplicates and invalid data, but there are missing data. Identfing and visualzing the missing data takes place.
```
</div>
To visualize the missing data by using 'mice' library and the md.pattern(missing data pattern) inside it
```{r fig.width=10, fig.height= 10, size = 30}
library(mice)
md.pattern(df_train)
```
As there are empty data frame cells , that should be replaced by NA value
```{r}
#df_train %>%
#select(HomePlanet, CryoSleep, Cabin, Destination, Age, VIP, RoomService, FoodCourt, ShoppingMall, Spa, VRDeck, Name,Transported)%>%
# mutate(df_train = na_if("")) %>%
# filter(is.na(df_train))
#View()
df_train[df_train == "" | df_train == " "] <- NA
View(df_train)
# we start analyzing by using summary () function that shows the structure, datatype and stastical values of the dataset
```
# Analyzing Data
```{r}
summary(df_train)
```
```{r}
y <- mean(df_train$Age, na.rm = TRUE )
y
```
```{r}
#library(attach)
x <- median(df_train$Age, na.rm = TRUE)
x
```
```{r}
```
```{r}
ggplot(df_train, mapping = aes(x= HomePlanet, y= Destination))+
geom_point(size = 5)+
geom_line(colour = 'red')
```
# Visualizing Data
```{r}
library(tidyverse)
library(ggplot2)
# using tidyverse library and ggplot function , analyzing and visualizing is taking place.
```
```{r}
pie = ggplot(df_train) +
geom_bar(mapping = aes(Transported , fill= HomePlanet),
position = "fill", drop_na(df_train, HomePlanet, Transported)
, alpha= 0.5) +
theme(aspect.ratio = 0.5) +
labs(title="Transported from Home Planet",
x= NULL, y= NULL)
pie + coord_polar()
```
```{r}
df_train%>%
drop_na(HomePlanet)%>%
drop_na(Destination)%>%
ggplot(aes(fill= HomePlanet, Destination))+
geom_bar(position = "dodge",size =5, alpha = 0.5)+
labs(title= "Home Planet and Destination")
```
```{r}
hist(df_train$Age)
```
```{r}
df_train%>%
drop_na(HomePlanet) %>%
drop_na(Destination)%>%
drop_na(VIP)%>%
drop_na(CryoSleep)%>%
ggplot(aes(HomePlanet, Destination))+
geom_boxplot()+
geom_point(alpha = 1,
aes( size= VIP,
colour = HomePlanet)) +
facet_wrap(~CryoSleep)
coord_flip()
theme_bw()
labs(title = "VIP Transported")
```
```{r}
df_train%>%
drop_na(HomePlanet)%>%
drop_na(VIP)%>%
ggplot(aes(VIP, HomePlanet),
) +
geom_point(size = 5,alpha = 0.5)+
coord_flip()
facet_wrap(~Destination)
theme_bw()
```
```{r}
df_train%>%
ggplot(aes(RoomService, FoodCourt))+
geom_line(colour = 'red' )+
geom_point(size = 0.5, color = 'green')
```
```{r}
#
Transported_data <- df_train%>%
drop_na(Transported)%>%
group_by(PassengerId)%>%
summarise(PassengerId, Transported
)
View(Transported_data)
```
```{r}
df_train%>%
drop_na(Transported)%>%
ggplot(aes(fill= Transported, PassengerId))+
geom_bar(position = "dodge",size =5, alpha = 0.5)+
facet_wrap(~Transported)
labs(title= "How many Transported")
```
```{r}
```
```
```