Skip to main content

Section A.8 Chapter 9: Introduction to Statistical Inference

Subsection A.8.1 Exercise 9.1

  1. Let us read the data into a data frame by the name β€œmagnets" and apply the function β€œsummary" to the data frame:
    magnets <- read.csv("_data/magnets.csv")
    summary(magnets)
    
    ##      score1          score2          change     active  
    ##  Min.   : 7.00   Min.   : 0.00   Min.   : 0.0   "1":29  
    ##  1st Qu.: 9.25   1st Qu.: 4.00   1st Qu.: 0.0   "2":21  
    ##  Median :10.00   Median : 6.00   Median : 3.5           
    ##  Mean   : 9.58   Mean   : 6.08   Mean   : 3.5           
    ##  3rd Qu.:10.00   3rd Qu.: 9.75   3rd Qu.: 6.0           
    ##  Max.   :10.00   Max.   :10.00   Max.   :10.0
    
    The variable β€œchange" contains the difference between the patient’s rating before the application of the device and the rating after the application. The sample average of this variable is reported as the β€œMean" for this variable and is equal to 3.5.
  2. The variable β€œactive" is a factor. Observe that the summary of this variable lists the two levels of the variable and the frequency of each level. Indeed, the levels are coded with numbers but, nonetheless, the variable is a factor[^12].
  3. Based on the hint we know that the expressions β€œchange[1:29]" and β€œchange[30:50]" produce the values of the variable β€œchange" for the patients that were treated with active magnets and by inactive placebo, respectively. We apply the function β€œmean" to these sub-sequences:
    mean(magnets$change[1:29])
    mean(magnets$change[30:50])
    
    ## [1] 5.241379
    ## [1] 1.095238
    
    The sample average for the patients that were treated with active magnets is 5.241379 and sample average for the patients that were treated with inactive placebo is 1.095238.
  4. We apply the function β€œsd" to these sub-sequences:
    sd(magnets$change[1:29])
    sd(magnets$change[30:50])
    
    ## [1] 3.236568
    ## [1] 1.578124
    
    The sample standard deviation for the patients that were treated with active magnets is 3.236568 and sample standard deviation for the patients that were treated with inactive placebo is 1.578124.
  5. We apply the function β€œboxplot" to each sub-sequences:
    boxplot(magnets$change[1:29])
    boxplot(magnets$change[30:50])
    
    The first box-plot corresponds to the sub-sequence of the patients that received an active magnet. There are no outliers in this plot. The second box-plot corresponds to the sub-sequence of the patients that received an inactive placebo. Three values, the values β€œ3", β€œ4", and β€œ5" are associated with outliers. Let us see what is the total number of observations that receive these values:
    table(magnets$change[30:50])
    
    ## 
    ##  0  1  2  3  4  5 
    ## 11  5  1  1  2  1
    
    One may see that a single observation obtained the value β€œ3", another one obtained the value β€œ5" and 2 observations obtained the value β€œ4", a total of 4 outliers[^13]. Notice that the single point that is associated with the value β€œ4" actually represents 2 observations and not one.

Subsection A.8.2 Exercise 9.2

  1. Let us run the following simulation:
    mu1 <- 3.5
    sig1 <- 3
    mu2 <- 3.5
    sig2 <- 1.5
    test.stat <- rep(0,10^5)
    for(i in 1:10^5) {
    X1 <- rnorm(29,mu1,sig1)
    X2 <- rnorm(21,mu2,sig2)
    X1.bar <- mean(X1)
    X2.bar <- mean(X2)
    X1.var <- var(X1)
    X2.var <- var(X2)
    test.stat[i] <- (X1.bar-X2.bar)/sqrt(X1.var/29 + X2.var/21)
    }
    quantile(test.stat,c(0.025,0.975))
    
    ##      2.5%     97.5% 
    ## -2.026108  2.010135
    
    Observe that each iteration of the simulation involves the generation of two samples. One sample is of size 29 and it is generated from the \(\mathrm{Normal}(3.5,3^2)\) distribution and the other sample is of size 21 and it is generated from the \(\mathrm{Normal}(3.5,1.5^2)\) distribution. The sample average and the sample variance are computed for each sample. The test statistic is computed based on these averages and variances and it is stored in the appropriate position of the sequence β€œtest.stat".
    The values of the sequence β€œtest.stat" at the end of all the iterations represent the sampling distribution of the static. The application of the function β€œquantile" to the sequence gives the 0.025-percentiles and the 0.975-percentiles of the sampling distribution, which are -2.014838 and 2.018435. It follows that the interval \([-2.014838, 2.018435]\) contains about 95% of the sampling distribution of the statistic.
  2. In order to evaluate the statistic for the given data set we apply the same steps that were used in the simulation for the computation of the statistic:
    x1.bar <- mean(magnets$change[1:29])
    x2.bar <- mean(magnets$change[30:50])
    x1.var <- var(magnets$change[1:29])
    x2.var <- var(magnets$change[30:50])
    (x1.bar-x2.bar)/sqrt(x1.var/29 + x2.var/21)
    
    ## [1] 5.985601
    
    In the first line we compute the sample average for the first 29 patients and in the second line we compute it for the last 21 patients. In the third and fourth lines we do the same for the sample variances of the two types of patients. Finally, in the fifth line we evaluate the statistic. The computed value of the statistic turns out to be 5.985601, a value that does not belong to the interval \([-2.014838, 2.018435]\text{.}\)