# ============================================
# 📘 0–100点スコア版:記述統計 & プロット
# ============================================

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
# --------------------------------------------
# 1️⃣ データ読み込み
# --------------------------------------------
setwd("~/Library/CloudStorage/Dropbox/土居_関口/2.Analysis_Doi")

# "writing_results_scored_total.xlsx"(総合スコア)
df <- read_excel("writing_results_scored_total.xlsx")

# 条件を英語化
df <- df %>%
  mutate(
    Condition = case_when(
      条件 == "ai-wcf" ~ "AI",
      条件 == "model text" ~ "Model",
      条件 == "control" ~ "Control",
      TRUE ~ 条件
    ),
    Condition = factor(Condition, levels = c("AI", "Model", "Control"))
  )

# --------------------------------------------
# 2️⃣ 記述統計(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 (0–100) 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 range from 0–100.",
    general_title = " "
  )
## Warning: 'xfun::attr()' is deprecated.
## Use 'xfun::attr2()' instead.
## See help("Deprecated")
Table 1. Descriptive Statistics for Total Scores (0–100) by Condition
Condition n M (Pre) SD (Pre) M (Post) SD (Post)
AI 20 38.65 9.58 44.25 9.50
Model 16 38.12 10.14 45.00 8.56
Control 19 38.16 11.69 40.16 9.70
Note. M = mean; SD = standard deviation; Scores range from 0–100.
# --------------------------------------------
# 3️⃣ 縦長化してプロット
# --------------------------------------------
df_long_total <- df %>%
  select(Condition, Pre_Total, Post_Total) %>%
  pivot_longer(
    cols = c(Pre_Total, Post_Total),
    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 Writing Scores by Condition",
    x = "Test Time", y = "Total Score (0–100)", 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")
  )

# --------------------------------------------
# 4️⃣ 改善量(Gain)を追加(オプション)
# --------------------------------------------
df_gain <- df %>%
  mutate(Gain = Post_Total - Pre_Total) %>%
  group_by(Condition) %>%
  summarise(
    n = n(),
    Mean_Gain = mean(Gain, na.rm = TRUE),
    SD_Gain = sd(Gain, na.rm = TRUE)
  )

df_gain %>%
  mutate(
    Mean_Gain = sprintf("%.2f", Mean_Gain),
    SD_Gain = sprintf("%.2f", SD_Gain)
  ) %>%
  kbl(
    caption = "Table 2. Improvement (Gain = Post – Pre) in Total Scores (0–100) by Condition",
    align = "lccc",
    col.names = c("Condition", "n", "Mean Gain", "SD")
  ) %>%
  kable_classic(full_width = FALSE, html_font = "Times New Roman") %>%
  footnote(
    general = "Note. Gain = Post-Test score minus Pre-Test score.",
    general_title = " "
  )
## Warning: 'xfun::attr()' is deprecated.
## Use 'xfun::attr2()' instead.
## See help("Deprecated")
Table 2. Improvement (Gain = Post – Pre) in Total Scores (0–100) by Condition
Condition n Mean Gain SD
AI 20 5.60 10.25
Model 16 6.88 7.93
Control 19 2.00 7.09
Note. Gain = Post-Test score minus Pre-Test score.