Assignment 2: Installing/Loading packages and some basic descriptive statistics

Installing/Loading the required packages simultaneously

This will only load the package if it is already installed

# installing/loading the package:
if(!require(moments)) { install.packages("moments"); require("moments")}
#load or install+load 'moments'package

if(!require(modeest)) { install.packages("modeest"); require("modeest")}
#load or install+load 'modeest'package

Basic Descriptive Statistics (Skewness & Kurtosis)

#Importing the dataset from the current directory assuming that the text file has already been saved in the current working directory.
text<-read.table("table6.4.txt",header=TRUE)

#seeing the structure of the data set
str(text)
## 'data.frame':    64 obs. of  4 variables:
##  $ CM  : int  128 204 202 197 96 209 170 240 241 55 ...
##  $ FLR : int  37 22 16 65 76 26 45 29 11 55 ...
##  $ PGNP: int  1870 130 310 570 2050 200 670 300 120 290 ...
##  $ TFR : num  6.66 6.15 7 6.25 3.81 6.44 6.19 5.89 5.89 2.36 ...
#Detailed summary statistics
library(psych)
describe(text)
##      vars  n    mean      sd median trimmed    mad    min      max   range
## CM      1 64  141.50   75.98 138.50  139.00  84.51  12.00   312.00   300.0
## FLR     2 64   51.19   26.01  48.00   51.17  31.88   9.00    95.00    86.0
## PGNP    3 64 1401.25 2725.70 620.00  837.69 578.21 120.00 19830.00 19710.0
## TFR     4 64    5.55    1.51   6.04    5.66   1.45   1.69     8.49     6.8
##       skew kurtosis     se
## CM    0.27    -0.70   9.50
## FLR   0.08    -1.39   3.25
## PGNP  5.16    30.51 340.71
## TFR  -0.61    -0.27   0.19
#aliter way to summarise
library(fBasics)
basicStats(text)
##                      CM         FLR         PGNP        TFR
## nobs          64.000000   64.000000 6.400000e+01  64.000000
## NAs            0.000000    0.000000 0.000000e+00   0.000000
## Minimum       12.000000    9.000000 1.200000e+02   1.690000
## Maximum      312.000000   95.000000 1.983000e+04   8.490000
## 1. Quartile   82.000000   29.000000 3.000000e+02   4.607500
## 3. Quartile  192.500000   77.250000 1.317500e+03   6.615000
## Mean         141.500000   51.187500 1.401250e+03   5.549688
## Median       138.500000   48.000000 6.200000e+02   6.040000
## Sum         9056.000000 3276.000000 8.968000e+04 355.180000
## SE Mean        9.497258    3.250982 3.407120e+02   0.188624
## LCL Mean     122.521244   44.690930 7.203915e+02   5.172752
## UCL Mean     160.478756   57.684070 2.082109e+03   5.926623
## Variance    5772.666667  676.408730 7.429417e+06   2.277060
## Stdev         75.978067   26.007859 2.725696e+03   1.508993
## Skewness       0.272827    0.076110 5.160060e+00  -0.606093
## Kurtosis      -0.695406   -1.393872 3.051475e+01  -0.270432
#lets calculate skewness & kurtosis using 'moments' package

library(moments)
#??moments
#??skewness   #getting help about skewness function
skewness(text$PGNP)
## [1] 5.16006
## attr(,"method")
## [1] "moment"
kurtosis(text$PGNP)
## [1] 30.51475
## attr(,"method")
## [1] "excess"
#??kurtosis   #getting help about kurtosis function

#Aliter way using 'e1071' package

#It takes only vector values
library(e1071)

skewness(text$PGNP, na.rm = FALSE, type =1)
## [1] 5.16006
## attr(,"method")
## [1] "moment"
kurtosis(text$PGNP, na.rm = FALSE, type = 2)  #there are various ways to calculate kurtosis/skewness and hence the answer may be slightly different
## [1] 30.51475
## attr(,"method")
## [1] "excess"
#Lets calculate mode of all the variables using 'modeest' package

library(modeest)
#?modeest
mfv(text$CM)
## [1] 142
mfv(text$FLR)
## [1] 22 31 45 85 88
mfv(text$PGNP)
## [1] 300 420
mfv(text$TFR)
## [1] 6.5
#or better way to get the output

mlv(text$CM,method="mfv")
## Mode (most frequent value): 142 
## Bickel's modal skewness: -0.046875 
## Call: mlv.integer(x = text$CM, method = "mfv")
mlv(text$FLR,method="mfv")
## Mode (most frequent value): 22 31 45 85 88 
## Bickel's modal skewness: -0.171875 
## Call: mlv.integer(x = text$FLR, method = "mfv")
mlv(text$PGNP,method="mfv")
## Mode (most frequent value): 300 420 
## Bickel's modal skewness: 0.390625 
## Call: mlv.integer(x = text$PGNP, method = "mfv")
mlv(text$TFR,method="mfv")
## Mode (most likely value): 6.5 
## Bickel's modal skewness: -0.390625 
## Call: mlv.default(x = text$TFR, method = "mfv")
#some of the variables in the dataset are multimodal