# ferdo R Lab | Lesson 6 | INTENTIONALLY BROKEN!
# Work on a COPY; seven issues may include silent logical errors.
# The acceptance test parses this file but DOES NOT execute it.

# BUG 1: incorrect filename
d <- read.csv("data/class_synthethic.csv",na.strings=c("","NA"),
              stringsAsFactors=FALSE)

# BUG 2: wrong variable name
print(d$quiz_scores)

# BUG 3: ignores NA in mean
avg_quiz <- mean(d$quiz_score)
print(avg_quiz)

# BUG 4: missing comparisons can include phantom rows
above_70 <- d[d$quiz_score >= 70, ,drop=FALSE]
print(above_70)

# BUG 5: grouping by student ID instead of group
by_group <- tapply(d$quiz_score,d$student_id,mean,na.rm=TRUE)
print(by_group)

# BUG 6: different row masks destroy x-y pairing
plot(d$study_hours[!is.na(d$study_hours)],
     d$quiz_score[!is.na(d$quiz_score)],
     xlab="Study hours",ylab="Quiz score",pch=19)

# BUG 7: invalid causal conclusion
interpretation <- "The plot PROVES study time CAUSES higher quiz scores."
print(interpretation)
