X3-无代写
时间:2023-03-19
Lab Week 4
Dr Clara Grazian
1. Define x=1:6. By applying rep() to x, create a vector X3 which simply repeats x 36 times.
set.seed(36784)
x=1:6
X3=rep(x,36)
X3
## [1] 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1
## [38] 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2
## [75] 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3
## [112] 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4
## [149] 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5
## [186] 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
2. Now create another vector X1 of length 216 with 36 1’s, 36 2’s,. . . ,36 6’s (hint: use a command of the
form rep(...,rep(...))).
X1=rep(x,rep(36,6))
X1
## [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
## [38] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3
## [75] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4
## [112] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5
## [149] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6
## [186] 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
3. Finally create a vector X2 of length 216 which takes a vector consisting of 6 1’s, 6 2’s,. . . ,6 6’s and then
repeats it 6 times (hint: rep(rep(...,rep(...)),...)).
X2=rep(rep(x,rep(6,6)) ,6)
X2
## [1] 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 1
## [38] 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 1 1
## [75] 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 1 1 1
## [112] 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 1 1 1 1
## [149] 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 1 1 1 1 1
## [186] 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6
4. The matrix outcomes=cbind(X1,X2,X3) gives a representation of all outcomes in the sample space
when sampling 3 times from S with replacement. Print the first 13 lines of it using outcomes[1:13,].
Obtain a vector sums of row sums of this matrix.
outcomes=cbind(X1,X2,X3)
outcomes[1:13,]
## X1 X2 X3
## [1,] 1 1 1
1
## [2,] 1 1 2
## [3,] 1 1 3
## [4,] 1 1 4
## [5,] 1 1 5
## [6,] 1 1 6
## [7,] 1 2 1
## [8,] 1 2 2
## [9,] 1 2 3
## [10,] 1 2 4
## [11,] 1 2 5
## [12,] 1 2 6
## [13,] 1 3 1
sums=apply(outcomes,1,sum)
sums
## [1] 3 4 5 6 7 8 4 5 6 7 8 9 5 6 7 8 9 10 6 7 8 9 10 11 7
## [26] 8 9 10 11 12 8 9 10 11 12 13 4 5 6 7 8 9 5 6 7 8 9 10 6 7
## [51] 8 9 10 11 7 8 9 10 11 12 8 9 10 11 12 13 9 10 11 12 13 14 5 6 7
## [76] 8 9 10 6 7 8 9 10 11 7 8 9 10 11 12 8 9 10 11 12 13 9 10 11 12
## [101] 13 14 10 11 12 13 14 15 6 7 8 9 10 11 7 8 9 10 11 12 8 9 10 11 12
## [126] 13 9 10 11 12 13 14 10 11 12 13 14 15 11 12 13 14 15 16 7 8 9 10 11 12
## [151] 8 9 10 11 12 13 9 10 11 12 13 14 10 11 12 13 14 15 11 12 13 14 15 16 12
## [176] 13 14 15 16 17 8 9 10 11 12 13 9 10 11 12 13 14 10 11 12 13 14 15 11 12
## [201] 13 14 15 16 12 13 14 15 16 17 13 14 15 16 17 18
5. Using the vector sums, determine (for the case n = 3)
• µY = E(Y ),
• σ2Y = E(Y 2)− [E(Y )]2 and
• P (Y ≥ 15) (note sum(vec>=x) counts the number of elements of the vector vec that are greater than
or equal to x; also mean(vec) is the same as sum(vec)/length(vec)).
EY=mean(sums)
EY
## [1] 10.5
VarY=mean((sums-EY)ˆ2)
VarY
## [1] 8.75
length(sums)
## [1] 216
sum(sums>=15)
## [1] 20
Prob=mean(sums>=15)
Prob
## [1] 0.09259259
library(MASS)
fractions(Prob)
## [1] 5/54
2
So E(Y ) = 10.5, V ar(Y ) = 8.75 and P (Y ≥ 15) = 0.0926 = 5/54.
6. A nice way to visualise the probability distribution of Y is to plot an ordinate diagram (as opposed to
a histogram, since the random variable is discrete).
table(sums)
## sums
## 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
## 1 3 6 10 15 21 25 27 27 25 21 15 10 6 3 1
propns=table(sums)/length(sums)
plot(propns,type="h")
0.
00
0.
04
0.
08
0.
12
sums
pr
op
ns
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
3


essay、essay代写