introduction to statistics in R

Course Description

Statistics is the study of how to collect, analyze, and draw conclusions from data. It’s a hugely valuable tool that you can use to bring the future into focus and infer the answer to tons of questions. For example, what is the likelihood of someone purchasing your product, how many calls will your support team receive, and how many jeans sizes should you manufacture to fit 95% of the population? In this course, you’ll use sales data to discover how to answer questions like these as you grow your statistical skills and learn how to calculate averages, use scatter plots to show the relationship between numeric values, and calculate correlation. You’ll also tackle probability, the backbone of statistical reasoning, and learn how to conduct a well-designed study to draw your own conclusions from data.

Summary Statistics

Summary statistics gives you the tools you need to boil down massive datasets to reveal the highlights. In this chapter, you’ll explore summary statistics including mean, median, and standard deviation, and learn how to accurately interpret them. You’ll also develop your critical thinking skills, allowing you to choose the best summary statistics for your data.

Descriptive statistics

There are five different measurement used in descriptive statistics namely;

  1. Measure of Central tendency
  2. Measure of Position
  3. Measure of Dispersion
  4. Measure of Shape
  5. Measure of Frequency

Example 1

We will compute all of the above measurements using different R package as follows:

library(survival)
data("cancer")
# How to know the structure of dataset 
str(lung)
## 'data.frame':    228 obs. of  10 variables:
##  $ inst     : num  3 3 3 5 1 12 7 11 1 7 ...
##  $ time     : num  306 455 1010 210 883 ...
##  $ status   : num  2 2 1 2 2 1 2 2 2 2 ...
##  $ age      : num  74 68 56 57 60 74 68 71 53 61 ...
##  $ sex      : num  1 1 1 1 1 1 2 2 1 1 ...
##  $ ph.ecog  : num  1 0 0 1 0 1 2 2 1 2 ...
##  $ ph.karno : num  90 90 90 90 100 50 70 60 70 70 ...
##  $ pat.karno: num  100 90 90 60 90 80 60 80 80 70 ...
##  $ meal.cal : num  1175 1225 NA 1150 NA ...
##  $ wt.loss  : num  NA 15 15 11 0 0 10 1 16 34 ...
#?lung
# How to see the first six rows of the dataset 
head(lung)
# How to select one variable in tha dataset 
time<-lung$time
# Descriptive statistics 
mean(time)
## [1] 305.2325
median(time)
## [1] 255.5
mode(time)
## [1] "numeric"
var(time)
## [1] 44371.54
max(time)
## [1] 1022
min(time)
## [1] 5
library(psych)
describe(time)
library(EnvStats)
## 
## Attaching package: 'EnvStats'
## The following objects are masked from 'package:stats':
## 
##     predict, predict.lm
summaryFull(time)
##                              time     
## N                             228     
## Mean                          305.2   
## Median                        255.5   
## 10% Trimmed Mean              281.1   
## Geometric Mean                226.5   
## Skew                            1.094 
## Kurtosis                        0.9401
## Min                             5     
## Max                          1022     
## Range                        1017     
## 1st Quartile                  166.8   
## 3rd Quartile                  396.5   
## Standard Deviation            210.6   
## Geometric Standard Deviation    2.477 
## Interquartile Range           229.7   
## Median Absolute Deviation     160.9   
## Coefficient of Variation        0.6901
library(AdequacyModel)
descriptive(time)
## $mean
## [1] 305.2325
## 
## $median
## [1] 255.5
## 
## $mode
## [1] 163  11
## 
## $variance
## [1] 44371.54
## 
## $Skewness
## [1] 1.08679
## 
## $Kurtosis
## [1] 0.89344
## 
## $minimum
## [1] 5
## 
## $maximum
## [1] 1022
## 
## $n
## [1] 228

Data visualization and presentation

To present a numerical data, we will introduce the 8 most common plots and graphs namely;

  • Box plot
  • Histogram
  • Density
  • polygon
  • vioplot
  • Beeswarm
  • Bean plot
  • Scatter plot
boxplot(time)

boxplot(time,col="red",xlab="time", ylab= "number of patient", main= "survival Data for lung cancer patients ")

hist(time)

hist(time,col="green",xlab="time", ylab= "number of patient", main= "survival Data for lung cancer patients ")

plot(density(time))

plot(density(time),col="blue",xlab="time", ylab= "number of patient", main= "survival Data for lung cancer patients ")

plot(density(time))
polygon(density(time), col="cyan",border = "black")