# ======== ライブラリ ======== #
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.2619  0.1310     2    52  0.3465    0.7088    
## Time           9.4410  9.4410     1    52 24.9805 6.935e-06 ***
## Condition:Time 1.0383  0.5191     2    52  1.3736    0.2622    
## ---
## 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: 279.2
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -1.84394 -0.55484 -0.04545  0.54726  2.06919 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  ID       (Intercept) 0.5077   0.7125  
##  Residual             0.3779   0.6148  
## Number of obs: 110, groups:  ID, 55
## 
## Fixed effects:
##                           Estimate Std. Error       df t value Pr(>|t|)    
## (Intercept)                3.70000    0.21043 78.27649  17.583  < 2e-16 ***
## ConditionModel            -0.20000    0.31565 78.27649  -0.634 0.528177    
## ConditionControl          -0.01579    0.30149 78.27649  -0.052 0.958365    
## TimePost                   0.70000    0.19441 52.00000   3.601 0.000708 ***
## ConditionModel:TimePost    0.05000    0.29161 52.00000   0.171 0.864525    
## ConditionControl:TimePost -0.38421    0.27853 52.00000  -1.379 0.173661    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) CndtnM CndtnC TimPst CnM:TP
## ConditinMdl -0.667                            
## CondtnCntrl -0.698  0.465                     
## TimePost    -0.462  0.308  0.322              
## CndtnMdl:TP  0.308 -0.462 -0.215 -0.667       
## CndtnCnt:TP  0.322 -0.215 -0.462 -0.698  0.465

# 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.3378  0.1689     2    52  0.4507    0.6397    
## Time           9.3918  9.3918     1    52 25.0613 6.741e-06 ***
## Condition:Time 0.2037  0.1019     2    52  0.2718    0.7631    
## ---
## 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: 278
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.16077 -0.46778  0.00935  0.51884  1.75971 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  ID       (Intercept) 0.4995   0.7067  
##  Residual             0.3748   0.6122  
## Number of obs: 110, groups:  ID, 55
## 
## Fixed effects:
##                           Estimate Std. Error      df t value Pr(>|t|)    
## (Intercept)                 3.6500     0.2091 78.4059  17.458  < 2e-16 ***
## ConditionModel             -0.0875     0.3136 78.4059  -0.279  0.78097    
## ConditionControl           -0.1763     0.2995 78.4059  -0.589  0.55781    
## TimePost                    0.6000     0.1936 52.0000   3.099  0.00313 ** 
## ConditionModel:TimePost     0.0875     0.2904 52.0000   0.301  0.76436    
## ConditionControl:TimePost  -0.1263     0.2773 52.0000  -0.455  0.65069    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) CndtnM CndtnC TimPst CnM:TP
## ConditinMdl -0.667                            
## CondtnCntrl -0.698  0.465                     
## TimePost    -0.463  0.309  0.323              
## CndtnMdl:TP  0.309 -0.463 -0.215 -0.667       
## CndtnCnt:TP  0.323 -0.215 -0.463 -0.698  0.465

# 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.5970  0.2985     2    52  0.7502    0.4773    
## Time           8.7913  8.7913     1    52 22.0942 1.946e-05 ***
## Condition:Time 0.0728  0.0364     2    52  0.0915    0.9127    
## ---
## 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: 275
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.72272 -0.51164  0.04315  0.52693  1.64557 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  ID       (Intercept) 0.4111   0.6411  
##  Residual             0.3979   0.6308  
## Number of obs: 110, groups:  ID, 55
## 
## Fixed effects:
##                           Estimate Std. Error       df t value Pr(>|t|)    
## (Intercept)                3.90000    0.20112 82.65733  19.392   <2e-16 ***
## ConditionModel            -0.08750    0.30168 82.65733  -0.290   0.7725    
## ConditionControl          -0.32105    0.28814 82.65733  -1.114   0.2684    
## TimePost                   0.50000    0.19947 52.00000   2.507   0.0154 *  
## ConditionModel:TimePost    0.12500    0.29921 52.00000   0.418   0.6778    
## ConditionControl:TimePost  0.07895    0.28579 52.00000   0.276   0.7835    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) CndtnM CndtnC TimPst CnM:TP
## ConditinMdl -0.667                            
## CondtnCntrl -0.698  0.465                     
## TimePost    -0.496  0.331  0.346              
## CndtnMdl:TP  0.331 -0.496 -0.231 -0.667       
## CndtnCnt:TP  0.346 -0.231 -0.496 -0.698  0.465

