You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 04-ames.Rmd
+61-13Lines changed: 61 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ tidymodels_prefer()
9
9
10
10
# The Ames housing data {#ames}
11
11
12
-
The Ames housing data set [@ames] is an excellent resource for learning about models that we will use throughout this book. It contains data on `r format(nrow(ames), big.mark = ",")` properties in Ames, Iowa, including columns related to
12
+
The Ames housing data set [@ames] is an excellent resource for learning about models that we will use throughout this book. It contains data on `r format(nrow(ames), big.mark = ",")` properties in Ames, Iowa, including columns related to:
13
13
14
14
* house characteristics (bedrooms, garage, fireplace, pool, porch, etc.),
15
15
* location (neighborhood),
@@ -43,24 +43,42 @@ dim(ames)
43
43
44
44
## Exploring important features
45
45
46
-
It makes sense to start with the outcome we want to predict: the last sale price of the house (in USD):
46
+
Let's start with the outcome we want to predict: the last sale price of the house (in USD), as shown in Figure \@ref(fig:ames-sale-price).
The data are right-skewed; there are more inexpensive houses than expensive ones. The median sale price was \$`r format(median(ames$Sale_Price), big.mark = ",")` and the most expensive house was \$`r format(max(ames$Sale_Price), big.mark = ",")`. When modeling this outcome, a strong argument can be made that the price should be log-transformed. The advantages of doing this are that no houses would be predicted with negative sale prices and that errors in predicting expensive houses will not have an undue influence on the model. Also, from a statistical perspective, a logarithmic transform may also _stabilize the variance_ in a way that makes inference more legitimate. Let's visualize the transformed data:
#| fig.cap = "Sale prices of houses in Ames, Iowa.",
62
+
#| fig.alt = "A histogram of the sale prices of houses in Ames, Iowa. The distribution has a long right tail."
63
+
```
64
+
65
+
The data are right-skewed; there are more inexpensive houses than expensive ones. The median sale price was \$`r format(median(ames$Sale_Price), big.mark = ",")` and the most expensive house was \$`r format(max(ames$Sale_Price), big.mark = ",")`. When modeling this outcome, a strong argument can be made that the price should be log-transformed. The advantages of doing this are that no houses would be predicted with negative sale prices and that errors in predicting expensive houses will not have an undue influence on the model. Also, from a statistical perspective, a logarithmic transform may also _stabilize the variance_ in a way that makes inference more legitimate. Figure \@ref(fig:ames-log-sale-price-hist) visualizes the transformed data.
#| fig.cap = "Sale prices of houses in Ames, Iowa after a log (base 10) transformation.",
79
+
#| fig.alt = "A histogram of the sale prices of houses in Ames, Iowa after a log (base 10) transformation. The distribution, while not perfectly symmetric, exhibits far less skewness."
80
+
```
81
+
64
82
While not perfect, this will probably result in better models than using the untransformed data.
65
83
66
84
:::rmdwarning
@@ -75,44 +93,74 @@ Despite these drawbacks, the models used in this book utilize the log transforma
Another important aspect of these data for our modeling are their geographic locations. This spatial information is contained in the data in two ways: a qualitative `Neighborhood` label as well as quantitative longitude and latitude data. To visualize the spatial information, let's use both together to plot the data on a map and color by neighborhood:
96
+
Another important aspect of these data for our modeling are their geographic locations. This spatial information is contained in the data in two ways: a qualitative `Neighborhood` label as well as quantitative longitude and latitude data. To visualize the spatial information, let's use both together to plot the data on a map and color by neighborhood in Figure \@ref(fig:ames-map).
#| fig.alt = "A scatter plot of house locations in Ames superimposed over a street map. There is a significant area in the center of the map where no homes were sold."
81
104
# See file extras/ames_sf.R
82
105
knitr::include_graphics("premade/ames.png")
83
106
```
84
107
85
-
We can see a few noticeable patterns. First, there is a void of data points in the center of Ames. This corresponds to Iowa State University. Second, while there are a number of neighborhoods that are geographically isolated, there are others that are adjacent to each other. For example, Timberland is located apart from almost all other neighborhoods:
108
+
We can see a few noticeable patterns. First, there is a void of data points in the center of Ames. This corresponds to Iowa State University. Second, while there are a number of neighborhoods that are geographically isolated, there are others that are adjacent to each other. For example, as Figure \@ref(fig:ames-timberland) shows, Timberland is located apart from almost all other neighborhoods.
#| fig.alt = "A scatter plot of locations of homes in Timberland, located in the southern part of Ames."
88
116
# See file extras/ames_sf.R
89
117
knitr::include_graphics("premade/timberland.png")
90
118
```
91
119
92
-
The Meadow Village neighborhood in Southwest Ames is like an island of properties ensconced inside the sea of properties that make up the Mitchell neighborhood:
120
+
Figure \@ref(fig:ames-mitchell) visualizes how the Meadow Village neighborhood in Southwest Ames is like an island of properties ensconced inside the sea of properties that make up the Mitchell neighborhood.
#| fig.cap = "Locations of homes in Meadow Village and Mitchell.",
127
+
#| fig.alt = "A scatter plot of locations of homes in Meadow Village and Mitchell. The small number of Meadow Village properties are enclosed inside the the ones labeled as being in Mitchell."
95
128
# See file extras/ames_sf.R
96
129
knitr::include_graphics("premade/mitchell.png")
97
130
```
98
131
99
-
A detailed inspection of the map also shows that the neighborhood labels are not completely reliable. For example, there are some properties labeled as being in Northridge that are surrounded by houses in the adjacent Somerset neighborhood:
132
+
A detailed inspection of the map also shows that the neighborhood labels are not completely reliable. For example, Figure \@ref(fig:ames-northridge) shows there are some properties labeled as being in Northridge that are surrounded by homes in the adjacent Somerset neighborhood.
#| fig.cap = "Locations of homes in Somerset and Northridge.",
139
+
#| fig.alt = "A scatter plot of locations of homes in Somerset and Northridge. There are a few homes in Somerset mixed in the periphery of Northridge (and vice versa)."
102
140
# See file extras/ames_sf.R
103
141
knitr::include_graphics("premade/northridge.png")
104
142
```
105
143
106
-
Also, there are ten isolated houses labeled as being in Crawford but are not close to the majority of the other houses in that neighborhood:
144
+
Also, there are ten isolated homes labeled as being in Crawford that you can see in Figure \@ref(fig:ames-crawford) but are not close to the majority of the other homes in that neighborhood:
#| fig.alt = "A scatter plot of locations of homes in Crawford. There is a large cluster of homes to the west of a small, separate cluster of properties also labeled as Crawford."
109
152
# See file extras/ames_sf.R
110
153
knitr::include_graphics("premade/crawford.png")
111
154
```
112
155
113
-
Also notable is the "Iowa Department of Transportation (DOT) and Rail Road" neighborhood adjacent to the main road on the east side of Ames. There are several clusters of houses within this neighborhood as well as some longitudinal outliers; the two houses furthest east are isolated from the other locations.
156
+
Also notable is the "Iowa Department of Transportation (DOT) and Rail Road" neighborhood adjacent to the main road on the east side of Ames, shown in Figure \@ref(fig:ames-dot_rr). There are several clusters of homes within this neighborhood as well as some longitudinal outliers; the two homes furthest east are isolated from the other locations.
#| fig.cap = "Homes labeled as 'Iowa Department of Transportation (DOT) and Rail Road'.",
163
+
#| fig.alt = "A scatter plot of locations of homes labeled as 'Iowa Department of Transportation (DOT) and Rail Road'. The longitude distribution is right-skewed with a few outlying properties."
Copy file name to clipboardExpand all lines: 05-data-spending.Rmd
+5-2Lines changed: 5 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -58,7 +58,10 @@ These objects are data frames with the same _columns_ as the original data but o
58
58
59
59
Simple random sampling is appropriate in many cases but there are exceptions. When there is a dramatic _class imbalance_ in classification problems, one class occurs much less frequently than another. Using a simple random sample may haphazardly allocate these infrequent samples disproportionately into the training or test set. To avoid this, _stratified sampling_ can be used. The training/test split is conducted separately within each class and then these subsamples are combined into the overall training and test set. For regression problems, the outcome data can be artificially binned into _quartiles_ and then stratified sampling conducted four separate times. This is an effective method for keeping the distributions of the outcome similar between the training and test set.
60
60
61
-
```{r ames-sale-price, echo = FALSE, fig.cap = "The distribution of the sale price (in log units) for the Ames housing data. The vertical lines indicate the quartiles of the data."}
61
+
```{r ames-sale-price, echo = FALSE}
62
+
#| fig.cap = "The distribution of the sale price (in log units) for the Ames housing data. The vertical lines indicate the quartiles of the data.",
63
+
#| fig.alt = "The distribution of the sale price (in log units) for the Ames housing data. The vertical lines indicate the quartiles of the data."
0 commit comments