Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

The Programming Language used here is R: The instructions are as provided. Please tell the codes used, not only the output, Thanks! Writing a histogram

The Programming Language used here is R: The instructions are as provided. Please tell the codes used, not only the output, Thanks!

image text in transcribed

image text in transcribed

Writing a histogram function We will write a version of the histogram function almost from scratch (some basic plotting functions will be assumed). If you struggle with this ques- tion, please read the O'Reilly text Learning R, or chapter 11 of the course text book Automated Data Collection with R. Question la, (4 points): Cre- ate a function in R named counts. This function should take as parameters a numeric vector I and also a number indicating a number of bins n. The function will consider the range (min(2), max()], and then consider a parti- tion of this interval into n non-overlapping equally sized half open intervals: 1 = (min(), b), 12 = [b1,b-2),..., n = [br-1, max(x)). Note that since the intervals are equally sized, the value of b, is constrained. The function will then return a vector of length n such that the i-th element of this vector is the number of coordinates of that lie in the interval I. Provide the code for this function: It should be of the following form: counts - function(...){ return(...) Question lb, (3 points): Create a function in R called histo. This function should take the same z and n parameters as the count function you wrote in the previous question, and it should plot a histogram of the vector x with n bins. The body of this function may make use of the count function you wrote in the previous question. The only plotting functions you may make use of are the plot function and the lines function (just add an egg). You may not make use of the hist function. Provide your code for this function. Hint: There are several ways to do this, one way would be to create a new and empty plot with: plot(1, type="n", xlab="x", ylab="Counts", xlim=c(...), ylim=c(...)) Then make a for loop through the bins and call lines in the body of the for loop so that three lines (delinating the left, top, and right of the box) are drawn. As before (although this time without a return statement), your code should be in the following form: histo = function(...){ Question 1c, (2 points): We will now test the histo function. Create a vector of length 200 such that the first 100 elements are independent draws from a normal distribution with mean -1 and variance 1 and the second 100 elements are independent draws from a normal distribution with mean 1 and variance 1. Call histo on this vector with 10 bins and provide a graphic including the resulting plot. Question 1d, (2 points): We will now test a corner case of the histo function. Call histo on the vector I=(0,0,0,1,1,2) with 3 bins, and provide a graphic including the resulting plot. Note that this is a corner case because the middle values lie at the boundary between two bins, and so their contribution to the histogram's height requires that the half-open nature of the intervals be respected. Writing a histogram function We will write a version of the histogram function almost from scratch (some basic plotting functions will be assumed). If you struggle with this ques- tion, please read the O'Reilly text Learning R, or chapter 11 of the course text book Automated Data Collection with R. Question la, (4 points): Cre- ate a function in R named counts. This function should take as parameters a numeric vector I and also a number indicating a number of bins n. The function will consider the range (min(2), max()], and then consider a parti- tion of this interval into n non-overlapping equally sized half open intervals: 1 = (min(), b), 12 = [b1,b-2),..., n = [br-1, max(x)). Note that since the intervals are equally sized, the value of b, is constrained. The function will then return a vector of length n such that the i-th element of this vector is the number of coordinates of that lie in the interval I. Provide the code for this function: It should be of the following form: counts - function(...){ return(...) Question lb, (3 points): Create a function in R called histo. This function should take the same z and n parameters as the count function you wrote in the previous question, and it should plot a histogram of the vector x with n bins. The body of this function may make use of the count function you wrote in the previous question. The only plotting functions you may make use of are the plot function and the lines function (just add an egg). You may not make use of the hist function. Provide your code for this function. Hint: There are several ways to do this, one way would be to create a new and empty plot with: plot(1, type="n", xlab="x", ylab="Counts", xlim=c(...), ylim=c(...)) Then make a for loop through the bins and call lines in the body of the for loop so that three lines (delinating the left, top, and right of the box) are drawn. As before (although this time without a return statement), your code should be in the following form: histo = function(...){ Question 1c, (2 points): We will now test the histo function. Create a vector of length 200 such that the first 100 elements are independent draws from a normal distribution with mean -1 and variance 1 and the second 100 elements are independent draws from a normal distribution with mean 1 and variance 1. Call histo on this vector with 10 bins and provide a graphic including the resulting plot. Question 1d, (2 points): We will now test a corner case of the histo function. Call histo on the vector I=(0,0,0,1,1,2) with 3 bins, and provide a graphic including the resulting plot. Note that this is a corner case because the middle values lie at the boundary between two bins, and so their contribution to the histogram's height requires that the half-open nature of the intervals be respected

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_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

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

Get Started

Students explore these related Databases questions