Skip to main content

Section A.10 Chapter 11: Confidence Intervals

Subsection A.10.1 Exercise 11.1

  1. We read the content of the file β€œteacher.csv" into a data frame by the name β€œteacher" and produce a summary of the content of the data frame:
    teacher <- read.csv("_data/teacher.csv")
    summary(teacher)
    
    ##  condition     rating     
    ##  C:25      Min.   :1.333  
    ##  P:24      1st Qu.:2.000  
    ##            Median :2.333  
    ##            Mean   :2.429  
    ##            3rd Qu.:2.667  
    ##            Max.   :4.000
    
    There are two variables: The variable β€œcondition" is a factor with two levels, β€œC" that codes the Charismatic condition and β€œP" that codes the Punitive condition. The second variable is β€œrating", which is a numeric variable.
  2. The sample average for the variable β€œrating" can be obtained from the summary or from the application of the function β€œmean" to the variable. The standard deviation is obtained from the application of the function β€œsd" to the variable:
    mean(teacher$rating)
    sd(teacher$rating)
    
    ## [1] 2.428567
    ## [1] 0.5651949
    
    Observe that the sample average is equal to \(2.428567\) and the sample standard deviation is equal to \(0.5651949\text{.}\)
  3. The sample average and standard deviation for each sub-sample may be produced with the aid of the function β€œtapply". We apply the function in the third argument, first β€œmean" and then β€œsd" to the variable rating, in the first argument, over each level of the factor β€œcondition" in the second argument:
    tapply(teacher$rating,teacher$condition,mean)
    tapply(teacher$rating,teacher$condition,sd)
    
    ##        C        P 
    ## 2.613332 2.236104
    
    ##         C         P 
    ## 0.5329833 0.5426667
    
    Obtain that average for the condition β€œC" is \(2.613332\) and the standard deviation is \(0.5329833\text{.}\)
    You may note that the rating given by students that were exposed to the description of the lecturer as charismatic is higher on the average than the rating given by students that were exposed to a less favorable description. The box plots of the ratings for the two conditions are presented in FigureΒ 11.3.1.
    cars <- read.csv("_data/cars.csv")
    cars$dif.mpg =  cars$highway.mpg - cars$city.mpg
    boxplot(dif.mpg ~ fuel.type, data=cars)
    
  4. The 99% confidence interval for the expectation is computed by the formula \(\bar x \pm \mbox{\texttt{qt(0.995,n-1)}} \cdot s/\sqrt{n}\text{.}\) Only 0.5% of the \(t\)-distribution on \((n-1)\) degrees of freedom resides above the percentile β€œqt(0.995,n-1)". Using this percentile leaves out a total of 1% in both tails and keeps 99% of the distribution inside the central interval.
    For the students that were exposed to Condition β€œC", \(\bar x = 2.613332\text{,}\) \(s = 0.5329833\text{,}\) and \(n = 25\text{:}\)
    2.613332 - qt(0.995,24)*0.5329833/sqrt(25)
    2.613332 + qt(0.995,24)*0.5329833/sqrt(25)
    
    ## [1] 2.315188
    ## [1] 2.911476
    
    The confidence interval for the expectation is \([2.315188, 2.911476]\text{.}\)
  5. The 90% confidence interval for the variance is computed by the formula \(\big[\frac{n-1}{\mbox{\texttt{qchisq(0.95,n-1)}}} s^2 ,\;\frac{n-1}{\mbox{\texttt{qchisq(0.05,n-1)}}} s^2\big]\text{.}\) Observe that 5% of the chi-square distribution on \((n-1)\) degrees of freedom is above the percentile β€œqchisq(0.95,n-1)" and 5% are below the percentile β€œqchisq(0.05,n-1)".
    For the students that were exposed to Condition β€œC", \(s = 0.5329833\text{,}\) and \(n = 25\text{:}\)
    (24/qchisq(0.95,24))*0.5329833^2
    (24/qchisq(0.05,24))*0.5329833^2
    
    ## [1] 0.1872224
    ## [1] 0.4923093
    
    The point estimate of the variance is \(s^2 = 0.5329833^2 = 0.2840712\text{.}\) The confidence interval for the variance is \([0.18722243, 0.4923093]\text{.}\)

