Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Please write the debug the follwing question in R. Please answer all questions and share outputs and code. Please do not write by hand.

1. Please write the debug the follwing question in R. Please answer all questions and share outputs and code.

Please do not write by hand. Please share code for how you run all test and outputs and how you correct the code and outputs.

Also, please answer all quetions.

Program to Debugg.

Functions in R

Debugging a function

a. In this problem, we will debug a function to compute the mean of each column of a matrix. We'll start by creating a test suite, a set of inputs where we know what the function should return, if it's working correctly.

Use the following code to create matrices to help you test the function.

test1 = matrix(1:9, nr = 3, nc = 3) test2 = matrix( c(3, 6, 1, 2), nr = 4, nc = 1 ) test3 = matrix(c(-1, 0, 1), nr = 1, nc = 3 ) 

View the contents of test1, test2, and test3.

 

Write 3 sentences describing what our function should return when it is applied to each of the matrices in the test suite, if the function is working correctly.

b. Evaluate the following cell to store the function matrixMeans in memory.

matrixMeans <- function(x){ # Computes the mean of each column of a matrix x. total = 0 count = 0 for(ii in 1:ncol(x)){ for(jj in 1:nrow(x)) total = total + x[ii, jj] count = count + 1 } return(total/count) } 

b. Run the function matrixMeans 3 times, with each of test1, test2, and test3 as its argument.

 
 
 

c. The results on the test suite demonstrate that the function matrixMeans is not working correctly. Let's begin by investigating the source of the error you got when analyzing test2 and test3.

c1. Based on what the error message says, do you have any initial thoughts about where the problem is or what sort of problem it is? Write your ideas here.

At this stage of the debugging process, it's fine if your answer is, "I have no clue!" But by reading the text of the error carefully, you can often make an educated guess.

By paying attention to error messages--and what you fixed to make them go away--you will gradually build your problem-solving skills in R and Python, making debugging faster and easier in the future.

c2. Open a web browser to a search engine. Type R and then copy the text of the error message.

Sometimes it's helpful to copy the whole error message; other times it works better to copy just the portion after the :.

Read at least 2 of the webpages that come up in the search results. Paste their URLs here.

Based on what you read, what ideas do you have about what sort of problem this error message represents?

If you have a specific idea about something to try fixing, proceed to part c4.

If you're unsure, do part c3 first.

c3. Based on the lines for(ii in 1:ncol(x)){ for(jj in 1:nrow(x)) what does ii represent?

What does jj represent?

Insert the line print( paste("ii =", ii)) immediately after for(ii in 1:ncol(x)){. Evaluate the code cell containing the function again, and run the function with test3 as input. What value of ii immediately precedes the error message?

Look at the line total = total + x[ii, jj]. How is the variable ii being used? Does this match what ii represents in the for loop?

c4. Change the code of the function to correct the issue that the error message is telling you about. Evaluate the code cell and run the function with test3 as input. When you have successfully eliminated the error message, explain what change you made.

matrixMeans(test3) 

d. Run the updated function on test2.

 

What problem do you see with the output?

Insert print statements into the code to keep track of the values of total and count. Use the results to help you identify how to change the code. Edit the code. Then evaluate the code and run it on test2 to verify that your correction worked.

 

Explain what you changed and why.

e. Run the function on test1 and test3.

 

What type of object does matrixMeans return? A number, a vector, a data frame, something else, ...?

What type of object should matrixMeans return for the inputs test1 and test3?

Modify the code so that matrixMeans returns a vector with the same length as the number of columns of its input.

The code toReturn = numeric( ncol(x) ) creates an empty vector with length equal to the number of columns of x.

Use square bracket notation to set particular elements of toReturn equal to the desired output.

Run the function on test1 and test3 again to verify that the function now returns the a vector of the correct length.

 

f. Which elements of the returned vectors are correct?

Depending on what changes you made in part d, you may need to make additional changes to the function to ensure that all elements of the returned vectors are correct. If so, focus on the results of the print statements that report the values of total and count to help you identify what changes to make. Run the function.

 

When you have verified that the function is working correctly, explain what you changed and why.

Creating your own function

In this problem, you will create your own function to combine values in a vector of numbers so that all entries in a corresponding vector are greater than or equal to a threshold. You will use this function on assignment 8.

a. Start by creating a test vector of numbers.

 

Now, write a function that takes 2 arguments:

a vector of numbers, and

a number n that tells how many elements of the vector to combine.

The function should return a vector where the first element is the sum of the first n elements of the input vector, and the rest of the vector is a copy of the other elements of the input vector. For example, if the input vector is (2, 3, 6, 7, 8) and n = 2, then the output should be the vector (5, 6, 7, 8).

Square bracket notation is likely to be helpful. For example, x[3:5] will return elements 3 through 5 of a vector x.

 

Test your function on the test vector with at least 2 different values of n. If your function does not work correctly, go back and fix it.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions