Statistics with R
In this post we will discuss about the statistics with R.
Introduction
Statistics is a branch of mathematics working with data collection, organization, analysis, interpretation and presentation.Statistics is very important in Data Analysis ,Data Science and AI.
In this post we will learn about the descriptive statistics with R.
Descriptive Statistics
Descriptive Statistics is used to summarize the data and it focuses on Distribution , the central tendancy and dispersion of the data . In this section we will learn to work on
- Distribution
- Central tendancy
- Dispersion
Measures for central tendency
Central tendency is a measure that best summarizes the data and is a measure that is related to the center of the data set. Mean, median, and mode are the most common measures for central tendency.
We will use mtcars dataset from the datasets package in R.
|
|
|
|
Mean
The mean is the average of the data. It is the sum of all data divided by the number of data points. mean() function gives the mean of the data.
|
|
[1] 20.09062
Median
The median is the Middle or midpoint of the data and is also the 50 percentile of the data. The median is not affected by the outliers and skewness of the data. median() function is used to get Median.
|
|
[1] 6
Mode
Mode is a value in data that that is repeated more often than any other.
|
|
[1] “3”
“3” is the mode of the gear column.
Measures of variability
Measures of variability is the measures of the spread of the data. It can be range ,interquartile range, variance, standard deviation.
Range
Range is the difference between the largest and smallest points in the data. range() function is used to find the range in R.
|
|
[1] 71.1 472.0
Interquartile Range
The interquartile range is the measure of the difference between the 75 percentile or third quartile and the 25 percentile or first quartile. IQR() function is used to get interquartile Range in R.
|
|
[1] 1.02875
quantile() function is used to get quartiles in R.
|
|
|
|
We can get the 25 and 75 percentiles of sugar in data.
|
|
|
|
|
|
|
|
Variance
The variance is the average of squared differences from the mean and it is used to measure the spreadness of the data. var() function is used to find the sample variance in R.
|
|
[1] 36.3241
var() and (N-1)/N is used to find the population variance.
|
|
[1] 35.18897
Standard Deviation
The standard deviation is the square root of a variance and it measures the spread of the data.
sd() is used to find the sample standard deviation of a dataset.
|
|
[1] 6.026948
Normal Distribution
Normal distribution is one of the most important theories because nearly all statistical tests require the data to be distributed normally. We can plot a distribution in R using hist() function.
|
|
qqnorm() and qqline functions are used to find whether data is normally distributed.
|
|
If the points do not deviate away from the line , the data is normally distributed.
Modality
The modality of a distribution is determined by the number of peaks it contains.
|
|
Skewness and Kurtosis
Skewness is a measurement of the symmetry of a distribution and how much the distribution is different from the normal distribution. Negative Skew is alos known as left skewed and positive skew is also known as right skewed. Th histogram from the previous section has a positive skew.
Kurtosis measures whether your dataset is heavy-tailed or light-tailed compared to a normal distribution. High Kurtosis means heavy tailed , so there are more outliers in the data. To find the kurtosis and skewness in R , we need moments package.
|
|
[1] 0.6404399
|
|
[1] 2.799467
summary() and str() function
The summary() and str() function are the fastest ways to get descriptive statistics of the data. We can get the basic descriptive statistics using the summary() function.
|
|
|
|
We can get the structure of the data using the str() function.
|
|
|
|
This all about the basic or descriptive statistics with R.