Subsection A.10.2 Exercise 11.2

  1. Let us produce the sampling distribution of the sample average and the sample standard deviation for 20 observations from the \(\mathrm{Exponential}(1/4)\) distribution:
    lam <- 1/4
    n <- 20
    X.bar <- rep(0,10^5)
    S <- rep(0,10^5)
    for(i in 1:10^5) {
    X <- rexp(n,lam)
    X.bar[i] <- mean(X)
    S[i] <- sd(X)
    }
    
    We compute the confidence level for a confidence interval with a nominal confidence level of 95%. Observe that using the Normal approximation of the sample average corresponds to the application of the Normal percentile in the construction of the confidence interval.
    norm.LCL <- X.bar - qnorm(0.975)*S/sqrt(n)
    norm.UCL <- X.bar + qnorm(0.975)*S/sqrt(n)
    mean((4 >= norm.LCL) & (4 <= norm.UCL))
    
    ## [1] 0.90342
    
    The expectation of the measurement is equal to 4. This number belongs to the confidence interval 90.47% of the times. Consequently, the actual confidence level is 90.47%.
  2. Using the same sampling distribution that was produced in the solution to QuestionΒ 1 we now compute the actual confidence level of a confidence interval that is constructed under the assumption that the measurement has a Normal distribution:
    t.LCL <- X.bar - qt(0.975,n-1)*S/sqrt(n)
    t.UCL <- X.bar + qt(0.975,n-1)*S/sqrt(n)
    mean((4 >= t.LCL) & (4 <= t.UCL))
    
    ## [1] 0.91782
    
    Based on the assumption we used the percentiles of the \(t\)-distribution. The actual significance level is 91.953% \(\approx\) 92%, still short of the nominal 95% confidence level.
  3. It would be preferable to use the (incorrect) assumption that the observations have a Normal distribution than to apply the Normal approximation to such a small sample. In the current setting the former produced a confidence level that is closer to the nominal one. In general, using the percentiles of the \(t\)-distribution will produce wider and more conservative confidence intervals than those produces under the Normal approximation of the average. To be on the safer size, one typically prefers the more conservative confidence intervals.

Subsection A.10.3 Exercise 11.3

  1. The range of the confidence interval with 99% confidence interval is bounded by
    \begin{equation*} \mbox{\texttt{qnorm(0.995)}}\cdot \{\hat P (1-\hat P)/n\}^{1/2} \leq 2.575829 \cdot \sqrt{0.25/n} = 1.287915/\sqrt{n}\;, \end{equation*}
    since qnorm(0.995) = 2.575829 and \(\hat P (1-\hat P) \leq 0.25\text{.}\) Consequently, the sample size \(n\) should satisfy the inequality:
    \begin{equation*} \begin{aligned} 1.287915/\sqrt{n} \leq 0.03\quad &\Longrightarrow \quad \sqrt{n} \geq 1.287915/0.03 = 42.9305\\ &\Longrightarrow \quad n \geq (42.9305)^2 = 1843.028\;.\end{aligned} \end{equation*}
    The smallest integer larger than the lower bound is \(n=1844\text{.}\)
    The 80% confidence interval for the probability is computed by the formula \(\hat p \pm \mbox{\texttt{qnorm(0.90)}} \cdot \sqrt{\hat p(1-\hat p)/n}\text{:}\)
    n <- 400
    p.hat <- 320/400
    p.hat - qnorm(0.90)*sqrt(p.hat*(1-p.hat)/n)
    p.hat + qnorm(0.90)*sqrt(p.hat*(1-p.hat)/n)
    
    ## [1] 0.774369
    ## [1] 0.825631
    
    We obtain a confidence interval of the form \([0.774369, 0.825631]\text{.}\)