print("Name: Evan Wilson")
## [1] "Name: Evan Wilson"
#Question 1 [10 points]: Use R to create two vectors r and s with 5 components/elements each and find r*s, r^s, r%%s. Print/show the output.

r <- c(1,2,3.2,4,5)
s <- c(2.6,3,4,5,6)

print(r*s)
## [1]  2.6  6.0 12.8 20.0 30.0
print(r^s)
## [1]     1.0000     8.0000   104.8576  1024.0000 15625.0000
print(r%%s)
## [1] 1.0 2.0 3.2 4.0 5.0
#Question 2 [10 points]: Use R to create variable t with value 1. Then create a while loop that prints variable t, as long as t is less than 12. Print/show the output.

t <- 1

while(t < 12){
  print(t)
  t = t + 1
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
## [1] 11
#Question 3 [10 points]: Use R to create a vector with 10 components/elements (from 1 to 10), then create a for loop that prints the numbers in the vector. Print/show the output.

for(i in 1:10){
  print(i)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
#Question 4 [15 points]: Use R to create a sequence and assign the sequence to a variable and make it a vector (Print the vector/sequence). Then find the maximum and minimum values of the vector. Print/show the output. 

s <- c(4, 2, 7, 4.5, 12, 8)
result.max <- max(s)
result.min <- min(s)

print(s)
## [1]  4.0  2.0  7.0  4.5 12.0  8.0
print(result.max)
## [1] 12
print(result.min)
## [1] 2
#Question 5 [15 points]: Use R to create a vector with some repeated components/elements then find the mean, median, and mode. Print/show the output.

s <- c(2, 3, 4, 2, 6, 6, 1, 2, 10)

result.mean <- mean(s)
result.median <- median(s)

getMode <- function(v) {
  uniqv <- unique(v)
  uniqv[which.max(tabulate(match(v, uniqv)))]
}

result.mode <- getMode(s)

print(result.mean)
## [1] 4
print(result.median)
## [1] 3
print(result.mode)
## [1] 2
#Question 6 [20 points]: Use R and complete the following parts:

#a) Find the regression line for the following data. Print/show the output.
x <- c(15, 17, 18, 20, 21, 24, 26, 29)
y <- c(84.3, 85.2, 84.0, 81.7, 80.9, 77.8, 74.3, 71.1)

relation <- lm(y~x)
print(relation)
## 
## Call:
## lm(formula = y ~ x)
## 
## Coefficients:
## (Intercept)            x  
##     102.085       -1.043
#b) Use the summary () function to get the summary of the relationship, especially the fivenumber summary. Print/show the output. 
print(summary(relation))
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.1339 -0.6737  0.5898  0.7342  0.8530 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 102.08511    1.99251   51.23 3.71e-09 ***
## x            -1.04342    0.09176  -11.37 2.77e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.159 on 6 degrees of freedom
## Multiple R-squared:  0.9557, Adjusted R-squared:  0.9483 
## F-statistic: 129.3 on 1 and 6 DF,  p-value: 2.772e-05
#c) Find the corresponding y value for x = 19. Print/show the output.
a <- data.frame(x = 19)
result <- predict(relation, a)
print(result)
##        1 
## 82.26019
#d) Draw/plot the regression line.
#png(file = "linearregression.png")

plot(y, x, col = "blue",
  main = "Midterm Regression Line",
  abline(lm(x~y)), cex = 1.3, pch = 16)

#Question 7 [10 points]: Use R to create a vector with 6 components/elements and plot the pie chart. Make sure each sector of the pie chart has a name. Print/show the output.

x <- c(21, 62, 10, 53, 84, 42)
labels <- c("London", "New York", "Singapore", "Mumbai", "Taipei", "Indianapolis")

pie(x, labels)

#Question 8 [10 points]: Use one of the small built-in data sets in R and draw the boxplot for two columns/features of the data set you choose. 

input <- mtcars[,c('mpg','cyl')]

boxplot(mpg ~ cyl, data = mtcars, xlab = "Number   of Cylinders", ylab = "Miles Per Gallon", main = "Milage Data")