Stats 102A
Stats代写 You will submit a minimum of three files, the core files must conform to the following naming conventions(including capitalization
Homework 5.00000000000000001
Due: July 26, 2019
General Notes Stats代写
- Youwill submit a minimum of three files, the core files must conform to the following naming conventions (including capitalization and underscores). 123456789 is a placeholder,please replace these nine digits with your nine-digit The files you must submit are:
1.123456789_stats102a_hw5.R An R script file containing all of the functions you wrote for the homework.The first line of your .Rmd file, after loading libraries, should be sourcing this script.Stats代写
2.123456789_stats102a_hw5.Rmd Your markdown file which generates the output file of your submission.
3.123456789_stats102a_hw5.html/pdfYour output file, either a PDF or an HTML file depending on the output you choose to generate.
4.included image files included image filesYou may name these what you choose, but you must include all of the image files you generated for your structured flowcharts, otherwise your file will not knit.
If you fail to submit any of the required core files you will receive ZERO points for the assignment.
If you submit any files which do not conform to the specified naming convention, you will receive (at most) half credit for the assignment.
- Your coding should adhere to the tidyverse style guide:https://style.tidyverse.org/
- All flowcharts should be done on separate sheets of paper, but be included, inline as images, in your final markdown
- Anyfunctions you write should be included in a separate functions
- Your .Rmd file must knit. If your .Rmd file does not knit you will receive (at most) half credit for the assignment.Stats代写
The two most common reasons files fail to knit are because of workspace/directory structure issues and because of missing include files. To remedy the first, ensure all of the file paths in your document are relative paths pointing at the current working directory. To remedy the second, simply make sure you upload any and all files you source or include in your .Rmd file.
- EVERYTHING you submit MUST be 100% your, original, work product. Any student suspected of plagiarizing, in whole or in part, any portion of this assignment, will be immediately referred to the Dean of Student’s office without warning, and you will get a zero for all parts of the tainted homework.
- YOU MAY collaborate on this assignment with a maximum of THREE other
- Eachperson must type up and submit their own
- Eachperson MUST include the name and UID of each collaborator at the top of their output fifile.Stats代写
- It is plagiarism if you collaborate with another student and do not disclose it.
Part 1: Dealing with Large Numbers
In order to calculate with large floating point numbers we define objects called (p, q) numbers. In R the (p, q) numbers are lists with four elements. The first element of the list is either the integer +1 or the integer 1. It gives the sign of the number. The second and third element are p and q. And the fourth element is a vector of p + q + 1 integers between zero and nine.
x <- structure(list(sign = 1, p = 3, q = 4, nums = 1:8), class = "pqnumber") str(x)
List of 4
$ sign: num 1 $ p : num 3 $ q : num 4 $ nums: int [1:8] 1 2 3 4 5 6 7 8 - attr(*, "class")= chr "pqnumber"
(a) First task: Write a constructor function, an appropriate predicate function, appropriate coercion functions, and a useful print() method.Stats代写
- Theconstructor takes the four arguments: sign, p, q, and nums. Then check if the arguments satisfy all requirements for a (p, q) If not, stop with an error message. If yes, return the (p, q) number as a list with attribute pqnumber using the structure() function.
- Apredicate is a function that returns a single TRUE or FALSE, like character(), all(), or is.NULL(). Your predicate function should be is_pqnumber() and should behave as expected.
- A coercion function forces an object to belong to a class, such as character() or as_tibble(). Stats代写Youwill create a generic coercion function as_pqnumber() which will accept a numeric or integer argument x and return the appropriate pqnumber. You should also create a generic as_numeric() function and a method to handle a pqnumber.
NOTE: The default as_numeric() can simply be a wrapper function for the base R function
as.numeric().
(b) Second Task: Addition and Subtraction Stats代写
Now suppose we have two positive (p, q) numbers x and y. What is their sum? Clearly its decimal representation is
but this cannot be used directly as the (p, q) number
structure(list(sign = 1, p = p, q = q, nums = x$nums + y$nums), class = “pqnumber”)
because we can have xs + ys > 9.
So we need a carry-over algorithm that moves the extra digits in the appropriate way. Same as one would do when adding two large numbers on paper. Also we need a special provision for overflow, because the sum of two (p, q) numbers can be too big to be a (p, q) number.
A subtraction algorithm should have a carry-over algorithm that borrows 10 in the same way as you would do a subtraction with pencil-and-paper.
Write a function which can add two pqnumber objects. Your function should work for both positive and negative (p, q) numbers. Use your addition function to write a second function which can subtract two pqnumber objects. (Hint: How is subtract related to addition?)
(c) Third Task: Multiplication Stats代写
Write a function which can multiply two pqnumber objects. Think about how you would multiply two large numbers by hand and implement that algorithm in R for two pqnumber objects.
HINTS
You may find having some helper functions useful. For instance, it might prove useful to have a function which provides a shortcut for multiplying a pqnumber by (positive or negative) powers of 10. You may also want functions to trim a pqnumber to its most compact representation or a function to conform two pqnumber objects to the same size representation.
Part 2: Statistical Computing Stats代写
Skewness and Kurtosis:
For a vector of values x = (x1, x2, . . . , xn), the kth sample moment of x around c is defined as
The first (k = 1) sample moment around c = 0 is called the sample mean x¯ = 1 Σn xi.
A sample moment is called central if it is a moment around the mean, i.e., c = x¯. That is, the kth sample central moment of x is
The skewness of x is defined as
and the kurtosis of x is defined as
where µk is the kth central moment of x.
Suppose we have a vector of numbers x.
(a) Write a function to compute the sample moments of x around 0 for k = 1, 2, 3, 4.
(b)Write a function to compute the sample central moments of x for k = 1, 2, 3,4.
(c)Writea function to compute the skewness and kurtosis of x.
For each question, the function should take the vector x as an argument and return a list or vector of results. The functions can call each other if that seems desirable. Try to avoid loops. Do not use built-in function such as mean() or var(), except possibly for checking your results. You may use the sum() function.Stats代写
For each function you need to test that it works by applying it to randomly generated numbers. Specifically, use at least 4 different built-in R functions for generating random numbers (use ?Distributions for a list of possible functions to use) to generate random vectors (of at least length 1,000) to try out your code. Generate a table with skewness and kurtosis results for the four distributions you chose. Do not print a table directly from R; collect the results that are needed and then put them in a Markdown table.
Part 3: Documentation Stats代写
Document each of your functions using the Roxygen2 format.
Say you had the following function add2() which adds two numbers together.
add2 <- function(x, y){ x + y }
We might document it in the following way:
#' Add Two Numbers #' #' Adding two numbers together is such an important task, we wrote a function to do it. #' @param x a number #' @param y a number #' #' @return A number (the sum of x and y). #' @export #' #' @examples #' add2(2, 3) add2 <- function(x, y){ x + y }
All of the documentation for your function goes directly above the function definition, and each line of the documentation starts with #’. Starting at the top:
- Thefirst line of the documentation can be considered the documentation You can optionally use a @title tag to explicitly set the title value.
- Thenext part of the documentation is a long-form description of the You can optionally use the @description tag to explicitly set the description (this is required if your description is more than one paragraph).Stats代写
- Afterthat, we list all of the arguments the function can take and a breif description of them following a
@param tage for each.
- Nextwe document what the return value of the function is with the @return
- Thetag @export is used to denote an externally visible function (one we would expect a user to call), if a function was meant to only ever be called from other functions we wrote, we might exclude this
- Finally,under @examples we might list some simple examples of things our function could For more details see: https://cran.r-project.org/web/packages/roxygen2/vignettes/rd.html
To speed up the documentation process you can type ctrl/cmd + shift + alt + r while your cursor is inside the body of a function you want to document and RStudio will helpfully produce a documentation skeleton for you which looks like this:
#' Title #' #' @param a #' @param b #' #' @return #' @export #' #' @examples my_function <- function(a, b){ (a + b) / b }
更多其他:C++代写 java代写 r代写 代码代写 金融代写 python代写 web代写 考试助攻 计算机代写 C语言代写 经济代写 金融代写 统计代写