---
title: "Untitled"
author: "Xuefeiyang Zhang"
date: "2025-08-13"
output: html_document
---

# Prepare modeling dataset

# Read data
data <- read.delim("master_analysis_table.tsv", sep = "\t")

# Filter three event types
data <- data[data$Label %in% c("laughter", "sigh", "throat-clearing"), ]

# Remove missing acoustic features
data <- data[data$Mean_HNR != "--undefined--" & 
             data$Spectral_Tilt != "--undefined--" & 
             data$ZCR != "--undefined--", ]

# Convert to numeric
data$Mean_HNR <- as.numeric(data$Mean_HNR)
data$Spectral_Tilt <- as.numeric(data$Spectral_Tilt)
data$ZCR <- as.numeric(data$ZCR)
data <- data[!is.na(data$Mean_HNR) & !is.na(data$Spectral_Tilt) & !is.na(data$ZCR), ]

# Generate variables
data$Speaker <- substr(data$FileName, 1, 3)
data$AnyError <- ifelse(data$Total_WER_Errors > 0, 1, 0)
data$DelOccur <- ifelse(data$Deletions > 0, 1, 0)
data$InsOccur <- ifelse(data$Insertions > 0, 1, 0)
data$SubOccur <- ifelse(data$Substitutions > 0, 1, 0)
data$zHNR <- scale(data$Mean_HNR)[, 1]
data$zTilt <- scale(data$Spectral_Tilt)[, 1]
data$zZCR <- scale(data$ZCR)[, 1]
data$Label <- factor(data$Label, levels = c("laughter", "sigh", "throat-clearing"))

# Check results
print(paste("Sample size:", nrow(data)))
print(table(data$Label))
print(paste("Speakers:", length(unique(data$Speaker))))
print(paste("zHNR mean:", round(mean(data$zHNR), 3), "SD:", round(sd(data$zHNR), 3)))

# Save
output_file <- "../logistic_model/modeling_dataset.tsv"
write.table(data, output_file, sep = "\t", row.names = FALSE, quote = FALSE)
