Is there a way to Run a One-Way Repeated Measures ANOVA to Compare Two Test Scores (Midterm Exam vs Final Exam) in RStudio?

I have two columns of data (one column for midterm exam grades for each student and one column for final exam grades for each student).

I want to compare each student's midterm and final exam grades (dependent groups with same subjects) using a repeated measures ANOVA just to see.

How would you go about that in RStudio? What's the easiest way to do this?


1 Answer 1


It's actually better if you provide your dataset, or in R, you can do dput() and paste the output.

Based on your description, i simulate data that might look like yours:

df = data.frame(student=factor(1:100),midterm=rnorm(100,50,5),final=rnorm(100,50,5))

  student  midterm    final
1       1 51.17610 52.99810
2       2 48.34632 44.19835
3       3 48.44188 52.19547
4       4 38.48827 51.02427
5       5 49.14562 46.50409
6       6 50.70139 45.36687

Above, if your id is numeric, you need to ensure it's a factor for the remaining analysis to be correct. Now you need to pivot the data columns long:

df_long = pivot_longer(df,cols=c("midterm","final"),names_to="exam_type")

     student exam_type value
  <fct>   <chr>     <dbl>
1 1       midterm    51.2
2 1       final      53.0
3 2       midterm    48.3
4 2       final      44.2
5 3       midterm    48.4
6 3       final      52.2

I store the long format as a new data.frame and apply the anova, since it is a one-way repeated measure, you want to partition the variance or sum of squares (SS) into $SS_{total} = SS_{exam type} + SS_{within_student} + SS_{error}$.

Depending on the package used in R, there are many ways of specifying this structure above, you can check out this link. Below I show one way of doing repeated measure anova using aov(), where the error term specified as (student/):

anova_test = aov(value ~ exam_type + Error(student/exam_type),data=df_long)

Error: student
          Df Sum Sq Mean Sq F value Pr(>F)
Residuals 99   2552   25.77               

Error: student:exam_type
          Df Sum Sq Mean Sq F value Pr(>F)
exam_type  1      0    0.02   0.001  0.979
Residuals 99   2857   28.86       
  • $\begingroup$ Thanks man. Sorry I didn't provide the data. May I ask why the error is necessary in repeated measures ANOVA? I never encountered this with some RStudio codes. $\endgroup$
    – Dew Man
    Commented Jun 22, 2020 at 18:49
  • $\begingroup$ Hi @DewMan, sorry just realized I made a mistake. The answer is now correct, I got a bit confused with something else I normally use. I have also added an explanation of why you need to have the Error().. $\endgroup$
    – StupidWolf
    Commented Jun 22, 2020 at 19:31
  • $\begingroup$ Or are you supposed to do actually something else that is not repeated measure? $\endgroup$
    – StupidWolf
    Commented Jun 22, 2020 at 19:31
  • 1
    $\begingroup$ No this is what I wanted. Thanks. I know how to use a paired t-test for this situation but not a repeated measures ANOVA (very similar to paired t-test but an extension of it). I just wanted to compare the results for both on two groups (midterm vs final). $\endgroup$
    – Dew Man
    Commented Jun 22, 2020 at 19:56
  • $\begingroup$ I ran the Repeated Measures ANOVA test and t-test separately but I got very different answers for some reasons. $\endgroup$
    – Dew Man
    Commented Jun 24, 2020 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.