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(kableExtra)
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
# ディレクトリ設定とデータ読み込み
setwd("~/Library/CloudStorage/Dropbox/土居_関口/2.Analysis_Doi")
df <- read_excel("writing_results_scored_4criteria.xlsx")

# 条件名を英語化
df <- df %>%
  mutate(
    Condition = case_when(
      条件 == "ai-wcf" ~ "AI",
      条件 == "model text" ~ "Model",
      条件 == "control" ~ "Control",
      TRUE ~ 条件
    )
  )
# Conditionの順序をAI→Model→Controlに固定
df <- df %>%
  mutate(Condition = factor(Condition, levels = c("AI", "Model", "Control")))
# 記述統計(Pre/Post別)
desc_total <- df %>%
  group_by(Condition) %>%
  summarise(
    n = n(),
    Mean_Pre  = mean(Pre_Total, na.rm = TRUE),
    SD_Pre    = sd(Pre_Total, na.rm = TRUE),
    Mean_Post = mean(Post_Total, na.rm = TRUE),
    SD_Post   = sd(Post_Total, na.rm = TRUE)
  )

# 表として整形
desc_total %>%
  mutate(
    Mean_Pre  = sprintf("%.2f", Mean_Pre),
    SD_Pre    = sprintf("%.2f", SD_Pre),
    Mean_Post = sprintf("%.2f", Mean_Post),
    SD_Post   = sprintf("%.2f", SD_Post)
  ) %>%
  kbl(
    caption = "Table 1. Descriptive Statistics for Total Scores by Condition",
    align = "lcccc",
    col.names = c("Condition", "n", "M (Pre)", "SD (Pre)", "M (Post)", "SD (Post)")
  ) %>%
  kable_classic(full_width = FALSE, html_font = "Times New Roman") %>%
  footnote(
    general = "Note. M = mean; SD = standard deviation; Scores are totals across four writing dimensions (0–36).",
    general_title = " "
  )
## Warning: 'xfun::attr()' is deprecated.
## Use 'xfun::attr2()' instead.
## See help("Deprecated")
Table 1. Descriptive Statistics for Total Scores by Condition
Condition n M (Pre) SD (Pre) M (Post) SD (Post)
AI 20 14.80 3.40 17.15 2.80
Model 16 14.44 4.52 17.19 2.48
Control 19 14.21 3.74 16.00 2.92
Note. M = mean; SD = standard deviation; Scores are totals across four writing dimensions (0–36).
# 縦長化
df_long_total <- df %>%
  select(Condition, Pre_Total, Post_Total) %>%
  pivot_longer(cols = starts_with("Pre_") | starts_with("Post_"),
               names_to = "Time", values_to = "Score") %>%
  mutate(Time = factor(Time, levels = c("Pre_Total", "Post_Total"),
                       labels = c("Pre-Test", "Post-Test")))

# 色設定
fill_colors <- c("Pre-Test" = "#1f78b4", "Post-Test" = "#e31a1c")

# プロット
ggplot(df_long_total, aes(x = Time, y = Score, fill = Time)) +
  geom_violin(trim = FALSE, alpha = 0.65, width = 0.8, color = "gray40") +
  geom_boxplot(width = 0.15, outlier.shape = NA, color = "gray20", alpha = 0.7) +
  stat_summary(fun = mean, geom = "point", shape = 8, color = "yellow", size = 2) +
  facet_wrap(~Condition, nrow = 1) +
  scale_fill_manual(values = fill_colors) +
  labs(
    title = "Distribution of Total Scores by Condition",
    x = "Test Time", y = "Total Score (0–36)", fill = "Test Type"
  ) +
  theme_minimal(base_size = 15) +
  theme(
    plot.title = element_text(hjust = 0.5, face = "bold"),
    axis.title = element_text(face = "bold"),
    legend.position = "top",
    strip.text = element_text(size = 13, face = "bold")
  )

