Skip to content

Instantly share code, notes, and snippets.

@LukasWallrich
Created November 10, 2025 22:40
Show Gist options
  • Select an option

  • Save LukasWallrich/01eae59cb003c143d1014f7a9a26e882 to your computer and use it in GitHub Desktop.

Select an option

Save LukasWallrich/01eae59cb003c143d1014f7a9a26e882 to your computer and use it in GitHub Desktop.
ML loss framing effect size calculation
``` r
# ============================================
# ORIGINAL STUDY (Tversky & Kahneman, 1981)
# ============================================
# Create contingency table
original <- matrix(c(
109, 43, # Gain frame: 72% chose A, 28% chose B
34, 121 # Loss frame: 22% chose C, 78% chose D
), nrow = 2, byrow = TRUE)
rownames(original) <- c("Gain frame", "Loss frame")
colnames(original) <- c("Sure thing", "Gamble")
print("Original Study Contingency Table:")
#> [1] "Original Study Contingency Table:"
print(original)
#> Sure thing Gamble
#> Gain frame 109 43
#> Loss frame 34 121
print(addmargins(original))
#> Sure thing Gamble Sum
#> Gain frame 109 43 152
#> Loss frame 34 121 155
#> Sum 143 164 307
# Chi-square test
original_chisq <- chisq.test(original, correct = FALSE)
print(original_chisq)
#>
#> Pearson's Chi-squared test
#>
#> data: original
#> X-squared = 76.411, df = 1, p-value < 2.2e-16
# Manual calculation to verify
observed <- c(109, 43, 34, 121)
expected <- original_chisq$expected
manual_chisq <- sum((observed - as.vector(expected))^2 / as.vector(expected))
cat("\nManual χ² calculation:", round(manual_chisq, 2), "\n")
#>
#> Manual χ² calculation: 77.48
# ============================================
# REPLICATION STUDY
# ============================================
# Total valid responses: 6344 - 73 = 6271
# Assume even split: 3135.5 per condition, round to 3136 and 3135
# Create contingency table
replication <- matrix(c(
round(3136 * 0.622), round(3136 * 0.378), # Gain: 62.2% A, 37.8% B
round(3135 * 0.335), round(3135 * 0.665) # Loss: 33.5% C, 66.5% D
), nrow = 2, byrow = TRUE)
rownames(replication) <- c("Gain frame", "Loss frame")
colnames(replication) <- c("Sure thing", "Gamble")
print("\n\nReplication Study Contingency Table:")
#> [1] "\n\nReplication Study Contingency Table:"
print(replication)
#> Sure thing Gamble
#> Gain frame 1951 1185
#> Loss frame 1050 2085
print(addmargins(replication))
#> Sure thing Gamble Sum
#> Gain frame 1951 1185 3136
#> Loss frame 1050 2085 3135
#> Sum 3001 3270 6271
# Chi-square test
replication_chisq <- chisq.test(replication, correct = FALSE)
print(replication_chisq)
#>
#> Pearson's Chi-squared test
#>
#> data: replication
#> X-squared = 518.22, df = 1, p-value < 2.2e-16
# Manual calculation to verify
observed_rep <- c(1951, 1185, 1050, 2085)
expected_rep <- replication_chisq$expected
manual_chisq_rep <- sum((observed_rep - as.vector(expected_rep))^2 / as.vector(expected_rep))
cat("\nManual χ² calculation:", round(manual_chisq_rep, 2), "\n")
#>
#> Manual χ² calculation: 534.82
# ============================================
# COMPARISON
# ============================================
cat("\n\n=== COMPARISON ===\n")
#>
#>
#> === COMPARISON ===
cat(
"Original Study: χ²(1) =", round(original_chisq$statistic, 2),
", p =", format.pval(original_chisq$p.value, digits = 4), "\n"
)
#> Original Study: χ²(1) = 76.41 , p = < 2.2e-16
cat(
"Replication Study: χ²(1) =", round(replication_chisq$statistic, 2),
", p =", format.pval(replication_chisq$p.value, digits = 4), "\n"
)
#> Replication Study: χ²(1) = 518.22 , p = < 2.2e-16
cat("Reported in paper: χ²(1) = 516.41, p < .0001\n")
#> Reported in paper: χ²(1) = 516.41, p < .0001
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment