-
Notifications
You must be signed in to change notification settings - Fork 1
/
linear_model_code.Rmd
247 lines (150 loc) · 4.13 KB
/
linear_model_code.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
---
title: "Linear Regression Code"
output: html_document
---
# Load and Format data
Here, we read and format data.
```{r}
library(readr)
avocado_df<-read_csv('avocado_df.csv')
avocado_df$AveragePrice <- as.numeric(avocado_df$AveragePrice)
avocado_df$TotalVolume <- as.numeric(avocado_df$TotalVolume)
avocado_df$Month <- as.factor(avocado_df$Month)
avocado_df$Type <- as.factor(avocado_df$Type)
avocado_df$Region <- as.factor(avocado_df$Region)
avocado_df$Year <- as.factor(avocado_df$Year)
```
# Summary of Data
```{r}
# head of data set
```
```{r}
# summary of data set
```
# Linear Modeling in R
## Constant Term
### Model
```{r}
# store as model_constant
```
### Model Summary
```{r}
```
```{r}
# show mean of AveragePrice
```
## TotalVolume + Constant Term
### Model
```{r}
# store model as model_volume
```
### Model Summary
```{r}
```
### Plot of model
We can plot the model using the fitted model points.
```{r}
# get model_volume$fitted.values store in fitted_values
```
```{r}
plot(avocado_df$TotalVolume,avocado_df$AveragePrice, xlab= 'Total Volume', ylab = 'Average Price')
lines(avocado_df$TotalVolume,fitted_values,col = 'blue')
```
We can plot the model using the coefficients of the model.
```{r}
# store coef(model_volume) as coefs
# print
```
```{r}
plot(avocado_df$TotalVolume,avocado_df$AveragePrice, xlab= 'Total Volume', ylab = 'Average Price')
abline(coefs[1],coefs[2], col='blue')
```
### Model Predictions
```{r}
unknown_df <- data.frame(TotalVolume=c(15.0,5.0))
# print predict with model_volume
```
### Model Comparison
```{r}
# compare model_volume to model_constant
```
## Model with Price and Type
### Model
```{r}
# create model with type and volume
```
### Model Summary
```{r}
# print summary
```
### Plot Model
```{r}
# get and store coef(model_volume_type) as coefs
# print coefs
```
```{r}
#create scatter plots
plot(avocado_df$TotalVolume[avocado_df$Type=='organic'],avocado_df$AveragePrice[avocado_df$Type=='organic'], xlab= 'Total Volume', ylab = 'Average Price', col = 'black',xlim=c(4,19))
points(avocado_df$TotalVolume[avocado_df$Type=='conventional'],avocado_df$AveragePrice[avocado_df$Type=='conventional'], xlab= 'Total Volume', ylab = 'Average Price', col = 'grey')
#create lines
abline(coefs[1]+coefs[3],coefs[2], col='red')
abline(coefs[1],coefs[2], col='blue')
legend(x=15,y=3,legend=c('Organic','Conventional','Predicted Organic','Predicted Conventional'),
col=c("black", "grey",'red','blue'),pch=c(1,1,NA,NA),lty=c(NA,NA,1,1), cex = 0.7)
```
### Model Predictions
```{r}
unknown_df <- data.frame(TotalVolume=c(15.0,5.0), Type = c("conventional","organic"))
# print predict of unknown_df using model_volume_type
```
### Model Comparison
```{r}
# compare model_volume_type to model_constant
```
```{r}
# compare model_volume_type to model_volume
```
## Model with Volume and Type Interaction
### Model
```{r}
# create model TotalVolume:Type interaction term
# print model summary
```
### Model Summary
```{r}
```
### Model Alternative
```{r}
# create model with TotalVolume*Type
```
### Model Summary
```{r}
# print model summary
```
### Plot of Model
```{r}
# sort coef in coefs and print
```
```{r}
plot(avocado_df$TotalVolume[avocado_df$Type=='organic'],avocado_df$AveragePrice[avocado_df$Type=='organic'], xlab= 'Total Volume', ylab = 'Average Price', col = 'black',xlim=c(4,19))
points(avocado_df$TotalVolume[avocado_df$Type=='conventional'],avocado_df$AveragePrice[avocado_df$Type=='conventional'], xlab= 'Total Volume', ylab = 'Average Price', col = 'grey')
legend(x=15,y=3,legend=c('Organic','Conventional'),col=c("black", "grey"),pch=1)
abline(coefs[1],coefs[2], col='blue')
abline(coefs[1]+coefs[3],coefs[2]+coefs[4], col='red')
```
### Model Predictions
```{r}
unknown_df <- data.frame(TotalVolume=c(15.0,5.0), Type = c("conventional","organic"))
# do predict(model_volume_type_interaction,unknown_df)
```
### Model Comparison
```{r}
# compare to model_volume_type_interaction to model_constant
```
```{r}
# compare to model_volume_type_interaction to model_volume_type
```
## Model with Total Volume and Year
```{r}
# create model with TotalVolume and Year
```