# ======== ライブラリ ======== #
library(readxl)
## Warning: package 'readxl' was built under R version 4.3.3
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(ggplot2)
library(lme4)
## Warning: package 'lme4' was built under R version 4.3.3
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
library(lmerTest)
## 
## Attaching package: 'lmerTest'
## The following object is masked from 'package:lme4':
## 
##     lmer
## The following object is masked from 'package:stats':
## 
##     step
library(emmeans)
## Warning: package 'emmeans' was built under R version 4.3.3
## Welcome to emmeans.
## Caution: You lose important information if you filter this package's results.
## See '? untidy'
library(emmeans)
library(effectsize)
## Warning: package 'effectsize' was built under R version 4.3.3
# ======== データ読み込み ======== #
df <- read_excel("~/Library/CloudStorage/Dropbox/土居_関口/2.Analysis_Doi/writing_results_scored_4criteria.xlsx")

# 条件を統一・順序付け
df <- df %>%
  mutate(
    Condition = factor(case_when(
      条件 == "ai-wcf" ~ "AI",
      条件 == "model text" ~ "Model",
      条件 == "control" ~ "Control",
      TRUE ~ 条件
    ), levels = c("AI", "Model", "Control")),
    ID = as.factor(学籍番号)
  )

# ======== LMEを回す関数を定義 ======== #
run_lme <- function(data, criterion_name, pre_col, post_col) {
  # データを縦長化
  df_long <- data %>%
    select(ID, Condition, !!sym(pre_col), !!sym(post_col)) %>%
    pivot_longer(
      cols = c(!!sym(pre_col), !!sym(post_col)),
      names_to = "Time",
      values_to = "Score"
    ) %>%
    mutate(
      Time = ifelse(Time == pre_col, "Pre", "Post"),
      Time = factor(Time, levels = c("Pre", "Post"))
    )
  
  # 混合効果モデル
  model <- lmer(Score ~ Condition * Time + (1 | ID), data = df_long)
  
  cat("\n\n===== LME結果:", criterion_name, "=====\n")
  print(anova(model))
  print(summary(model))
  
  # EMM
  emm <- emmeans(model, ~ Condition * Time)
  
  # プロット
  emm_df <- as.data.frame(emm)
  p <- ggplot(emm_df, aes(x = Time, y = emmean, group = Condition, color = Condition)) +
    geom_line(linewidth = 1.2) +
    geom_point(size = 3) +
    geom_errorbar(aes(ymin = emmean - SE, ymax = emmean + SE), width = 0.1, linewidth = 0.8) +
    scale_color_manual(values = c("AI" = "#1f78b4", "Model" = "#33a02c", "Control" = "#e31a1c")) +
    labs(
      title = paste("Interaction Plot:", criterion_name, "(Condition × Time)"),
      x = "Time (Pre / Post)",
      y = "Estimated Mean Score",
      color = "Condition"
    ) +
    theme_minimal(base_size = 14) +
    theme(
      plot.title = element_text(face = "bold", hjust = 0.5),
      axis.title = element_text(face = "bold"),
      axis.text = element_text(size = 12),
      legend.position = "top",
      legend.title = element_text(face = "bold")
    ) +
    coord_cartesian(ylim = c(0, 9))
  
  print(p)
}

# ======== 各観点+合計スコアでLME実行 ======== #

# Task Achievement
run_lme(df, "Task Achievement", "Pre_TA", "Post_TA")
## 
## 
## ===== LME結果: Task Achievement =====
## Type III Analysis of Variance Table with Satterthwaite's method
##                 Sum Sq Mean Sq NumDF DenDF F value    Pr(>F)    
## Condition       0.4006  0.2003     2    91  0.3774    0.6867    
## Time           15.1152 15.1152     1    91 28.4823 6.884e-07 ***
## Condition:Time  1.2658  0.6329     2    91  1.1926    0.3081    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: Score ~ Condition * Time + (1 | ID)
##    Data: df_long
## 
## REML criterion at convergence: 539
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.14714 -0.42083  0.07508  0.57099  2.44408 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  ID       (Intercept) 0.6964   0.8345  
##  Residual             0.5307   0.7285  
## Number of obs: 188, groups:  ID, 94
## 
## Fixed effects:
##                           Estimate Std. Error       df t value Pr(>|t|)    
## (Intercept)                 3.1000     0.2022 137.6606  15.328  < 2e-16 ***
## ConditionModel              0.2125     0.2815 137.6606   0.755 0.451634    
## ConditionControl            0.1500     0.2815 137.6606   0.533 0.595011    
## TimePost                    0.7333     0.1881  91.0000   3.899 0.000185 ***
## ConditionModel:TimePost    -0.1083     0.2618  91.0000  -0.414 0.680011    
## ConditionControl:TimePost  -0.3896     0.2618  91.0000  -1.488 0.140206    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) CndtnM CndtnC TimPst CnM:TP
## ConditinMdl -0.718                            
## CondtnCntrl -0.718  0.516                     
## TimePost    -0.465  0.334  0.334              
## CndtnMdl:TP  0.334 -0.465 -0.240 -0.718       
## CndtnCnt:TP  0.334 -0.240 -0.465 -0.718  0.516

