-
Notifications
You must be signed in to change notification settings - Fork 221
/
eda_ames_houseprice.Rmd
352 lines (239 loc) · 6.79 KB
/
eda_ames_houseprice.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# 探索性数据分析-ames房屋价格 {#eda-ames-houseprice}
```{r, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
warning = FALSE,
message = FALSE,
fig.showtext = TRUE
)
```
## 数据故事
```{r eda-ames-houseprice-1, out.width = '75%', echo = FALSE, fig.cap = "这是数据故事的地图"}
knitr::include_graphics("images/ames.png")
```
这是一份**Ames**房屋[数据](https://www.kaggle.com/c/house-prices-advanced-regression-techniques),您可以把它想象为房屋中介推出的成都市武侯区、锦江区以及高新区等各区县的房屋信息
```{r eda-ames-houseprice-2, message=FALSE, warning=FALSE}
library(tidyverse)
ames <- read_csv("./demo_data/ames_houseprice.csv") %>%
janitor::clean_names()
glimpse(ames)
```
感谢曾倬同学提供的解释说明文档
```{r eda-ames-houseprice-3}
explanation <- readxl::read_excel("./demo_data/ames_houseprice_explanation.xlsx")
explanation %>%
knitr::kable()
```
## 探索设想
- 读懂数据描述,比如
- 房屋设施 (bedrooms, garage, fireplace, pool, porch, etc.),
- 地理位置 (neighborhood),
- 土地信息 (zoning, shape, size, etc.),
- 品相等级
- 出售价格
- 探索影响房屋价格的因素
- 必要的预处理(缺失值处理、标准化、对数化等等)
- 必要的可视化(比如价格分布图等)
- 必要的统计(比如各地区房屋价格的均值)
- 合理选取若干预测变量,建立多元线性模型,并对模型结果给出解释
- 房屋价格与预测变量(房屋大小、在城市的位置、房屋类型、与街道的距离)
## 变量选取
我们选取下列变量:
- lot_frontage, 建筑离街道的距离
- lot_area, 占地面积
- neighborhood, 建筑在城市的位置
- gr_liv_area, 地上居住面积
- bldg_type, 住宅类别(联排别墅、独栋别墅...)
- year_built 房屋修建日期
```{r eda-ames-houseprice-4}
d <- ames %>%
select(sale_price,
lot_frontage,
lot_area,
neighborhood,
gr_liv_area,
bldg_type,
year_built
)
d
```
## 缺失值处理
```{r eda-ames-houseprice-5}
d %>%
summarise(
across(everything(), function(x) sum(is.na(x)) )
)
```
找出来看看
```{r eda-ames-houseprice-6}
d %>%
filter_all(
any_vars(is.na(.))
)
```
```{r eda-ames-houseprice-7, eval=FALSE}
library(visdat)
d %>% vis_dat()
```
如果不选择`lot_frontage` 就不会有缺失值,如何选择,自己抉择
```{r eda-ames-houseprice-8, eval=FALSE}
d %>%
select(-lot_frontage) %>%
visdat::vis_dat()
```
我个人觉得这个变量很重要,所以还是保留,牺牲一点样本量吧
```{r eda-ames-houseprice-9}
d <- d %>%
drop_na()
```
```{r eda-ames-houseprice-10, eval=FALSE}
d %>% visdat::vis_dat()
```
## 预处理
- 标准化
```{r eda-ames-houseprice-11}
standard <- function(x) {
(x - mean(x)) / sd(x)
}
d %>%
mutate(
across(where(is.numeric), standard),
across(where(is.character), as.factor)
)
```
- 对数化
```{r eda-ames-houseprice-12}
d %>%
mutate(
log_sale_price = log(sale_price)
)
```
```{r eda-ames-houseprice-13}
d %>%
mutate(
across(where(is.numeric), log),
across(where(is.character), as.factor)
)
```
- 标准化 vs 对数化
选择哪一种,我们看图说话
```{r eda-ames-houseprice-14}
d %>%
ggplot(aes(x = sale_price)) +
geom_density()
```
```{r eda-ames-houseprice-15}
d %>%
ggplot(aes(x = log(sale_price))) +
geom_density()
```
我们选择对数化,并保存结果
```{r eda-ames-houseprice-16}
d <- d %>%
mutate(
across(where(is.numeric),
.fns = list(log = log),
.names = "{.fn}_{.col}"
),
across(where(is.character), as.factor)
)
```
## 有趣的探索
### 各区域的房屋价格均值
```{r eda-ames-houseprice-17}
d %>% count(neighborhood)
```
```{r eda-ames-houseprice-18}
d %>%
group_by(neighborhood) %>%
summarise(
mean_sale = mean(sale_price)
) %>%
ggplot(
aes(x = mean_sale, y = fct_reorder(neighborhood, mean_sale))
) +
geom_col(aes(fill = mean_sale < 150000), show.legend = FALSE) +
geom_text(aes(label = round(mean_sale, 0)), hjust = 1) +
# scale_x_continuous(
# expand = c(0, 0),
# breaks = c(0, 100000, 200000, 300000),
# labels = c(0, "1w", "2w", "3w")
# ) +
scale_x_continuous(
expand = c(0, 0),
labels = scales::dollar
) +
scale_fill_viridis_d(option = "D") +
theme_classic() +
labs(x = NULL, y = NULL)
```
### 房屋价格与占地面积
```{r eda-ames-houseprice-19}
d %>%
ggplot(aes(x = log_lot_area, y = log_sale_price)) +
geom_point(colour = "blue") +
geom_smooth(method = lm, se = FALSE, formula = "y ~ x")
```
```{r eda-ames-houseprice-20}
d %>%
ggplot(aes(x = log_lot_area, y = log_sale_price)) +
geom_point(aes(colour = neighborhood)) +
geom_smooth(method = lm, se = FALSE, formula = "y ~ x")
```
```{r eda-ames-houseprice-21}
d %>%
ggplot(aes(x = log_lot_area, y = log_sale_price)) +
geom_point(colour = "blue") +
geom_smooth(method = lm, se = FALSE, formula = "y ~ x", fullrange = TRUE) +
facet_wrap(~neighborhood) +
theme(strip.background = element_blank())
```
### 房屋价格与房屋居住面积
```{r eda-ames-houseprice-22}
d %>%
ggplot(aes(x = log_gr_liv_area, y = log_sale_price)) +
geom_point(aes(colour = neighborhood)) +
geom_smooth(method = lm, se = FALSE, formula = "y ~ x")
```
```{r eda-ames-houseprice-23}
d %>%
ggplot(aes(x = log_gr_liv_area, y = log_sale_price)) +
geom_point() +
geom_smooth(method = lm, se = FALSE, formula = "y ~ x", fullrange = TRUE) +
facet_wrap(~neighborhood) +
theme(strip.background = element_blank())
```
### 车库与房屋价格
车库大小是否对销售价格有帮助?
```{r}
ames %>%
#select(garage_cars, garage_area, sale_price) %>%
ggplot(aes(x = garage_area, y = sale_price)) +
geom_point(
data = select(ames, -garage_cars),
color = "gray50"
) +
geom_point(aes(color = as_factor(garage_cars))) +
facet_wrap(vars(garage_cars)) +
theme(legend.position = "none") +
ggtitle("This is the influence of garage for sale price")
```
## 建模
```{r eda-ames-houseprice-24}
lm(log_sale_price ~ 1 + log_gr_liv_area + neighborhood, data = d) %>%
broom::tidy()
```
```{r eda-ames-houseprice-25}
library(lme4)
lmer(log_sale_price ~ 1 + log_gr_liv_area + (log_gr_liv_area | neighborhood),
data = d) %>%
broom.mixed::tidy()
```
```{r eda-ames-houseprice-26, echo = F}
# remove the objects
# ls() %>% stringr::str_flatten(collapse = ", ")
rm(ames, d, explanation)
```
```{r eda-ames-houseprice-27, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```