Homework 3 Stats 20 Lec 1 Winter 2020 General Guidelines Please use R Markdown for your submission. Include the following files: • Your .Rmd file. • The compiled/knitted HTML document. The knitted...

i need help from question 4-9


Homework 3 Stats 20 Lec 1 Winter 2020 General Guidelines Please use R Markdown for your submission. Include the following files: • Your .Rmd file. • The compiled/knitted HTML document. The knitted document should be clear, well-formatted, and contain all relevant R code, output, and explanations. R code style should follow the Tidyverse style guide: https://style.tidyverse.org/. Collaboration must adhere to Level 1 collaboration described in the Stats 20 Collaboration Policy. Note: Questions 1–4 on this homework should be done without loops or other control flow statements. All questions on this homework should be done using only functions or syntax discussed in the lecture notes. No credit will be given for use of outside functions. Question 1 Suppose Andy Dwyer tracks his commute time to his women’s studies class for ten days and records the following times (in minutes): 20 18 20 28 20 19 15 20 12 14 (a) Store Andy’s commute times as a vector in your workspace called commute_times. Use a logical index to determine which days Andy had a commute time that was more than one standard deviation away (longer or shorter) from the average (mean) commute time? What were those commute times? Hint: The abs() and sign() functions compute, respectively, the absolute values and signs of the elements of a numeric vector. (b) Using the same logical index from part (a), determine which days Andy had a commute time that was within one standard deviation (longer or shorter) of the mean commute time? What were those commute times? (c) Using the same logical index from part (a), what proportion of days did Andy have a commute time that was within one standard deviation of the mean commute time? Hint: Can arithmetic operators/functions for numeric vectors work for logical vectors? What do sum() and mean() compute for logical vectors? 1 Question 2 (a) Write a function called is_multiple() that inputs two logical vectors x and y and determines if the corresponding element of x is a multiple of y or vice versa. That is, the ith element of is_multiple(x, y) is TRUE if either x[i] is a multiple of y[i] or y[i] is a multiple of x[i] and FALSE otherwise. (b) Verify that your is_multiple() function works by executing the following command: is_multiple(x = c(1, 6, 8, 12, 18, 0), y = c(3, 5, 16, 8, 2, 0)) Question 3 Logical vectors in R can contain NA in addition to TRUE and FALSE. An NA in a logical context can be interpreted as “unknown.” Consider the following commands: NA & 4 > 3 [1] NA NA & 4 < 3="" [1]="" false="" na="" |="" 4="">< 3="" [1]="" na="" na="" |="" 4=""> 3 [1] TRUE Explain why there is a difference in the output of these four commands. Why are they not all NA? 2 Question 4 Recall the standard PEMDAS order of operations: • Parentheses () • Exponents ˆ • Multiplication and Division *, / • Addition and Subtraction +, - We have now learned several additional operations in R to consider. The combined order of operations is: • Parentheses () • Exponents ˆ • Unary operators -, + (changing the sign of a number, e.g. -1) • Colon operator : (making a regular sequence) • Infix operators of the form %xyz% (e.g., mod %%, integer division %/%, or matrix multiplication %*%) • Multiplication and Division *, / • Addition and subtraction +, - • Relational operators >, >=, <,><=, =="," !="•" logical="" negation="" !="" •="" logical="" and="" &,="" &&="" •="" logical="" or="" |,="" ||="" •="" assignment="" operator=""><- use="" only="" parentheses,="" the="" order="" of="" operations,="" and="" coercion="" rules="" to="" change="" the="" following="" line="" of="" code="" so="" that="" the="" jerry="" object="" will="" contain="" the="" numeric="" vector="" c(2,1),="" and="" the="" statement="" should="" not="" produce="" a="" warning.="" jerry=""><- 2:8="" *="" 5="" %%="" 3^-2:7=""> 2 jerry [1] FALSE FALSE TRUE TRUE TRUE TRUE TRUE Hint: A minimal solution contains no more than three sets of parentheses. Question 5 (a) Write a function called my_min() that computes the minimum of a numeric vector without the min(), max(), sort(), order(), or rank() functions. Include a logical argument called na.rm that specifies whether to remove NAs. The output of my_min(x) and min() should be identical for any numeric vector x. (b) Test your my_min() function from (a) with the following inputs: (i) numeric(0) (ii) 7 (iii) c(4, 1, 0, 2, -3, -5, -4) 3 Question 6 Consider the while() loop below that computes all Fibonacci numbers less than 500. fib1 <- 1="" fib2=""><- 1="" full_fib=""><- c(fib1,="" fib2)="" while="" (fib1="" +="" fib2="">< 500)="" {="" old_fib2=""><- fib2="" fib2=""><- fib1="" +="" fib2="" full_fib=""><- c(full_fib,="" fib2)="" fib1=""><- old_fib2="" }="" full_fib="" [1]="" 1="" 1="" 2="" 3="" 5="" 8="" 13="" 21="" 34="" 55="" 89="" 144="" 233="" 377="" (a)="" the="" variable="" old_fib2="" is="" not="" actually="" necessary.="" rewrite="" the="" while()="" loop="" with="" the="" update="" of="" fib1="" based="" on="" just="" the="" current="" values="" of="" fib1="" and="" fib2.="" (b)="" in="" fact,="" fib1="" and="" fib2="" are="" not="" necessary="" either.="" rewrite="" the="" while()="" loop="" without="" using="" any="" variables="" except="" full_fib.="" (c)="" determine="" the="" number="" of="" fibonacci="" numbers="" less="" than="" 5000000.="" question="" 7="" (a)="" write="" a="" function="" called="" my_ifelse()="" that="" implements="" a="" vector="" form="" of="" the="" if-else="" statement="" without="" the="" ifelse()="" function.="" that="" is,="" the="" ith="" element="" of="" my_ifelse(test,="" yes,="" no)="" will="" be="" yes[i]="" if="" test[i]="" is="" true="" and="" no[i]="" if="" test[i]="" is="" false.="" (b)="" verify="" that="" your="" my_ifelse()="" function="" works="" by="" executing="" the="" following="" commands:="" x=""><- (1:10)="" *="" pi="" my_ifelse(x="" %%="" 1="">= 0.5, x %/% 1 + 1, x %/% 1) (c) Use your my_ifelse() function to write my_abs() and my_sign() functions that, respectively, compute the absolute values and signs of the elements of a numeric vector. The respective outputs of my_abs(x) and my_sign() should be identical to abs(x) and sign(x) for any numeric vector x. Hint: It may be helpful to use my_abs() when writing my_sign(). 4 Question 8 There are many ways to sort a vector. In this problem, you will implement one of the more interesting: the merge sort. Merge sort is an example of a “divide and conquer” algorithm, which means it solves the problem it is working on by breaking it up into smaller and smaller pieces so that by the time the smaller problems are solved, the larger problem will seem to have magically resolved itself. It is also interesting because it is an example of a recursive algorithm, which means it calls itself (generating Fibonacci numbers is another example of a recursive algorithm). One way to think of a merge sort is like this: Suppose you were responsible for sorting a stack of papers. You could just sort them all yourself, but who has time for that? So, you find two friends who owe you a favor and you split the stack of papers in half, giving one half to each of your friends and tell them to sort their stacks. Your plan is to simply “merge” their two sorted stacks together after they give them back to you by repeatedly looking at the first paper on each stack and putting the appropriate one of those two stacks next in a new stack, until only that stack remains. The magic part comes in when each of your friends does the same thing, splitting their stacks in half and each giving those smaller stacks to two other friends, and so on. If anyone gets a stack of length 1 or 0 papers, they simply hand it back up the chain. Here is an example: Level 1: You Dasul, Chengyi, Bianca, Hyunjee, Giorgia, Filippo, Eva, Albert Level 2: You split the vector in two and give it to two others Dasul, Chengyi, Bianca, Hyunjee Giorgia, Filippo, Eva, Albert Level 3: They each split their vectors in two and give them two two others each Dasul, Chengyi Bianca, Hyunjee Giorgia, Filippo Eva, Albert Level 4: Each of them split their vectors again and give them to two others Dasul Chengyi Bianca Hyunjee Giorgia Filippo Eva Albert Everybody at level 4 was given a stack of just one paper, so they have already “sorted” their stack and they have them back up the chain: Level 3: Get back: Dasul & Chengyi Bianca & Hyunjee Giorgia & Filippo Eva & Albert Merge to and pass up: Chenyi, Dasul Bianca, Hyunjee Filippo, Giorgia Albert, Eva Level 2: Get back: Chenyi, Dasul & Bianca, Hyunjee Filippo, Giorgia & Albert, Eva Merge to and pass up: Bianca, Chenyi, Dasul, Hyunjee Albert, Eva, Filippo, Giorgia Level 1: Get back: Bianca, Chenyi, Dasul, Hyunjee & Albert, Eva, Filippo, Giorgia Merge to and pass up: Albert, Bianca, Chenyi, Dasul, Eva, Filippo, Giorgia, Hyunjee All any one person in the system needs to do is: Take the stack they are given. If the stack is length 1 or zero, immediately return it to the person who gave it to them, otherwise split their stack of papers in two and give each half to someone else to sort. When they get back the two sorted halves, merge them into one sorted stack and pass that back. 5 (a) Read through the following pseudocode: FUNCTION: merge() INPUTS: left, right, two sorted numeric vectors OUTPUT: A single combined sorted vector merged <- numeric="" vector="" of="" length="" 0.="" while="" length="" of="" left=""> 0 IF length right > 0 IF the first element of left < the="" first="" element="" of="" right="" merged=""><- combination="" of="" merged="" and="" the="" first="" element="" of="" left="" remove="" the="" first="" element="" of="" left="" else="" merged=""><- combination="" of="" merged="" and="" the="" first="" element="" of="" right="" remove="" the="" first="" element="" of="" right="" else="" merged=""><- combination="" of="" merged="" and="" left="" remove="" all="" the="" elements="" of="" left="" return="" combination="" of="" merged="" and="" right="" function:="" merge_sort()="" inputs:="" x,="" a="" numeric="" vector="" output:="" a="" vector="" containing="" the="" elements="" of="" x,="" sorted="" from="" smallest="" to="" largest.="" if="" length="" x=""> 1 split x roughly into half, as two vectors named left and right sorted_left <- left="" sorted="" by="" merge_sort()="" sorted_right=""><- right="" sorted="" by="" merge_sort()="" x=""><- merge sorted_left and sorted_right with the merge() function return x using the provided pseudocode, write the merge() and merge_sort() functions. (b) test your merge_sort() function on the following vectors: (i) numeric(0) (ii) 7 (iii) 10:1 6 question 9 download the dna.rdata file from ccle and save it to your current working directory. then run the command load("dna.rdata") to load the objects dna1 and dna2 in your workspace. the dna1 and dna2 vectors represent nucleotide sequences of dna (deoxyribonucleic acid). the letters a, c, g, and t respectively represent the four nucleotide bases of a dna strand: adenine, cytosine, guanine, and thymine. (a) write a function called locate_motif() with two character vector arguments strand and motif that returns the index of the start of the motif sequence located merge="" sorted_left="" and="" sorted_right="" with="" the="" merge()="" function="" return="" x="" using="" the="" provided="" pseudocode,="" write="" the="" merge()="" and="" merge_sort()="" functions.="" (b)="" test="" your="" merge_sort()="" function="" on="" the="" following="" vectors:="" (i)="" numeric(0)="" (ii)="" 7="" (iii)="" 10:1="" 6="" question="" 9="" download="" the="" dna.rdata="" file="" from="" ccle="" and="" save="" it="" to="" your="" current="" working="" directory.="" then="" run="" the="" command="" load("dna.rdata")="" to="" load="" the="" objects="" dna1="" and="" dna2="" in="" your="" workspace.="" the="" dna1="" and="" dna2="" vectors="" represent="" nucleotide="" sequences="" of="" dna="" (deoxyribonucleic="" acid).="" the="" letters="" a,="" c,="" g,="" and="" t="" respectively="" represent="" the="" four="" nucleotide="" bases="" of="" a="" dna="" strand:="" adenine,="" cytosine,="" guanine,="" and="" thymine.="" (a)="" write="" a="" function="" called="" locate_motif()="" with="" two="" character="" vector="" arguments="" strand="" and="" motif="" that="" returns="" the="" index="" of="" the="" start="" of="" the="" motif="" sequence="">
Feb 04, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here