# Coherence & Cohesion
run_lme(df, "Coherence & Cohesion", "Pre_CC", "Post_CC")
## 
## 
## ===== LME結果: Coherence & Cohesion =====
## Type III Analysis of Variance Table with Satterthwaite's method
##                 Sum Sq Mean Sq NumDF DenDF F value    Pr(>F)    
## Condition       0.6492  0.3246     2    91  0.7369    0.4814    
## Time           12.4004 12.4004     1    91 28.1522 7.839e-07 ***
## Condition:Time  1.6613  0.8307     2    91  1.8859    0.1576    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: Score ~ Condition * Time + (1 | ID)
##    Data: df_long
## 
## REML criterion at convergence: 519.9
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.15376 -0.47666  0.03972  0.63654  1.76965 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  ID       (Intercept) 0.7186   0.8477  
##  Residual             0.4405   0.6637  
## Number of obs: 188, groups:  ID, 94
## 
## Fixed effects:
##                             Estimate Std. Error         df t value Pr(>|t|)    
## (Intercept)                 3.133333   0.196561 131.467173  15.941  < 2e-16 ***
## ConditionModel              0.085417   0.273602 131.467173   0.312  0.75539    
## ConditionControl           -0.008333   0.273602 131.467173  -0.030  0.97575    
## TimePost                    0.666667   0.171362  91.000000   3.890  0.00019 ***
## ConditionModel:TimePost    -0.041667   0.238526  91.000000  -0.175  0.86172    
## ConditionControl:TimePost  -0.416667   0.238526  91.000000  -1.747  0.08404 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) CndtnM CndtnC TimPst CnM:TP
## ConditinMdl -0.718                            
## CondtnCntrl -0.718  0.516                     
## TimePost    -0.436  0.313  0.313              
## CndtnMdl:TP  0.313 -0.436 -0.225 -0.718       
## CndtnCnt:TP  0.313 -0.225 -0.436 -0.718  0.516

# Lexical Resource
run_lme(df, "Lexical Resource", "Pre_LR", "Post_LR")
## 
## 
## ===== LME結果: Lexical Resource =====
## Type III Analysis of Variance Table with Satterthwaite's method
##                 Sum Sq Mean Sq NumDF DenDF F value    Pr(>F)    
## Condition       0.9444  0.4722     2    91  0.9056    0.4079    
## Time           22.0153 22.0153     1    91 42.2193 4.264e-09 ***
## Condition:Time  0.7607  0.3803     2    91  0.7294    0.4850    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: Score ~ Condition * Time + (1 | ID)
##    Data: df_long
## 
## REML criterion at convergence: 546.6
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.39589 -0.48692 -0.00265  0.59679  1.94321 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  ID       (Intercept) 0.8027   0.8960  
##  Residual             0.5215   0.7221  
## Number of obs: 188, groups:  ID, 94
## 
## Fixed effects:
##                            Estimate Std. Error        df t value Pr(>|t|)    
## (Intercept)                 3.23333    0.21010 133.09007  15.390  < 2e-16 ***
## ConditionModel              0.26667    0.29244 133.09007   0.912    0.363    
## ConditionControl           -0.07708    0.29244 133.09007  -0.264    0.793    
## TimePost                    0.86667    0.18645  91.00000   4.648 1.13e-05 ***
## ConditionModel:TimePost    -0.27292    0.25953  91.00000  -1.052    0.296    
## ConditionControl:TimePost  -0.27292    0.25953  91.00000  -1.052    0.296    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) CndtnM CndtnC TimPst CnM:TP
## ConditinMdl -0.718                            
## CondtnCntrl -0.718  0.516                     
## TimePost    -0.444  0.319  0.319              
## CndtnMdl:TP  0.319 -0.444 -0.229 -0.718       
## CndtnCnt:TP  0.319 -0.229 -0.444 -0.718  0.516

