# 載入套件
library(readr)
library(dplyr)
library(tidyr)
library(lavaan)
library(car)
library(glue)
library(ggplot2)
library(knitr)

資料前處理

###資料前處理
#計算量表平均
df$C_AVG <- rowMeans(df[, c("C1", "C2", "C3", "C4", "C5")], na.rm = TRUE)
df$N_AVG <- rowMeans(df[, c("N1", "N2", "N3", "N4", "N52", "N7", "N92", "N12")], na.rm = TRUE)
df$abcon <- as.factor(df$abcon)
df$wwo   <- as.factor(df$wwo)
#因子轉換
Manipulation1 <- "Column18"
Manipulation2 <- "Column21"

# 重新命名人口統計變項與控制變項
# Column40 -> Control (控制變項)
# Column45 -> Gender (性別)
# Column46 -> Age (年齡)
# Column47 -> Education (教育程度)
# Column48 -> Background (背景)
mapping <- c(
  "Control" = "Column40",
  "Gender" = "Column45", 
  "Age" = "Column46", 
  "Education" = "Column47", 
  "Background" = "Column48"
)
existing_cols <- mapping[mapping %in% names(df)]
if(length(existing_cols) > 0) {
  df <- df %>% rename(!!!existing_cols)
}

IV1 <- "abcon"
IV2 <- "wwo"

MED1 <- "N_AVG"
MED2 <- "C_AVG"

#DVs <- c("罪名2", "判決信心", "刑期2")
continuous_DVs <- c("判決信心", "刑期2")

描述性統計

樣本背景分析

以下呈現樣本的人口統計變項分佈情形。

性別分佈

if("Column45" %in% names(df)) {
  # 假設 Column45 是性別
  gender_tbl <- table(df$Column45)
  gender_prop <- round(prop.table(gender_tbl) * 100, 2)
  
  kable(cbind(人數 = gender_tbl, 百分比 = gender_prop), caption = "性別分佈統計")
}

教育程度分佈

if("Column47" %in% names(df)) {
  # 假設 Column47 是教育程度
  edu_tbl <- table(df$Column47)
  edu_prop <- round(prop.table(edu_tbl) * 100, 2)
  
  kable(cbind(人數 = edu_tbl, 百分比 = edu_prop), caption = "教育程度分佈統計")
}

年齡統計

if("Column46" %in% names(df) && is.numeric(df$Column46)) {
  age_stats <- data.frame(
    指標 = c("平均年齡 (Mean)", "標準差 (SD)", "最小值 (Min)", "最大值 (Max)"),
    數值 = c(
      round(mean(df$Column46, na.rm=TRUE), 2),
      round(sd(df$Column46, na.rm=TRUE), 2),
      min(df$Column46, na.rm=TRUE),
      max(df$Column46, na.rm=TRUE)
    )
  )
  kable(age_stats, caption = "年齡描述性統計")
}

各實驗組別摘要表

下表顯示在不同實驗情境(語言具體性 × 品格證據)下,依變項(判決信心、刑期)與中介變項(信賴感、敘事傳輸)的平均數與標準差。

group_stats <- df %>%
  group_by(abcon, wwo) %>%
  summarise(
    N = n(),
    信心_M = round(mean(判決信心, na.rm = TRUE), 2),
    信心_SD = round(sd(判決信心, na.rm = TRUE), 2),
    刑期_M = round(mean(刑期2, na.rm = TRUE), 2),
    刑期_SD = round(sd(刑期2, na.rm = TRUE), 2),
    信賴_M = round(mean(N_AVG, na.rm = TRUE), 2),
    信賴_SD = round(sd(N_AVG, na.rm = TRUE), 2),
    傳輸_M = round(mean(C_AVG, na.rm = TRUE), 2),
    傳輸_SD = round(sd(C_AVG, na.rm = TRUE), 2),
    .groups = 'drop'
  )

kable(group_stats, caption = "各實驗組別之依變項與中介變項摘要 (abcon: 語言具體性, wwo: 品格證據)")
各實驗組別之依變項與中介變項摘要 (abcon: 語言具體性, wwo: 品格證據)
abcon wwo N 信心_M 信心_SD 刑期_M 刑期_SD 信賴_M 信賴_SD 傳輸_M 傳輸_SD
0 0 27 3.19 1.27 1.37 0.56 3.82 1.11 3.59 0.97
0 1 34 4.09 1.73 1.53 0.61 4.23 1.14 3.51 1.16
1 0 31 3.71 1.66 1.32 0.54 4.79 0.82 3.76 1.06
1 1 40 3.88 1.71 1.48 0.55 4.77 0.74 3.32 0.93

