data(mammalsleep) #Sleep in Mammals: Ecological and Constitutional Correlates
sum(is.na(mammalsleep)) #To check if there is na
## [1] 38
ms<-na.omit(mammalsleep) #Omit na and new name
View(ms) #View mammalsleep
dim(ms) #Columns and rows
## [1] 42 10
str(ms) #Structure of mammalsleep
## 'data.frame': 42 obs. of 10 variables:
## $ body : num 1 2547 10.55 0.023 160 ...
## $ brain : num 6.6 4603 179.5 0.3 169 ...
## $ nondream : num 6.3 2.1 9.1 15.8 5.2 10.9 8.3 11 3.2 6.3 ...
## $ dream : num 2 1.8 0.7 3.9 1 3.6 1.4 1.5 0.7 2.1 ...
## $ sleep : num 8.3 3.9 9.8 19.7 6.2 14.5 9.7 12.5 3.9 8.4 ...
## $ lifespan : num 4.5 69 27 19 30.4 28 50 7 30 3.5 ...
## $ gestation: num 42 624 180 35 392 63 230 112 281 42 ...
## $ predation: int 3 3 4 1 4 1 1 5 5 1 ...
## $ exposure : int 1 5 4 1 5 2 1 4 5 1 ...
## $ danger : int 3 4 4 1 4 1 1 4 5 1 ...
## - attr(*, "na.action")= 'omit' Named int [1:20] 1 3 4 13 14 19 20 21 24 26 ...
## ..- attr(*, "names")= chr [1:20] "African.elephant" "Arctic.Fox" "Arctic.ground.squirrel" "Desert.hedgehog" ...
summary(ms) #Summary of mammalsleep
## body brain nondream dream
## Min. : 0.0050 Min. : 0.14 Min. : 2.100 Min. :0.000
## 1st Qu.: 0.3162 1st Qu.: 3.60 1st Qu.: 6.150 1st Qu.:0.900
## Median : 2.2500 Median : 12.20 Median : 8.500 Median :1.650
## Mean : 100.8139 Mean : 218.68 Mean : 8.743 Mean :1.900
## 3rd Qu.: 10.4125 3rd Qu.: 155.50 3rd Qu.:11.000 3rd Qu.:2.375
## Max. :2547.0000 Max. :4603.00 Max. :17.900 Max. :6.600
## sleep lifespan gestation predation
## Min. : 2.90 Min. : 2.00 Min. : 12.0 Min. :1.000
## 1st Qu.: 8.05 1st Qu.: 5.25 1st Qu.: 32.0 1st Qu.:2.000
## Median : 9.80 Median : 11.20 Median : 90.0 Median :3.000
## Mean :10.64 Mean : 19.37 Mean :129.9 Mean :2.952
## 3rd Qu.:13.60 3rd Qu.: 27.00 3rd Qu.:195.0 3rd Qu.:4.000
## Max. :19.90 Max. :100.00 Max. :624.0 Max. :5.000
## exposure danger
## Min. :1.000 Min. :1.00
## 1st Qu.:1.000 1st Qu.:1.25
## Median :2.000 Median :2.50
## Mean :2.357 Mean :2.69
## 3rd Qu.:3.750 3rd Qu.:4.00
## Max. :5.000 Max. :5.00
Description: The mammalsleep data frame has 62 rows and 10 columns. Sleep in Mammals: Ecological and Constitutional Correlates
Format: body:body weight in kg
brain:brain weight in g
nondream:slow wave (“nondreaming”) sleep (hrs/day)
dream:paradoxical (“dreaming”) sleep (hrs/day)
sleep:total sleep (hrs/day) (sum of slow wave and paradoxical sleep)
lifespan:maximum life span (years)
gestation:gestation time (days)
predation:predation index (1-5) 1 = minimum (least likely to be preyed upon) to 5 = maximum (most likely to be preyed upon)
exposure:sleep exposure index (1-5) 1 = least exposed (e.g. animal sleeps in a well-protected den) 5 = most exposed
danger:overall danger index (1-5) (based on the above two indices and other information) 1 = least danger (from other animals) 5 = most danger (from other animals)
When we omit NA values, it left 42 rows and 10 columns. And I changed the data name to ms. I use str() and summary() function to see the structure and summary of mammalsleep.
plot(ms) #To see the plot of 2-2 variables
par(mfrow=c(3,3))
hist(ms$body);hist(ms$brain);hist(ms$nondream); hist(ms$dream);hist(ms$sleep);hist(ms$lifespan);hist(ms$gestation) #hist
plot(density(ms$nondream));plot(density(ms$sleep)) #some density plot
histogram(~ nondream, data = ms, xlab = 'nondream', ylab='prob',type = "density")
ms_c <- ms[, c('body', 'brain', 'nondream', 'dream', 'sleep','lifespan','gestation')]
pairs(ms_c, pch = '.', upper.panel = panel.smooth, lower.panel = NULL, col = 'brown')
round(cor(ms$nondream,ms$sleep),3) #to see if nondream and sleep cor
## [1] 0.968
round(cor(ms$lifespan,ms$gestation),3)#to see if lifespan and gestation cor
## [1] 0.646
round(cor(ms_c), 3) #to see all cors in ms_c
## body brain nondream dream sleep lifespan gestation
## body 1.000 0.956 -0.394 -0.075 -0.343 0.470 0.714
## brain 0.956 1.000 -0.387 -0.074 -0.337 0.629 0.734
## nondream -0.394 -0.387 1.000 0.518 0.968 -0.372 -0.606
## dream -0.075 -0.074 0.518 1.000 0.717 -0.268 -0.409
## sleep -0.343 -0.337 0.968 0.717 1.000 -0.382 -0.614
## lifespan 0.470 0.629 -0.372 -0.268 -0.382 1.000 0.646
## gestation 0.714 0.734 -0.606 -0.409 -0.614 0.646 1.000
cor.test( ~ lifespan + gestation, data =ms_c) #to test if lifespan and gestation has correlation
##
## Pearson's product-moment correlation
##
## data: lifespan and gestation
## t = 5.3579, df = 40, p-value = 3.762e-06
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.4261846 0.7942779
## sample estimates:
## cor
## 0.6463887
rcorr(as.matrix(ms_c), type="pearson")
## body brain nondream dream sleep lifespan gestation
## body 1.00 0.96 -0.39 -0.07 -0.34 0.47 0.71
## brain 0.96 1.00 -0.39 -0.07 -0.34 0.63 0.73
## nondream -0.39 -0.39 1.00 0.52 0.97 -0.37 -0.61
## dream -0.07 -0.07 0.52 1.00 0.72 -0.27 -0.41
## sleep -0.34 -0.34 0.97 0.72 1.00 -0.38 -0.61
## lifespan 0.47 0.63 -0.37 -0.27 -0.38 1.00 0.65
## gestation 0.71 0.73 -0.61 -0.41 -0.61 0.65 1.00
##
## n= 42
##
##
## P
## body brain nondream dream sleep lifespan gestation
## body 0.0000 0.0099 0.6374 0.0262 0.0017 0.0000
## brain 0.0000 0.0114 0.6401 0.0290 0.0000 0.0000
## nondream 0.0099 0.0114 0.0004 0.0000 0.0152 0.0000
## dream 0.6374 0.6401 0.0004 0.0000 0.0858 0.0072
## sleep 0.0262 0.0290 0.0000 0.0000 0.0124 0.0000
## lifespan 0.0017 0.0000 0.0152 0.0858 0.0124 0.0000
## gestation 0.0000 0.0000 0.0000 0.0072 0.0000 0.0000
In the cor test of lifespan and gestation, I can find out that p-value is 3.762e-06. And the interval doesn’t contain 0. So it means that I need to reject H0. The true correlation is not equal to 0.
Among the result, something is interesting. We can find out that mammals brain weight and body weight/ nondream and sleep have strong correlation. The gestation and brain/body also have a great correlation. It’s cool!!