# Grammatical Range & Accuracy
run_lme(df, "Grammatical Range & Accuracy", "Pre_GRA", "Post_GRA")
## 
## 
## ===== LME結果: Grammatical Range & Accuracy =====
## Type III Analysis of Variance Table with Satterthwaite's method
##                 Sum Sq Mean Sq NumDF DenDF F value   Pr(>F)    
## Condition       0.5846  0.2923     2    91  0.6447   0.5272    
## Time           12.9082 12.9082     1    91 28.4726 6.91e-07 ***
## Condition:Time  0.4735  0.2368     2    91  0.5222   0.5950    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: Score ~ Condition * Time + (1 | ID)
##    Data: df_long
## 
## REML criterion at convergence: 519.5
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.06640 -0.44075 -0.06999  0.57978  1.76979 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  ID       (Intercept) 0.6813   0.8254  
##  Residual             0.4534   0.6733  
## Number of obs: 188, groups:  ID, 94
## 
## Fixed effects:
##                            Estimate Std. Error        df t value Pr(>|t|)    
## (Intercept)                 3.06667    0.19448 133.76897  15.768  < 2e-16 ***
## ConditionModel              0.27708    0.27071 133.76897   1.024 0.307894    
## ConditionControl            0.02708    0.27071 133.76897   0.100 0.920457    
## TimePost                    0.66667    0.17385  91.00000   3.835 0.000231 ***
## ConditionModel:TimePost    -0.19792    0.24199  91.00000  -0.818 0.415563    
## ConditionControl:TimePost  -0.22917    0.24199  91.00000  -0.947 0.346138    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) CndtnM CndtnC TimPst CnM:TP
## ConditinMdl -0.718                            
## CondtnCntrl -0.718  0.516                     
## TimePost    -0.447  0.321  0.321              
## CndtnMdl:TP  0.321 -0.447 -0.231 -0.718       
## CndtnCnt:TP  0.321 -0.231 -0.447 -0.718  0.516

# Total
run_lme(df, "Total Score", "Pre_Total", "Post_Total")
## 
## 
## ===== LME結果: Total Score =====
## Type III Analysis of Variance Table with Satterthwaite's method
##                 Sum Sq Mean Sq NumDF DenDF F value    Pr(>F)    
## Condition        8.625   4.312     2    91  0.6507    0.5241    
## Time           246.304 246.304     1    91 37.1629 2.585e-08 ***
## Condition:Time  13.283   6.642     2    91  1.0021    0.3711    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: Score ~ Condition * Time + (1 | ID)
##    Data: df_long
## 
## REML criterion at convergence: 1017
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.26134 -0.42777  0.00483  0.53980  1.99887 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  ID       (Intercept) 11.399   3.376   
##  Residual              6.628   2.574   
## Number of obs: 188, groups:  ID, 94
## 
## Fixed effects:
##                            Estimate Std. Error        df t value Pr(>|t|)    
## (Intercept)                12.53333    0.77516 130.01427  16.169  < 2e-16 ***
## ConditionModel              0.84167    1.07898 130.01427   0.780    0.437    
## ConditionControl            0.09167    1.07898 130.01427   0.085    0.932    
## TimePost                    2.93333    0.66472  91.00000   4.413  2.8e-05 ***
## ConditionModel:TimePost    -0.62083    0.92524  91.00000  -0.671    0.504    
## ConditionControl:TimePost  -1.30833    0.92524  91.00000  -1.414    0.161    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) CndtnM CndtnC TimPst CnM:TP
## ConditinMdl -0.718                            
## CondtnCntrl -0.718  0.516                     
## TimePost    -0.429  0.308  0.308              
## CndtnMdl:TP  0.308 -0.429 -0.221 -0.718       
## CndtnCnt:TP  0.308 -0.221 -0.429 -0.718  0.516

