-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModels.Rmd
342 lines (287 loc) · 8.92 KB
/
Models.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
---
title: "Models"
author: "Gideon Wolf"
date: "4/15/2024"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(include = FALSE)
### From ReadData.R
library(tidyverse)
library(mice)
library(ggplot2)
library(glmnet)
library(caret)
library(tree)
library(randomForest)
library(missMethods)
library(leaps)
library(moments)
# Read in data, handle NAs
housetrain <- read.csv("train.csv")
housetest <- read.csv("test.csv")
housetest$SalePrice <- NA
house <- rbind(housetrain, housetest)
#ggplot(house) +
# geom_bar(aes(x=is.na(house)))
NAtoNone <- c(2,3,7,9,10,24,25,26,31,32,33,34,36,54,56,58,59,61,64,65,73,74,75,79)
for (i in NAtoNone) {
housetmp <- house
housetmp[i][is.na(housetmp[i])] <- "None"
house <- housetmp
}
house <- house[-1380,]
# house$Utilities <- factor(house$Utilities, levels = c("NoSeWa", "None", "AllPub"))
# house$MSZoning <- factor(house$MSZoning, levels = c("C (all)","FV","RH","RL","RM","None"))
house <- house %>% mutate_if(is.character, as.factor)
house <- house %>% mutate_at(c('MSSubClass','MoSold', 'OverallQual', 'OverallCond'), as_factor)
# Imputation
YrMoSold <- do.call(paste, c(sep='',list(house$YrSold),list(rep("-", times = length(house$YrSold))),list(house$MoSold),list(rep("-01", times = length(house$YrSold)))))
YrMoSold <- as_date(YrMoSold)
house <- as.data.frame(append(house, list(YrMoSold = YrMoSold), after = 78))
# Take out GarageYrBlt
house <- house[-60]
numhouse <- select_if(house, is.numeric)
numhouse <- numhouse[,-length(numhouse)]
imputed_house <- mice(numhouse,
m = 5,
method = 'pmm',
seed = 1)
#imputed_house <- impute_median(numhouse)
#house <- imputed_house
set.seed(1)
imphouse <- complete(imputed_house)
housetmp <- house
s = 1
for (i in names(imphouse)[2:length(names(imphouse))]) {
housetmp[i] <- imphouse[i]
}
house <- housetmp
###
house$Id <- NULL
testing <- house[1460:2918,]
house <- house[1:1459,]
```
# Outliers
```{r}
# housetmp <- select_if(house, is.numeric)
# housetmp1 <- house
# outliers <- vector("numeric")
# for (i in 1:length(housetmp)) {
# col <- housetmp[,i]
# z <- vector("numeric", length = length(col))
# for (j in 1:length(col)) {
# z[j] <- (col[j]-mean(col))/sd(col)
# outliers <- append(outliers, if(abs(z[j]) > 3) abs(z[j]))
# if (abs(z[j]) > 3) {
# housetmp1 <- housetmp1[-j,]
# #cat("Row",j,"was removed because",names(housetmp)[i],"was",z[j], "standard deviations from the standard normal mean of 0.\n")
# }
# }
# }
# hist(outliers, breaks = 20)
# house <- housetmp1
lows <- c()
colnaml <- c()
colnamh <- c()
highs <- c()
rownout <- c()
housetmp <- select_if(house, is.numeric)
housetmp <- housetmp[]
for (i in 1:(length(housetmp)-1)) {
column <- housetmp[[i]]
head(column)
p <- quantile(column, prob=c(.25,.5,.75))
IQR <- p[3]-p[1]
Q1 <- p[1]
Q3 <- p[3]
if (sum(column < (Q1-1.5*IQR))) {
colnaml <- append(colnaml, names(housetmp)[i])
lows <- append(lows, sum(column < (Q1-1.5*IQR)))
rownout <- append(rownout, which(column < (Q1-1.5*IQR)))
}
if (sum(column > (Q3+1.5*IQR))) {
colnamh <- append(colnamh, names(housetmp)[i])
highs <- append(highs, sum(column > (Q3+1.5*IQR)))
rownout <- append(rownout, which(column > (Q3+1.5*IQR)))
}
}
colnaml
colnamh
l <- data.frame(colnaml, lows)
l <- rename(l, colnam = colnaml)
h <- data.frame(colnamh, highs)
h <- rename(h, colnam = colnamh)
outs <- merge(l, h, by='colnam', all=TRUE)
skews <- c()
for (i in 1:nrow(outs)) {
g <- grep(outs$colnam[i], names(housetmp))
skews <- append(skews, skewness(housetmp[g]))
}
outs$skewness <- skews
outs_sort <- outs[order(outs$skewness, decreasing = TRUE),]
o <- rmarkdown::paged_table(outs_sort)
o
u <- c()
rown <- c()
for (i in 1:nrow(housetmp)) {
if (sum(rownout == i)>2) {
rown <- append(rown, i)
u <- append(u, sum(rownout == i))
}
}
u
rown
house <- house[-rown,]
# write.table(o, file = "outliertable.csv",row.names = FALSE)
```
```{r}
set.seed(66)
training <- mutate(house, id = row_number())
train <- sample_frac(training, .75)
test <- anti_join(training, train, by = 'id')
train <- dplyr::select(train, -id)
test <- dplyr::select(test, -id)
x_train <- model.matrix(SalePrice~.,train)#[,-length(train)]
y_train <- train[length(train)]$SalePrice
x_test <- model.matrix(SalePrice~., test)#[,-length(train)]
y_test <- test[length(train)]$SalePrice
```
Working on making Lasso, Ridge, and RF
## Using GLMNet to find Lasso CV model
```{r}
cv <- cv.glmnet(x_train,y_train, alpha=1,nfolds = 20)
plot(cv, xlim=c(5,11))
minlam1 = cv$lambda.min
print("Lasso CV")
print("Lambda: ")
minlam1
cvlas <- glmnet(x_train,y_train,family = "gaussian", alpha=1, lambda = minlam1)
cvpred <- predict(cvlas, s =minlam1, newx = x_test)
print("MSE: ")
mean((cvpred - y_test)^2)
# Dr. Khormali found: https://drbeane.github.io/_pages/courses/mth345/24%20-%20Lasso%20and%20Ridge.nb.html
# for cv lasso
y_train_pred_lasso <- predict(cvlas, x_train)
sse <- sum((y_train - y_train_pred_lasso)^2)
sst <- sum((y_train - mean(y_train))^2)
training_rsq <- 1 - sse / sst
y_test_pred_lasso <- cvpred
sse <- sum((y_test - y_test_pred_lasso)^2)
sst <- sum((y_test - mean(y_test))^2)
testing_rsq <- 1 - sse / sst
lasso_rsq <- c(training_rsq, testing_rsq)
names(lasso_rsq) <- c('Training Rsq', 'Testing Rsq')
lasso_rsq
# https://stats.stackexchange.com/questions/25817/is-it-possible-to-calculate-aic-and-bic-for-lasso-regression-models
fit <- cvlas
tLL <- fit$nulldev - deviance(fit)
k <- fit$df
n <- fit$nobs
AICc <- -tLL+2*k+2*k*(k+1)/(n-k-1)
print("AIC: ")
AICc
```
```{r}
cvimp <- varImp(cvlas, scale = FALSE, lambda = minlam1)
cvimp
plot(cvimp, top = 20)
#model_subset <- regsubsets(SalePrice ~ ., nbest = 3, data = train, really.big = TRUE, nvmax = 50)
#plot(model_subset)
```
## Using GLMNet to find Ridge Regression Model
```{r}
# use variable selection to get subset of variables to find another model
rid <- cv.glmnet(x_train,y_train, alpha = 0, lambda = exp(seq(from = 5,to = 15,length = 100)))
plot(rid, xlim=c(5,15))
minlam2 = rid$lambda.min
print("Ridge Regression")
print("Lambda: ")
minlam2
ridge <- glmnet(x_train,y_train,family = "gaussian", alpha=0, lambda = minlam2)
ridpred <- predict(ridge, s =minlam2, newx = x_test)
print("MSE: ")
mean((ridpred - y_test)^2)
# Dr. Khormali found: https://drbeane.github.io/_pages/courses/mth345/24%20-%20Lasso%20and%20Ridge.nb.html
# for cv lasso
y_train_pred_lasso <- predict(ridge, x_train)
sse <- sum((y_train - y_train_pred_lasso)^2)
sst <- sum((y_train - mean(y_train))^2)
training_rsq <- 1 - sse / sst
y_test_pred_lasso <- ridpred
sse <- sum((y_test - y_test_pred_lasso)^2)
sst <- sum((y_test - mean(y_test))^2)
testing_rsq <- 1 - sse / sst
lasso_rsq <- c(training_rsq, testing_rsq)
names(lasso_rsq) <- c('Training Rsq', 'Testing Rsq')
lasso_rsq
# https://stats.stackexchange.com/questions/25817/is-it-possible-to-calculate-aic-and-bic-for-lasso-regression-models
fit <- ridge
tLL <- fit$nulldev - deviance(fit)
k <- fit$df
n <- fit$nobs
AICc <- -tLL+2*k+2*k*(k+1)/(n-k-1)
print("AIC: ")
AICc
```
```{r}
rf <- randomForest(SalePrice ~ .,
data = train,
mtry = 9,
ntree = 200,
importance = TRUE)
i <- importance(rf, scale=TRUE)
out <- names(which(i[,1] > 10))
out
rfpred <- predict(rf, newdata = test)
cat("MSE = ",mean((y_test - rfpred)^2))
plot(rf,
main = "Random Forest without Variable Selection Levels Off near 200")
```
# EXPERIMENTING WITH TAKING OUT OUTLIERS AND VARIABLE SELECTION
```{r test}
a <- c()
for (i in out) {
a <- append(a, grep(i, names(house)))
}
house <- house[,-a]
set.seed(66)
training <- mutate(house, id = row_number())
train <- sample_frac(training, .75)
test <- anti_join(training, train, by = 'id')
train <- dplyr::select(train, -id)
test <- dplyr::select(test, -id)
x_train <- model.matrix(SalePrice~.-1, train)[,-length(train)]
y_train <- train[length(train)]$SalePrice
x_test <- model.matrix(SalePrice~.-1, test)[,-length(test)]
y_test <- test[length(test)]$SalePrice
rfvs <- randomForest(SalePrice ~ .,
data = train,
mtry = 9,
ntree = 300,
importance = TRUE)
importance(rfvs, scale=TRUE)
rfpredvs <- predict(rfvs, newdata = test)
cat("MSE = ",mean((y_test - rfpredvs)^2))
plot(rfvs,
main = "Random Forest with Variable Selection Levels Off near 300")
```
```{r}
sqrt(mean((y_test - ridpred)^2))
sqrt(mean((y_test - cvpred)^2))
sqrt(mean((y_test - rfpred)^2))
sqrt(mean((y_test - rfpredvs)^2))
```
# Testing
```{r}
SalePrice <- predict(rf, newdata = testing)
Id <- 1461:2919
results <- data.frame(Id,SalePrice)
write.csv(results, "y_predicitons_rf.csv", row.names = FALSE)
```
```{r}
x_pred <- model.matrix(SalePrice~., testing)
SalePrice <- predict(cvlas,s=minlam1, newx = x_pred,)
results <- data.frame(Id,SalePrice)
write.csv(results, "y_predicitons_lasso.csv", row.names = FALSE)
```