
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

22S:166 Fall 2008Instructor: Cowles
Homework 4
Due 09/26 in class
Work out the R code for these problems. Then create a
noweb
source
file to create a document that has the R code and your answers tothe questions below. Use Sweave and L
ATEX to produce a PDF file
that shows the code, its output, and your answers.
system.time
function in R to time the performance of the same
task in two different ways:(a) Generate a vector of 500,000 random variates from a Normal(0,1)
density and use the
sum
function to calculate their sum.
(b) Create a variable called
answer
and initialize it to 0.
Then, using
a^
for
loop, do the following steps 500,000 times: generate a single
Normal(0,1) value and add it to the sum contained in
answer
Besides including the R code and output, add a sentence in which youcompare the relevant timings for both methods and state which one ismore efficient.
(a) create a matrix called
with the following entries:
(b) create a vector called
v
with the following entries:
(c) compute and display the product
M v
produced by matrix multipli-
cation (d) compute and display the transpose of
(e) display only those elements of
v
that have values less than 50
rameter 3 and rate parameter 0.5. Produce and display a histrogram ofyour sample.
(a) In R, use the
help.search
function to locate a package that contains
a function to compute the skewness of a vector of numbers. What isthe name of the function, and which package is it in?
(b) Locate an R function that computes the five-number summary of a
vector of numbers.
What is the name of the function, and which
package is it in? (c) Write an R function that does the following:
i. Accepts one argument: a vectorii. Checks whether the vector is numericiii. If not, displays the message “Vector must be numeric” and exitsiv. If yes, computes the skewness of the values (after removing any
missing values)A. if the absolute value of skewness is less than 1, returns a
list containing two objects:
skewness in an object named
“skewness” ; a vector consisting of the mean and standarddeviation in an object named “descstats” B. otherwise, returns a list containing two objects:
skewness
in an object named “skewness” ; a vector consisting of thefive-number summary in an object named “descstats”
(d) Run your function in R three times, using the following vectors as
arguments:
i. c(”Arthur”, ”Mary”, ”Rover”)ii. rnorm( 100 )iii. rexp( 100, 5)
(e) In the document that you submit for homework, include the text of
your function as well as the R code and output for the 3 calls to it.
The output from your own function ought to look something like the follow- ing (with different numeric values): > mydesc( rnorm(100) )$skewness[1] 0.1931722$descstats[1] -0.
mydesc( rexp(100,5) )$skewness[1] 1.462030$descstats[1] 0.000019662 0.057816812 0.105732124 0.265288314 0.
To submit your homework, upload your .Rnw file and the resulting .pdf file into the ICON dropbox.