R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

# Load necessary libraries
library(MASS)  # For normal distribution in simulation

# Set a fixed random seed for reproducibility
set.seed(10923)

# Number of students
num_students <- 500

# Simulate study hours (ranging from 1 to 20 hours)
study_hours <- sample(1:20, num_students, replace = TRUE)

# Simulate quiz scores (ranging from 0 to 100)
quiz_scores <- sample(0:100, num_students, replace = TRUE)

# Simulate forum participation (ranging from 0 to 50 posts)
forum_posts <- sample(0:50, num_students, replace = TRUE)

# Simulate previous grades (ranging from 0 to 100)
previous_grades <- sample(0:100, num_students, replace = TRUE)

# Simulate final grades (ranging from 0 to 100)
final_grades <- 0.3 * study_hours + 0.4 * quiz_scores + 0.2 * forum_posts + 0.1 * previous_grades + rnorm(num_students, mean = 0, sd = 5) + 25

# Create a data frame
student_data <- data.frame(StudyHours = study_hours, QuizScores = quiz_scores, ForumPosts = forum_posts, PreviousGrades = previous_grades, FinalGrades = final_grades)

# View the first few rows of the generated data
print(head(student_data))
##   StudyHours QuizScores ForumPosts PreviousGrades FinalGrades
## 1         20         91         22             78    80.80895
## 2         12         26         27              1    46.45853
## 3         13          5          8             60    40.22946
## 4          4         96         13             78    70.64216
## 5          5         74         45             31    62.35254
## 6         18          1         47             50    48.42835
# Splitting the data into training and testing sets (80% training, 20% testing)
set.seed(10923) # Set seed for reproducibility
sample_index <- sample(1:nrow(student_data), 0.8 * nrow(student_data))
train_data <- student_data[sample_index, ]
test_data <- student_data[-sample_index, ]

# Building several Linear Regression models
model_all <- lm(FinalGrades ~ StudyHours + QuizScores + ForumPosts + PreviousGrades, data = train_data)
model_without_forum <- lm(FinalGrades ~ StudyHours + QuizScores + PreviousGrades, data = train_data)
model_only_scores_and_hours <- lm(FinalGrades ~ StudyHours + QuizScores, data = train_data)
model_random <- lm(FinalGrades ~ QuizScores + ForumPosts, data = train_data)

# List of models
models <- list(model_all, model_without_forum, model_only_scores_and_hours, model_random)
model_names <- c("All Features", "No Forum", "Only Study and Quiz", "Random Features")

# Evaluate each model
mse_values <- numeric(length(models))
accuracy_values <- numeric(length(models))

for (i in seq_along(models)) {
    predictions <- predict(models[[i]], newdata = test_data)
    mse_values[i] <- mean((test_data$FinalGrades - predictions)^2)

    pred_int <- predict(models[[i]], newdata = test_data, interval = "prediction")
    lower_bound <- pred_int[, "lwr"]
    upper_bound <- pred_int[, "upr"]
    actual_values <- test_data$FinalGrades
    correct_predictions <- actual_values >= lower_bound & actual_values <= upper_bound
    accuracy_values[i] <- sum(correct_predictions) / length(correct_predictions)

    cat("Model:", model_names[i], "\n")
    cat("Mean Squared Error:", mse_values[i], "\n")
    cat("Accuracy using Prediction Interval:", accuracy_values[i], "\n")
    cat("\n")
}
## Model: All Features 
## Mean Squared Error: 22.34656 
## Accuracy using Prediction Interval: 0.96 
## 
## Model: No Forum 
## Mean Squared Error: 33.1469 
## Accuracy using Prediction Interval: 0.96 
## 
## Model: Only Study and Quiz 
## Mean Squared Error: 40.43793 
## Accuracy using Prediction Interval: 0.96 
## 
## Model: Random Features 
## Mean Squared Error: 27.62451 
## Accuracy using Prediction Interval: 0.96
# Report a summary of findings
best_model_index <- which.min(mse_values)
cat("Summary of Findings:\n")
## Summary of Findings:
cat("The model with the lowest MSE is", model_names[best_model_index], "with an MSE of", mse_values[best_model_index], "\n")
## The model with the lowest MSE is All Features with an MSE of 22.34656
cat("Accuracies using prediction intervals range from", min(accuracy_values), "to", max(accuracy_values), "\n")
## Accuracies using prediction intervals range from 0.96 to 0.96

Summary of Model Evaluations

In the analysis of different linear regression models to predict students’ final grades, the models compared varied in the features included:

  1. All Features: Included Study Hours, Quiz Scores, Forum Posts, and Previous Grades.
  2. No Forum: Excluded Forum Posts, using only Study Hours, Quiz Scores, and Previous Grades.
  3. Only Study and Quiz: Used only Study Hours and Quiz Scores.
  4. Random Features: Included only Quiz Scores and Forum Posts.

Here are the key findings from the evaluations:

  • Model Performance on MSE: The model incorporating all features performed the best, with the lowest Mean Squared Error (MSE) of 22.34656. This suggests that using a comprehensive set of features provides a more accurate prediction of final grades. In contrast, the model with only Study Hours and Quiz Scores had the highest MSE at 40.43793, indicating less predictive accuracy.

  • Consistency in Accuracy: Despite the variations in MSE, all models exhibited the same level of accuracy in terms of prediction intervals, with each model achieving an accuracy rate of 96%. This high consistency across different models suggests that the primary determinant of whether final grades fall within predicted intervals is robust across different feature combinations.

  • Summary: The model including all available features not only minimized error but also maintained high predictive consistency, reinforcing the value of a more inclusive feature set in predictive accuracy for this specific scenario. The consistent prediction interval accuracy across models highlights the effectiveness of the linear regression approach in capturing the variance of the final grades under different configurations.

This analysis demonstrates that while the choice of features can influence the error rates of predictions, the overall ability to classify grades within a predicted range remains high. For applications seeking to minimize predictive error, it is beneficial to utilize a full range of available data features.

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.