-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.Rmd
176 lines (134 loc) · 3.83 KB
/
main.Rmd
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
---
title: "beadando"
author: "euv0j3"
date: "`r Sys.Date()`"
output:
pdf_document: default
html_document: default
---
# exams_data_writing_score
## Introduction
### Változók leírása
- Gender
- Race/ethnicity
- parental level of education
- lunch
- test preparation course
- math score
- reading score
- writing score
### Osztályok
- writing_score: 75% alatti eredmények és 75% feletti eredmények
### Feladat
a meglévő adatok alapján egy osztályozó modell létrehozása R-ben, a modell futtatása és beküldése a Moodle-ba.
Két fájlt kell feltöltenI:
1. R program
2. Az eredmények leírása Word fájlban 1 oldalon
## Setup
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(dplyr)
library(tidyverse)
library(caret)
library(janitor)
library(rpart)
library(rpart.plot)
library(randomForest)
library(gmodels)
library(ggplot2)
library(C50)
```
## Load the Data Set
```{r}
data <- read.csv(file.choose(), sep=",") # exams_writing_score_dataset.csv
data <- janitor::clean_names(data, "snake")
data
```
## Check data
```{r}
print(sum(is.na(data)))
```
There are no missing data values
```{r}
ggplot(data = data, aes(x = math_percentage, y = reading_score_percentage, color = sex)) +
geom_point() +
labs(x = "Math Percentage", y = "Reading Percentage", title = "Math vs. Reading Scores")
```
```{r}
ggplot(data = data, aes(x = parental_level_of_education, y = writing_score_percentage, fill = sex)) +
geom_boxplot() +
labs(x = "Parental Level of Education", y = "Writing Percentage", title = "Writing Scores by Parental Level of Education") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
```
```{r}
ggplot(data = data, aes(x = race_ethnicity, fill = sex)) +
geom_bar() +
labs(x = "Race/Ethnicity", y = "Count", title = "Count of Students by Race/Ethnicity") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
```
```{r}
ggplot(data = data, aes(x = math_percentage, fill = sex)) +
geom_histogram(binwidth = 0.1) +
labs(x = "Math Percentage", y = "Count", title = "Distribution of Math Scores by Lunch and Test Preparation Course") +
facet_grid(lunch ~ test_preparation_course)
```
## Binary Variable
Create a new binary variable called writing_class based on the writing_score column (1 for results above 75% and 0 for results below or equal to 75%)
```{r}
data$writing_class <- ifelse(data$writing_score_percentage > 0.75, 1, 0)
# Remove the old column
data$writing_score_percentage <- NULL
```
```{r}
# Convert writing_class to a factor
data$writing_class <- as.factor(data$writing_class)
```
## Split the dataset into training and testing sets:
```{r}
set.seed(12345)
# Split the data into training (80%) and testing (20%) sets
sample_size <- floor(0.8 * nrow(data))
train_indices <- sample(seq_len(nrow(data)), size = sample_size)
train_data <- data[train_indices, ]
test_data <- data[-train_indices, ]
```
```{r}
model <- C5.0(train_data[, -which(names(train_data) == "writing_class")],
train_data$writing_class,
rules = FALSE)
# Print the model
print(model)
```
```{r}
summary(model)
```
```{r}
predictions <- predict(model, test_data[, -which(names(test_data) == "writing_class")], type = "class")
```
```{r}
confusion_matrix <- confusionMatrix(predictions, test_data$writing_class)
# Print the confusion matrix
print(confusion_matrix)
```
```{r}
plot(model)
```
```{r}
if (!requireNamespace("pROC", quietly = TRUE)) {
install.packages("pROC")
}
# Load the pROC package
library(pROC)
# Predict the probabilities of the positive class (writing_class = 1)
predicted_probabilities <- predict(model, test_data, type = "prob")[, "1"]
roc_obj <- roc(test_data$writing_class, predicted_probabilities)
plot(roc_obj, main = "Decision Tree Model")
```
```{r}
# Calculate the AUC
auc_value <- auc(roc_obj)
# Print the AUC value
print(auc_value)
```