-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercice2.Rmd
More file actions
145 lines (104 loc) · 2.64 KB
/
Exercice2.Rmd
File metadata and controls
145 lines (104 loc) · 2.64 KB
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
---
title: "R Notebook"
output:
html_document: null
df_print: paged
pdf_document: default
word_document: default
html_notebook: default
---
$\textbf{Read Data, train and test}$
```{r}
library(ggplot2)
#1.********************Logistic regression model for data************************************************************
data <- read.csv(file = '/home/anouar/TP.ADD/data/data.csv')
train <- read.csv(file = '/home/anouar/TP.ADD/data/train.csv')
test <- read.csv(file = '/home/anouar/TP.ADD/data/test.csv')
```
```{r}
#plot data
#Convert y into factor type in data,train and test to ignore the #problem of continious data
#Convert y into factor type in data,train and test to ignore the problem of continious data
data$y=as.factor(data$y)
train$y=as.factor(train$y)
test$y=as.factor(test$y)
ggplot(data, aes(x=X0, y=X1, group=y)) + geom_point(aes(shape=y, color=y))
```
$\textbf{Linear Regression}$
```{r}
model <- lm(y ~ X0 + X1, data = train)
summary(model)
pred <- predict(model, newdata = test, type = "response")
```
```{r}
#Prediction of the values X0=2.75,X1=1.5
X0=2.75
X1=1.5
values=data.frame(X0,X1)
pred <- predict(model, newdata =values, type = "response")
pred
```
```{r}
pred <- predict(model, newdata =test, type = "response")
pred
```
```{r}
```
If we do pred>1 than y=1 else y=0
```{r}
prob_pred <- ifelse(pred >1,1, 0)
prob_pred
```
```{r}
library(caret)
confusionMatrix(factor(prob_pred), factor(test$y), positive = as.character(1))
# pr create prediction between the estimate and the real values
pr <- prediction(prob_pred,test$y)
# plot ruce curve
perf <- performance(pr,measure = "tpr",x.measure = "fpr")
```
```{r}
plot(perf)
# Return roc curve values auc(test$y,prob_pred)
```
```{r}
auc(test$y,prob_pred)
```
```{r}
logistic_model <- glm (y ~., data = train, family ="binomial")
```
```{r}
pred <- predict(logistic_model, newdata = test, type = "response")
```
```{r}
# prob_pred to return two values 0 or 1 if pred> 0.5 then prob_pred is 1 else is 0
prob_pred <- ifelse(pred > 0.5, 1, 0)
#Prediction of the values X0=2.75,X1=1.5
X0=2.75
X1=1.5
values=data.frame(X0,X1)
pred <- predict(model, newdata =values, type = "response")
pred
```
```{r}
prob_pred
```
```{r}
library(caret)
pred <- predict(logistic_model, newdata =test, type = "response")
prob_pred <- ifelse(pred >0.5, 1, 0)
pred
prob_pred
```
```{r}
confusionMatrix(factor(prob_pred), factor(test$y), positive = as.character(1))
```
```{r}
pr <- prediction(prob_pred,test$y)
# plot ruce curve
perf <- performance(pr,measure = "tpr",x.measure = "fpr")
plot(perf)
```
```{r}
auc(test$y,prob_pred)
```