##Part 1: Analysis

##################### construct data frame
student_stress = data.frame(student = seq(1, 10), time1 = c(5, 7, 4, 6, 
    8, 4, 5, 8, 4, 6), time2 = c(6, 4, 7, 6, 7, 4, 7, 7, 4, 6), time3 = c(6, 
    7, 8, 6, 9, 5, 9, 8, 5, 9))
##################### 

####################### compute contrast codes time_lin -> -1 if time1, 0 if time2, +1 if
####################### time3 time_quad -> -1 if time1, +2 if time2, -1 if time3
student_stress$time_lin = (-1 * (student_stress$time1) + 1 * (student_stress$time3))/sqrt(2)
student_stress$time_quad = (-1 * (student_stress$time1) + 2 * (student_stress$time2) - 
    1 * (student_stress$time3))/sqrt(6)

# compute w0
student_stress$w0 = (student_stress$time1 + student_stress$time2 + student_stress$time3)/sqrt(3)
####################### 
# model testing linear effects
model.time_lin = lm(student_stress$time_lin ~ 1)
mcSummary(model.time_lin)
## Loading required package: car
## Warning: package 'car' was built under R version 3.6.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 3.6.3
## lm(formula = student_stress$time_lin ~ 1)
## 
## Omnibus ANOVA
##               SS df   MS EtaSq F p
## Model       0.00  0  Inf     0    
## Error      11.25  9 1.25          
## Corr Total 11.25  9 1.25          
## 
##   RMSE AdjEtaSq
##  1.118        0
## 
## Coefficients
##               Est StErr t SSR(3) EtaSq tol CI_2.5 CI_97.5     p
## (Intercept) 1.061 0.354 3  11.25   0.5  NA  0.261    1.86 0.015
# model testing quadratic effects
model.time_quad = lm(student_stress$time_quad ~ 1)
mcSummary(model.time_quad)
## lm(formula = student_stress$time_quad ~ 1)
## 
## Omnibus ANOVA
##               SS df    MS EtaSq F p
## Model      0.000  0   Inf     0    
## Error      8.017  9 0.891          
## Corr Total 8.017  9 0.891          
## 
##   RMSE AdjEtaSq
##  0.944        0
## 
## Coefficients
##                Est StErr      t SSR(3) EtaSq tol CI_2.5 CI_97.5     p
## (Intercept) -0.531 0.298 -1.778  2.817  0.26  NA -1.206   0.144 0.109
# model assessing between-group variance
model.w0 = lm(student_stress$w0 ~ 1)
mcSummary(model.w0)
## lm(formula = student_stress$w0 ~ 1)
## 
## Omnibus ANOVA
##                SS df   MS EtaSq F p
## Model       0.000  0  Inf     0    
## Error      42.033  9 4.67          
## Corr Total 42.033  9 4.67          
## 
##   RMSE AdjEtaSq
##  2.161        0
## 
## Coefficients
##                Est StErr      t   SSR(3) EtaSq tol CI_2.5 CI_97.5 p
## (Intercept) 10.796 0.683 15.798 1165.633 0.965  NA   9.25  12.342 0
library(kableExtra)
## Warning: package 'kableExtra' was built under R version 3.6.3
################################################### pull results from output to construct source table
mySource = data.frame(source = c("Between", "Within", "day.lin", "day.lin-by-p", 
    "day.quad", "day.quad-by-p", "Total - within", "Total - overall"), 
    b = c("-", "-", 1.061, "-", -0.531, "-", "-", "-"), SS = c(42.033, 
        "-", 11.25, 11.25, 2.817, 8.017, 33.33, 108.7), df = c(9, "-", 
        1, 9, 1, 9, 20, 29), MS = c(4.67, "-", 11.25, 1.25, 2.817, 0.891, 
        1.665, 3.75), F = c("-", "-", 9, "-", 3.16, "-", "-", "-"), PRE = c("-", 
        "-", 0.5, "-", 0.26, "-", "-", "-"), p = c("-", "-", 0.015, "-", 
        0.109, "-", "-", "-"))

knitr::kable(mySource, digits = 3, align = "lcccc") %>% kable_styling("striped")
source b SS df MS F PRE p
Between
42.033 9 4.67
Within
day.lin 1.061 11.25 1 11.25 9 0.5 0.015
day.lin-by-p
11.25 9 1.25
day.quad -0.531 2.817 1 2.817 3.16 0.26 0.109
day.quad-by-p
8.017 9 0.891
Total - within
33.33 20 1.665
Total - overall
108.7 29 3.75

##Part 2: Summary

To assess the linear and quadratic effects of testing period (that is, before each of the first midterm, second midterm and final) on student stress levels, a one-way, within-group, repeated-measures ANOVA was conducted. In this model, testing period considered within each student exhibited significant linear effects on stress levels (b = 1.061, F = 9, PRE = .5, p = .015), such that students reporting prior to the first midterm showed reduced stress levels (M = 5.7) compared to those reporting before the final (M = 7.2). However, no significant quadratic effects (b = -.531, F = 3.16, PRE = .26, p = .109) were detected.