-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathsaratoga_lm.R
54 lines (45 loc) · 1.73 KB
/
saratoga_lm.R
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
library(tidyverse)
library(ggplot2)
library(modelr)
library(rsample)
library(mosaic)
data(SaratogaHouses)
glimpse(SaratogaHouses)
####
# Compare out-of-sample predictive performance
####
# Split into training and testing sets
saratoga_split = initial_split(SaratogaHouses, prop = 0.8)
saratoga_train = training(saratoga_split)
saratoga_test = testing(saratoga_split)
# Fit to the training data
# Sometimes it's easier to name the variables we want to leave out
# The command below yields exactly the same model.
# the dot (.) means "all variables not named"
# the minus (-) means "exclude this variable"
lm1 = lm(price ~ lotSize + bedrooms + bathrooms, data=saratoga_train)
lm2 = lm(price ~ . - pctCollege - sewer - waterfront - landValue - newConstruction, data=saratoga_train)
lm3 = lm(price ~ (. - pctCollege - sewer - waterfront - landValue - newConstruction)^2, data=saratoga_train)
coef(lm1) %>% round(0)
coef(lm2) %>% round(0)
coef(lm3) %>% round(0)
# Predictions out of sample
# Root mean squared error
rmse(lm1, saratoga_test)
rmse(lm2, saratoga_test)
rmse(lm3, saratoga_test)
# Can you hand-build a model that improves on all three?
# Remember feature engineering, and remember not just to rely on a single train/test split
out = do(100)*{
saratoga_split = initial_split(SaratogaHouses, prop = 0.8)
saratoga_train = training(saratoga_split)
saratoga_test = testing(saratoga_split)
lm2 = lm(price ~ (. - pctCollege - sewer - waterfront - landValue -
newConstruction), data=saratoga_train)
lm3 = lm(price ~ (. - pctCollege - sewer - waterfront - landValue -
newConstruction)^2, data=saratoga_train)
rmse2 = rmse(lm2, saratoga_test)
rmse3 = rmse(lm3, saratoga_test)
c(rmse2, rmse3)
}
colMeans(out)