-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW03.Rmd
63 lines (46 loc) · 1.35 KB
/
HW03.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
---
title: "HW03"
output: html_document
date: "2023-03-27"
---
```{r}
## 0. Prepare
df <- read.csv("BostonHousing.csv")
df
# 변수 500개 선택
house.df <- df[1:500,]
# 제시 조건 제외 변수 제외시키기
selected.var <- c(1, 2, 4, 5, 6, 7, 8, 9, 11, 12, 13)
# 데이터 분할
set.seed(1)
train.index <- sample(c(1:500), 300)
train.df <- house.df[train.index, selected.var]
valid.df <- house.df[-train.index, selected.var]
house.lm <- lm(MEDV ~., data = house.df)
options(scipen = 999)
summary(house.lm)
## 1. Exhaustive Search
library(leaps)
MEDV <- as.data.frame(model.matrix(~ 0 + MEDV, data=train.df))
train.df1 <- cbind(train.df[,-4], MEDV[,-1])
head(train.df1)
search <- regsubsets(MEDV ~ ., data = train.df1, nbest = 1, nvmax = dim(train.df1)[2],
method = "exhaustive")
sum <- summary(search)
sum$which
sum$rsq
sum$adjr2
## 2. forward Selection
house.lm.null <- lm(MEDV~1, data = house.df)
# use step() to run forward selection
house.lm.step <- step(house.lm.null,
scope=list(lower=house.lm.null, upper=house.lm), direction =
"forward")
summary(house.lm.step)
## 3. Backward Elimination
house.lm.step <- step(house.lm, direction = 'backward')
summary(house.lm.step)
## 4. Stepwise Selection
house.lm.step <- step(house.lm, direction = 'both')
summary(house.lm.step)
```