Question 1

Calculate descriptive statistics for \([1,8,5,4,7,5,6,3,6,4,7,5,7,5]\).

# install.packages("summarytools")
library(summarytools)
nums <- c(1,8,5,4,7,5,6,3,6,4,7,5,7,5)
descr(nums)
## Descriptive Statistics  
## nums  
## N: 14  
## 
##                       nums
## ----------------- --------
##              Mean     5.21
##           Std.Dev     1.85
##               Min     1.00
##                Q1     4.00
##            Median     5.00
##                Q3     7.00
##               Max     8.00
##               MAD     1.48
##               IQR     2.50
##                CV     0.35
##          Skewness    -0.56
##       SE.Skewness     0.60
##          Kurtosis    -0.33
##           N.Valid    14.00
##                 N    14.00
##         Pct.Valid   100.00

Question 2

Find the derivative of the function \(h(x) = \frac{\cos(x)}{\sin^2 (x)}\).

# install.packages("Deriv")
library(Deriv)
h <- function(x) {
  cos(x) / ((sin(x))^2)
}
h_prime <- Deriv(h)
h_prime
## function (x) 
## {
##     .e1 <- sin(x)
##     -((1 + 2 * (cos(x)^2/.e1^2))/.e1)
## }

Question 3

Evaluate the following limit.

\[\lim_{x \to \infty} \frac{1}{x - 1}\]

# install.packages("Ryacas")
library(Ryacas)
## Warning: package 'Ryacas' was built under R version 4.5.2
## 
## Attaching package: 'Ryacas'
## The following object is masked from 'package:stats':
## 
##     integrate
## The following objects are masked from 'package:base':
## 
##     %*%, det, diag, diag<-, lower.tri, upper.tri
x <- ysym("x")
f <- 1 / (x - 1)
result <- lim(f,x,Inf)
result
## y: 0

Question 4

A special die is made in the shape of an octahedron. The die has 8 (equilateral) triangular faces marked with the numbers 1 to 8. The die is thrown 30 times. Run a Monte Carlo simulation to determine the probability that the number is prime.

# install.packages("pracma")
library(pracma) # for the isprime() function
die <- 1:8 # 1 to 8 inclusive - possible rolls
counter <- 0 # number of prime numbers
N <- 30 # 30 rolls
for (i in 1:N) {
  roll <- sample(x = die,size = 1,replace = T)
  if (isprime(roll) == TRUE) {
    counter <- counter + 1
  }
}
probability <- counter / N
cat("The probability of rolling a prime number is:",probability,"\n")
## The probability of rolling a prime number is: 0.4666667

Question 5

The data frame below shows the number of days of sunshine at a popular resort for each month of last year. Construct a bar graph for the data and determine how many months had less than 15 days of sunshine.

  1. Define the data frame.
q5_data <- data.frame(Month = month.name, # month.name: built-in R constant that has the months of the year as strings
                      Days = c(20,11,12,15,21,23,29,22,19,10,12,14))
q5_data
##        Month Days
## 1    January   20
## 2   February   11
## 3      March   12
## 4      April   15
## 5        May   21
## 6       June   23
## 7       July   29
## 8     August   22
## 9  September   19
## 10   October   10
## 11  November   12
## 12  December   14
  1. Construct a bar graph.
# install.packages("tidyverse")
library(tidyverse)
## Warning: package 'lubridate' was built under R version 4.5.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.1     ✔ stringr   1.5.2
## ✔ ggplot2   4.0.0     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ purrr::cross()    masks pracma::cross()
## ✖ dplyr::filter()   masks stats::filter()
## ✖ dplyr::lag()      masks stats::lag()
## ✖ purrr::simplify() masks Ryacas::simplify()
## ✖ tibble::view()    masks summarytools::view()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
ggplot(q5_data,aes(x = factor(Month,levels = month.name),y = Days)) +
  geom_col() +
  labs(title = "Number of Days of Sunshine for the Resort",
       x = "Month",
       y = "Number of Days of Sunshine") +
  theme_gray(base_size = 14) +
  theme(axis.text.x = element_text(hjust = 1,angle = 45))

  1. Determine how many months had less than 15 days of sunshine.
# install.packages("tidyverse")
library(tidyverse)
answer <- q5_data %>%
  filter(Days < 15) %>%
  nrow()
cat("There are",answer,"months with less than 15 days of sunshine.","\n")
## There are 5 months with less than 15 days of sunshine.