PART 1 - Data Setup

1) Import the inbuilt mtcars data and view it

2) Display the data structures of all the columns in mtcars using str()

## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

3a) Display the data structure of a specific column (e.g. cyl) using class()

## [1] "numeric"
## [1] "numeric"
## [1] "numeric"
## [1] "numeric"

3c) Review the “levels” of a factor variable, using levels()

## [1] "4" "6" "8"
## [1] "0" "1"
## [1] "0" "1"
## [1] "3" "4" "5"

PART 2 - Data Visualization

Pie Chart using the plotly package

##   Var1 Freq
## 1    0 59.4
## 2    1 40.6

Creating the same Bar Chart using the plotly package

##   Var1  Freq
## 1    4 34.38
## 2    6 21.88
## 3    8 43.75

PART 3 - Statistical Testing

Comparing Proportions using Chi-Square Goodness of Fit

## The following object is masked from package:ggplot2:
## 
##     mpg
## am
##   0   1 Sum 
##  19  13  32
## am
##      0      1 
## 59.375 40.625

CHI-SQUARE TEST FOR GIVEN PROBABILITIES

Null Hypothesis H0a: True proportion of am=1 cars is 60%, am=0 cars is 40%

## 
##  Chi-squared test for given probabilities
## 
## data:  table_am
## X-squared = 0.0052083, df = 1, p-value = 0.9425

Since p value > 0.05 , we fail to reject the Null Hypothesis H0a

Null Hypothesis H0b: True proportion of am=1 cars is 80%, am=0 cars is 20%

## 
##  Chi-squared test for given probabilities
## 
## data:  table_am
## X-squared = 8.5078, df = 1, p-value = 0.003536

Since p value < 0.05 , we reject the Null Hypothesis H0b

Null Hypothesis H0c: True proportion of am=1 cars is 50%, am=0 is 50%

## 
##  Chi-squared test for given probabilities
## 
## data:  table_am
## X-squared = 1.125, df = 1, p-value = 0.2888

Since p value > 0.05 , we fail to reject the Null Hypothesis H0c

Cylinders

## cyl
##   4   6   8 Sum 
##  11   7  14  32
## cyl
##      4      6      8 
## 34.375 21.875 43.750

Null Hypothesis H0d: True proportion of car cylinders = {4,6,8} is {1/3, 1/3, 1/3}

## 
##  Chi-squared test for given probabilities
## 
## data:  tablecyl
## X-squared = 2.3125, df = 2, p-value = 0.3147

Since p value > 0.05, we fail to reject the Null Hypothesis H0d