comp03sol

pdf

School

The University of Sydney *

*We aren’t endorsed by this school

Course

2011

Subject

Mathematics

Date

Jan 9, 2024

Type

pdf

Pages

4

Uploaded by JusticeAntelopePerson4988

Report
Prac week 3 - Solutions Dr Clara Grazian 1. Create a vector urn1=rep(c("white","black"),c(100,100)) representing an urn with 100 white and 100 black chips. set.seed( 1234 ) urn1 = rep(c( "white" , "black" ),c( 100 , 100 )) urn1 ## [1] "white" "white" "white" "white" "white" "white" "white" "white" "white" ## [10] "white" "white" "white" "white" "white" "white" "white" "white" "white" ## [19] "white" "white" "white" "white" "white" "white" "white" "white" "white" ## [28] "white" "white" "white" "white" "white" "white" "white" "white" "white" ## [37] "white" "white" "white" "white" "white" "white" "white" "white" "white" ## [46] "white" "white" "white" "white" "white" "white" "white" "white" "white" ## [55] "white" "white" "white" "white" "white" "white" "white" "white" "white" ## [64] "white" "white" "white" "white" "white" "white" "white" "white" "white" ## [73] "white" "white" "white" "white" "white" "white" "white" "white" "white" ## [82] "white" "white" "white" "white" "white" "white" "white" "white" "white" ## [91] "white" "white" "white" "white" "white" "white" "white" "white" "white" ## [100] "white" "black" "black" "black" "black" "black" "black" "black" "black" ## [109] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [118] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [127] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [136] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [145] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [154] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [163] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [172] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [181] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [190] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [199] "black" "black" 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. Xwith1 = apply(with1== "white" , 2 ,sum) Xwith1_b = c() for (i in 1 :ncol(with1)){ 1
Xwith1_b[i] = sum(with1[,i]== "white" ) } Xwith1 ## [1] 47 47 42 49 49 43 53 49 52 46 54 53 48 47 52 48 48 45 47 51 52 54 50 48 57 ## [26] 49 46 49 46 50 50 44 49 57 49 56 52 53 55 55 52 48 46 51 48 46 36 43 52 55 ## [51] 50 54 46 46 49 52 51 46 41 41 45 55 57 56 56 50 54 62 51 39 41 54 47 53 50 ## [76] 51 54 57 50 47 44 48 49 36 55 43 43 59 54 48 50 58 42 48 50 44 48 55 39 48 Xwith1_b ## [1] 47 47 42 49 49 43 53 49 52 46 54 53 48 47 52 48 48 45 47 51 52 54 50 48 57 ## [26] 49 46 49 46 50 50 44 49 57 49 56 52 53 55 55 52 48 46 51 48 46 36 43 52 55 ## [51] 50 54 46 46 49 52 51 46 41 41 45 55 57 56 56 50 54 62 51 39 41 54 47 53 50 ## [76] 51 54 57 50 47 44 48 49 36 55 43 43 59 54 48 50 58 42 48 50 44 48 55 39 48 5. Repeat questions 2 to 4, but this time sample without replacement. Use object names without1 and Xwithout1 . without1 = matrix( 0 , 100 , 100 ) for (i in 1 : 100 ){ without1[,i] <- sample( x= urn1, size= 100 , replace= FALSE) } Xwithout1 = apply(without1== "white" , 2 ,sum) Xwithout1 ## [1] 49 45 42 50 46 52 54 49 49 48 56 48 53 51 52 53 54 54 49 51 50 47 55 49 53 ## [26] 46 49 51 52 52 45 47 51 50 53 48 47 56 54 52 50 55 47 53 49 52 45 48 49 46 ## [51] 53 49 57 50 48 53 50 51 48 52 50 49 43 51 47 49 50 58 50 53 47 51 52 46 44 ## [76] 48 46 54 51 52 50 46 52 46 44 54 48 55 50 50 49 50 48 51 47 42 52 48 52 51 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. urn2 = rep(c( "white" , "black" ),c( 20 , 180 )) urn2 ## [1] "white" "white" "white" "white" "white" "white" "white" "white" "white" ## [10] "white" "white" "white" "white" "white" "white" "white" "white" "white" ## [19] "white" "white" "black" "black" "black" "black" "black" "black" "black" ## [28] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [37] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [46] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [55] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [64] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [73] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [82] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [91] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [100] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [109] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [118] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [127] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [136] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [145] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [154] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [163] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [172] "black" "black" "black" "black" "black" "black" "black" "black" "black" 2
## [181] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [190] "black" "black" "black" "black" "black" "black" "black" "black" "black" ## [199] "black" "black" with2 = matrix( 0 , 100 , 100 ) for (i in 1 : 100 ){ with2[,i] <- sample( x= urn2, size= 100 , replace= TRUE) } Xwith2 = apply(with2== "white" , 2 ,sum) without2 = matrix( 0 , 100 , 100 ) for (i in 1 : 100 ){ without2[,i] <- sample( x= urn2, size= 100 , replace= FALSE) } Xwithout2 = apply(without2== "white" , 2 ,sum) 7.Prepare the graph window for a 2-by-2 array using par(mfrow=c(2,2)) . 8. Create 4 histograms all on the same scale using of the four objects defined before. par( mfrow= c( 2 , 2 )) hist(Xwith1, breaks= 0 : 100 , ylim= c( 0 , 0.25 ), prob= TRUE) hist(Xwith2, breaks= 0 : 100 , ylim= c( 0 , 0.25 ), prob= TRUE) hist(Xwithout1, breaks= 0 : 100 , ylim= c( 0 , 0.25 ), prob= TRUE) hist(Xwithout2, breaks= 0 : 100 , ylim= c( 0 , 0.25 ), prob= TRUE) Histogram of Xwith1 Xwith1 Density 0 20 40 60 80 100 0.00 0.15 Histogram of Xwith2 Xwith2 Density 0 20 40 60 80 100 0.00 0.15 Histogram of Xwithout1 Xwithout1 Density 0 20 40 60 80 100 0.00 0.15 Histogram of Xwithout2 Xwithout2 Density 0 20 40 60 80 100 0.00 0.15 9. Compute the means (with mean() ) and standard deviations (with sd() ) of the four Xwith* vectors. 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
mean(Xwith1) ## [1] 49.34 mean(Xwith2) ## [1] 10.01 mean(Xwithout1) ## [1] 49.93 mean(Xwithout2) ## [1] 9.76 sd(Xwith1) ## [1] 4.999434 sd(Xwith2) ## [1] 3.301347 sd(Xwithout1) ## [1] 3.235707 sd(Xwithout2) ## [1] 2.225291 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. We expect the sds to be smaller for urn 2 in both cases (with and without replacement), since for a true p near 0 or 1 the observed proportion is less variable. Also, we expect without replacement to be less variable than with replacement, since the very smallest and largest conceivable samples in the case with replacement are not possible when sampling without replacement. The observed sd’s agree with these comments. 4