-
Notifications
You must be signed in to change notification settings - Fork 1
/
linear_model_exercises_solution.Rmd
165 lines (115 loc) · 4.46 KB
/
linear_model_exercises_solution.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
---
title: "R Notebook"
output: html_notebook
---
# Exercise 0
0. Load data set avocado_df.csv and run the code below to format the dataset.
```{r}
library(readr)
avocado_df<-read_csv('avocado_df.csv')
```
```{r}
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)
```
# Exercise 1
1. Create a linear model of Price as a function of Total Volume and Year.
```{r}
# Solution here
model_volume_year <- lm(AveragePrice ~ 1 + TotalVolume + Year, data=avocado_df)
```
2. Print the summary. Interpret the output.
```{r}
# Solution here
summary(model_volume_year)
```
3a. Get the coefficients of the model and store them as "coefs".
```{r}
# Solution here
coefs <- coef(model_volume_year)
```
3b. Run the code below and understand what it is doing.
```{r}
plot(avocado_df$TotalVolume[avocado_df$Year==2015],avocado_df$AveragePrice[avocado_df$Year==2015], xlab= 'Total Volume', ylab = 'Average Price', col = 'black',xlim=c(4,19))
points(avocado_df$TotalVolume[avocado_df$Year==2016],avocado_df$AveragePrice[avocado_df$Year==2016], xlab= 'Total Volume', ylab = 'Average Price', col = 'grey')
points(avocado_df$TotalVolume[avocado_df$Year==2017],avocado_df$AveragePrice[avocado_df$Year==2017], xlab= 'Total Volume', ylab = 'Average Price', col = 'red')
points(avocado_df$TotalVolume[avocado_df$Year==2018],avocado_df$AveragePrice[avocado_df$Year==2018], xlab= 'Total Volume', ylab = 'Average Price', col = 'blue')
legend(x=15,y=3,legend=c('2015','2016','2017','2018'),col=c("black", "grey","red","blue"),pch=1)
abline(coefs[1],coefs[2], col='black')
abline(coefs[1]+coefs[3],coefs[2], col='grey')
abline(coefs[1]+coefs[4],coefs[2], col='red')
abline(coefs[1]+coefs[5],coefs[2], col='blue')
```
4a. Compare this model to the null model using ANOVA.
```{r}
model_constant <- lm(AveragePrice ~ 1 , data=avocado_df)
# Solution here
anova(model_constant, model_volume_year)
```
4b. Compare this model to the model with total volume only using ANOVA.
```{r}
model_volume<- lm(AveragePrice ~ 1 + TotalVolume, data=avocado_df)
# Solution here
anova(model_volume, model_volume_year)
```
5. Predicted the price of the samples in "unknown_df".
```{r}
unknown_df <- data.frame(TotalVolume=c(15.0,5.0), Type = c("conventional","organic"), Year=c("2016","2015"))
predict(model_volume_year, unknown_df)
```
# Exercise 2
1. Create a linear model of Price as a function of Total Volume, Type, Year, and Total Volume and Type interaction.
```{r}
# Solution here
model_year_total_volume_type_interaction <- lm(AveragePrice ~ 1 + Year + TotalVolume*Type, data=avocado_df)
```
2. Print the summary. Interpret the output.
```{r}
# Solution here
summary(model_year_total_volume_type_interaction)
```
3. Get the coefficients of the model and store them as "coefs".
```{r}
# Solution here
coefs<- coef(model_year_total_volume_type_interaction)
```
4a. Compare this model to the null model using ANOVA.
```{r}
model_constant <- lm(AveragePrice ~ 1 , data=avocado_df)
# Solution here
anova(model_constant,model_year_total_volume_type_interaction)
```
4b. Compare this model to the model with total volume only using ANOVA.
```{r}
model_volume<- lm(AveragePrice ~ 1 + TotalVolume, data=avocado_df)
# Solution here
anova(model_volume,model_year_total_volume_type_interaction)
```
4c. Compare this model to the model with Total Volume and Type using ANOVA.
```{r}
model_volume_type <- lm(AveragePrice ~ 1 + TotalVolume + Type, data=avocado_df)
# Solution here
anova(model_volume_type,model_year_total_volume_type_interaction)
```
4d. Compare this model to the model with Total Volume and Type interaction using ANOVA.
```{r}
model_volume_type_interaction <- lm(AveragePrice ~ 1 + TotalVolume*Type, data=avocado_df)
# Solution here
anova(model_volume_type_interaction,model_year_total_volume_type_interaction)
```
4e. Compare this model to the model with Total Volume and Year using ANOVA.
```{r}
model_year <- lm(AveragePrice ~ 1 + Year, data=avocado_df)
# Solution here
anova(model_year,model_year_total_volume_type_interaction)
```
5. Predicted the price of the samples in "unknown_df".
```{r}
# Solution here
unknown_df <- data.frame(TotalVolume=c(15.0,5.0), Type = c("conventional","organic"), Year=c("2016","2015"))
predict(model_year_total_volume_type_interaction,unknown_df)
```