# CS代考 ################################################### – cscodehelp代写

###################################################
# Computer Lab 2 – F71SM
###################################################

## In the tasks below, some (but not all) R code is given
## for guidance. In some places the code is also incomplete,
## so you will need to figure out how to fill in the details.
## Make sure you use the help menus in R (e.g.”?mean” will
## open up a help window for “mean()”).

###################################################
# Tutorial 1, Question 2:
# Repeat the calculations
# The data can be found in file T1Q2.RData

q = quantile(x,probs=c(0.25,0.75))
# Note that R by default uses a different definition of quantiles
# The definition in lectures notes can be obtained using
# type=6 in quantile()

###################################################
# Tutorial 1, Question 7:
# Confirm the answers by computing the mean, sd and skewness
# of the two samples

a = rep(c(1:4),c(80,60,40,20))

# For skewness, use function from lab 1

###################################################
# Worked example 2.4:
# Confirm the answers for all values of k

p = numeric(5)
# Note that p[0] is not defined for a vector in R
for(k in 0:4){
p[k+1] = choose(4,k)*choose(7,(6-k))/choose(11,6)

###################################################
# Simulate 10,000 values throws of a fair six-sided die
# Let X be the r.v. giving the score of the die
# Compute (approximately) the mean and variance of X
# (this is also worked example 4.1).

# A single value of X can be simulated as (check):
trunc(6*runif(1, 0, 1)) + 1
# So 10^4 values simulated as
mean(x); var(x)

# Compute (approximately) P(X<=3) length(x[x<=3]) / length(x) # Repeat these calculations to see how the anwsers # change slightly ################################################### # Worked example 3.3: # Simulate 10,000 values of r.v. Y, where Y is # number of throws until we get a "6" # Confirm the mean and variance of Y # A sigle value of y can be simulated as follows: # Generate throws of the die as in Task 3 # and stop when a 6 is thrown while(x!=6){ x = trunc(6*runif(1, 0, 1)) + 1 # Use for loop to simulate 10^4 values of Y y = numeric(10^4) for (i in 1:10^4){ # Again, repeat these calculations to see how the anwsers # change slightly ################################################### # Worked examples 3.5, 3.6: # Plot the pdf and cdf of r.v. X x1 = seq(1.001,4,by=0.01) f.x1 = ... F.x1 = ... # Change the plotting window layout par(mfrow=c(1,2)) plot(x1,f.x1,type="l",xlab="x", ylab="f(x)", main="PDF") plot(x1,F.x1,type="l",xlab="x", ylab="F(x)", main="CDF") # Can also change the limits of the x axis: plot(x1,f.x1,type="l",xlim=c(0,3.3), xlab="x", ylab="f(x)", main="PDF") plot(x1,F.x1,type="l",xlim=c(-1,3.3),xlab="x", ylab="F(x)", main="CDF") ################################################### # Worked example 4.2 # X ~ Binomial(10,0.2) # Calculate pmf f(3), cdf F(3) #Simulate 10^3 values of X and confirm E(X), Var(X) x = rbinom(...) mean(x); var(x) ################################################### # Worked example 4.3 # X ~ Poisson(7) # Calculate pmf f(9), P(X>=8)
dpois(…)
1-ppois(…)

# Simulate N=10^4 values of X and confirm E(X), Var(X)
# Use rpois()
# Repeat above with N=10^2. Comment.

###################################################
# Worked example 4.5
# X ~ Binomial(200,0.02)
# Calcualate P(X = 5 or 6) using the binomial above and a Poisson approximation
pbinom(…) – pbinom(…)
ppois(…) – ppois(…)

###################################################