Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

STATS 782 Statistical Computing

Assignment 4(2022.2)

1.  [31 marks]        This question uses R S4 OOP.

Define an S4 class "Circle" which represents a circle in a plane.  To parametrise the circle, use x and y (numeric scalars) to define the location of its centre in Cartesian coordinates and r (positive numeric scalar) for its radius.

(a) Define a constructor Circle(x,  y,  r) creating an object of the above class "Circle".

Make sure the returned object is valid and the default should be a unit circle (i.e., centered

at the origin with radius 1).                                                                                   [4 marks]

Test with the following:

>  (c1  <-  Circle())

>  (c2  <-  Circle(2 ,  1))

>  (c3  <-  Circle(2 ,  2 ,  3))

(b) Define two generic R S4 accessor functions centre, radius and corresponding methods

for the class Circle to return the location of the centre (named, numeric vector of length 2 with elements x and y) and the radius respectively.  Test both methods on the above examples.       [4 marks]

(c) Write a show method to display the objects, such as:                                           [3 marks]

(d) Implement the plot method for the "Circle" class. You should allow for a logical argu- ment add which determines whether a new plot will be created (add=FALSE, default) or the circle added to an existing plot (add=TRUE). Graphical parameters should be honoured. Use the following code to reproduce the gure below:                                          [5 marks]

−2

0

2

x

4

6

(e) Implement addition, subtraction and multiplication on the circle class objects as follows:

● Adding two circles means creating a circle with centre (α1 + α2.夕1 + 夕2 ) where (α1.夕1 ) and (α2.夕2 ) are the centres of the arguments. The resulting circle’s radius is the mean of the two circle radii.

● A circle can be multiplied by a scalar numeric value which is equivalent to multipying the radius with that number (no change to the centre).

● Unary minus reflects the circle centre in the origin: (α.夕) → (-α.-夕).

● Binary minus follows the rule α - b = α + (-b).

Any other type combinations are not allowed.  You can test your solution against these examples:      [5 marks]

(f) Define a subclass "Ball" of "Circle" which adds another component "col" (string) with

the default "black" and the corresponding constructor Ball(x,  y,  r,  col).  It should also be possible to create a ball from an existing circle, i.e., Ball(c1,  col="red") should work with c1 being of class "Circle". Then define a show method for "Ball" which also

[4 marks]

>  (b1  <-  Ball(c1,  col="red"))

Ball  at  (0,  0)  with  radius  1  and  colour  red

(g) Do whatever is necessary such that the plot function will draw a ball by lling the circle

with the contained colour and no borders.

Test it with plot(Ball(c1,  col="green")).                                                       [3 marks]

(h) Run the following code:

Depending on your implementation of the + and * operators you should see results that are either of class  "Circle" or  "Ball" - your results  may  be  different from the  above output! Explain the class of each of the operator results for your output. In case they are not both "Ball" write R code which makes sure that both results will be of class "Ball" and explain any decisions you had to make.  [3 marks]

2.  [19 marks]        The le source .R contains R code which plots a simple Parallel Coordinates Plot. It consists of private functions norm1, normN and a public function pcp which draws the plot from a supplied list of variables.

The purpose of this question is to create an R package.  The list of tasks below defines the marks for each subtask and the recommended order, but at the end of it you are expected to upload the full package pcp 1 .0 .tar .gz to Canvas which will be your submission for this question. You are submitting your nal work not individual steps.

(a) Edit source .R and add comments before every complete code line (multi-line function

calls count as one line),  explaining what each line does.   Your task is to explain the

purpose, not to translate the R code to English.                                                   [3 marks]

(b)  Create a valid R package named pcp from the source le.  Make sure all your comments

from the previous question are included!  Update DESCRIPTION to add all important in-

formation, you can use GPL-2 as the license.                                                         [2 marks]

(c) Edit NAMESPACE to make sure only the public function is exported.  Make sure functions from other packages you use are imported and adjust DESCRIPTION accordingly if neces- sary.             [2 marks]

(d)  Create complete documentation for the public function pcp. Describe every argument as well as the return value. (You can remove pcp-package .Rd file if you created the package using a skeleton, because your function has the same name as the package so we don’t need two documentation pages.)       [3 marks]

(e) Add a working example to the pcp documentation which uses the function based on the

iris dataset in R. Colour the groups by the Species variable.                           [3 marks]

(f) Run R  CMD  check on your package and resolve any ERRORs, WARNINGs or NOTEs by ad-

dressing any issues the check has found (with the exception of PDF version of manual”

- if you don’t have TeX available you can ignore that error).                               [2 marks]

(g)  Create a subdirectory tests in the package and a le test .R therein with code that tests

the pcp function. Make sure your test covers 100% of the package, i.e. after you run the code in test .R every line of the functions defined in the package has been executed at least once.                                                                                                               [4 marks] Assuming your package source is in the pcp directory you can check the coverage by installing the "covr" package from CRAN and calling:

You can also see detailed report on which lines are covered and which are not by running