Menu

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

10- # Problem 1 11 12 *The function parent.envo returns the parent environment of an input 13 environment, and if env is a45 *The following code will be used to test that fib_gen works as intended 46 47- **{r, error = TRUE} 48 # Test code; DO NO77 since $SAE(theta)$ is not differentiable in $theta$ everywhere. For this 78 reason as well, optimize does not work well116 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

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 77 since $SAE(theta)$ is not differentiable in $theta$ everywhere. For this 78 reason as well, optimize does not work well since the numerical procedure 79 requires the input function being minimized also be differentiable.)* 80 81 – “{r, error = TRUE} 82 # Your code here 83 84 85 – # Problem 4 86 87 *Recall the bootstrapping procedure, where one resamples from the original data 88 set. with replacement, to produce new data sets on which statistics can be 89 computed. Write a function “bootstrap_gen( that returns closures that produce 90 bootstrap replicates of an input data set. Use this function to produce 91 bootstrap confidence intervals for the mean of the following data set ‘dat’.* 92 93 – “*'{r, error = TRUE} 94 dat <- ((1.2, 2.1, 0.3, 8.7, 4.4, 5.4) # DO NOT EDIT 95 – bootstrap_gen <- function() { 96 # Your code here 97 } 98 99 100 – # Problem 5 101 102 *Write a function multiply – that can be used to multiply a selected element 103 in the numeric vector supplied to the function by the amount listed to the right 104 of the assignment operator. * 105 106- ***{r, error = TRUE} 107 multiply<-” <- function(x, pos = 1, value) { 108 # Your code here 109 } 110 111 112 *The following will be used to test that your function works properly: 113 114 – **{r, error = TRUE} 115 # Test code; DO NOT EDIT! 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