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

STAT2011 Probability and Estimation Theory – Semester 1, 2022

Computer Practical Sheet Week 3

If you want the same (pseudo-)random numbers generated each time, place a command like

set.seed(548309576)

in your first chunk so that the (pseudo-)random number generator always starts from the same “point”.

You should get familiar with using R’s built-in help.  For example, typing ?apply at the R console will open the help page for the apply() function. These help pages can be a bit difficult to read but the Examples near the bottom of each page (which can always be copied-and-pasted into the R console) are usually pretty useful.

This week we simulate drawing chips from an urn, both with and without replacement, using the sample() function. Before you commence, finish off any work from last week if necessary.

Computer Problems for Week 3

1. Create a vector urn1=rep(c("white","black"),c(100,100)) representing an urn with

100 white and 100 black chips.

2. Create a 100-by-100 matrix of zeroes called with1:

with1  <- matrix(0,100,100)

3. Execute the following for loop, which draws 100 samples of size 100 with replacement, storing each in a column of with1:

for  (i  in  1:100){

with1[,i]  <-  sample(x=urn1,size=100,replace=TRUE)

}

4. Obtain a vector Xwith1 consisting of the numbers of "white" in each column. There are various ways to do this.

For instance,

sum(with1[,1]=="white")

gives the number of "white" in column 1 of with1.

A slower (but easier) method uses a for loop based on this, putting e.g.  [,i] instead of [,1]. A very quick (but tricky) method uses apply(...) (hint:  (with1=="white") is a matrix!).

5. Repeat questions 2 to 4, but this time sample without replacement.  Use object names without1 and Xwithout1.

6. Repeat questions 1 to 5, but this time use an urn with 20 white and 180 black chips; use object names urn2, with2, without2, etc.

7. Prepare the graph window for a 2-by-2 array using par(mfrow=c(2,2)) (mfrow stands for “multiple figures, filling along rows”). In your text file you’ll need to use this inside a single chunk:

‘‘‘{r  Q7}                par(mfrow=c(2,2))

...

...

...

...

‘‘‘

<--- your  commands  from  Q8 below go here

8. Create 4 histograms all on the same scale using e.g.

hist(Xwith1,breaks=0:100,ylim=c(0,0.25),prob=TRUE)

Arrange them so that Xwith1 is above Xwithout1 and beside Xwith2 (you need to put all 4 commands in the same chunk that begins with par(mfrow=c(2,2)).

9. Compute the means (with mean()) and standard deviations (with sd()) of the four Xwith* vectors.

10. Comment on similarities, differences and any other interesting features of these vectors; in particular consider the questions below.

❼ What do you expect the means to be roughly equal to in each case?

❼ Do you expect some standard deviations to be bigger or smaller than others? Explain

clearly.