# ==== 単純効果比較関数 ==== #
compare_prepost_by_condition <- function(data, criterion_name, pre_col, post_col) {
  df_long <- data %>%
    select(ID, Condition, !!sym(pre_col), !!sym(post_col)) %>%
    pivot_longer(
      cols = c(!!sym(pre_col), !!sym(post_col)),
      names_to = "Time",
      values_to = "Score"
    ) %>%
    mutate(
      Time = ifelse(Time == pre_col, "Pre", "Post"),
      Time = factor(Time, levels = c("Pre", "Post"))
    )
  
  # LME
  model <- lmer(Score ~ Condition * Time + (1 | ID), data = df_long)
  emm <- emmeans(model, ~ Condition * Time)
  
  cat("\n\n===== 単純効果比較: ", criterion_name, "=====\n")
  # 各群内のPre vs Post
  result <- pairs(emm, by = "Condition", adjust = "tukey")
  print(result)
  
  # 平均とSEを一緒に出す
  cat("\n推定平均(参考):\n")
  print(summary(emm))
}

# ==== 各観点で実行 ==== #

compare_prepost_by_condition(df, "Task Achievement", "Pre_TA", "Post_TA")
## 
## 
## ===== 単純効果比較:  Task Achievement =====
## Condition = AI:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.733 0.188 91  -3.899  0.0002
## 
## Condition = Model:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.625 0.182 91  -3.432  0.0009
## 
## Condition = Control:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.344 0.182 91  -1.887  0.0623
## 
## Degrees-of-freedom method: kenward-roger 
## 
## 推定平均(参考):
##  Condition Time emmean    SE  df lower.CL upper.CL
##  AI        Pre    3.10 0.202 138     2.70     3.50
##  Model     Pre    3.31 0.196 138     2.93     3.70
##  Control   Pre    3.25 0.196 138     2.86     3.64
##  AI        Post   3.83 0.202 138     3.43     4.23
##  Model     Post   3.94 0.196 138     3.55     4.32
##  Control   Post   3.59 0.196 138     3.21     3.98
## 
## Degrees-of-freedom method: kenward-roger 
## Confidence level used: 0.95
compare_prepost_by_condition(df, "Coherence & Cohesion", "Pre_CC", "Post_CC")
## 
## 
## ===== 単純効果比較:  Coherence & Cohesion =====
## Condition = AI:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.667 0.171 91  -3.890  0.0002
## 
## Condition = Model:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.625 0.166 91  -3.767  0.0003
## 
## Condition = Control:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.250 0.166 91  -1.507  0.1353
## 
## Degrees-of-freedom method: kenward-roger 
## 
## 推定平均(参考):
##  Condition Time emmean    SE  df lower.CL upper.CL
##  AI        Pre    3.13 0.197 131     2.74     3.52
##  Model     Pre    3.22 0.190 131     2.84     3.60
##  Control   Pre    3.12 0.190 131     2.75     3.50
##  AI        Post   3.80 0.197 131     3.41     4.19
##  Model     Post   3.84 0.190 131     3.47     4.22
##  Control   Post   3.38 0.190 131     3.00     3.75
## 
## Degrees-of-freedom method: kenward-roger 
## Confidence level used: 0.95
compare_prepost_by_condition(df, "Lexical Resource", "Pre_LR", "Post_LR")
## 
## 
## ===== 単純効果比較:  Lexical Resource =====
## Condition = AI:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.867 0.186 91  -4.648  <.0001
## 
## Condition = Model:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.594 0.181 91  -3.289  0.0014
## 
## Condition = Control:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.594 0.181 91  -3.289  0.0014
## 
## Degrees-of-freedom method: kenward-roger 
## 
## 推定平均(参考):
##  Condition Time emmean    SE  df lower.CL upper.CL
##  AI        Pre    3.23 0.210 133     2.82     3.65
##  Model     Pre    3.50 0.203 133     3.10     3.90
##  Control   Pre    3.16 0.203 133     2.75     3.56
##  AI        Post   4.10 0.210 133     3.68     4.52
##  Model     Post   4.09 0.203 133     3.69     4.50
##  Control   Post   3.75 0.203 133     3.35     4.15
## 
## Degrees-of-freedom method: kenward-roger 
## Confidence level used: 0.95
compare_prepost_by_condition(df, "Grammatical Range & Accuracy", "Pre_GRA", "Post_GRA")
## 
## 
## ===== 単純効果比較:  Grammatical Range & Accuracy =====
## Condition = AI:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.667 0.174 91  -3.835  0.0002
## 
## Condition = Model:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.469 0.168 91  -2.785  0.0065
## 
## Condition = Control:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.438 0.168 91  -2.599  0.0109
## 
## Degrees-of-freedom method: kenward-roger 
## 
## 推定平均(参考):
##  Condition Time emmean    SE  df lower.CL upper.CL
##  AI        Pre    3.07 0.194 134     2.68     3.45
##  Model     Pre    3.34 0.188 134     2.97     3.72
##  Control   Pre    3.09 0.188 134     2.72     3.47
##  AI        Post   3.73 0.194 134     3.35     4.12
##  Model     Post   3.81 0.188 134     3.44     4.18
##  Control   Post   3.53 0.188 134     3.16     3.90
## 
## Degrees-of-freedom method: kenward-roger 
## Confidence level used: 0.95
compare_prepost_by_condition(df, "Total Score", "Pre_Total", "Post_Total")
## 
## 
## ===== 単純効果比較:  Total Score =====
## Condition = AI:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post    -2.93 0.665 91  -4.413  <.0001
## 
## Condition = Model:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post    -2.31 0.644 91  -3.593  0.0005
## 
## Condition = Control:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post    -1.62 0.644 91  -2.525  0.0133
## 
## Degrees-of-freedom method: kenward-roger 
## 
## 推定平均(参考):
##  Condition Time emmean    SE  df lower.CL upper.CL
##  AI        Pre    12.5 0.775 130     11.0     14.1
##  Model     Pre    13.4 0.751 130     11.9     14.9
##  Control   Pre    12.6 0.751 130     11.1     14.1
##  AI        Post   15.5 0.775 130     13.9     17.0
##  Model     Post   15.7 0.751 130     14.2     17.2
##  Control   Post   14.2 0.751 130     12.8     15.7
## 
## Degrees-of-freedom method: kenward-roger 
## Confidence level used: 0.95
# --- データを縦長に変換(Total Score用) ---
df_long <- df %>%
  select(ID, Condition, Pre_Total, Post_Total) %>%
  pivot_longer(
    cols = c(Pre_Total, Post_Total),
    names_to = "Time",
    values_to = "Score"
  ) %>%
  mutate(
    Time = factor(ifelse(Time == "Pre_Total", "Pre", "Post"),
                  levels = c("Pre", "Post")),
    Condition = factor(Condition, levels = c("AI", "Model", "Control"))
  )

