Instructions

  1. Import data from the “population.csv” file.
  2. Generate a random sample of 2,000 observations from the Population Data.
  3. For the Population and Sample Data, calculate the descriptive statistics seen in class for the age variable. Compare the results.
  4. For the Population, plot a boxplot for the age variable.
  5. For the Population, plot histograms with 10, 50 and 100 bins for the age variable. Compare the results.
  6. Plot a Normal Distribution Probability Function for the age variable. Compare the results with the histogram.
  7. How many standard deviations is your age different from the population mean?
  8. What is the probability that a student is between 22.50 and 24.20 years old?
  9. Do the same analysis from instructions 3 to 7 using the name.length variable.
  10. What is the probability that a student has a name with more than 28 characters?

Install and load packages

if(! "tidyverse" %in% rownames(installed.packages())) install.packages("tidyverse")
if(! "gridExtra" %in% rownames(installed.packages())) install.packages("gridExtra")
if(! "rmarkdown" %in% rownames(installed.packages())) install.packages("rmarkdown")
if(! "knitr"     %in% rownames(installed.packages())) install.packages("knitr")
if(! "shiny"     %in% rownames(installed.packages())) install.packages("shiny")

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.5.0     ✔ 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
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## 
## The following object is masked from 'package:dplyr':
## 
##     combine
library(rmarkdown)
library(knitr)
library(shiny)

1. Import data

# Remove objects from workspace
remove(list = objects())

Import data from the “population.csv” file.

# Import data from cloud
population <- read.csv(file = "https://raw.githubusercontent.com/homerodp91/Raw_data/main/spring2024.csv")

2. Sample data

population <- population [! is.na(population$age), ]

sample <- population [1:2000, ]

3. Descriptive statistics

min(population$age)
## [1] 14.91
max(population$age)
## [1] 58.07
summary(population$age)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   14.91   19.63   20.81   20.96   22.01   58.07
summary(sample$age)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   18.11   20.05   21.66   21.87   23.32   47.30

4. Boxplot

boxplot(population$age)

ggplot(data = population, aes(x = 0, y = age)) + geom_boxplot()

5. Histogram

ggplot(data = population, aes(x = age)) + 
  geom_histogram(bins = 10)

ggplot(data = population, aes(x = age)) + 
  geom_histogram(bins = 50)

ggplot(data = population, aes(x = age)) + 
  geom_histogram(bins = 100, color = "#19BDAB", fill = "white")

6. Normal Distribution Probability Function

#Calculate the probability
population$prob.age <- dnorm(x = population$age,
                             mean = mean(population$age),
                             sd = sd(population$age))
ggplot(data = population, aes(x = age)) +
  geom_line(aes(x = age, y = prob.age)) + 
  geom_density(color = "red")

7. How many standard deviations is your age different from the population mean

my.age <- as.numeric(as.Date("2024-02-16") - as.Date("2003-12-06"))

(my.age - mean(population$age)) / sd(population$age)
## [1] 4070.882

8. Calculate a probability

What is the probability that a student is between 22.50 and 24.20 years old?

pnorm(q = 24.2, 
      mean = mean(population$age),
      sd = sd(population$age)) -

pnorm(q = 22.5,
      mean = mean(population$age),
      sd = sd(population$age))
## [1] 0.1608237

9. Descriptive statistics for name.length variable

summary(population$name.length)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    5.00   20.00   23.00   22.82   26.00   50.00
summary(sample$name.length)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    9.00   20.00   23.00   23.05   26.00   42.00
boxplot(population$name.length)

ggplot(data = population, aes(x = 0, y = name.length)) + geom_boxplot()

ggplot(data = population, aes(x = name.length)) + 
  geom_histogram(bins = 10)

ggplot(data = population, aes(x = name.length)) + 
  geom_histogram(bins = 50)

ggplot(data = population, aes(x = name.length)) + 
  geom_histogram(bins = 100, color = "#19BDAB", fill = "white")

population$prob.name.length <- dnorm(x = population$name.length,
                                     mean = mean(population$name.length),
                                     sd = sd(population$name.length))

ggplot(data = population, aes(x = name.length)) +
  geom_line(aes(x = name.length, y = prob.name.length)) + 
  geom_density(color = "red")

10. Probability that a student has a name with more than 28 characters

pnorm(q = 28, 
          mean = mean(population$name.length),
          sd = sd(population$name.length))
## [1] 0.8967415