# ferdo Lab — WEEK 3 · PUBLIC ORIGINAL INSTRUCTOR SOLUTION
# AQ response data are synthetic; this is NOT a clinical diagnosis.
# Run from the extracted package root. Base R only.
aq <- read.csv("data/aq10.csv",check.names=FALSE)
formats <- read.csv("data/aq_formats.csv")
scoring <- read.csv("data/aq_scoring.csv")
items <- paste0("Q",1:10)
long <- reshape(aq,varying=items,v.names="response",
                timevar="question",times=items,direction="long")
rownames(long) <- NULL
long <- merge(long,formats,by="question")
long$agrees <- grepl("Agree",long$response,fixed=TRUE)
long$response_kind <- ifelse(long$agrees,"Agree","Disagree")
long <- merge(long,scoring,by=c("format","response_kind"))
totals <- aggregate(score~id,long,sum)
print(totals)
print(mean(totals$score))
print(sd(totals$score))
stopifnot(nrow(totals)==8L,all(totals$score>=0),all(totals$score<=10))
cat("W3 PASS: 8 original fictional AQ-like screening totals\n")
# Reverse-keying and wording are teaching illustrations only.
