-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogistic_model_exercises.Rmd
154 lines (82 loc) · 2.72 KB
/
logistic_model_exercises.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
---
title: "R Notebook"
output: html_notebook
---
# Exercise 0
0. Load data set avocado_df_categories.csv and run the code below to format the dataset.
```{r}
library("readr")
avocado_df<-read_csv('avocado_df_categories.csv')
avocado_df$PriceCategory <- factor(avocado_df$PriceCategory, levels= c("Cheap","Expensive"),labels=c(0,1))
avocado_df$TotalVolume <- as.numeric(avocado_df$TotalVolume)
avocado_df$Type <- as.factor(avocado_df$Type)
avocado_df$Month <- as.factor(avocado_df$Month)
avocado_df$Year <- as.factor(avocado_df$Year)
```
# Exercise 1: Model with Price and Type Interaction
1. Run the code below to create a logistic model with price and type interaction variable.
```{r}
#solution here
```
2. Print the summary of the model.
```{r}
#solution here
```
3. Interpret the output of summary. What do the coefficients mean for each of the cases below?
i. Model for conventional avocado
ii. Model for organic avocado
4. What are the predicted probabilities for the dataset, df, below?
```{r}
df = data.frame(TotalVolume=c(15.0,5.0), Type=c("conventional","organic"))
```
```{r}
#solution here
```
5. With a threshold value of 0.5, print the confusion matrix of the logistic model.
```{r}
library(caret)
#solution here
```
6. Compare the model with the null model. Is there a siginificant decrease in the deviance?
```{r}
#solution here
```
7. Compare the model aganist the model with no interaction term between price and type. Is there a siginificant decrease in the deviance?
```{r}
#solution here
```
# Exercise 2: Model with Price, Type and Year.
1. Create a model with price and type interaction variable.
```{r}
#solution here
```
2. Print the summary of the model.
```{r}
#solution here
```
3. Interpret the output of summary. What do the coefficients mean for each of the cases below?
i) If the sample is conventional and taken in 2015,
ii) If the sample is conventional and taken in 2016,
iii) If the sample is conventional and taken in 2017,
iv) If the sample is conventional and taken in 2018,
v) If the sample is organic and taken in 2015,
vi) If the sample is organic and taken in 2016,
vii) If the sample is organic and taken in 2017,
viii) If the sample is organic and taken in 2018,
4. What are the predicted probabilities for the dataset, df, below?
```{r}
#solution here
```
5. With a threshold value of 0.5, print the confusion matrix of the logistic model.
```{r}
library(caret)
#solution here
```
6. Compare the model with the null model. Is there a siginificant decrease in the deviance?
```{r}
#solution here
```
7. Compare the model aganist the model with no interaction term between price and type. Is there a siginificant decrease in the deviance?
```{r}
#solution here
```