STAT 516 hw 7

Author

Karl Gregory

Students in several classes were asked to measure on their left hands the distances \(B\) and \(A\) depicted in the diagram below:

Do our fingers grow according to the golden ratio?

It is of interest to see whether our (humans’) fingers grow according to the golden ratio such that the mean of the ratio \(B/A\) in the adult human population is equal to \((1+\sqrt{5})/2 \approx 1.618\). Due to possible inconsistencies in the collection of these numbers in the several classes, assume the measurements have arisen according to the one-way random effects model \[ Y_{ij} = \mu + A_i + \varepsilon_{ij} \] for \(i = 1,\dots,a\), \(j = 1,\dots,n_i\), where the \(A_i\) are independent \(\mathcal{N}(0,\sigma_A^2)\) random variables, the \(\varepsilon_{ij}\) are independent \(\mathcal{N}(0,\sigma_\varepsilon^2)\) random variables, \(a\) is the number of classes and the \(n_i\) are the numbers of students in the classes.

The data can be read into R with the following code:

link <- url("https://gregorkb.github.io/data/golden_ratio_combined.csv")
gr <- read.csv(link, skip = 1,# replace pathtofile 
               colClasses = c("numeric","numeric","factor"))
gr$r <- gr$B / gr$A # compute ratio B / A

There were different numbers of students in each class, so the design is unbalanced. In the first part of this homework we will make a balanced version of the data set; in the second part we will work with the full data set.

Part I

Construct a balanced data set by running the code below, which draws (without replacement) a subsample of size \(n = 15\) from each data set.

# fix the random seed so the same subsets 
# are drawn every time the code runs
set.seed(1) 

classes <- levels(gr$class)
a <- length(classes)
n <- 15
gr_bal <- data.frame()
for(i in 1:a){
  
  ind <- which(gr$class == classes[i])
  gr_bal <- rbind(gr_bal,gr[sample(ind,n),])  
  
}

head(gr_bal)
     B   A       class        r
71 4.1 2.5 515_fa_2023 1.640000
50 5.4 3.2 515_fa_2023 1.687500
53 5.3 3.2 515_fa_2023 1.656250
47 5.3 3.2 515_fa_2023 1.656250
48 5.3 3.3 515_fa_2023 1.606061
57 4.7 3.1 515_fa_2023 1.516129
table(gr_bal$class)

515_fa_2023 515_sp_2024 515_sp_2026 516_fa_2026 516_sp_2026 
         15          15          15          15          15 

1.

Using the balanced data, report the mean of the ratio \(B/A\) for the subsample drawn from each class.

aggregate(r ~ class, data = gr_bal, mean)
        class        r
1 515_fa_2023 1.555191
2 515_sp_2024 1.663325
3 515_sp_2026 1.646862
4 516_fa_2026 1.445466
5 516_sp_2026 1.551581

2.

Produce side-by-side boxplots of the observed responses in the different classes.

boxplot(r ~ class, data = gr_bal)

3.

Obtain method of moments estimators for \(\sigma_A^2\) and \(\sigma_\varepsilon^2\).

lm_out <- lm(r ~ class, data = gr_bal)
y <- gr_bal$r
y.. <- mean(gr_bal$r)
SStot <- sum((y - y..)**2)
SSerror <- sum(lm_out$residuals**2)
SStrt <- SStot - SSerror
MSerror <- SSerror / (a*(n-1))
MStrt <- SStrt / (a-1)

sigma_e_mom <- sqrt(MSerror)
sigma_A_mom <- sqrt((MStrt - MSerror)/n)
We obtain 0.005166207 for the random 
effect variance and 0.03745857 for the error 
term variance.

4.

Check whether these are the same as the restricted maximum likelihood estimates obtained with lmer().

library(lmerTest)
lmer_out <- lmer(r ~ 1 + (1 | class),data = gr_bal)
summary(lmer_out)
Linear mixed model fit by REML. t-tests use Satterthwaite's method [
lmerModLmerTest]
Formula: r ~ 1 + (1 | class)
   Data: gr_bal

REML criterion at convergence: -24.2

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-2.9999 -0.3381  0.1542  0.6026  2.3069 

Random effects:
 Groups   Name        Variance Std.Dev.
 class    (Intercept) 0.005166 0.07188 
 Residual             0.037459 0.19354 
Number of obs: 75, groups:  class, 5

Fixed effects:
            Estimate Std. Error      df t value Pr(>|t|)    
(Intercept)  1.57249    0.03915 4.00000   40.17  2.3e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

5.

Report the test statistic and \(p\) value for testing whether class-to-class variability accounts for any significant portion of the variability in the measurements. What do you conclude?

anova(lm_out)
Analysis of Variance Table

Response: r
          Df  Sum Sq  Mean Sq F value  Pr(>F)  