# --- LMEモデル構築 ---
model_total <- lmer(Score ~ Condition * Time + (1 | ID), data = df_long)

# --- 効果量算出 ---
eta_sq <- eta_squared(model_total, partial = TRUE)
eta_sq
## # Effect Size for ANOVA (Type III)
## 
## Parameter      | Eta2 (partial) |       95% CI
## ----------------------------------------------
## Condition      |           0.01 | [0.00, 1.00]
## Time           |           0.29 | [0.17, 1.00]
## Condition:Time |           0.02 | [0.00, 1.00]
## 
## - One-sided CIs: upper bound fixed at [1.00].
# ============================================
# 3要因混合効果モデル
# Criterion × Condition × Time + (1 | ID)
# ============================================

# --------------------------------------------
# 縦長に変換(Pre/Post × 4観点)
# --------------------------------------------
df_long <- df %>%
  select(ID, Condition,
         starts_with("Pre_TA"), starts_with("Pre_CC"), starts_with("Pre_LR"), starts_with("Pre_GRA"),
         starts_with("Post_TA"), starts_with("Post_CC"), starts_with("Post_LR"), starts_with("Post_GRA")) %>%
  pivot_longer(
    cols = -c(ID, Condition),
    names_to = c("Time", "Criterion"),
    names_pattern = "(Pre|Post)_(.*)",
    values_to = "Score"
  ) %>%
  mutate(
    Time = factor(Time, levels = c("Pre", "Post")),
    Criterion = factor(Criterion, levels = c("TA", "CC", "LR", "GRA"))
  )

# --------------------------------------------
# 3要因混合効果モデルの構築
# --------------------------------------------
model_3way <- lmer(Score ~ Criterion * Condition * Time + (1 | ID), data = df_long)