# 観点を一括処理
criteria <- c("TA", "CC", "LR", "GRA")

desc_list <- lapply(criteria, function(crit) {
  df %>%
    group_by(Condition) %>%
    summarise(
      Mean_Pre  = mean(get(paste0("Pre_", crit)), na.rm = TRUE),
      SD_Pre    = sd(get(paste0("Pre_", crit)), na.rm = TRUE),
      Mean_Post = mean(get(paste0("Post_", crit)), na.rm = TRUE),
      SD_Post   = sd(get(paste0("Post_", crit)), na.rm = TRUE)
    ) %>%
    mutate(Criterion = crit)
})

desc_each <- bind_rows(desc_list)

# 出力
desc_each %>%
  select(Criterion, Condition, Mean_Pre, SD_Pre, Mean_Post, SD_Post) %>%
  mutate(across(starts_with("Mean"), ~ sprintf("%.2f", .)),
         across(starts_with("SD"), ~ sprintf("%.2f", .))) %>%
  kbl(
    caption = "Table 2. Descriptive Statistics for Each Writing Criterion by Condition",
    align = "lccccc"
  ) %>%
  kable_classic(full_width = FALSE, html_font = "Times New Roman")
## Warning: 'xfun::attr()' is deprecated.
## Use 'xfun::attr2()' instead.
## See help("Deprecated")
Table 2. Descriptive Statistics for Each Writing Criterion by Condition
Criterion Condition Mean_Pre SD_Pre Mean_Post SD_Post
TA AI 3.70 0.92 4.40 0.82
TA Model 3.50 1.15 4.25 0.77
TA Control 3.68 1.06 4.00 0.88
CC AI 3.65 0.93 4.25 0.79
CC Model 3.56 1.15 4.25 0.86
CC Control 3.47 1.07 3.95 0.78
LR AI 3.90 0.97 4.40 0.75
LR Model 3.81 1.17 4.44 0.63
LR Control 3.58 1.02 4.16 0.76
GRA AI 3.55 0.76 4.10 0.72
GRA Model 3.56 1.15 4.25 0.58
GRA Control 3.47 0.90 3.89 0.74
# ======== 各観点別・条件別バイオリンプロット(Viewer表示用 完全版)======== #

library(dplyr)
library(tidyr)
library(ggplot2)

# --- データ縦長化 ---
df_long_criteria <- df %>%
  select(
    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 = -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")),
    Condition = factor(Condition, levels = c("AI", "Model", "Control"))  # ← 順序固定
  )

# --- 色指定(Pre=青, Post=赤)---
fill_colors <- c("Pre" = "#1f78b4", "Post" = "#e31a1c")

# --- プロット ---
ggplot(df_long_criteria, aes(x = Time, y = Score, fill = Time)) +
  geom_violin(trim = FALSE, alpha = 0.65, width = 0.8, color = "gray40") +
  geom_boxplot(width = 0.15, outlier.shape = NA, color = "gray20", alpha = 0.7) +
  stat_summary(fun = mean, geom = "point", shape = 8, color = "yellow", size = 2) +
  facet_wrap(~ Criterion + Condition, ncol = 3, labeller = label_value) +
  scale_fill_manual(values = fill_colors) +
  labs(
    title = "Distribution of Writing Scores by Criterion and Condition",
    x = "Test Time", y = "Score (0–9)", fill = "Test Type"
  ) +
  theme_minimal(base_size = 14) +
  theme(
    plot.title = element_text(hjust = 0.5, face = "bold", size = 16),
    axis.title = element_text(size = 13, face = "bold"),
    axis.text.x = element_text(size = 11),
    axis.text.y = element_text(size = 11),
    legend.position = "top",
    legend.title = element_text(face = "bold"),
    legend.text = element_text(size = 11),
    strip.text = element_text(size = 12, face = "bold"),
    panel.spacing = unit(1, "lines")
  )