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)
# 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")
population <- population [! is.na(population$age), ]
sample <- population [1:2000, ]
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
boxplot(population$age)
ggplot(data = population, aes(x = 0, y = age)) + geom_boxplot()
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")
#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")
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
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
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")
pnorm(q = 28,
mean = mean(population$name.length),
sd = sd(population$name.length))
## [1] 0.8967415