說明: abcon (1=具體, 0=抽象), wwo (1=有攻擊, 0=無攻擊)

anova

for (DV in continuous_DVs) {
  cat("===========================\n")
  cat(DV, "的 2x2 ANOVA \n")
  cat("===========================\n")
  formula <- as.formula(paste(DV, "~", IV1, "*", IV2))
  fit <- lm(formula, data = df)
  print(Anova(fit, type = 3))
  cat("\n")
}
## ===========================
## 判決信心 的 2x2 ANOVA 
## ===========================
## Anova Table (Type III tests)
## 
## Response: 判決信心
##             Sum Sq  Df  F value  Pr(>F)    
## (Intercept) 273.93   1 103.8670 < 2e-16 ***
## abcon         3.97   1   1.5053 0.22211    
## wwo          12.27   1   4.6535 0.03286 *  
## abcon:wwo     4.40   1   1.6682 0.19883    
## Residuals   337.57 128                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## ===========================
## 刑期2 的 2x2 ANOVA 
## ===========================
## Anova Table (Type III tests)
## 
## Response: 刑期2
##             Sum Sq  Df  F value Pr(>F)    
## (Intercept) 50.704   1 156.3268 <2e-16 ***
## abcon        0.033   1   0.1016 0.7504    
## wwo          0.381   1   1.1736 0.2807    
## abcon:wwo    0.000   1   0.0011 0.9737    
## Residuals   41.516 128                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# glm 代表 Generalized Linear Model (廣義線性模型)
# family = binomial() 指定了這是二元分布,要使用 Logit 連結函數
fit_logit <- glm(罪名2 ~ abcon * wwo, data = df, family = binomial())

# Anova(type = 3) 會輸出類似 ANOVA 的表格,告訴您主效應和交互作用是否顯著
# 這裡輸出的統計量是 Chi-square (LR Chisq),而非 F 值
print(Anova(fit_logit, type = 3))
## Analysis of Deviance Table (Type III tests)
## 
## Response: 罪名2
##           LR Chisq Df Pr(>Chisq)  
## abcon       2.9135  1    0.08784 .
## wwo         1.5231  1    0.21715  
## abcon:wwo   1.0337  1    0.30930  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

ancova

CVs <- c("Control")

for (DV in continuous_DVs) {
  cat("#################################\n")
  cat("### ANCOVA 分析報告 (共變數分析) ###\n")
  cat("#################################\n")
  cat("依變項 (Dependent Variable):", DV, "\n")
  cat("獨變項 (Independent Variables):", IV1, "(語言具體性) *", IV2, "(品格證據)\n")
  cat("控制共變數 (Covariates):", paste(CVs, collapse = ", "), "\n")
  cat("-------------------------------------------------------\n")
  
  # 建立 ANCOVA 公式: DV ~ IV1 * IV2 + CV1 + CV2 ...
  cv_formula_part <- paste(CVs, collapse = " + ")
  formula_ancova <- as.formula(paste(DV, "~", IV1, "*", IV2, "+", cv_formula_part))
  
  cat("使用的統計模型公式:\n")
  print(formula_ancova)
  cat("\n")
  # 擬合模型
  fit_ancova <- lm(formula_ancova, data = df)
  print(Anova(fit_ancova, type = 3))
} 
## #################################
## ### ANCOVA 分析報告 (共變數分析) ###
## #################################
## 依變項 (Dependent Variable): 判決信心 
## 獨變項 (Independent Variables): abcon (語言具體性) * wwo (品格證據)
## 控制共變數 (Covariates): Control 
## -------------------------------------------------------
## 使用的統計模型公式:
## 判決信心 ~ abcon * wwo + Control
## 
## Anova Table (Type III tests)
## 
## Response: 判決信心
##             Sum Sq  Df F value    Pr(>F)    
## (Intercept) 176.98   1 68.7162 1.392e-13 ***
## abcon         5.36   1  2.0828   0.15143    
## wwo          15.13   1  5.8763   0.01675 *  
## Control      10.49   1  4.0714   0.04572 *  
## abcon:wwo     6.43   1  2.4959   0.11663    
## Residuals   327.09 127                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## #################################
## ### ANCOVA 分析報告 (共變數分析) ###
## #################################
## 依變項 (Dependent Variable): 刑期2 
## 獨變項 (Independent Variables): abcon (語言具體性) * wwo (品格證據)
## 控制共變數 (Covariates): Control 
## -------------------------------------------------------
## 使用的統計模型公式:
## 刑期2 ~ abcon * wwo + Control
## 
## Anova Table (Type III tests)
## 
## Response: 刑期2
##             Sum Sq  Df  F value  Pr(>F)    
## (Intercept) 31.975   1 101.9755 < 2e-16 ***
## abcon        0.002   1   0.0068 0.93439    
## wwo          0.607   1   1.9361 0.16653    
## Control      1.695   1   5.4055 0.02166 *  
## abcon:wwo    0.041   1   0.1317 0.71729    
## Residuals   39.821 127                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

