-
Notifications
You must be signed in to change notification settings - Fork 0
/
_075_logistic_regression.r
61 lines (46 loc) · 1.79 KB
/
_075_logistic_regression.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Filename: _075_logistic_regression.R
# Title: Logistic regression in R
# Author: Raghava | GitHub: @raghavtwenty
# Date Created: July 6, 2024 | Last Updated: July 6, 2024
# Language: R | Version: 4.4.0
# Load necessary libraries
install.packages('popbio')
library(popbio)
# Load the dataset
students <- read.csv("/datasets/student_data.csv")
# View the dataset
View(students)
# Extract the relevant columns
Gender <- students$Gender
Marks <- students$Marks
# Convert Gender to a binary code: 1 for Female, 0 for Male
Gendercode <- ifelse(Gender == "F", 1, 0)
# Plot the data
plot(Marks, jitter(Gendercode, 1), pch = 19, xlab = "Exam grade(mm)", ylab = "Gender (0-male, 1-female)")
# Fit the logistic regression model
model <- glm(Gendercode ~ Marks, family = binomial)
# Create a sequence of values for Marks to predict Gendercode
xv <- seq(min(Marks), max(Marks), 0.01)
# Predict Gendercode using the logistic regression model
yv <- predict(model, list(Marks = xv), type = "response")
# Add the prediction line to the plot
lines(xv, yv, col = "red")
# Create a histogram plot with logistic regression fit
logi.hist.plot(Marks, Gendercode, boxp = FALSE, type = "count", col = "gray", xlabel = "Marks")
# Function to predict gender based on new input for exam grade
predict_gender <- function(new_mark) {
# Create a data frame with the new mark
new_data <- data.frame(Marks = new_mark)
# Predict the probability of being female (Gendercode = 1)
predicted_prob <- predict(model, new_data, type = "response")
# Convert probability to class label
if(predicted_prob > 0.5) {
return("Female")
} else {
return("Male")
}
}
# New prediction
new_mark <- 85
predicted_gender <- predict_gender(new_mark)
cat("Predicted gender for the given mark (", new_mark, ") is:", predicted_gender, "\n")