STAT2005 Programming Languages for Statistics Assignment 1 solution

$24.99

Original Work ?

Download Details:

  • Name: Assignment-1-p9qcun.zip
  • Type: zip
  • Size: 168.81 KB

Category: You will Instantly receive a download link upon Payment||Click Original Work Button for Custom work

Description

5/5 - (2 votes)

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 43 2 5432 0 x x x xx     .
(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 0 ൑ 𝜌 ൑ 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.