「在控制了共變數(Control)的影響後,ANCOVA 分析結果顯示:判決信心:『品格證據』具有顯著的主效應 (\(F(1, 127) = 5.88, p < .05\)),顯示檢察官使用人格攻擊策略會顯著改變國民法官的判決信心。然而,語言具體性的主效應與兩者的交互作用則不顯著。量刑建議:所有實驗操弄變項(語言具體性、品格證據)及其交互作用對量刑長短皆無顯著影響 (\(ps > .05\))。唯控制變項在模型中達顯著水準 (\(p < .05\)),顯示受試者的個人特質對量刑決策具有解釋力。

分組檢定

# 定義要進行分組的變項名稱
subgroup_vars <- c("Gender", "Age", "Education")

# 依序對每個變項進行分析
for (var_name in subgroup_vars) {
  
  # 檢查變項是否存在於資料框中
  if (var_name %in% names(df)) {
    
    # 1. 輸出大標題 (例如:## 依 Gender 分組)
    cat(paste0("\n## 依 ", var_name, " 分組\n\n"))
    
    # 取得該變項的所有層級 (去除 NA)
    levels_vec <- unique(na.omit(df[[var_name]]))
    
    # 若為數值型態(如年齡),進行排序讓報告更整齊
    if(is.numeric(levels_vec)) levels_vec <- sort(levels_vec)
    
    # 依序對該變項的每個層級進行分析 (例如:男、女)
    for (lvl in levels_vec) {
      
      # 2. 輸出次標題 (例如:### Gender:男)
      cat(paste0("\n### ", var_name, ":", lvl, "\n\n"))
      
      # 篩選出該層級的子樣本
      sub_data <- df %>% filter(.data[[var_name]] == lvl)
      
      # 輸出樣本數資訊
      cat(paste0("**樣本數:** ", nrow(sub_data), "\n\n"))
      
      # 樣本數檢查:若大於 10 才執行統計檢定,避免自由度不足
      if(nrow(sub_data) > 10) {
        
        # 對兩個依變項分別跑 ANOVA
        for (dv in c("判決信心", "刑期2")) {
          
          # 輸出小標題 (例如:#### 依變項:判決信心)
          cat(paste0("#### 依變項:", dv, "\n\n"))
          
          # 建立公式:依變項 ~ 語言具體性 * 品格證據
          f <- as.formula(paste(dv, "~ abcon * wwo"))
          
          # 嘗試執行 ANOVA,使用 tryCatch 避免因單一組別錯誤導致整個報告中斷
          tryCatch({
            fit <- lm(f, data = sub_data)
            
            # 使用 kable 輸出漂亮的表格
            # 注意:在 results='asis' 的迴圈中,必须明確使用 print()
            print(knitr::kable(Anova(fit, type = 3), 
                               caption = paste("ANOVA 摘要表:", var_name, "=", lvl, "-", dv),
                               digits = 3)) # 設定小數點位數
            
          }, error = function(e) {
            # 若發生錯誤 (如某組變異數為0),印出提示
            cat(paste0("> *無法計算 (可能原因:樣本不足或該組別內數值皆相同)*\n"))
          })
          
          cat("\n") # 表格後的換行
        }
      } else {
        cat("> *樣本過少 (< 10),跳過統計檢定。*\n")
      }
      
      # 加一條分隔線
      cat("\n---\n")
    }
  } else {
    # 若找不到該變項
    cat(paste0("\n> [警告] 資料中找不到變項:", var_name, ",請檢查欄位名稱。\n"))
  }
}
## 
## ## 依 Gender 分組
## 
## 
## ### Gender:1
## 
## **樣本數:** 30
## 
## #### 依變項:判決信心
## 
## 
## 
## Table: ANOVA 摘要表: Gender = 1 - 判決信心
## 
## |            | Sum Sq| Df| F value| Pr(>F)|
## |:-----------|------:|--:|-------:|------:|
## |(Intercept) | 20.250|  1|   7.014|  0.014|
## |abcon       |  6.750|  1|   2.338|  0.138|
## |wwo         | 17.112|  1|   5.927|  0.022|
## |abcon:wwo   |  6.338|  1|   2.195|  0.150|
## |Residuals   | 75.062| 26|      NA|     NA|
## 
## #### 依變項:刑期2
## 
## 
## 
## Table: ANOVA 摘要表: Gender = 1 - 刑期2
## 
## |            | Sum Sq| Df| F value| Pr(>F)|
## |:-----------|------:|--:|-------:|------:|
## |(Intercept) |  6.250|  1|  13.000|  0.001|
## |abcon       |  0.083|  1|   0.173|  0.681|
## |wwo         |  0.450|  1|   0.936|  0.342|
## |abcon:wwo   |  0.017|  1|   0.035|  0.854|
## |Residuals   | 12.500| 26|      NA|     NA|
## 
## 
## ---
## 
## ### Gender:2
## 
## **樣本數:** 99
## 
## #### 依變項:判決信心
## 
## 
## 
## Table: ANOVA 摘要表: Gender = 2 - 判決信心
## 
## |            |  Sum Sq| Df| F value| Pr(>F)|
## |:-----------|-------:|--:|-------:|------:|
## |(Intercept) | 235.636|  1|  96.733|  0.000|
## |abcon       |   1.099|  1|   0.451|  0.503|
## |wwo         |   1.536|  1|   0.631|  0.429|
## |abcon:wwo   |   0.095|  1|   0.039|  0.844|
## |Residuals   | 231.414| 95|      NA|     NA|
## 
## #### 依變項:刑期2
## 
## 
## 
## Table: ANOVA 摘要表: Gender = 2 - 刑期2
## 
## |            | Sum Sq| Df| F value| Pr(>F)|
## |:-----------|------:|--:|-------:|------:|
## |(Intercept) | 43.682|  1| 150.290|  0.000|
## |abcon       |  0.095|  1|   0.326|  0.570|
## |wwo         |  0.012|  1|   0.043|  0.837|
## |abcon:wwo   |  0.096|  1|   0.329|  0.568|
## |Residuals   | 27.612| 95|      NA|     NA|
## 
## 
## ---
## 
## ### Gender:3
## 
## **樣本數:** 1
## 
## > *樣本過少 (< 10),跳過統計檢定。*
## 
## ---
## 
## ### Gender:4
## 
## **樣本數:** 2
## 
## > *樣本過少 (< 10),跳過統計檢定。*
## 
## ---
## 
## ## 依 Age 分組
## 
## 
## ### Age:1
## 
## **樣本數:** 1
## 
## > *樣本過少 (< 10),跳過統計檢定。*
## 
## ---
## 
## ### Age:2
## 
## **樣本數:** 104
## 
## #### 依變項:判決信心
## 
## 
## 
## Table: ANOVA 摘要表: Age = 2 - 判決信心
## 
## |            |  Sum Sq|  Df| F value| Pr(>F)|
## |:-----------|-------:|---:|-------:|------:|
## |(Intercept) | 210.182|   1|  87.048|  0.000|
## |abcon       |   3.562|   1|   1.475|  0.227|
## |wwo         |  10.182|   1|   4.217|  0.043|
## |abcon:wwo   |   1.870|   1|   0.775|  0.381|
## |Residuals   | 241.455| 100|      NA|     NA|
## 
## #### 依變項:刑期2
## 
## 
## 
## Table: ANOVA 摘要表: Age = 2 - 刑期2
## 
## |            | Sum Sq|  Df| F value| Pr(>F)|
## |:-----------|------:|---:|-------:|------:|
## |(Intercept) | 40.909|   1| 124.654|  0.000|
## |abcon       |  0.010|   1|   0.030|  0.863|
## |wwo         |  0.229|   1|   0.698|  0.405|
## |abcon:wwo   |  0.013|   1|   0.040|  0.843|
## |Residuals   | 32.818| 100|      NA|     NA|
## 
## 
## ---
## 
## ### Age:3
## 
## **樣本數:** 17
## 
## #### 依變項:判決信心
## 
## 
## 
## Table: ANOVA 摘要表: Age = 3 - 判決信心
## 
## |            | Sum Sq| Df| F value| Pr(>F)|
## |:-----------|------:|--:|-------:|------:|
## |(Intercept) | 50.000|  1|  15.745|  0.002|
## |abcon       |  0.667|  1|   0.210|  0.654|
## |wwo         |  0.083|  1|   0.026|  0.874|
## |abcon:wwo   |  0.418|  1|   0.132|  0.723|
## |Residuals   | 41.283| 13|      NA|     NA|
## 
## #### 依變項:刑期2
## 
## 
## 
## Table: ANOVA 摘要表: Age = 3 - 刑期2
## 
## |            | Sum Sq| Df| F value| Pr(>F)|
## |:-----------|------:|--:|-------:|------:|
## |(Intercept) |  4.500|  1|  12.904|  0.003|
## |abcon       |  0.167|  1|   0.478|  0.502|
## |wwo         |  0.333|  1|   0.956|  0.346|
## |abcon:wwo   |  0.004|  1|   0.011|  0.917|
## |Residuals   |  4.533| 13|      NA|     NA|
## 
## 
## ---
## 
## ### Age:4
## 
## **樣本數:** 4
## 
## > *樣本過少 (< 10),跳過統計檢定。*
## 
## ---
## 
## ### Age:5
## 
## **樣本數:** 4
## 
## > *樣本過少 (< 10),跳過統計檢定。*
## 
## ---
## 
## ### Age:6
## 
## **樣本數:** 2
## 
## > *樣本過少 (< 10),跳過統計檢定。*
## 
## ---
## 
## ## 依 Education 分組
## 
## 
## ### Education:2
## 
## **樣本數:** 8
## 
## > *樣本過少 (< 10),跳過統計檢定。*
## 
## ---
## 
## ### Education:3
## 
## **樣本數:** 71
## 
## #### 依變項:判決信心
## 
## 
## 
## Table: ANOVA 摘要表: Education = 3 - 判決信心
## 
## |            |  Sum Sq| Df| F value| Pr(>F)|
## |:-----------|-------:|--:|-------:|------:|
## |(Intercept) | 204.765|  1|  79.805|  0.000|
## |abcon       |   0.005|  1|   0.002|  0.963|
## |wwo         |   1.323|  1|   0.516|  0.475|
## |abcon:wwo   |   0.000|  1|   0.000|  0.996|
## |Residuals   | 171.910| 67|      NA|     NA|
## 
## #### 依變項:刑期2
## 
## 
## 
## Table: ANOVA 摘要表: Education = 3 - 刑期2
## 
## |            | Sum Sq| Df| F value| Pr(>F)|
## |:-----------|------:|--:|-------:|------:|
## |(Intercept) | 31.118|  1|  94.272|  0.000|
## |abcon       |  0.018|  1|   0.053|  0.818|
## |wwo         |  0.020|  1|   0.062|  0.805|
## |abcon:wwo   |  0.408|  1|   1.235|  0.270|
## |Residuals   | 22.116| 67|      NA|     NA|
## 
## 
## ---
## 
## ### Education:4
## 
## **樣本數:** 53
## 
## #### 依變項:判決信心
## 
## 
## 
## Table: ANOVA 摘要表: Education = 4 - 判決信心
## 
## |            |  Sum Sq| Df| F value| Pr(>F)|
## |:-----------|-------:|--:|-------:|------:|
## |(Intercept) |  78.125|  1|  30.046|  0.000|
## |abcon       |   3.719|  1|   1.430|  0.237|
## |wwo         |   8.847|  1|   3.402|  0.071|
## |abcon:wwo   |   6.798|  1|   2.614|  0.112|
## |Residuals   | 127.407| 49|      NA|     NA|
## 
## #### 依變項:刑期2
## 
## 
## 
## Table: ANOVA 摘要表: Education = 4 - 刑期2
## 
## |            | Sum Sq| Df| F value| Pr(>F)|
## |:-----------|------:|--:|-------:|------:|
## |(Intercept) | 18.000|  1|  61.782|  0.000|
## |abcon       |  0.274|  1|   0.939|  0.337|
## |wwo         |  0.359|  1|   1.232|  0.272|
## |abcon:wwo   |  0.337|  1|   1.155|  0.288|
## |Residuals   | 14.276| 49|      NA|     NA|
## 
## 
## ---