Docsity

Homework 4 - Computing in Statistics - Fall 2008 | 22S 166, Assignments of Statistics

Toggle sidebar
bg1
22S:166 Fall 2008
Instructor: 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 co de and your answers to
the questions below. Use Sweave and L
A
T
E
X to produce a PDF file
that shows the code, its output, and your answers.
1. Use the system.time function in R to time the pe rformance 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
afor 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 you
compare the relevant timings for both methods and state which one is
more efficient.
2. Use R to do the following:
(a) create a matrix called Mwith the following entries:
1 3 5
2 4 6
3 6 9
(b) create a vector called vwith the following entries:
17 46 181
(c) compute and display the product M v produced by matrix multipli-
cation
(d) compute and display the transpos e of M
(e) display only those elements of vthat have values less than 50
3. Create a vector containing 4 repeats of the sequence (4,3,7).
4. Generate a sample of 500 values from a Gamma density with shape pa-
rameter 3 and rate parameter 0.5. Pro duce and display a histrogram of
your sample.
5. (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 is
the name of the function, and which package is it in?
1
(b) Lo cate 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 vector
ii. Checks whether the vector is numeric
iii. If not, displays the message “Vector must be numeric” and exits
iv. 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 mea n and standard
deviation in an object named “descstats”
B. otherwise, returns a list containing two objects: skewness
in an object named “skewness” ; a vector consisting o f the
five-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.03160733 0.94527957
> mydesc( rexp(100,5) )
$skewness
[1] 1.462030
$descstats
[1] 0.000019662 0.057816812 0.105732124 0.265288314 0.806777159
To submit your homework, upload your .Rnw file and the resulting .pdf file
into the ICON dropbox.
2

Partial preview of the text

Download Homework 4 - Computing in Statistics - Fall 2008 | 22S 166 and more Assignments Statistics in PDF only on Docsity!

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.

  1. Use the

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.

  1. Use R to do the following:

(a) create a matrix called

M

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

M

(e) display only those elements of

v

that have values less than 50

  1. Create a vector containing 4 repeats of the sequence (4,3,7).4. Generate a sample of 500 values from a Gamma density with shape pa-

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.