(Solved) : 10 Problem 1 11 12 Function Parentenvo Returns Parent Environment Input 13 Environment Env Q43984100 . . .


![116 vec <- 1:10 117 multiply(vec) <- 2 118 multiply(vec, 3) <- 10 119 vec # vec[1] should be 2 and vec[3] should be 30 120 12](https://media.cheggcdn.com/media/4c9/4c9c5a9e-1c11-4693-b09e-c2ff06abfd16/phpM1PBfW.png)
The program is r markdown
10- # Problem 1 11 12 *The function ‘parent.envo’ returns the parent environment of an input 13 environment, and if env’ is an environment, then ls(envir = env) returns a 14 character vector listing all objects in the environment. Write a function 15 “Is_ancestors that takes an environment ‘envir’ as an input and returns all 16 object names not just in the input environment but all ancestor environments 17 too. (Hint: your function should use a while loop that terminates when it 18 reaches the ultimate ancestor of all environments: the empty environment, the 19 only environment without an ancestor.) **BEWARE: IF YOUR CODE TAKES LONGER THAN 20 10 SECONDS TO RUN, YOU WILL NOT RECEIVE CREDIT!*** 21 22 – “‘{r, error = TRUE} 23 – ls_ancestors <- function(envir) { 24 # Your code here 25 } 26 27 28 – # Problem 2 29 30 *The Fibonacci sequence is a sequence $f_n$ such that $f_1 – 1$, $f_2 – 1$, and 31 for all $n geq 3$,* 32 33 $$f_n = f_{n – 1} + f_{n – 2}.$$ fre = fn-1 + fn-2. 35 36 37 *Write a function that returns a closure that produces the next element of the Fibonacci sequence every time is called, with the exception of the first two terms. Call the function returning the closure fib_gen .* 38 39 **{r, error = TRUE} 40 – fib_gen <- function(f1 = 1, F2 = 1) { # Could choose different sequence inits 41 # Your code here 42 } 43 45 *The following code will be used to test that ‘fib_gen’ works as intended 46 47- **{r, error = TRUE} 48 # Test code; DO NOT EDIT! 49 f <- fib_gen 50 f # Should be 2 51 f # Should be 3 52 f # Should be 5 53 f0 # Should be 8 54 fO # Should be 13 55 57 – # Problem 3 59 *Consider the problem of plotting and minimizing the sum of square errors 60 presented in the lecture. The **sum of absolute errors (SAE)** can be 61 similarly defined as:* 62 63 $$SAE(theta) = sum_{i=1}^n left|x_i – theta right|.$$ SAE(O) = = -01. 64 65 *Handle SSAECtheta)$ like SSSECtheta)$ was handled in the lecture by writing a function that returns closures representing $SAE(theta)$ for input data sets. 67 Demonstrate your function works by:* 68 69 70 71 72 1. *Generating a random data set using ‘rnorm with a mean of $mu=10$;* 2. *Plotting $SAE(theta)s for the data set in the neighborhood of $theta – 10$; and* 3. *Minimizing $SAE(theta)$ with optimize(*.* 74 75 76 *Compare the results of optimize with the median of your data set. (Theoretically, it can be shown that the $theta$ that minimizes $SAE(theta)$ is the sample median; however, this* cannot *be shown using simple calculus 116 vec <- 1:10 117 multiply(vec) <- 2 118 multiply(vec, 3) <- 10 119 vec # vec[1] should be 2 and vec[3] should be 30 120 121 122 Show transcribed image text 10- # Problem 1 11 12 *The function ‘parent.envo’ returns the parent environment of an input 13 environment, and if env’ is an environment, then ls(envir = env) returns a 14 character vector listing all objects in the environment. Write a function 15 “Is_ancestors that takes an environment ‘envir’ as an input and returns all 16 object names not just in the input environment but all ancestor environments 17 too. (Hint: your function should use a while loop that terminates when it 18 reaches the ultimate ancestor of all environments: the empty environment, the 19 only environment without an ancestor.) **BEWARE: IF YOUR CODE TAKES LONGER THAN 20 10 SECONDS TO RUN, YOU WILL NOT RECEIVE CREDIT!*** 21 22 – “‘{r, error = TRUE} 23 – ls_ancestors
Expert Answer
Answer to 10- # Problem 1 11 12 *The function ‘parent.envo’ returns the parent environment of an input 13 environment, and if env’…
OR