-
Notifications
You must be signed in to change notification settings - Fork 1
/
house.r
123 lines (86 loc) · 3.23 KB
/
house.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
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
library("readxl")
read_excel("datahouse.xlsx")
house <- read_excel("datahouse.xlsx")
df <- data.frame(house)
data(df)
data (df)
data(house)
data(df)
head(df)
install.packages("caret")
library(caret)
# Simple linear regression model (lm means linear model)
# model <- train(mpg ~ wt,
# data = mtcars,
# method = "lm")
# Multiple linear regression model
model <- train(price ~ .,
data = df,
method = "lm")
# Ridge regression model
# model <- train(mpg ~ .,
# data = mtcars,
# method = "ridge") # Try using "lasso"
fitControl <- trainControl(method = "repeatedcv",
number = 10, # number of folds
repeats = 10) # repeated ten times
model.cv <- train(price ~ .,
data = df,
method = "lasso", # now we're using the lasso method
trControl = fitControl)
model.cv
library(caret)
fitControl <- trainControl(method = "repeatedcv",
number = 10, # number of folds
repeats = 10) # repeated ten times
model.cv <- train(mpg ~ .,
data = mtcars,
method = "lasso", # now we're using the lasso method
trControl = fitControl)
model.cv
install.packages(10-fold CV)
install.packages("10-fold CV")
fitControl <- trainControl(method = "repeatedcv",
number = 10, # number of folds
repeats = 10) # repeated ten times
model.cv <- train(price ~ .,
data = df,
method = "lasso", # now we're using the lasso method
trControl = fitControl)
model.cv
install.packages("elasticnet")
fitControl <- trainControl(method = "repeatedcv",
number = 10, # number of folds
repeats = 10) # repeated ten times
model.cv <- train(price ~ .,
data = df,
method = "lasso", # now we're using the lasso method
trControl = fitControl)
model.cv
fitControl <- trainControl(method = "repeatedcv",
number = 10, # number of folds
repeats = 10) # repeated ten times
lambdaGrid <- expand.grid(lambda = 10^seq(10, -2, length=100))
model.cv <- train(price ~ .,
data = df,
method = "lasso", # now we're using the lasso method
trControl = fitControl,
preProcess = c('scale', 'center'),
tuneGrid = lambdaGrid, # Test all the lambda values in the lambdaGrid dataframe
na.action = na.omit)
model.cv
fitControl <- trainControl(## 10-fold CV
method = "repeatedcv",
number = 10,
repeats = 10,
search = "random") # hyper-parameters random search
model.cv <- train(price ~ .,
data = df,
method = "ridge",
trControl = fitControl,
preProcess = c('scale', 'center'),
na.action = na.omit)
model.cv
ggplot(varImp(model.cv))
predictions <- predict(model.cv, df)
predictions