代写 R graph statistic Problem 1

Problem 1
HW2 Due:July 8th
Suppose a person decides that he shall do something if he gets more successes(heads) than failures out of 7 tosses. Write a function in R that estimates this probability given any p where p is probability of success. Run this function for p ∈ {.3, .5, .7}. Also report the standard errors of the estimates in the 3 cases. You can only use the uniform as a starting point.(10 points)
Problem 2
i. Use only uniforms to write a function that generates χ2n. Estimate the mean of the distribution and report the monte Carlo standard error.(Choose any n = 10)
ii. A Γ(n, λ) distribution can be generated using X1 + X2 + · · · + Xn where Xi ∼ Exp(λ). Using this fact, generate iid observations from Γ(n,2). Compare it with the mean of a χ2n using the previous function. Report standard errors in both cases. (10 points)
Problem 3
1 The truncated normal is defined using the pdf 2π
−x2 √e2
where a < x < b. Φ(b)−Φ(a) i.Prove this is a proper pdf. ii. If b > 0 and a = −b, what is the expectation of X following this distribution.
iii.Using dnorm,pnorm and seq draw the graph of the density when a = −1, b = 2.
iv. Write a function that generates iid observations from this distribution. The function should have arguments n-number of samples, a, b-lower and upper limits.
v.For values a = −1, b = 2 generate n = 500 observations and draw a histogram.
vi. Do a Monte Carlo simulation of the 95% coverage interval for the mean of this distribution. Here σ is unknown, so use the CI T ̄ ± t.975,n−1S/√n.(20 points)
Problem 4
Let X ∼ Bin(n, θ), n ≥ 10. Suppose we want to test
H0 :θ=.5
Ha :θ=.25
Our decision shall be based on whether X ≤ 3 . If this happens we reject, otherwise, we do not reject.
n 10
i.Let n = 10 Calculate type 1 error rate and power of the test. Use the pbinom function.
1

ii.Do a simulation study to corroborate your results about the power of this decision rule from part i.. Let reps = 104. We are interested in studying pˆ = P (X ≤ 3 | θ = .25). Get a conservative confidence interval

for the power by calculating pˆ ± 1/ 104. You may not use the built in R function rbinom. Comment on
the results.
iii. Repeat the simulatio for n ∈ {10, 20, 30, 40, 50}. Comment on your results.(10 points)
Problem 5
Assume that X1,X2,…,Xn are independent copies of the random variable X = Y.V where Y ∼ Ber(θ) and V ∼ Exp(λ). Assume 0 ≤ θ ≤ 1 and λ > 0 and Y, V are independent. We shall consider
H0 :θλ=1
Ha :θλ̸=1
a. ShowE(X)=θλandVar(X)=λ2θ(2−θ). Setθ=1/4andλ=3.
b.Estimate E(X) and V ar(X) using Monte Carlo simulations based on drawing n = 104 iid copies of X. You can only use the runif function to generate draws from a distribution, so you will need to take the necessary steps to draw realizations from the appropriate distributions. Comment on the results.
c. Let T = √n(X ̄ − 1)/S be the usual test statistic. Perform a simulation to check whether this is a good approximation of the T-distribution with n − 1 degrees of freedom. Use n = 200, θ = 1/, λ = 2. Use a Q-Q plot and set reps = 104. Comment on the results.
d. Set n = 200,θ = 1/2,α = .05. Produce a plot of a simulated estimate of the power curve of this test formed by using λ ∈ {2, 2.5, . . . , 19.5, 20}. Comment on results.(10 points)
Problem 6
Use the function for Chi-sq in Problem 2 to do the following. Suppose we have 2 groups- the first with n1 observations such that X1, X2, . . . , Xn1 ∼ χ2k iid X and group 2 has n2 observations of the random variable Y such that Y1,Y2,…,Yn2 ∼ χ2l .
The researchers are interested in testing the hypothesis
H0 : k = l
Ha :k̸=l
Let k = 11, l = 12.
a. Suppose n1 = n2 = 500.Test your function by creating two QQ plots of your realizations compared to the true distributions of both X and Y . Use the 1st, 2nd,…, and 99th percentiles (so the plots have 99 points). You may use qchisq to do this problem. Comment on the results.
b.The researchers will use the two independent samples model to build a test statistic for their hypothesis.Use simulation to find the value of n1 = n2 such that the power of the the two independent sample t − test is roughly 90%. when α = .05. Use 105 replications. Conclue your results.(10 points)
Problem 7
Consider two Binomial random variables V and W that share the same parameter n. We know V ∼ Bin(n, θ1) and W ∼ Bin(n, θ2). We are interested in
H0 :θ1 =θ2
Ha :θ1 ̸=θ2
Letθˆ1=V/nandθˆ2=W/nandθˆ=(V+W)/2n.TheteststatisticshallbeT= θˆ1−θˆ2 .Underthenull 􏰀 2θˆ(1−θˆ)
hypothesis T ∼ N (0, 1). You are not allowed to use rbinom in this problem. 2
n

a. Write a function propHypothesisTest that simulates the hypothesis test by generating data, calculating the test statistic and then outputting the random p-values. The function takes in as arguments n, θ1, θ2, reps. The function should return a vector of size reps of the random p values. Make sure to return a probability value from a normal distribution using pnorm.
b. Test your function by setting the parameters n = 500,θ1 = θ2 = .68,reps = 1000. Since the null hypothesis is true compare your outputted p-values to a U(0,1) distribution by using the Q-Q plot. Use the 1st,2nd ,..,99th percentile. Comment on the plot.
c. Use your function to do a simulation study on how changing θ2 affects the power of the test. Run your function for values of θ2 ∈ {.520,.525,…,.675,.680} while n = 500,θ1 = .68,reps = 1000. Use α = .03 Make a plot of your findings and include the lower and upper bounds of a 95% Clopper-Pearson C.I. for the power.(15 points)
Problem 8
We want to compare the test scores of students before and after taking a class. Let X be the former random variable and Y be the latter random variable.
The model for the i-th student is
Xi =μ1 +Ai +L1i (0.1)
Yi =μ2 +Ai +L2i (0.2)
Here Ai ∼ N(0,σ2) iid and can be thought of as each individual students ability. Assume L1i ∼ N(0,γ2) and L2i ∼ Tdist(4). Since the T-distribution has fatter tails than the normal we can think of this as the student’s luck being more extreme after taking the class. Let μ1 = 68, μ2 = 70, σ2 = 2, γ2 = 1. You may use rt for this problem.
a. It can be shown that E(X) = 68 and Var(X) = 3. Create 50,000 realizations of X and produce a simulation based 99% C.I. for V ar(X) = E(X − 68)2. Is 3 in this interval?
b. It can be shown that E(Y ) = 70 and V ar(Y ) = 4. Create 50,000 realizations of Y and produce a simulation based 99% approximate C.I. for V ar(Y ) = E(Y − 70)2. Is 4 contained within this interval?
c. Suppose we are interested in the two sample paired t-test for
H0 :μ1 =μ2
Ha :μ1 ̸=μ2
Use simulation to find the value n such that the power of the test statistic for the two sample paired t-test is approximately 90% using significance level α = .05 and 10,000 repetitions. Conclude with a brief sentence about your result. (15 points)
3