# --------------------------------------------
# 結果の出力
# --------------------------------------------
cat("\n===== Type III ANOVA: Criterion × Condition × Time =====\n")
## 
## ===== Type III ANOVA: Criterion × Condition × Time =====
anova(model_3way, type = 3)
## Type III Analysis of Variance Table with Satterthwaite's method
##                          Sum Sq Mean Sq NumDF DenDF  F value    Pr(>F)    
## Criterion                 5.863   1.954     3   637   5.8615  0.000596 ***
## Condition                 0.434   0.217     2    91   0.6507  0.524114    
## Time                     61.576  61.576     1   637 184.6821 < 2.2e-16 ***
## Criterion:Condition       0.840   0.140     6   637   0.4201  0.865799    
## Criterion:Time            0.863   0.288     3   637   0.8628  0.460115    
## Condition:Time            3.321   1.660     2   637   4.9800  0.007144 ** 
## Criterion:Condition:Time  0.840   0.140     6   637   0.4201  0.865799    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# --------------------------------------------
# 効果量(η²)を算出
# --------------------------------------------
eta_sq <- eta_squared(model_3way, partial = TRUE)
cat("\n===== 効果量 (partial η²) =====\n")
## 
## ===== 効果量 (partial η²) =====
print(eta_sq)
## # Effect Size for ANOVA (Type III)
## 
## Parameter                | Eta2 (partial) |       95% CI
## --------------------------------------------------------
## Criterion                |           0.03 | [0.01, 1.00]
## Condition                |           0.01 | [0.00, 1.00]
## Time                     |           0.22 | [0.18, 1.00]
## Criterion:Condition      |       3.94e-03 | [0.00, 1.00]
## Criterion:Time           |       4.05e-03 | [0.00, 1.00]
## Condition:Time           |           0.02 | [0.00, 1.00]
## Criterion:Condition:Time |       3.94e-03 | [0.00, 1.00]
## 
## - One-sided CIs: upper bound fixed at [1.00].
# --------------------------------------------
# 交互作用の推定平均(EMM)と単純効果比較
# --------------------------------------------
emm_3way <- emmeans(model_3way, ~ Criterion * Condition * Time)
cat("\n===== 単純効果比較: 各Criterion内のPre/Post差 =====\n")
## 
## ===== 単純効果比較: 各Criterion内のPre/Post差 =====
pairs(emm_3way, by = c("Criterion", "Condition"))
## Criterion = TA, Condition = AI:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.733 0.149 637  -4.919  <.0001
## 
## Criterion = CC, Condition = AI:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.667 0.149 637  -4.472  <.0001
## 
## Criterion = LR, Condition = AI:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.867 0.149 637  -5.813  <.0001
## 
## Criterion = GRA, Condition = AI:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.667 0.149 637  -4.472  <.0001
## 
## Criterion = TA, Condition = Model:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.625 0.144 637  -4.330  <.0001
## 
## Criterion = CC, Condition = Model:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.625 0.144 637  -4.330  <.0001
## 
## Criterion = LR, Condition = Model:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.594 0.144 637  -4.113  <.0001
## 
## Criterion = GRA, Condition = Model:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.469 0.144 637  -3.247  0.0012
## 
## Criterion = TA, Condition = Control:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.344 0.144 637  -2.381  0.0175
## 
## Criterion = CC, Condition = Control:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.250 0.144 637  -1.732  0.0838
## 
## Criterion = LR, Condition = Control:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.594 0.144 637  -4.113  <.0001
## 
## Criterion = GRA, Condition = Control:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.438 0.144 637  -3.031  0.0025
## 
## Degrees-of-freedom method: kenward-roger
# --------------------------------------------
# 推定平均を確認
# --------------------------------------------
emm_df <- as.data.frame(emm_3way)
head(emm_df)
##  Criterion Condition Time   emmean        SE     df lower.CL upper.CL
##  TA        AI        Pre  3.100000 0.2009374 155.66 2.703084 3.496916
##  CC        AI        Pre  3.133333 0.2009374 155.66 2.736417 3.530249
##  LR        AI        Pre  3.233333 0.2009374 155.66 2.836418 3.630249
##  GRA       AI        Pre  3.066667 0.2009374 155.66 2.669751 3.463582
##  TA        Model     Pre  3.312500 0.1945568 155.66 2.928188 3.696812
##  CC        Model     Pre  3.218750 0.1945568 155.66 2.834438 3.603062
## 
## Degrees-of-freedom method: kenward-roger 
## Confidence level used: 0.95