library(rstatix)
## 
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
## 
##     filter
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(gt)
## Warning: package 'gt' was built under R version 4.3.3

正規性の検定(Shapiro-Wilk test)

#library(dplyr)

# CSV読み込み
df <- read.csv("~/Library/CloudStorage/Dropbox/土居_関口/2.Analysis_Doi/writing_results_scored.csv")

# データ確認(任意)
# head(df)

# 数値型へ明示的に変換(万一文字扱いだった場合に備えて)
df <- df %>%
  mutate(
    Pre_Score = as.numeric(Pre_Score),
    Post_Score = as.numeric(Post_Score)
  )

# 差分の作成
df <- df %>%
  mutate(Diff = Post_Score - Pre_Score)

# 条件ごとのShapiro–Wilk検定
normality_results <- df %>%
  group_by(条件) %>%
  summarise(
    p_value = shapiro.test(Diff)$p.value
  )

print(normality_results)
## # A tibble: 3 × 2
##   条件         p_value
##   <chr>          <dbl>
## 1 ai-wcf     0.00405  
## 2 control    0.0000135
## 3 model text 0.00355

ノンパラメトリック検定(Wilcoxon signed-rank test)

results <- df %>%
  group_by(条件) %>%
  summarise(
    Shapiro_p = shapiro.test(Post_Score - Pre_Score)$p.value,
    Test = "Wilcoxon signed-rank test",
    p_value = wilcox.test(Pre_Score, Post_Score, paired = TRUE)$p.value
  )
## Warning: There were 6 warnings in `summarise()`.
## The first warning was:
## ℹ In argument: `p_value = wilcox.test(Pre_Score, Post_Score, paired =
##   TRUE)$p.value`.
## ℹ In group 1: `条件 = "ai-wcf"`.
## Caused by warning in `wilcox.test.default()`:
## ! cannot compute exact p-value with ties
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 5 remaining warnings.
print(results)
## # A tibble: 3 × 4
##   条件       Shapiro_p Test                      p_value
##   <chr>          <dbl> <chr>                       <dbl>
## 1 ai-wcf     0.00405   Wilcoxon signed-rank test 0.0150 
## 2 control    0.0000135 Wilcoxon signed-rank test 0.0305 
## 3 model text 0.00355   Wilcoxon signed-rank test 0.00786
# データを縦長に変換
df_long <- df %>%
  pivot_longer(cols = c(Pre_Score, Post_Score),
               names_to = "Time",
               values_to = "Score")

# Wilcoxon検定+効果量
effect_results <- df_long %>%
  group_by(条件) %>%
  wilcox_test(Score ~ Time, paired = TRUE, detailed = TRUE) %>%
  mutate(effect_size_r = abs(statistic / sqrt(nrow(df_long[df_long$条件 == unique(条件), ]))))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `effect_size_r = abs(...)`.
## Caused by warning in `df_long$条件 == unique(条件)`:
## ! longer object length is not a multiple of shorter object length
effect_results
## # A tibble: 3 × 14
##   条件       estimate .y.   group1 group2    n1    n2 statistic       p conf.low
##   <chr>         <dbl> <chr> <chr>  <chr>  <int> <int>     <dbl>   <dbl>    <dbl>
## 1 ai-wcf         1.00 Score Post_… Pre_S…    20    20      50.5 0.015      0.500
## 2 control        1.50 Score Post_… Pre_S…    19    19      21   0.0305     1    
## 3 model text     1.50 Score Post_… Pre_S…    16    16      45   0.00786    1    
## # ℹ 4 more variables: conf.high <dbl>, method <chr>, alternative <chr>,
## #   effect_size_r <dbl>

効果量(effect size)

# 縦長データが df_long として既にある前提

# 効果量 r を条件ごとに計算
effect_sizes <- df_long %>%
  group_by(条件) %>%
  wilcox_effsize(Score ~ Time, paired = TRUE)

effect_sizes
## # A tibble: 3 × 8
##   .y.   group1     group2    effsize 条件          n1    n2 magnitude
## * <chr> <chr>      <chr>       <dbl> <chr>      <int> <int> <ord>    
## 1 Score Post_Score Pre_Score   0.570 ai-wcf        20    20 large    
## 2 Score Post_Score Pre_Score   0.560 control       19    19 large    
## 3 Score Post_Score Pre_Score   0.736 model text    16    16 large
final_results <- df_long %>%
group_by(条件) %>%
wilcox_test(Score ~ Time, paired = TRUE, detailed = TRUE) %>%
left_join(
df_long %>%
group_by(条件) %>%
wilcox_effsize(Score ~ Time, paired = TRUE),
by = "条件"
) %>%
select(条件, p, effsize, magnitude) %>%
rename(
`p値` = p,
`効果量 r` = effsize,
`効果の大きさ` = magnitude
) %>%
mutate(
判定 = case_when(
`p値` < 0.001 ~ "***",
`p値` < 0.01  ~ "**",
`p値` < 0.05  ~ "*",
TRUE ~ "n.s."
)
)

final_results %>%
  gt() %>%
  fmt_number(columns = c(`p値`, `効果量 r`), decimals = 3) %>%
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_body(columns = vars(判定))
  )
## Warning: Since gt v0.3.0, `columns = vars(...)` has been deprecated.
## • Please use `columns = c(...)` instead.
条件 p値 効果量 r 効果の大きさ 判定
ai-wcf 0.015 0.570 large *
control 0.030 0.560 large *
model text 0.008 0.736 large **