Instructions

R markdown is a plain-text file format for integrating text and R code, and creating transparent, reproducible and interactive reports. An R markdown file (.Rmd) contains metadata, markdown and R code “chunks”, and can be “knit” into numerous output types. Answer the test questions by adding R code to the fenced code areas below each item. Once completed, you will “knit” and submit the resulting .html file, as well the .Rmd file. The .html will include your R code and the output.

Before proceeding, look to the top of the .Rmd for the (YAML) metadata block, where the title and output are given. Please change title from ‘Programming with R Assignment #1’ to your name, with the format ‘lastName_firstName.’

If you encounter issues with knitting the .html, please send an email via Canvas to your TA.

Each code chunk is delineated by six (6) backticks; three (3) at the start and three (3) at the end. After the opening ticks, arguments are passed to the code chunk and in curly brackets. Please do not add or remove backticks, or modify the arguments or values inside the curly brackets. An example code chunk is included here:

# Comments are included in each code chunk, simply as prompts

...R code placed here

...R code placed here

You need only enter text inside the code chunks for each test item.

Depending on the problem, grading will be based on: 1) the correct result, 2) coding efficiency and 3) graphical presentation features (labeling, colors, size, legibility, etc). I will be looking for well-rendered displays. In the “knit” document, only those results specified in the problem statements should be displayed. For example, do not output - i.e. send to the Console - the contents of vectors or data frames unless requested by the problem. You should be able to code for each solution in fewer than ten lines; though code for your visualizations may exceed this.

Submit both the .Rmd and .html files for grading


Example Problem with Solution: Use rbinom() to generate two random samples of size 10,000 from the binomial distribution. For the first sample, use p = 0.45 and n = 10. For the second sample, use p = 0.55 and n = 10.

  1. Convert the sample frequencies to sample proportions and compute the mean number of successes for each sample. Present these statistics.
set.seed(123)
sample.one <- table(rbinom(10000, 10, 0.45)) / 10000
sample.two <- table(rbinom(10000, 10, 0.55)) / 10000

successes <- seq(0, 10)

sum(sample.one * successes) # [1] 4.4827
## [1] 4.4827
sum(sample.two * successes) # [1] 5.523
## [1] 5.523
  1. Present the proportions in a vertical, side-by-side barplot color coding the two samples.
counts <- rbind(sample.one, sample.two)

