3 descriptive statistics with R

17
Dr Nisha Arora Descriptive Statistics with R

Transcript of 3 descriptive statistics with R

Page 1: 3 descriptive statistics with R

Dr Nisha Arora

Descriptive Statistics with R

Page 2: 3 descriptive statistics with R

Contents

2

Tabulation & Cross Tabulation

Built-in Functions for Descriptive Statistics

Built-in Functions for Probability Distribution

References

Page 3: 3 descriptive statistics with R

3

Frequency Tables

To create a one-dimensional frequency table

Load & understand data

data = iris; names(data); help(iris)

Create frequency table for variable ‘Species’ of ‘iris’ data set

table(iris$Species)

Page 4: 3 descriptive statistics with R

4

Cross Tabulation

To create a two-dimensional contingency table

Load & understand data

data = iris; names(data); help(iris)

Create frequency table of sepal length of different species of

‘iris’ data set

table(iris$Sepal.Length, iris$Species)

Example_2:

mtcars; table(mtcars$cyl, mtcars$gear)

Page 5: 3 descriptive statistics with R

5

Cross Tabulation

To create a two-dimensional contingency table

Load & understand data

read.csv("people2.csv"); names(people2)

Create frequency table of eye color of people categorized by

sex

table2 <- table(people2$Eye.Color, people2$Sex)

Page 6: 3 descriptive statistics with R

6

Cross Tabulation

Table of proportions

prop.table(table2)

# table with each cell count expressed as a proportion of the

total count

prop.table(table2, margin=1)

# table with each cell count expressed as a proportion of row

total

prop.table(table2, margin=2) # table with each cell count

expressed as a proportion of column total

Table of percentages

round(prop.table(table2)*100)

Page 7: 3 descriptive statistics with R

7

Cross Tabulation

Table with marginal sums

add.margins(table2)

# table with row and column totals

addmargins(table2, margin=1)

# table with row total

addmargins(table2, margin= 2)

# table with column total

To perform chi-square test of association

summary(table2)

Page 8: 3 descriptive statistics with R

8

Cross Tabulation

To create a three-dimensional contingency table

Load & understand data

people2 <- read.csv("people2.csv"); names(people2)

Create frequency table of eye color of people categorized by

their sex & height

table(people2$Eye.Color, people2$Sex, people2$Height.Cat)

Page 9: 3 descriptive statistics with R

Built-in functions for Descriptive Statistics

9

Statistics Function name/command

Mean mean

Median median

Standard Deviation sd

Median Absolute Deviation mad

Variance var

Maximum Value max

Minimum Value min

Range range

Interquartile range IQR

Page 10: 3 descriptive statistics with R

Built-in functions for Descriptive Statistics

10

Statistics Function name/command

Quantiles quantile

Tukey’s five-number

summary

[min, I, II, III quartile, max

value]

fivenum

Sum sum

Product prod

Number of observations length

Standardize/ Computing

z-score

scale(x, center=TRUE, scale=TRUE)

Mean Centering scale(x, scale=FALSE)

Page 11: 3 descriptive statistics with R

Built-in functions for Probability Distribution

11

Statistics Function name/command

Normal PDF dnorm

Cumulative Normal

Probability pnorm

Normal Quantile qnorm

Random Normal Variable rnorm

Similarly, there are functions for other distributions like rbinom,

rpois, rt, runif, rf etc.

Page 12: 3 descriptive statistics with R

12 Source: Google Images

Page 17: 3 descriptive statistics with R

Thank You