class      4 0.45981 0.114952  3.0688 0.02178 *
Residuals 70 2.62210 0.037459                  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Ftest <- MStrt / MSerror
pval <- 1 - pf(Ftest,a-1,a*(n-1))
The F statistic for testing whether the variance of 
the random effect for class is greater than zero is 3.068768 
with a p value of 0.02177513. The fairly small p value indicates 
evidence of significant variability in the responses 
from classroom to classroom.

6.

Obtain for each class a prediction (a guess) of the realized value of the class random effect \(A_i\).

yi. <- aggregate(r~class, mean, data = gr_bal)$r
Ahat <- (n * sigma_A_mom**2)/(n * sigma_A_mom**2 + sigma_e_mom**2) * (yi. - y..)
names(Ahat) <- classes
Ahat
515_fa_2023 515_sp_2024 515_sp_2026 516_fa_2026 516_sp_2026 
-0.01165890  0.06123862  0.05014039 -0.08562808 -0.01409204 

7.

Construct a \(95\%\) confidence interval for the overall mean of the ratio \(B/A\). State whether you think it is plausible that our fingers grow according to the golden ratio such that the population mean of \(B/A\) is equal to \(1.618\).

alpha <- 0.05
tval <- qt(1-alpha/2,a-1)
me <- tval * sqrt(MStrt/(a*n))
lo <- y.. - me
up <- y.. + me
We obtain the interval [1.463789,1.681182].
# Automatic lmer confidence interval
confint(lmer_out)
Computing profile confidence intervals ...
                2.5 %    97.5 %
.sig01      0.0000000 0.1631437
.sigma      0.1654236 0.2306305
(Intercept) 1.4882973 1.6566731
Since the 95% confidence interval (computed by 
either method) contains the null 
value 1.618, it does seems like a plausible claim that 
the true mean is equal to this value.

8.

Remove the set.seed(1) command from the code above (so that new subsamples of data are drawn in the construction of the balanced data set) and run your analysis again: Do you get the same results? How stable is the analysis? Do you have any concerns?

This is an open-ended question.  Some 
subsamples will carry insufficient evidence for the rejection 
of the null hypothesis that the random effect variance is 
equal to zero, suggesting that the full data do not carry 
strong evidence against this null.  One obtains most of the 
time an interval for the overall mean which includes the golden 
ratio, so our main conclusion appears to be quite 
reliable.

Part II

Now use the entire data set.

1.

Produce side-by-side boxplots of the responses in the treatment groups.

boxplot(r ~ class, data = gr)

2.

Report the number of observations in each class.

table(gr$class)

515_fa_2023 515_sp_2024 515_sp_2026 516_fa_2026 516_sp_2026 
         27          21          25          18          19 

3.

Check whether the assumptions of the one-way random effects model hold for these data.

lmer_out <- lmer(r ~ 1 + (1|class), data = gr)
plot(lmer_out)

yhat <- predict(lmer_out)
y <- gr$r
ehat <- y - yhat
qqnorm(scale(ehat))
abline(0,1)

There seems to be some deviation from 
normality in the normal QQ plot, and one of the classes 
appears to have a variance somewhat smaller than that 
of the others.  Since the sample sizes are moderate, 
the sample means will be somewhat normal, though 
we should still be a little skeptical of our 
confidence intervals and p values.

4.

Obtain a \(95\%\) confidence interval for the overall mean and interpret the interval. Since the design is now unbalanced, you can use a built-in R function to obtain the interval.

confint(lmer_out)
Computing profile confidence intervals ...
                 2.5 %    97.5 %
.sig01      0.02335089 0.1684726
.sigma      0.15916208 0.2087806
(Intercept) 1.48543112 1.6556272

5.

Consider the interval computed by the code below. Explain the strategy behind the construction of this interval and carefully explain whether you think it is appropriate or inappropriate (trustworthy or untrustworthy).

y <- gr$r
y.. <- mean(y)
sn <- sd(y)
N <- length(y)
alpha <- 0.05
tval <- qt(1-alpha/2,N-1)
lo <- y.. - tval * sn / sqrt(N)
up <- y.. + tval * sn / sqrt(N)
c(lo,up)
[1] 1.539438 1.612607
This is the ordinary confidence interval for a mean 
based on a single random sample.  This is the interval one would 
compute if one had collected all the data in a single class, 
regarding this as a random sample.  Note that one does not come to 
a different conclusion vis-a-vis the golden ratio hypothesis 
based on this interval. However, this interval ignores the 
possibility of random class-to-class variability, which may not be 
appropriate.  If one has not found any significant 
evidence of class-to-class variability, it may be okay to pool 
all the data together and treat the values as though they arose 
from a single sample. I believe, however, that this would be 
somewhat naive, due to the fact that (at least in our exploration 
of the balanced data) we obtained a fairly small p value for testing 
the null hypothesis that the variance governing class-to-class 
variation was equal to zero (this p value is somewhat harder 
to obtain in the unbalanced case). A possible warning against 
using this interval is that it turned out to be narrower than 
the interval we obtain for the overall mean under the one-way 
random effects model.  If there were no need to include the random 
effect, the two intervals would have more similar widths.