barplot(counts, main = "Comparison of Binomial Sample Proportions", 
  ylab = "Frequency", ylim = c(0,0.3),xlab = "Number of Successes",
  beside = TRUE, col = c("darkblue","red"),legend = rownames(counts),
  names.arg = c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"))

Please delete the Instructions and Examples shown above prior to submitting your .Rmd and .html files.


Test Items starts from here - There are 5 sections - 50 points total

Read each question carefully and address each element. Do not output contents of vectors or data frames unless requested.

Section 1: (8 points) This problem deals with vector manipulations.

(1)(a) Create a vector that contains the following: * The integer sequence 1 to 5, inclusive, * The square root of 2, * The product of 17 and 14, and * Three (3) repetitions of the vector c(2.5, 5, 7.5).

Assign the vector to the name q1_vector and output. You will use q1_vector for the following four (4) questions.

q1_vector <- c(seq(1,5, by=1), sqrt(2), (17 * 14),rep(c(2.5, 5, 7.5), 3) )
print(q1_vector )
##  [1]   1.000000   2.000000   3.000000   4.000000   5.000000   1.414214
##  [7] 238.000000   2.500000   5.000000   7.500000   2.500000   5.000000
## [13]   7.500000   2.500000   5.000000   7.500000

(1)(b) Create a new vector with all the elements of q1_vector less than or equal to 7. Assign it to the name q1_vector_7. What is the length of q1_vector_7?

q1_vector_7 <- q1_vector[q1_vector <= 7]
length(q1_vector_7)
## [1] 12

(1)(c) Sort q1_vector in ascending order and assign the sorted vector to the name q1_vector_sorted. What is the sum of the 5th through 10th elements of q1_vector_sorted, inclusive?

q1_vector_sorted <- sort(q1_vector, decreasing = FALSE)
sum(q1_vector_sorted[5:10])
## [1] 22

(1)(d) Square each element of q1_vector and assign the new, squared value vector to the name q1_vector_sqrd. How many elements of q1_vector_sqrd are greater than 25?

q1_vector_sqrd <- q1_vector^2
length(q1_vector_sqrd[q1_vector_sqrd > 25])
## [1] 4

(1)(e) Remove the first and last elements of q1_vector. Assign the two (2) removed elements to the name q1_vector_short. What is the product of the elements of q1_vector_short?

q1_vector_short <- c(head(q1_vector,1),tail(q1_vector,1))
prod(q1_vector_short)
## [1] 7.5

Section 2: (10 points) The expression y = sin(x/2) - cos(x/2) is a trigonometric function.

(2)(a) Create a user-defined function - via function() - that implements the trigonometric function above, accepts numeric values, “x,” calculates and returns values “y.”

mytrig <- function(x){
  
  if(is.numeric(x) == TRUE){
    y <- sin(x/2) - cos(x/2)
  }
  else{
    y <- 'Please enter numeric value!'  
  }
    
  return(y)
}

(2)(b) Create a vector, x, of 4001 equally-spaced values from -2 to 2, inclusive. Compute values for y using the vector x and your function from (2)(a). Do not output x or y. Find the value in the vector x that corresponds to the minimum value in the vector y. Restrict attention to only the values of x and y you have computed; i.e. do not interpolate. Round to 3 decimal places and output both the minimum y and corresponding x value.

Finding the two desired values can be accomplished in as few as two lines of code. Do not use packages or programs you may find on the internet or elsewhere. Do not output the other elements of the vectors x and y. Relevant coding methods are given in the Quick Start Guide for R.

x <- seq(-2,2,length.out=4001)
y <- mytrig(x)

sprintf("Min value of y is: %.3f, when x = %.3f", round(min(y),3) , round(x[which.min(y)],3))
## [1] "Min value of y is: -1.414, when x = -1.571"

(2)(c) Plot y versus x in color, with x on the horizontal axis. Show the location of the maximum value of y determined in 2(b). Show the values of x and y corresponding to the maximum value of y in the display. Add a title and other features such as text annotations. Text annotations may be added via text() for base R plots and geom_text() or geom_label() for ggplots.

y_coor <-round(max(y),3)
x_coor <-round(x[which.max(y)],3)

plot(x,y, type="l", col="blue")
title(main = 'Y = sin(x/2) - cos(x/2)')
text(x_coor, y_coor, '(2.0, 0.301)', pos =2)
points(x_coor, y_coor, col="red", pch =19)


Section 3: (8 points) This problem requires finding the point of intersection of two functions. Using the function y = cos(x / 2) * sin(x / 2), find where the curved line y = -(x/2)^3 intersects it within the range of values used in part (2) (i.e. 4001 equally-spaced values from -2 to 2). Plot both functions on the same display, and show the point of intersection. Present the coordinates of this point as text in the display.
my_y <- function(x){
  return(cos(x/2)*sin(x/2))
}
my_y2 <- function(x){
  return(-(x/2)^3)
}
y <- my_y(x)
y2 <- my_y2(x)

#find intersection
intersection_idx <- y == y2
intersection <-c(x[intersection_idx], y[intersection_idx], y2[intersection_idx])

#Plot Area
plot(x,y, type="l", col="blue")
lines(x,y2, col = "red")
points(x[intersection_idx], y[intersection_idx], col="black", pch =19)
text(x[intersection_idx], y[intersection_idx], '(0, 0)', pos =2)
title(main = 'Intersection')
legend(x = -1, y = 0.5,          # Position
       legend = c("cos(x / 2) * sin(x / 2)", "-(x/2)^3 "),  # Legend texts
       lty = c(1, 1),           # Line types
       col = c("blue", "red"),  # Line colors
       lwd = 1)                 # Line width


Section 4: (12 points) Use the “trees” dataset for the following items. This dataset has three variables (Girth, Height, Volume) on 31 felled black cherry trees.

(4)(a) Use data(trees) to load the dataset. Check and output the structure with str(). Use apply() to return the mean values for the three variables. Output these values. Using R and logicals, determine the number of trees with Volume greater than the mean Volume; effectively, how many rows have Volume greater than the mean Volume.

data(trees)
str(trees)
## 'data.frame':    31 obs. of  3 variables:
##  $ Girth : num  8.3 8.6 8.8 10.5 10.7 10.8 11 11 11.1 11.2 ...
##  $ Height: num  70 65 63 72 81 83 66 75 80 75 ...
##  $ Volume: num  10.3 10.3 10.2 16.4 18.8 19.7 15.6 18.2 22.6 19.9 ...
apply(trees,2,mean)
##    Girth   Height   Volume 
## 13.24839 76.00000 30.17097
length(which(trees$Volume > mean(trees$Volume)))
## [1] 12

(4)(b) Girth is defined as the diameter of a tree taken at 4 feet 6 inches from the ground. Convert each diameter to a radius, r. Calculate the cross-sectional area of each tree using pi times the squared radius. What is the interquartile range (IQR) of areas?

radius <- trees$Girth/2
area <- pi * radius^2
IQR(sort(area))
## [1] 87.1949

(4)(c) Create a histogram of the areas calculated in (b). Title and label the axis.

classes = seq(10, 400, by = 35)
colors = c("red", "blue", "steelblue2", "violet", "pink", "purple", "black")
hist(area, breaks = classes, main = "Area Distribution",
     xlab = "Area", col=colors)

(4)(d) Identify the tree with the largest area and output on one line its row number and three measurements.

df.area <- data.frame(radius, area)
trees[df.area$area == max(area), ]
##    Girth Height Volume
## 31  20.6     87     77

Section 5: (12 points) The Student’s t distribution is an example of a symmetric, bell-shaped distribution but with ‘heavier’ tails than a normal distribution. This problem involves comparing the two.

5(a) Use set.seed(9999) and rt() with n = 100, df = 10 to generate a random sample designated as y. Generate a second random sample designated as x with set.seed(123) and rnorm() using n = 100, mean = 0 and sd = 1.25.

Generate a new object using cbind(x, y). Do not output this object; instead, assign it to a new name. Pass this object to apply() and compute the inter-quartile range (IQR) for each column: x and y. Use the function IQR() for this purpose. Round the results to four decimal places and present (this exercise shows the similarity of the IQR values.).

For information about rt(), use help(rt) or ?rt(). Do not output x or y.

set.seed(9999)
y <- rt(n = 100, df = 10)

set.seed(123)
x <- rnorm(n=100, mean=0, sd=1.25)

dat <- cbind(x, y)
round(apply(dat, 2, IQR), 4)
##      x      y 
## 1.4821 1.5242

(5)(b) This item will illustrate the difference between a normal and heavy-tailed distribution. For base R plots, use par(mfrow = c(2, 2)) to generate a display with four diagrams; grid.arrange() for ggplots. On the first row, for the normal results, present a histogram and a horizontal boxplot for x in color. For the t results, present a histogram and a horizontal boxplot for y in color.

par(mfrow = c(2, 2))
colors = c("steelblue", "orange", "steelblue1", "violet", "pink", "cyan", "green", "grey", "steelblue2")

#Normal Distribution
hist(x, breaks=7,main = "Histogram of Normal Distribution",col=colors)
boxplot(x, col=3, main='Boxplot of Normal Distribution', xlab = 'x', horizontal = TRUE)


#T-Distribution
hist(y,breaks = 7,main = "Histogram of T Distribution",col=colors)
boxplot(y, col="red", main='Boxplot of of T Distribution', xlab = 'y', horizontal = TRUE)

(5)(c) QQ plots are useful for detecting the presence of heavy-tailed distributions. Present side-by-side QQ plots, one for each sample, using qqnorm() and qqline(). Add color and titles. In base R plots, “cex” can be used to control the size of the plotted data points and text; “size” for ggplot2 figures. Lastly, determine if there are any extreme outliers in either sample.Remember extreme outliers are based on 3 multiplied by the IQR in the box plot. R uses a default value of 1.5 times the IQR to define outliers (not extreme) in both boxplot and boxplot stats.

par(mfrow=c(1,2))

#Normal Distribution
qqnorm(x,col = 2, main = 'QQ Plot: Norm')
qqline(x,col=2)

#T Distribution
qqnorm(y,col=3,main='QQ Plot: Exp')
qqline(y,col=3)

#Find outliners by using 3 * IQR()
boxplot.stats(y, coef = 3.0)
## $stats
## [1] -3.348114108 -0.751765006 -0.005347442  0.806521129  3.295581470
## 
## $n
## [1] 100
## 
## $conf
## [1] -0.2515567  0.2408618
## 
## $out
## numeric(0)
boxplot.stats(x, coef = 3.0)
## $stats
## [1] -2.88646109 -0.62084664  0.07719539  0.86874760  2.73416624
## 
## $n
## [1] 100
## 
## $conf
## [1] -0.1581605  0.3125513
## 
## $out
## numeric(0)