Descritive Statistics

Data Structures in R

There are five common data structures in R namely:

  1. vector
  2. matrix - collection of vectors
  3. array - collection of matrix
  4. Data frame - collection of arrays
  5. lists - collection of data frames

The vector can be categorized up to six levels namely:

  • logical - Binary
  • Numerical - Count
  • Integer - Count
  • Character - Categorical through transformation
  • Factor - character - no order
  • ordinal - character - order
y1<-c(1.13,1.11,0.629,0.616,0.265,0.320,
      0.849,0.378,0.056,0.044,0.241,0.221,
      0.110,0.190,0.151,0.049)
y2 <- c(T,T,T,F,F,F,T,T,T,F,F,F,T,T,T,F)
y3<- 1:16
y4<- c("abdi","ali","ali","ali","awo","awo","awo","ali",
       "omer","ali","abdi","ali","awo","ayan","ali","omer")


data<- data.frame(y1,y2,y3,y4)
write.csv(data,"~/jibril.csv")

class(y1)
## [1] "numeric"
# Data transformation
y5<- c("n","n","p","p","p","s","s","s","s","s","u","u","u","u","u","u")
y6<-ordered(y5)
y7<-as.factor(y5)
y8 <- as.numeric(y3)

mean(y1)
## [1] 0.3974375
median(y1)
## [1] 0.253
mode(y1)
## [1] "numeric"
sd(y1)
## [1] 0.3631802
var(y1)
## [1] 0.1318999
max(y1)
## [1] 1.13
min(y1)
## [1] 0.044
range(y1)
## [1] 0.044 1.130
# Descriptive Statistics (packages)
# 1. psych
# 2. Envstats (the best one)
# 3. Adequacymodel
# 4. moments

library(AdequacyModel)
descriptive(y1)
## $mean
## [1] 0.39744
## 
## $median
## [1] 0.253
## 
## $mode
## [1] 0.1
## 
## $variance
## [1] 0.1319
## 
## $Skewness
## [1] 0.98292
## 
## $Kurtosis
## [1] -0.34013
## 
## $minimum
## [1] 0.044
## 
## $maximum
## [1] 1.13
## 
## $n
## [1] 16
library(psych)

describe(y1)
##    vars  n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 16  0.4 0.36   0.25    0.37 0.25 0.04 1.13  1.09 0.89    -0.66 0.09
library(EnvStats)
## 
## Attaching package: 'EnvStats'
## The following objects are masked from 'package:stats':
## 
##     predict, predict.lm
summaryFull(y1)
##                              y1      
## N                            16      
## Mean                          0.3974 
## Median                        0.253  
## 10% Trimmed Mean              0.3704 
## Geometric Mean                0.2522 
## Skew                          1.088  
## Kurtosis                      0.01794
## Min                           0.044  
## Max                           1.13   
## Range                         1.086  
## 1st Quartile                  0.1407 
## 3rd Quartile                  0.6192 
## Standard Deviation            0.3632 
## Geometric Standard Deviation  2.884  
## Interquartile Range           0.4785 
## Median Absolute Deviation     0.252  
## Coefficient of Variation      0.9138
# Data Visualization

# Numerical data
## No need for packages
# 1. Histogram
# 2. Density
# 3. Polygon
# 4. box plot
## Need packages
# 5. Vio plot
# 6. Bean plot
# 7. bee swarm plot
# 8. scatter plot
# 9. Heat map


hist(y1)

hist(y1, col = "green", main = "Final Exams Scores",
     xlab = "Mathematics", ylab = "Grades")

plot(density(y1), col = "red", main = "Final Exams Scores",
     xlab = "Mathematics", ylab = "Grades")

plot(density(y1), col = "green", main = "Final Exams Scores",
             xlab = "Mathematics", ylab = "Grades")
polygon(density(y1), col = "green", border = "black", main = "Final Exams Scores",
        xlab = "Mathematics", ylab = "Grades") 

boxplot((y1), col = "red", main = "Final Exams Scores",
        xlab = "Mathematics", ylab = "Grades")

library(vioplot)
## Loading required package: sm
## Package 'sm', version 2.2-5.7: type help(sm) for summary information
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
quartz()
vioplot(y1, col = "green", main = "Final Exams Scores",
        xlab = "Mathematics", ylab = "Grades")

library(beanplot)

quartz()
beanplot(y1, col = "green", main = "Final Exams Scores",
         xlab = "Mathematics", ylab = "Grades")
## log="y" selected
library(beeswarm)

quartz()
beeswarm(y1, col = "red", main = "Final Exams Scores",
         xlab = "Mathematics", ylab = "Grades")