install.packages('GGally', repos='https://cran.r-project.org')
## Installing package into 'C:/Users/Navya/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'GGally' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\Navya\AppData\Local\Temp\RtmpMfrGRM\downloaded_packages
# 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
# Summary statistics
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
# Scatterplot matrix
library(GGally)
## Warning: package 'GGally' was built under R version 4.3.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.3.3
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
ggpairs(student_data)
# Correlation matrix
cor_matrix <- cor(student_data)
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
# Heat map of correlation matrix
library(ggplot2)
library(reshape2) # Load reshape2 package
## Warning: package 'reshape2' was built under R version 4.3.3
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'
# 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 a Linear Regression model using the train data and assign it to an object called model.
# Target variable is FinalGrades and the Features are StudyHours, QuizScores, ForumPosts, and PreviousGrades
model <- lm(FinalGrades ~ StudyHours + QuizScores + ForumPosts + PreviousGrades, data = train_data)
# Making predictions on the test set. use the model object to make prediction.
predictions <- predict(model, newdata = test_data)
# Evaluation metrics
# Compute the mean squared error and R-squared
mse <- mean((test_data$FinalGrades - predictions)^2)
rsquared <- summary(model)$r.squared
# Print evaluation metrics
print(paste("Mean Squared Error:", mse))
## [1] "Mean Squared Error: 22.3465631070855"
print(paste("R-squared:", rsquared))
## [1] "R-squared: 0.864833778721095"
# Get the predictions and prediction intervals
pred_int <- predict(model, newdata = test_data, interval = "prediction")
# Extract lower and upper bounds of the prediction interval
lower_bound <- pred_int[, "lwr"]
upper_bound <- pred_int[, "upr"]
# Actual values from the test data
actual_values <- test_data$FinalGrades
# Check if the actual values fall within the prediction interval
correct_predictions <- actual_values >= lower_bound & actual_values <= upper_bound
# Compute accuracy
accuracy <- sum(correct_predictions) / length(correct_predictions)
# Print accuracy
cat("Model Accuracy using Prediction Interval:", accuracy, "\n")
## Model Accuracy using Prediction Interval: 0.96
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:
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
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.