# ferdo R Lab | Lesson 5 v1.0 | Correlation and simple linear regression
# Fictional 12-row CSV reused from lessons 2-4. No extra packages.
# Set RStudio working directory to the extracted folder ROOT.
stopifnot(file.exists("data/class_synthetic.csv"))
d <- read.csv("data/class_synthetic.csv",
              na.strings=c("","NA"), stringsAsFactors=FALSE)
ok <- complete.cases(d[,c("study_hours","quiz_score")])
paired <- d[ok, ,drop=FALSE]
excluded_ids <- d$student_id[!ok]
cat("Complete pairs:",nrow(paired),"\n")
cat("Excluded IDs:",paste(excluded_ids,collapse=", "),"\n")

# Correlation: unit-free linear association, not causation
r_value <- cor(paired$study_hours,paired$quiz_score)
cat("Pearson r:",r_value,"\n")

# OLS model: fitted association, NOT an experimental effect
model <- lm(quiz_score ~ study_hours,data=paired)
model_coefficients <- unname(coef(model))
r_squared <- unname(summary(model)$r.squared)
cat("Intercept and slope:",model_coefficients,"\n")
cat("Sample R-squared:",r_squared,"\n")

# Predictions inside observed x range (1 to 6) vs extrapolation
prediction_four <- unname(predict(model,
                         newdata=data.frame(study_hours=4)))
prediction_ten <- unname(predict(model,
                        newdata=data.frame(study_hours=10)))
cat("Prediction at 4 hours:",prediction_four,"\n")
cat("Extrapolation at 10 hours:",prediction_ten,"\n")

# Residual = observed - fitted
model_residuals <- unname(residuals(model))
paired$fitted_quiz <- unname(fitted(model))
paired$residual_quiz <- model_residuals

# Show plots SEQUENTIALLY; no cramped 2x2 RStudio plotting grid
plot(paired$study_hours,paired$quiz_score,
     xlab="Study hours (hours)",ylab="Quiz score",
     main="Fictional data: fitted association",pch=19)
abline(model,lwd=2)
plot(paired$fitted_quiz,paired$residual_quiz,
     xlab="Fitted quiz score",ylab="Residual (observed - fitted)",
     main="Residual plot (fictional data)",pch=19)
abline(h=0,lty=2)

# Sensitivity teaching demonstration ONLY. Original CSV stays unchanged.
altered <- paired
altered$quiz_score[altered$student_id=="S08"] <- 50
sensitivity_r <- cor(altered$study_hours,altered$quiz_score)
cat("Correlation after SYNTHETIC copy modification:",sensitivity_r,"\n")
# Student: explain why a large r and R-squared cannot establish causation.
