Universit´e Joseph Fourier L2/STA230
Lab 7: Statistical testing with a simulated single sample
Objectives: Compute the test statistic, the decision rule for two-sided or one-sided tests.
A statistical test is a decision taken from the information available within the data. Several steps are required:
1. Model the variable of interest with an appropriate random variable.
2. Define the null and alternative hypothesis, calledH0 andH1.
3. Construct the decision rule to rejectH0or not. The decision rule is based on a well-chosen estimator and its distribution underH0. No observation are required to construct the rule.
4. Apply the rule to data and decide if H0 is rejected or not.
Exercise 1
1. Setµ= 2, σ= 3, and N = 1000 andn= 100.
2. SimulateN samples of sizenwith the distributionN(µ, σ2). Compute theN empirical means ¯Xn, and called it Xbar.
We want to choose between the two sets
H0:µ= 2 or (µ≤2) and H1:µ >2 3. First rule: if ¯Xn is greater than 2, then chooseH1; else chooseH0.
Apply this rule to the N samples. What proportion of samples choose H1 ? What does represent this proportion ?
4. Second rule: if ¯Xn is greater than 3, then chooseH1; else chooseH0.
Apply this rule to the N samples. What proportion of samples choose H1 ? What does represent this proportion ? What rule is more conservative ofH0 ?
5. Third rule: we wand to construct a rule at level 5%. The statistic used for this test is T = ( ¯Xn−µ0)
√n σ .
Give the distribution of T under H0. Give the definition of the first kind risk α. Construct the decision rule to rejectH0, for a givenα. Give the definition of a p-value.
6. Apply this third rule to the N samples. What proportion of samples chooseH1 ? Comment.
Exercise 2
1. Simulate a sampleXof lengthn= 1000 with a normal distribution with meanµ= 10 and standard deviation σ= 1.
2. We consider a test at level 5% for comparing the empirical mean of a sample X to µ0, assuming the variance known. Define the null and alternative hypothesis. Is it a two-tailed or one-tailed test
? Construct the decision rule to reject H0, for a givenα.
3. Compute the p-value of the two-sided test at level 5% for comparing the empirical mean of X to µ0= 9, assuming the data are normal, and the variance known. Repeat withµ0= 10.
4. Compute the p-value of the two-sided test at level 5% for comparing the empirical mean of X to µ0= 9, assuming the data are normal, and the variance unknown. Repeat withµ0= 10.
5. Compare the outputs with t.test(X, mu=9, sd=1) t.test(X, mu=10, sd=1)
6. Understand the output of t.test with the following inputs.
(a) x=rnorm(100), mu=0, alternative="two.sided"
(b) x=rnorm(100), mu=0.5, alternative="less"
(c) x=rnorm(100), mu=0.5, alternative="greater"
(d) x=rnorm(100), mu=-0.5, alternative="greater"
(e) x=rnorm(100,-1,1), mu=-0.5, alternative="greater"
(f) x=rnorm(100,1,10), mu=0.5, alternative="greater"
2