```{r}
library(tidyverse)
library(dplyr)
library(jmvReadWrite)
library(haven)
library(jmv)
## Formatting p values
format.pnum <- function(p, precision = 0.001) {
# Calculate the number of digits needed based on the precision
digits <- max(0, -floor(log10(precision)))
# Format the p-value with the required number of digits
if (p < precision) {
formatted_p <- format(precision, nsmall = digits, scientific = FALSE)
} else {
formatted_p <- format(p, nsmall = digits, scientific = FALSE)
}
# Remove leading zeros if present
formatted_p <- sub("^0\\.", ".", formatted_p)
return(formatted_p)
}
# Formatting p-values formula
format.p <- function(p, precision = 0.001) {
digits <- -log(precision, base = 10)
p <- formatC(p, format = 'f', digits = digits)
if (p < .001) {
p = paste0('< ', precision)}
if (p >= .001) {
p = paste0('= ', p) }
sub("0", "", p)
}
#BG <- jmvReadWrite::read_omv('data/hindsight_BGanova.omv')
#WG <- jmvReadWrite::read_omv('data/hindsight_WGanova.omv')
# Load the data
BG <- haven::read_sav("hindsight-bias/data/hindsight_BGanova.sav")
# Preprocess and mutate the data
BGdata <- BG |>
filter(correct == 1) |> # Limit to correct trials
mutate(hb = score_2 - score_1) # Compute Mdiff for hindsight bias
# Run the ANOVA
BGanova <- jmv::ANOVA(data = BGdata,
dep = "hb",
factors = "condition",
emMeans = list("condition"),
emmPlots = TRUE,
emmTables = TRUE)
print(BGanova)
#Use the str() function to inspect the structure of the BGanova object and identify where the ANOVA table is stored.
#print(names(BGanova))
#str(BGanova)
# Assuming the ANOVA results are stored in a component, let's say `main`
if ("main" %in% names(BGanova)) {
BGanova_table <- as.data.frame(BGanova$main)
print(BGanova_table)
} else {
print("ANOVA results component not found.")
}
# Once you have the ANOVA table as a data frame, you can extract the p-value, F-statistic, and Mean Square Error by referencing the appropriate columns. These columns are typically named something like p, F, and MSE or Error.
# Assuming the ANOVA table is already in BGanova_table
# and that you have checked the names of the columns:
# Extract p-value (usually from the first row, 'condition' effect)
p_value <- BGanova_table$p[1]
# Extract F-statistic (usually from the first row, 'condition' effect)
F_statistic <- BGanova_table$F[1]
# Extract Mean Square Error (from the 'ms' column, second row for residuals)
MSe <- BGanova_table$ms[2]
# Extract degrees of freedom
df1 <- BGanova_table$df[1]
df2 <- BGanova_table$df[2]
# Print extracted values
print(paste("P-Value: ", p_value))
print(paste("F-Statistic: ", F_statistic))
print(paste("Mean Square Error: ", MSe))
# Round the values
F_value_rounded <- round(F_statistic, 2)
MSe_rounded <- round(MSe, 2)
p_value_rounded <- ifelse(p_value < .001, "< .001", round(p_value, 3))
# Print extracted values
print(paste("P-Value: ", p_value_rounded))
print(paste("F-Statistic: ", F_value_rounded))
print(paste("Mean Square Error: ", MSe_rounded))
# Create the formatted string
apa_result <- sprintf("F(%d, %d) = %.2f, MSe = %.2f, p %s", df1, df2, F_value_rounded, MSe_rounded, p_value_rounded)
# Print the result
cat("Results of the between-groups ANOVA:\n")
cat(apa_result, "\n")
```