# 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.3696  0.1848     2    52  0.4685    0.6285    
## Time           8.3291  8.3291     1    52 21.1177 2.786e-05 ***
## Condition:Time 0.3086  0.1543     2    52  0.3913    0.6782    
## ---
## 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: 261.8
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.76504 -0.57180  0.07507  0.55286  1.73361 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  ID       (Intercept) 0.2807   0.5298  
##  Residual             0.3944   0.6280  
## Number of obs: 110, groups:  ID, 55
## 
## Fixed effects:
##                           Estimate Std. Error       df t value Pr(>|t|)    
## (Intercept)                3.55000    0.18373 88.67004  19.322  < 2e-16 ***
## ConditionModel             0.01250    0.27559 88.67004   0.045  0.96393    
## ConditionControl          -0.07632    0.26323 88.67004  -0.290  0.77255    
## TimePost                   0.55000    0.19860 52.00000   2.769  0.00777 ** 
## ConditionModel:TimePost    0.13750    0.29790 52.00000   0.462  0.64632    
## ConditionControl:TimePost -0.12895    0.28453 52.00000  -0.453  0.65230    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) CndtnM CndtnC TimPst CnM:TP
## ConditinMdl -0.667                            
## CondtnCntrl -0.698  0.465                     
## TimePost    -0.540  0.360  0.377              
## CndtnMdl:TP  0.360 -0.540 -0.251 -0.667       
## CndtnCnt:TP  0.377 -0.251 -0.540 -0.698  0.465

# 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        4.009   2.005     2    52  0.4506    0.6397    
## Time           143.718 143.718     1    52 32.3027 6.057e-07 ***
## Condition:Time   4.101   2.050     2    52  0.4608    0.6333    
## ---
## 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: 540.7
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.51356 -0.41599 -0.02121  0.52255  1.85533 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  ID       (Intercept) 6.813    2.610   
##  Residual             4.449    2.109   
## Number of obs: 110, groups:  ID, 55
## 
## Fixed effects:
##                           Estimate Std. Error      df t value Pr(>|t|)    
## (Intercept)                14.8000     0.7504 76.1363  19.723  < 2e-16 ***
## ConditionModel             -0.3625     1.1256 76.1363  -0.322 0.748301    
## ConditionControl           -0.5895     1.0751 76.1363  -0.548 0.585097    
## TimePost                    2.3500     0.6670 52.0000   3.523 0.000898 ***
## ConditionModel:TimePost     0.4000     1.0005 52.0000   0.400 0.690949    
## ConditionControl:TimePost  -0.5605     0.9556 52.0000  -0.587 0.560045    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) CndtnM CndtnC TimPst CnM:TP
## ConditinMdl -0.667                            
## CondtnCntrl -0.698  0.465                     
## TimePost    -0.444  0.296  0.310              
## CndtnMdl:TP  0.296 -0.444 -0.207 -0.667       
## CndtnCnt:TP  0.310 -0.207 -0.444 -0.698  0.465

