##### loading the data set in R
source("http://www.openintro.org/stat/data/cdc.R")
library(tidyverse) 
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
##### viewing the names of the variables 
names(cdc)
## [1] "genhlth"  "exerany"  "hlthplan" "smoke100" "height"   "weight"   "wtdesire"
## [8] "age"      "gender"
## Introduction to Data

### Exercise 1
#### How many cases are there in this data set? 
#### How many variables? 
#### For each variable, identify its data type (e.g. categorical, numerical).
glimpse(cdc)
## Rows: 20,000
## Columns: 9
## $ genhlth  <fct> good, good, good, good, very good, very good, very good, very…
## $ exerany  <dbl> 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1…
## $ hlthplan <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1…
## $ smoke100 <dbl> 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0…
## $ height   <dbl> 70, 64, 60, 66, 61, 64, 71, 67, 65, 70, 69, 69, 66, 70, 69, 7…
## $ weight   <int> 175, 125, 105, 132, 150, 114, 194, 170, 150, 180, 186, 168, 1…
## $ wtdesire <int> 175, 115, 105, 124, 130, 114, 185, 160, 130, 170, 175, 148, 2…
## $ age      <int> 77, 33, 49, 42, 55, 55, 31, 45, 27, 44, 46, 62, 21, 69, 23, 7…
## $ gender   <fct> m, f, f, f, f, f, m, m, f, m, m, m, m, m, m, m, m, m, m, f, f…
#### Cases: 20,000
#### Variables: 9 
#### Categorical Variables: genhlth, exerany, hlthplan, smoke100, 
#### numerical variables: weight, height, wtdesire, age

### Exercise 2 

#### Create a numerical summary for height and age, and compute the interquartile 
#### range for each. Compute the relative frequency distribution for gender and 
#### exerany. How many males are in the sample? What proportion of the sample 
#### reports being in excellent health?

#### Height 
summary(cdc$height)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   48.00   64.00   67.00   67.18   70.00   93.00
IQR(cdc$height)
## [1] 6
#### Age 
summary(cdc$age)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   18.00   31.00   43.00   45.07   57.00   99.00
IQR(cdc$age)
## [1] 26
#### Gender
table(cdc$gender)
## 
##     m     f 
##  9569 10431
table(cdc$gender)/20000
## 
##       m       f 
## 0.47845 0.52155
#### Exerany 
table(cdc$exerany)/20000
## 
##      0      1 
## 0.2543 0.7457
#### genhlth 
table(cdc$genhlth)/20000
## 
## excellent very good      good      fair      poor 
##   0.23285   0.34860   0.28375   0.10095   0.03385
#### from the data sample there are 9569 male.

#### from the data sample 23% of the people are reported to be 
#### in excellent health.

## Example 3 
#### What does the mosaic plot reveal about smoking habits and gender? 
mosaicplot(table(cdc$gender, cdc$smoke100))

#### From the mosaic plot we can infer that males have smoked at least 100 
#### cigarettes in their lifetime at a higher proportion than females have. 

## Exercise 4 
#### Create a new object called under23_and_smoke that contains all observations 
#### respondents under the age of 23 that have smoked 100 cigarettes in their 
#### lifetime. Write the command you used to create the new object as the answer 
#### to this exercise. 

under23_and_smoke = subset(cdc, age < 23 & smoke100 == 1)


## Exercise 5 
#### create a side-by-side box plot that displays the relationship between BMI and 
#### gender. What does the figure seem to suggest? 

bmi = (cdc$weight / cdc$height^2) * 703
boxplot(bmi ~ cdc$gender)

## Problem 1

#### Make a scatterplot of weight versus desired weight. 
#### Describe the relationship between these two variables.

plot(x = cdc$wtdesire, y = cdc$weight, pch = 20, col = 'blue', 
     xlab = 'Desired Weight', ylab ='Weight', main ='Desired Weight vs. Weight')
abline(a = 0, b = 1, col = 'red')

#### what we can infer from the scatterplot is that the majority of people's 
#### weight is higher than the desired weight. Also there are two distinct 
#### stragglers who desired to way around 600 and 700 but were roughly around 
#### 300 and 200. 

## Problem 2 
#### Let???s consider a new variable: the difference between desired weight 
#### (wtdesire) and current weight (weight). Create this new variable by 
#### subtracting the two columns in the data frame and assigning them to a new 
#### object called wdiff.

wdiff = cdc$weight - cdc$wtdesire

## Problem 3 
#### wdiff is a numerical variable. If an observation wdiff is 0, this tells us 
#### that this person achieved there desired weight. In other words, their 
#### desired weight and weight match hence why "weight - desired weight" equals 
#### 0. 
#### If wdiff is positive this tells us the person is over their desired weight.
#### But, if the persons wdiff is negative this tells us that the person is 
#### under their desired weight. 


## Problem 4 
#### Describe the distribution of wdiff in terms of its center, shape, and 
#### spread, including any plots you use. What does this tell us about how 
#### people feel about their current weight?

summary(wdiff)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## -500.00    0.00   10.00   14.59   21.00  300.00
#### We can see here that the data leans positively seeing as the mean is 14.59
#### and the median is 10. 

hist(wdiff, breaks = 50)

boxplot(wdiff, horizontal = TRUE)

## Problem 5 
#### Using numerical summaries and a side-by-side box plot, determine if males
#### tend to view their weight differently than females.

boxplot(wdiff ~ cdc$gender, horizontal = TRUE)

#### From the data we can infer that females have generally a bigger diffrence 
#### between their weight and desired weight.


## Problem 6 
#### Now it???s time to get creative. Find the mean and standard deviation of 
#### weight and determine what proportion of the weights are within one 
#### standard deviation of the mean.

sum(cdc$weight > mean(cdc$weight) - sd(cdc$weight) & cdc$weight < 
      mean(cdc$weight) + sd(cdc$weight))/20000
## [1] 0.7076
#### Around 70.8% of people's weight is within one standard deviation of the 
####  mean weight.