Question
Create a function in R named counts. This function should take as parameters a numeric vector x and also a number indicating a number of
Create a function in R named counts. This function should take as parameters a numeric vector x and also a number indicating a number of bins n. The function will consider the range [min(x), max(x)], and then consider a parti- tion of this interval into n non-overlapping equally sized half open intervals: I1 = [min(x), b1), I2 = [b1, b 2), . . . , In = [bn 1, max(x)). Note that since the intervals are equally sized, the value of bi 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 x that lie in the interval Ii. Provide the code for this function:
Question 1b, (3 points): Create a function in R called histo. This function should take the same x 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: 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:
Please write the code in Rstudio and the following is the count function
count = function (x,n) {l tmp = cut(x, breaks=seq(min(x), max(x), length.out = n+1), right = FALSE) y = as.vector(table(tmp)) return(y) count = function (x,n) {l tmp = cut(x, breaks=seq(min(x), max(x), length.out = n+1), right = FALSE) y = as.vector(table(tmp)) return(y)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started