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

STATS 380 Statistical Computing

TERMS TEST

SECOND SEMESTER, 2022

(Time allowed: 60 minutes + 15 extra minutes)

INSTRUCTIONS

* This test is open book, and you are permitted to access your course book and other written materials including online resources.

* Calculators are permitted.

* Ensure your submission is a PDF ile.

* Ensure you successfully submit it on time.

* Attempt all questions.

* There are 50 marks in total for this test.

* unless asked for, avoid using explicit loops or anything equivalent if ever possible.

1.  use  : ’ seq()’ rep()and some other commonly-used operators/functions’ but def- initely not c()’ append()or a loop’ to create the following patterned sequences’ using only a single non-compound R expression.

(a)  0 5  10  15 20 25 30 35  40 45 50 [3 marks]

(b)  1  2  4 8  16 32 64  128 256 512  1024 [3 marks]

(c)  100  101  102  103  104 200 201 202 203 204 300 301 302 303 304 [3 marks]

(d)  4 5  1 2 3 4 5  1 2 3 4 5  1 2 3 4 5  1 2 3 4 [3 marks]

[12 marks]

2. The object auckland is a data frame that comprises two numeric variables:  year (year) and temp (the corresponding annual average temperature in Auckland).  A few of its rows are shown below:

year   temp

##  

2012  15.41

2013  16.06

2014  15.61

2015  15.71

2016  16.32

##  

with this data frame’ write down some R code that inds and returns (only):

(a)  The increase in the mean (average) temperature from the 1970s (1970-1979) to the 2010s (2010-2019). [4 marks]

(b) The ive hottest years’ according to the annual average temperatures. [4 marks]

[8 marks]

3. write an R function that inds and returns, in a 2-column matrix, all of the pairs in a numeric sequence, at which the sequence changes signs from negative to positive (or 0). For example, passing the following sequence

> x =  c(48, -62, -8, 0, 20, 7, 34, 2, -6, -58, 30, -27, -16, 51)

to your function as its argument, it should return

[,1]  [,2]

[1,]     -8       0

[2,]   -58     30

[3,]   -16     51

where each row stores a sign-changing pair as deined above.

[10 marks]

4. Many statistical methods require that data be standardised irst. write an R func- tion that standardises a given numeric matrix, in the sense that the values in each column will have mean 0 and the column vector length 1.  For example, passing the following matrix

>  x  =  cbind(0:2,  c(0,0,10))

to your function as its argument, it should return

[,1]             [,2]

[1,] -0.7071068 -0.4082483

[2,]   0.0000000 -0.4082483

[3,]   0.7071068   0.8164966

Note:  The length of a vector a =  (a1, . . . , an) is given by .  Hence the vector a/ has length 1.

[10 marks]

5. The following graph mimics a face of Rubik,s cube. writesome R code to reproduce the grid and title as similarly as possible and randomly allocate 6 evenly spaced colours from the hcl function to the 9 squares.  Make sure that no square is dis- torted, whenever the graph is produced.  Make use of vectorised computation as much as you can.

A Face of Rubik's Cube


[10 marks]