# ==== 単純効果比較関数 ==== #
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.700 0.194 52  -3.601  0.0007
## 
## Condition = Model:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.750 0.217 52  -3.451  0.0011
## 
## Condition = Control:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.316 0.199 52  -1.583  0.1194
## 
## Degrees-of-freedom method: kenward-roger 
## 
## 推定平均(参考):
##  Condition Time emmean    SE   df lower.CL upper.CL
##  AI        Pre    3.70 0.210 78.3     3.28     4.12
##  Model     Pre    3.50 0.235 78.3     3.03     3.97
##  Control   Pre    3.68 0.216 78.3     3.25     4.11
##  AI        Post   4.40 0.210 78.3     3.98     4.82
##  Model     Post   4.25 0.235 78.3     3.78     4.72
##  Control   Post   4.00 0.216 78.3     3.57     4.43
## 
## 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.600 0.194 52  -3.099  0.0031
## 
## Condition = Model:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.688 0.216 52  -3.176  0.0025
## 
## Condition = Control:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.474 0.199 52  -2.385  0.0208
## 
## Degrees-of-freedom method: kenward-roger 
## 
## 推定平均(参考):
##  Condition Time emmean    SE   df lower.CL upper.CL
##  AI        Pre    3.65 0.209 78.4     3.23     4.07
##  Model     Pre    3.56 0.234 78.4     3.10     4.03
##  Control   Pre    3.47 0.215 78.4     3.05     3.90
##  AI        Post   4.25 0.209 78.4     3.83     4.67
##  Model     Post   4.25 0.234 78.4     3.78     4.72
##  Control   Post   3.95 0.215 78.4     3.52     4.37
## 
## 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.500 0.199 52  -2.507  0.0154
## 
## Condition = Model:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.625 0.223 52  -2.802  0.0071
## 
## Condition = Control:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.579 0.205 52  -2.829  0.0066
## 
## Degrees-of-freedom method: kenward-roger 
## 
## 推定平均(参考):
##  Condition Time emmean    SE   df lower.CL upper.CL
##  AI        Pre    3.90 0.201 82.7     3.50     4.30
##  Model     Pre    3.81 0.225 82.7     3.37     4.26
##  Control   Pre    3.58 0.206 82.7     3.17     3.99
##  AI        Post   4.40 0.201 82.7     4.00     4.80
##  Model     Post   4.44 0.225 82.7     3.99     4.88
##  Control   Post   4.16 0.206 82.7     3.75     4.57
## 
## 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.550 0.199 52  -2.769  0.0078
## 
## Condition = Model:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.688 0.222 52  -3.096  0.0032
## 
## Condition = Control:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post   -0.421 0.204 52  -2.066  0.0438
## 
## Degrees-of-freedom method: kenward-roger 
## 
## 推定平均(参考):
##  Condition Time emmean    SE   df lower.CL upper.CL
##  AI        Pre    3.55 0.184 88.7     3.18     3.92
##  Model     Pre    3.56 0.205 88.7     3.15     3.97
##  Control   Pre    3.47 0.189 88.7     3.10     3.85
##  AI        Post   4.10 0.184 88.7     3.73     4.47
##  Model     Post   4.25 0.205 88.7     3.84     4.66
##  Control   Post   3.89 0.189 88.7     3.52     4.27
## 
## 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.35 0.667 52  -3.523  0.0009
## 
## Condition = Model:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post    -2.75 0.746 52  -3.688  0.0005
## 
## Condition = Control:
##  contrast   estimate    SE df t.ratio p.value
##  Pre - Post    -1.79 0.684 52  -2.615  0.0117
## 
## Degrees-of-freedom method: kenward-roger 
## 
## 推定平均(参考):
##  Condition Time emmean    SE   df lower.CL upper.CL
##  AI        Pre    14.8 0.750 76.1     13.3     16.3
##  Model     Pre    14.4 0.839 76.1     12.8     16.1
##  Control   Pre    14.2 0.770 76.1     12.7     15.7
##  AI        Post   17.1 0.750 76.1     15.7     18.6
##  Model     Post   17.2 0.839 76.1     15.5     18.9
##  Control   Post   16.0 0.770 76.1     14.5     17.5
## 
## 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.02 | [0.00, 1.00]
## Time           |           0.38 | [0.22, 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                 3.605   1.202     3   364   4.2724 0.00555 ** 
## Condition                 0.253   0.127     2    52   0.4506 0.63972    
## Time                     35.930  35.930     1   364 127.7290 < 2e-16 ***
## Criterion:Condition       0.858   0.143     6   364   0.5082 0.80213    
## Criterion:Time            0.024   0.008     3   364   0.0280 0.99367    
## Condition:Time            1.025   0.513     2   364   1.8222 0.16314    
## Criterion:Condition:Time  0.598   0.100     6   364   0.3545 0.90711    
## ---
## 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.02 | [0.00, 1.00]
## Time                     |           0.26 | [0.20, 1.00]
## Criterion:Condition      |       8.31e-03 | [0.00, 1.00]
## Criterion:Time           |       2.31e-04 | [0.00, 1.00]
## Condition:Time           |       9.91e-03 | [0.00, 1.00]
## Criterion:Condition:Time |       5.81e-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.700 0.168 364  -4.174  <.0001
## 
## Criterion = CC, Condition = AI:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.600 0.168 364  -3.577  0.0004
## 
## Criterion = LR, Condition = AI:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.500 0.168 364  -2.981  0.0031
## 
## Criterion = GRA, Condition = AI:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.550 0.168 364  -3.279  0.0011
## 
## Criterion = TA, Condition = Model:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.750 0.188 364  -4.000  0.0001
## 
## Criterion = CC, Condition = Model:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.688 0.188 364  -3.666  0.0003
## 
## Criterion = LR, Condition = Model:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.625 0.188 364  -3.333  0.0009
## 
## Criterion = GRA, Condition = Model:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.688 0.188 364  -3.666  0.0003
## 
## Criterion = TA, Condition = Control:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.316 0.172 364  -1.835  0.0673
## 
## Criterion = CC, Condition = Control:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.474 0.172 364  -2.753  0.0062
## 
## Criterion = LR, Condition = Control:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.579 0.172 364  -3.364  0.0008
## 
## Criterion = GRA, Condition = Control:
##  contrast   estimate    SE  df t.ratio p.value
##  Pre - Post   -0.421 0.172 364  -2.447  0.0149
## 
## 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.7000 0.2013696 104.36 3.300693 4.099307
##  CC        AI        Pre  3.6500 0.2013696 104.36 3.250693 4.049307
##  LR        AI        Pre  3.9000 0.2013696 104.36 3.500693 4.299307
##  GRA       AI        Pre  3.5500 0.2013696 104.36 3.150693 3.949307
##  TA        Model     Pre  3.5000 0.2251381 104.36 3.053561 3.946439
##  CC        Model     Pre  3.5625 0.2251381 104.36 3.116061 4.008939
## 
## Degrees-of-freedom method: kenward-roger 
## Confidence level used: 0.95