STAT2005 Programming Languages for Statistics Assignment 1
Due: 30 September 2020, 5pm
1. Using rep() and seq() as needed to create the following vectors. (The use of c() function is prohibited in this question.)
(a) 10 12 14 16 18 20 22 24 26 28 30
(b) 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
2. Search for an R function that solves the roots of polynomial equations. (a) Find all roots of the following equation and save it as roots.
5×5 4×4 3×3 2×2 x0. (b) What is the mode of roots?
(c) Use a single line command to sort the values in roots by ascending order of its imaginary part.
(Note: the imaginary part of a complex number a+bi is b, where 𝑖 √1. You can read the help document of the order() function by entering help(order).)
3. A standard deck of playing cards can be created in R as a data frame with the following commands.
suits <- c("D","C","H","S")
# D = ♦ Diamond, C = ♣ Club, H = ♥ Heart, S = ♠ Spade
ranks <- 2:14
# 11 = Jack, 12 = Queen, 13 = King, 14 = Ace
deck <- matrix(, nrow = 52, ncol = 2)
colnames(deck) = c("suit", "rank")
deck <- as.data.frame(deck)
deck$suit <- rep(suits, 13)
deck$rank <- rep(ranks, 4)
(a) Describe the structure of the data frame deck, what are the information contained in its row and column?
(b) A poker hand is a set of five playing cards. Sample a poker hand using the data frame deck and name it as hand.
(c) A flush is a hand that contains five cards all of the same suit. Create a logical value named is.flush which is TURE if and only if hand is a flush.
Hint: You may use hand <- deck[c(17,9,1,49,41),] as a test case. The unique() function would be useful.
(d) A straight is a hand that contains five cards of sequential rank. Note that both A♦ K♣ Q♣ J♦ 10♠ and 5♥ 4♠ 3♥ 2♣ A♦ are considered to be straight, but Q♠ K♠ A♣ 2♥ 3♦ is
not. Create a logical value named is.straight which is TURE if and only if hand is a straight. Use a test case similar to that in (c) to verify your answer.
Hint: The all() function would be useful.
(e) A straight flush is a hand that is both a straight and a flush. Create a logical value named is.straightflush which is TURE if and only if hand is a straight flush. Modify the logical values is.flush and is.straight in (c) and (d) such that they becomes FALSE if hand is a straight flush. Use a test case similar to that in (c) to verify your answer.
4. (a) Consider a two‐dimensional random walk
𝑋 𝑋 𝑍, 𝑋 0, 𝑌 𝑌 𝑊, 𝑌 0,
where, 𝑍 , 𝑊 , 𝑡 1,2,3, ... are independent and identically distributed standard normal random variables. Simulate and plot the sample path of 𝑋 , 𝑌 for 𝑡 0,1, ... ,100. A sample is shown below.
(b) Let 1 𝜌 1, and 𝑈 𝜌𝑍 1 𝜌𝑊, with 𝑍, 𝑊 defined as in (a). Redefine the two‐dimensional random walk as
𝑋 𝑋 𝑍, 𝑋 0, 𝑌 𝑌 𝑈, 𝑌 0.
i. Simulate 100 sample of 𝑍 and 𝑈 with 𝜌 0.5. Check the normality of the sample from 𝑈 with the normal QQ plot. Verify that 𝐸𝑈 0, 𝑉𝑎𝑟𝑈 1, and
Corr𝑍 , 𝑈 0.5 using simulation.
ii. With 𝜌 0.99, simulate and plot the sample path of 𝑋 , 𝑌 for 𝑡 0,1, ... ,100. How is it different from the plot in (a)?
You should submit a file asg1.r via Blackboard, which contains all the R codes you use to finish this assignment. The codes should be commented as clearly as possible. Written work (if any) should also be submitted to the assignment drop‐box.