代写 R Programming Assignment 2

Programming Assignment 2
Submit Assignment
Due Saturday by 11:59pm Points 100 Submitting a file upload File Types zip
DO NOT DISTRIBUTE THIS ASSIGNMENT OUTSIDE OF CFRM 425, WINTER 2020. ANY VIOLATIONS OF THIS RULE WILL BE TREATED AS AN ACADEMIC MISCONDUCT CASE PER UNIVERSITY OF WASHINGTON POLICY, WHETHER PROVIDED OR RECEIVED.
In this assignment, you will write your code in two files:
Assignment02_Fcns.R: You will write functions only in this file (as directed). You will then need to source this file, and call these functions from your other file,
Assignment02_IF.R: The “IF” refers to “interface”. You will use this file to call functions in the previous file, plus generate additional objects and plots.
You should then compress these files into a zip file of the form Surname_GivenName.zip, and submit this file electronically via Canvas.
One of the goals here is to get into the practice of separating interface from implementation, a best practice in quantitative programming. It also makes it easier to grade (but in practice better for your boss too) 😉
1. (10 points) In your functions file, write a function called ranStud(.), where ran stands for “random”, and “Stud” stands for “Student-t distribution”. It will take in the following arguments:
seed: Seed value for generating a sequence of random variates dfr: Degrees of freedom for a t-distribution
n: Number of random numbers to be generated
This function will return a vector of random variates as specified in its argument. You need not provide actual tests, but you should perform them by calling the function from a separate R script (again, not to turn in). We will have our own test functions that will verify your function that you do provide.
2. (20 points) Again, in your functions file, write a function called tMtx(.), which takes in the following arguments:
initMtx: A 2-row matrix in which the first row represents integer seed values, and the second row will contain integer degrees of freedom parameters for a Student-t distribution.
n: The number of generated random variates desired for each (seed, df) column in the initMtx input argument.
As suggested by the input parameters, your function will generate a matrix of columns of randomly generated t distribution values corresponding to the (seed, df) values in initMtx. To generate these columns, use the ranStud(.) function you wrote in pblm 1.
Before proceeding to the random number generation, however, you need to check the validity of initMtx;

more specifically
a) initMtx is a matrix object
b) All elements of initMtx are integer types
c) The degrees of freedom values are all strictly positive d) initMtx has strictly two rows
If any of these conditions is violated, halt execution of the program and display the message “Error: initMtx must contain integer values, and positive seeds” to the console. You should check these in a single if condition using a concatenated multiple OR statement. If there is no error, then generate the random variates in the else clause.
In a separate R file (again, not to turn in), you should check that violation of each of these conditions will trigger the error message and halt the program. We will have our own tests that we will use to check.
3. (10 points) Now, in your interface file, generate a matrix with four columns of 100 random variates
from a t-distribution, with seeds 100, 200, 300, 400, and degrees of freedom 5, 6, 7, 8 stored in a matrix called mtx2 . Call the tMtx(.) function with this parameter matrix, for 100 random draws each. Store the result in an object called rants. Then, display the 1st and last three rows of the result (put the actual rows in comments inside your file).
4. (10 points) This problem is divided into two parts:
a) (5 points) Write a function in your functions file called posCount(.). It will take in a single vector argument v, and it will return the number of strictly positive elements in a vector. Hint: We covered this briefly in class; the function
which(some condition on v)
will return a vector of the indices of the elements that satisfy the condition. You do not need to verify the input to this function is a vector.
b) (5 points) In your interface file, use the posCount(.) function you wrote in order to return a vector of the number of positive values in each column of your rants matrix. You should call this function using the appropriate vectorized R function for matrix objects (apply(.) or sapply(.)). Show your results in a comment.
5. (20 points) In your interface file, do the following:
a) (5 points) Convert the rants matrix to a dataframe called rants.df. Label the columns “Seed 1” “Seed 2” “Seed 3” “Seed 4”
Generate and show (in comments) the 1st and last three rows of this dataframe.
b) (10 points) Using the appropriate vectorized function (apply(.) or sapply(.)) for columns in a dataframe, compute the vectors containing the skewness (skews), kurtosis (kurts), and first moments (firstMoments) of each column in the dataframe. You need only show your code here; your results will be shown in the next part.
c) (5 points) Combine these columns in a new dataframe called stats, and show your results in a comment. 6. (30 points) In your interface file, do the following:

a) (10 points) Using the rants matrix you generated above, create a new four-column matrix called rantsMod by transforming rants as follows:
exp(col1), -exp(col2), (col3)^2, -(col3)^2
This should be done in a one-line command. Generate and show the first and last three lines of your result (in comments).
b) (20 points) Plot each column of rantsMod in an overlayed line plot, as follows: i) Use the color goldenrod4 for the first column.
ii) Remaining columns will use color indices 2, 3, 4, and plot these using a loop. iii) The line width should be 50% of the default.
iv) Your horizontal axis should be scaled to 0 – 100, and your vertical axis scaled as we discussed in class.
v) The overhead title of your plot should be “Modified Random Scenarios from t-Distributions”, horizontal axis simply labeled “x”, and vertical axis labeled “Mod t Variate”.
[END]
DO NOT DISTRIBUTE THIS ASSIGNMENT OUTSIDE OF CFRM 425, WINTER 2020. ANY VIOLATIONS OF THIS RULE WILL BE TREATED AS AN ACADEMIC MISCONDUCT CASE PER UNIVERSITY OF WASHINGTON POLICY, WHETHER PROVIDED OR RECEIVED.