# R Programming/Descriptive Statistics

R Programming
* R Basics
** Introduction
** Sample Session
** Manage your workspace
** Settings and Installation
** Documentation
** Control Structures
** Working with functions
** Debugging
** Using C or Fortran
** Utilities
** Estimation utilities
** Packages
* Data Management
** Data types
** Working with data frames
** Importing and exporting data
** Text Processing
** Times and Dates
* Graphics
** Grammar of graphics
* Publication quality output
* Descriptive Statistics
* Mathematics
** Optimization
** Probability Distributions
** Random Number Generation
* Statistical Core Methods
** Maximum Likelihood
** Method of Moments
** Bayesian Methods
** Bootstrap
** Multiple Imputation
** Nonparametric Methods
* Regression Models
** Linear Models
** Quantile Regression
** Binomial Models
** Multinomial Models
** Tobit And Selection Models
** Count Data Models
** Duration Analysis
* Time Series
* Factor Analysis
* Classification
** Ordination
** Clustering
* Network Analysis
* High Performance Computing
** Profiling R code
** Parallel computing with R
* Appendix
** Sources
** Index
In this section, we present descriptive statistics, ie a set of tools to describe and explore data. This mainly includes univariate and bivariate statistical tools.

## Generic Functions

We introduce some functions to describe a dataset.
* names gives the names of each variable
* str gives the structure of the dataset
* summary gives the mean, median, min, max, 1st and 3rd quartile of each variable in the data.
* describe (Hmisc package) gives more details than summary
* contents (Hmisc package)
* dims in the Zelig package.
* descr in the descr package gives min, max, mean and quartiles for continuous variables, frequency tables for factors and length for character vectors.
* whatis (YaleToolkit) gives a good description of a dataset.
* detail in the SciencesPo package gives a broad range of statistics for continuous variables, frequency tables for factors and length for character vectors.
* describe in the psych package also provides summary statistics:

## Univariate analysis
### Continuous variable
#### Moments

* mean computes the mean
* the variance : var.
* the standard deviation sd.
* the skewness skewness (fUtilities, moment or e1071)
* the kurtosis : kurtosis (fUtilities, moment or e1071)
* all the moments : moment (moment) and all.moments (moment).

#### Order statistics

* the range, the minimum and the maximum : range returns the range of a vector (minimum and maximum of a vector), min the minimum and max the maximum.
* IQR computes the interquartile range. median computes the median and mad the median absolute deviation.
* quantile, hdquantile in the Hmisc package and kuantile in the quantreg packages computes the sample quantiles of a continuous vector. kuantile may be more efficient when the sample size is big.

#### Inequality Index

* The gini coefficient : Gini (ineq) and gini (reldist).
* ineq (ineq) gives all inequalities index.
* Concentration index
* Poverty index

#### Plotting the distribution

We can plot the distribution using a box plot (boxplot), an histogram (hist), a kernel estimator (plot with density) or the empirical cumulative distribution function (plot with ecdf). See the Nonparametric section to learn more about histograms and kernel density estimators. qqnorm produces a normal QQ plot and qqline adds a line to the QQ plot which passes through the first and the third quartile.
* A box-plot is a graphical representation of the minimum, the first quartile, the median, the third quartile and the maximum.
* stripchart and stem are also availables.

#### Goodness of fit tests

Kolmogorov Smirnov Test :
The KS test is one sample goodness of fit test. The test statistic is simply the maximum of the absolute value of the difference between the empirical cumulative distribution function and the theoritical cumulative distribution function. KSd (sfsmisc) gives the critical values for the KS statistic. As an example, we draw a sample from a Beta(2,2) distribution and we test if it fits a Beta(2,2) a Beta(1,1) and a uniform distribution.
Some tests are specific to the normal distribution. The Lillie Test is an extension of the KS test when the parameters are unknown. This is implemented with the lillie.test in the nortest package. shapiro.test implements the Shapiro Wilk Normality Test
* Andersen Darling Test :
See also the package ADGofTest for another version of this test.
* Shapiro-Francia normality test :
* Pearson chi-square normality test :
* Cramer-von Mises normality test
* Jarque-Bera test :

### Discrete variable

We generate a discrete variable using sample and we tabulate it using table. We can plot using a pie chart (pie), a bar chart (barplot or barchart (lattice)) or a dot chart (dotchart or dotplot (lattice)).
* freq (descr) prints the frequency, the percentages and produces a barplot. It supports weights.

## Multivariate analysis
### Continuous variables

* Covariance : cov
* Pearson's linear correlation : cor.
* Pearson's correlation test cor.test performs the test.
* Spearman's rank correlation :
** cor with method = "spearman".
** spearman (Hmisc)
* Spearman's rank correlation test :
** spearman2 (Hmisc)
** spearman.test (Hmisc)
** spearman.test (pspearman package) performs the Spearman’s rank correlation test with precomputed exact null distribution for n <= 22.
* Kendall's correlation : cor with method = "kendall". See also the Kendall package.

### Discrete variables

* table, xtabs and prop.table for contingency tables. ftable (stats package) for a flat (nested) table.
* assocplot and mosaicplot for graphical display of contingency table.
* CrossTable (descr) is similar to SAS Proc Freq. It returns a contingency table with Chi square and Fisher independence tests.
* my.table.NA and my.table.margin (cwhmisc)
* chisq.detail (TeachingDemos)

### Discrete and Continuous variables

* bystats Statistics by Categories in the Hmisc package
* summaryBy (doBy)
* Multiple box plots : plot or boxplot
* Equality of two sample mean t.test and wilcox.test, Equality of variance var.test, equality of two distributions ks.test.

## References

* ↑ Carlos J. Gil Bellosta (2009). ADGofTest: Anderson-Darling GoF test. R package version 0.1. http://CRAN.R-project.org/package=ADGofTest