Lab 4 - Code
#install.packages('corrplot') # Uncomment this line if you haven't installed the 'corrplot' package yet
library(ggplot2) # Load ggplot2 package
library(reshape2) # Load reshape2 package
library(corrplot) # Load corrplot package
## corrplot 0.92 loaded
# 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
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
# Descriptive statistics for the total data
summary(student_data)
## StudyHours QuizScores ForumPosts PreviousGrades
## Min. : 1.00 Min. : 0.00 Min. : 0.00 Min. : 0.00
## 1st Qu.: 6.00 1st Qu.: 24.00 1st Qu.:12.00 1st Qu.: 23.00
## Median :11.00 Median : 48.00 Median :24.00 Median : 51.00
## Mean :10.67 Mean : 48.54 Mean :24.26 Mean : 50.05
## 3rd Qu.:16.00 3rd Qu.: 73.00 3rd Qu.:37.00 3rd Qu.: 75.00
## Max. :20.00 Max. :100.00 Max. :50.00 Max. :100.00
## FinalGrades
## Min. :24.19
## 1st Qu.:47.15
## Median :57.18
## Mean :57.35
## 3rd Qu.:67.01
## Max. :95.36
# Correlation matrix
cor_matrix <- cor(student_data)
print(cor_matrix)
## StudyHours QuizScores ForumPosts PreviousGrades FinalGrades
## StudyHours 1.00000000 0.025275340 -0.018693242 0.01980435 0.1521135
## QuizScores 0.02527534 1.000000000 -0.004709808 0.07537841 0.8732645
## ForumPosts -0.01869324 -0.004709808 1.000000000 0.04874554 0.2194791
## PreviousGrades 0.01980435 0.075378406 0.048745536 1.00000000 0.2759020
## FinalGrades 0.15211349 0.873264537 0.219479119 0.27590199 1.0000000
# Heatmap of correlation matrix
ggplot(data = melt(cor_matrix), aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), space = "Lab",
name="Correlation") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 12, hjust = 1)) +
coord_fixed()

# Distribution of Final Grades
ggplot(student_data, aes(x = FinalGrades)) +
geom_histogram(binwidth = 5, fill = "skyblue", color = "black") +
labs(title = "Distribution of Final Grades", x = "Final Grades", y = "Frequency") +
theme_minimal()

# Relationship between Study Hours and Final Grades
ggplot(student_data, aes(x = StudyHours, y = FinalGrades)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "red") +
labs(title = "Relationship between Study Hours and Final Grades", x = "Study Hours", y = "Final Grades") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

# Pairwise Scatterplots
pairs(student_data)

# Boxplots
par(mfrow = c(2, 2)) # Set up a 2x2 grid for plotting
boxplot(student_data$StudyHours, main = "Study Hours", col = "skyblue")
boxplot(student_data$QuizScores, main = "Quiz Scores", col = "lightgreen")
boxplot(student_data$ForumPosts, main = "Forum Posts", col = "salmon")
boxplot(student_data$PreviousGrades, main = "Previous Grades", col = "lightyellow")

# Density Plots
par(mfrow = c(2, 2)) # Set up a 2x2 grid for plotting
plot(density(student_data$StudyHours), main = "Density Plot: Study Hours", col = "skyblue")
plot(density(student_data$QuizScores), main = "Density Plot: Quiz Scores", col = "lightgreen")
plot(density(student_data$ForumPosts), main = "Density Plot: Forum Posts", col = "salmon")
plot(density(student_data$PreviousGrades), main = "Density Plot: Previous Grades", col = "lightyellow")

# Correlation Heatmap
correlation_matrix <- cor(student_data)
corrplot(correlation_matrix, method = "color", type = "upper", order = "hclust",
tl.col = "black", tl.srt = 45)
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
