Directions:
Use this code template to help guide you through the Homework 2 Questions on Canvas. You must turn in a filled out copy of the knitted html version of this code in order to receive the full 10/10 for the code portion of the homework.
Read through the questions carefully on Canvas as you go.
You only need to turn this completed code in once per homework cycle.
Save this file as any name you like, as long as it is descriptive, i.e. “HW2_Lastname.Rmd” etc.
In the heading above change “Your Name Here” with your name and keep the quotation marks. If you delete the quotation marks your html file will not render!
The numbering of the questions goes off of the black box number in the Canvas assignment.
This is graded for completeness not accuracy
To Do: Run the entire code block (green play arrow in top right corner), and fill in the code to generate the answer for question 1 and question 2.
rm(list=ls()) # This is to clear your environment
set.seed(100, kind = "Mersenne-Twister", normal.kind = "Inversion") ## this is used to generate reproducible random results across OS systems
fruits <- sample(c("apple", "banana", "cherry"), size=30, replace=TRUE) # Only run ONCE
table(fruits)
## fruits
## apple banana cherry
## 5 12 13
# Question 1
##### Below create code to find out what type of data the vector fruits is #####
class(fruits)
## [1] "character"
# Question 2
##### Below type the answer to how many bananas there are from the table output ##### 12
table("bananas")
##
## bananas
## 1
No code needed for this question. This is a conceptual question you should be able to answer from STAT 201.
To Do: Run the code from the homework and look at the output from the
variable mat (Hint: you will have to do something in order
to see the output), and test out each of the code options from the
homework to see which of them are correct.
mat = matrix((1:12)^2, nrow = 3, ncol = 4, byrow = TRUE)
# Print the variable mat below:
mat
## [,1] [,2] [,3] [,4]
## [1,] 1 4 9 16
## [2,] 25 36 49 64
## [3,] 81 100 121 144
# Test out each of the four options from the homework here:
# option 1:
mat[2][3]
## [1] NA
# option 2:
mat[2]
## [1] 25
# option 3:
mat[2,3]
## [1] 49
# option 4:
mat[3,2]
## [1] 100
To Do: Use the variable mat to answer this question and
test out the answer choices given as options. The variable is already
stored in your environment if you ran the code block above.
# print the variable mat below:
mat
## [,1] [,2] [,3] [,4]
## [1,] 1 4 9 16
## [2,] 25 36 49 64
## [3,] 81 100 121 144
# Test out each of the options and look at the output to see if it answers the question.
# Option 1:
sum(mat[3,])
## [1] 446
# Option 2:
sum(mat[,3])
## [1] 179
# Option 3:
sum(mat[3,1:4])
## [1] 446
# Option 4:
4*mat[3]
## [1] 324
To Do: Run each of the following code chunks. Use these outputs to
help guide you to answering the question. You may also use the help
function or ? on any of the functions to help if you are
stuck on figuring out which are continuous.
runif(n=10, min=0, max=1) # Uniform distribution
## [1] 0.7775844 0.8273034 0.6033244 0.4912318 0.7803585 0.8842270 0.2077139
## [8] 0.3070859 0.3305298 0.1986791
rnorm(n=10, mean=0, sd=1) # Normal distribution
## [1] -0.72022155 0.23094453 -1.15772946 0.24707599 -0.09111356 1.75737562
## [7] -0.13792961 -0.11119350 -0.69001432 -0.22179423
rpois(n=10, lambda=5) # Poisson distribution
## [1] 5 9 6 6 7 7 7 2 5 5
rbinom(n=10, size=10, prob=0.2) # Binomial distribution
## [1] 4 5 0 2 3 1 1 3 4 1
rexp(n=10, rate=1) # Exponential distribution
## [1] 1.1256991 0.9630859 0.0349195 1.3882071 3.6926827 1.2510620 3.0862769
## [8] 1.1387337 0.1419562 0.3697605
rchisq(n=10, df=3) # Chi-squared distribution
## [1] 7.5951194 3.9219719 1.4925032 1.3140093 6.9304765 0.2660878 3.4795068
## [8] 2.4237229 1.8626103 2.7187377
rgeom(n=10, prob=0.4) # Geometric distribution
## [1] 2 1 1 3 0 2 0 1 0 1
To Do: Run each of the code chunks below, Fill in the blanks to help guide you to the correct answers.
set.seed(100)
r1 = rnorm(n=10, mean=0, sd=1) # Normal distr. with mean = 0 and sd = 1
# print r1 here:
r1
## [1] -0.50219235 0.13153117 -0.07891709 0.88678481 0.11697127 0.31863009
## [7] -0.58179068 0.71453271 -0.82525943 -0.35986213
# optional:calculate the mean of r1:
mean(r1)
## [1] -0.01795716
# optional:create a histogram of r1:
hist(r1)
# optional:find the max value of r1 hint: max() function:
max(r1)
## [1] 0.8867848
set.seed(100)
r2 = rnorm(n=10, mean=100, sd=1) # Normal distr. with mean = 100 and sd = 1
# print r2 here:
r2
## [1] 99.49781 100.13153 99.92108 100.88678 100.11697 100.31863 99.41821
## [8] 100.71453 99.17474 99.64014
# optional:calculate the mean of r2:
mean(r2)
## [1] 99.98204
# optional:create a histogram of r2:
hist(r2)
# optional:find the max value of r2 hint: max() function:
max(r2)
## [1] 100.8868
set.seed(100)
r3 = rnorm(n=10, mean=0, sd=100) # Normal distr. with mean = 0 and sd = 100
# print r3 here:
r3
## [1] -50.219235 13.153117 -7.891709 88.678481 11.697127 31.863009
## [7] -58.179068 71.453271 -82.525943 -35.986213
# optional:calculate the mean of r3:
mean(r3)
## [1] -1.795716
# optional:create a histogram of r3:
hist(r3)
# optional:find the max value of r3 hint: max() function:
max(r3)
## [1] 88.67848
To Do: fill in the code with the correct values to answer the question on the homework. The dropdown values on Canvas will give you hints. Your code should be able to run once it is filled in and the line uncommented.
Where there are — it is considered a blank to fill in.
# Uncomment the line below (delete the hashtag) and then you may begin to fill it in
dbinom(x = 2, size = 10, prob = 0.3)
## [1] 0.2334744
To Do: Fill in the blank with the correct value from the drop down answer choices.Your code should be able to run once it is filled in and the line uncommented.
# Uncomment the line below (delete the hashtag) and then you may begin to fill it in.
sum(dbinom(0:7, size = 10, prob = 0.3))
## [1] 0.9984096
To Do: Fill the allocated lines in the code chunk
below with your answers and then run your code chunk once using
the green arrow, and use that output to answer the questions. If you are
unsure about the inputs to rpois run ?rpois in
you console or look at the previous questions from the homework for
hints.
Your answers must be rounded to the nearest hundredth decimal place when entering them into Canvas
rm(list=ls()) # clears your environment
set.seed(100, kind = "Mersenne-Twister", normal.kind = "Inversion") # reproducibility across different OS systems
# Fill in the following prompts below:
# Generate 100 instances of a Poisson(3) random variable and name it r4:
r4 <- rpois(n = 100.00, lambda = 3)
# Compute the sample mean of r4:
mean(r4)
## [1] 3.09
# Compute the sample variance of r4:
var(r4)
## [1] 2.345354
# Compute the standard deviation of r4:
sd(r4)
## [1] 1.531455