-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataVisualizationandDescStatsForMammals.R
More file actions
303 lines (206 loc) · 9.61 KB
/
Copy pathDataVisualizationandDescStatsForMammals.R
File metadata and controls
303 lines (206 loc) · 9.61 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
################################
## Author : Can Kocagil ###
################################
################################
#### --- Dependencies --- ####
#### 1) ggplot2 ####
#### 1) cowplot ####
################################
# 0) Uploading necessary R packages to proceed
# 1) Explanatory data analysis to familiarize the dataset(EDA)
# 2) Extra plotting for EDA
# 3) Statistical features of the dataset (Descriptive statistics)
# 4) Question for Hypothesis testing
# 5) Trying to answer question by analyzing data
##################################################################################################################
##################################################################################################################
# ------- Step 0 ------- #
##########################
# Uploading necessary packages :
# install.packages('ggplot2')
library(ggplot2)
data(msleep)
## ---- Step 0 Finish ---- ##
# ------- Step 1 ------- #
##########################
# Preliminary Explanatory data analysis to familiarize the dataset(EDA)
# Getting the data
df <- msleep
# Looking first rows from beginning :
head(df)
# Loaking last rows from end :
tail(df)
ls(df)
# Getting column names :
df_col_names = colnames(df)
df_col_names
# Reading the written features of the data
?msleep
# here are the explanations of the features :
#############################################
# name
# ---- common name
# genus
# vore
# --- carnivore, omnivore or herbivore?
# order
# conservation
# --- the conservation status of the animal
# sleep_total
# --- total amount of sleep, in hours
# sleep_rem
# --- rem sleep, in hours
# sleep_cycle
# --- length of sleep cycle, in hours
# awake
# --- amount of time spent awake, in hours
# brainwt
# --- brain weight in kilograms
# bodywt
# --- body weight in kilograms
#############################################
# Dimension/size of the dataset
size_df = dim(df) ;size_df
# So we have 83 rows with 11 features
# Checking the missing values of the dataset
df_na = is.na.data.frame(df) ;df_na
##########################
## ---- Step 1 Finish ---- ##
# ------- Step 2 ------- #
##########################
# Extra plotting for Explanatory Data Analysis :
# In this section, I am going to visualize the data to gain insight of it
# the columns 6-9 are selected since they are not categorical(numerical) :
pairs(df[ ,c(6:9)])
# Plot the distribution of vores in the msleep dataset
pie(table(df$vore))
# Histogram total sleep grouped by vores
ggplot(data = df,aes(df$sleep_total)) + geom_histogram() +facet_grid(~vore)
# Histogram of the number of hours per day mammals are asleep (using 20 bins along the x axis)
ggplot(data=df, aes(x=sleep_total)) +
geom_histogram(bins=20) +
labs(x='Total sleep time (h)',
y='Frequency') + theme_bw()
# Explanatory data analysis for body weights and sleeping time grouped by vore
ggplot(df)+ geom_point(aes(log10(df$bodywt),sleep_total))+facet_wrap(~vore)
# Explanatory data analysis for brain weights and sleeping time grouped by vore
ggplot(df)+ geom_point(aes(log10(df$brainwt),sleep_total))+facet_wrap(~vore)
# Labels for following plotting :
labels = c('herbi'='Herbivore','carni'='Carnivore','omni'='Omnivore','insecti'='Insectivore')
# Visuzalization of the vores w.r.t. number of them
ggplot(data=df,aes(x=vore,fill=vore)) +
geom_bar() +
scale_x_discrete(name = 'Feeding Type',labels=labels) +
scale_fill_discrete(name = 'Feeding Type',
labels=labels) +
labs(x='Feeding Type',y='# of Species')
# Plot REM sleep against total sleep in units of hour per day :
ggplot(data=df,aes(x=sleep_total,y=sleep_rem)) +
geom_point(na.rm=TRUE) +
labs(x='Total sleep',y='REM sleep')+
geom_smooth(method='lm',na.rm=T)
# ------- Step 3 ------- #
##########################
# Statistical features of the dataset :
# Basic central tendency analysis :
# Measures of central tendency for sleep_total
# The argument na.rm=TRUE removes any missing values
# Expected value for sleep time
mean(df$sleep_total, na.rm=T)
# Medium value for sleep time
median(df$sleep_total, na.rm=T)
# Now looking the spread of the data :
sd(df$sleep_total,na.rm = T)
# Mean absolute deviation :
mad(df$sleep_total, na.rm=T)
# Inter quartile range :
IQR(df$sleep_total, na.rm=T)
# Getting statistical information about the dataset
summary(df)
# Maximum sleep time :
max(df$sleep_total, na.rm=T)
# Minimum sleep time :
min(df$sleep_total, na.rm=T)
# Range of sleep time : (Maximum - Minimum)
range(df$sleep_total, na.rm=T)
# Correlation part :
# I am going to use Pearson correlation coefficient :
# Correlations close to -1 are very strong negative relationships
# Correlations close to 0 are weak relationships
# Correlations close to 1 are very strong positive relationships
# Finding the correlation between body weight and total sleep time :
cor((df$bodywt),msleep$sleep_total,use = "complete.obs",method = 'pearson')
# Finding the correlation between body weight and total sleep time in log10 bases :
cor(log10(df$bodywt),msleep$sleep_total,use = "complete.obs",method = 'pearson')
# Finding the correlation between brain weight and total sleep time :
cor((df$brainwt),msleep$sleep_total,use = "complete.obs",method = 'pearson')
# Finding the correlation between brain weight and total sleep time in log10 bases :
cor(log10(df$brainwt),msleep$sleep_total,use = "complete.obs",method = 'pearson')
# Qualitative analysis :
table(df$vore)
table(df$sleep_total,df$vore)
# I am going to further Qualitative analysis part in the visualization part
##########################
# ------- Step 4 ------- #
##########################
# Question for Hypothesis testing :
# I am going to find an answer to the question :
# Does body weight or brain weight, which one have more
# impact on with total rem sleep time?
# Expected value for sleep time
expected = mean(df$sleep_rem, na.rm=T) ;expected
# Medium value for sleep time
medium = median(df$sleep_rem, na.rm=T) ;medium
# Now looking the spread of the data :
std = sd(df$sleep_rem,na.rm = T);std
# Maximum sleep time :
max_val =max(df$sleep_rem, na.rm=T) ;max_val
# Minimum sleep time :
min_val = min(df$sleep_rem, na.rm=T);min_val
# Finding the correlation between body weight and total sleep time :
corr = cor((df$bodywt),df$sleep_rem,use = "complete.obs",method = 'pearson')
# Finding the correlation between body weight and total sleep time in log10 bases :
corrlog10 = cor(log10(df$bodywt),df$sleep_rem,use = "complete.obs",method = 'pearson');corrlog10
info = cat('Mean :', round(expected,2), '\nMedium :', round(medium,2), '\nStandart Deviation :' ,round(std));info
##########################
# ------- Step 5 ------- #
##########################
# Trying to answer question by analyzing data :
library(cowplot)
theme <- theme(plot.title = element_text(face = "bold",size = 14,color = 'deepskyblue1',hjust = 0.5),
plot.subtitle = element_text(face = 'bold',color = 'greenyellow',hjust = 0.5),
plot.caption = element_text(color = "grey52", face = "italic",hjust = 0.5),
legend.position = 'right',
legend.title = element_text(colour="black", size=10,
face="bold"),
legend.background = element_rect(fill="lightblue",
size=0.5, linetype="solid",
colour ="darkblue"),
axis.title.x = element_text(color="darkblue", size=14, face="bold"),
axis.title.y = element_text(color="darkblue", size=14, face="bold"))
labels = c('herbi'='Herbivore','carni'='Carnivore','omni'='Omnivore','insecti'='Insectivore')
caption1 = 'Descriptive Statistics of Total Rem Sleep Time \n
Mean : 1.87 Medium : 1.5 Standart Deviation : 1.29\n Max : 6.6 Min : 0.1
Pearson Correlation : -0.32
Data source : Msleep '
caption2 = 'Descriptive Statistics of Total Rem Sleep Time \nMean : 10.43 Medium : 10.1
Standart Deviation : 4.45\n Max : 19.9 Min : 1.9
Pearson Correlation : -0.56
Data source : Msleep'
p1 <- ggplot(data=msleep,aes(x=log10(df$bodywt),y=sleep_rem,fill = vore,colour = log10(bodywt))) +
geom_point(na.rm=TRUE) +
geom_smooth(method='loess',na.rm=T)+
facet_wrap(~vore)+
labs(x='Total Rem Sleep(hour per day)',y='Body Weight(kg)',caption = caption1,
colour = 'Body Weight',fill = 'Vores')+
ggtitle('The Effect of Body Weight on Total Rem Sleep Time\n',subtitle = 'Mammals Grouped by Vores')+theme
p2 <- ggplot(data=msleep,aes(x=log10(df$brainwt),y=sleep_rem,fill = vore,colour = log10(brainwt))) +
geom_point(na.rm=TRUE) + geom_smooth(method='loess',na.rm=T)+
facet_wrap(~vore)+ labs(x='Total Rem Sleep(hour per day)',y='Brain Weight(kg)',caption = caption2,
colour = 'Brain Weight',fill = 'Vores')+
ggtitle('The Effect of Brain Weight on Total Rem Sleep Time\n',subtitle = 'Mammals Grouped by Vores')+ theme
p3 <-ggplot(data=df,aes(x=vore,fill=vore)) +geom_bar() +scale_x_discrete(name = 'Feeding Type',labels=labels) +
scale_fill_discrete(name = 'Feeding Type',labels=labels)+labs(x='Feeding Type',y='# of Species')
p4 <-ggplot(data=df,aes(x=sleep_total,y=sleep_rem))+geom_point(na.rm=T) +labs(x='Total sleep',y='REM sleep')+
geom_smooth(method='loess',na.rm=T)
plot_grid(p1,p2,p3,p4)