-
Notifications
You must be signed in to change notification settings - Fork 2
/
PLS.R
192 lines (120 loc) · 3.84 KB
/
PLS.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
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
library(ISLR)
library(caret)
library(dplyr)
library(ggplot2)
library(funModeling)
library(PerformanceAnalytics)
library(pls)
df <- Hitters
df <- na.omit(df)
rownames(df) <- c()
train_indeks <- createDataPartition(df$Salary, p = 0.8, times = 1)
head(train_indeks)
train <- df[train_indeks$Resample1, ]
test <- df[-train_indeks$Resample1, ]
train_x <- train %>% dplyr::select(-Salary)
train_y <- train %>% dplyr::select(Salary)
test_x <- test %>% dplyr::select(-Salary)
test_y <- test %>% dplyr::select(Salary)
training <- data.frame(train_x, Salary = train_y)
plot_num(training)
summary(training)
profiling_num(training)
chart.Correlation(df %>% dplyr::select(-c("League", "NewLeague", "Division")))
#model kurulması
lm_fit <- lm(Salary~., data = training)
summary(lm_fit)
# model nesnesi içinden alabileceğimiz değerleri görmek için şunları kullanabiliriz
names(lm_fit)
attributes(lm_fit)
sonuc <- data.frame(obs=training$Salary, pred=lm_fit$fitted.values)
defaultSummary(sonuc)
pred = predict(lm_fit, test_x)
sonuc_test <- data.frame(obs=test_y$Salary , pred=pred)
defaultSummary(sonuc_test)
# model_validation
kontrol <- trainControl(method = "cv", number = 10)
#bu şekilde herhangi bir fonksiyonun bütün parametrelerini görebiliriz.
names(trainControl())
lm_val_fit <-
train(
x = train_x,
y = train_y$Salary,
method = "lm",
trControl = kontrol
)
lm_val_fit
summary(lm_val_fit)
names(lm_val_fit)
lm_val_fit$bestTune
lm_val_fit$finalModel
#principle component regression - PCR
pcr_fit <- pcr(Salary~., data=training, scale=T, validation="CV")
summary(pcr_fit)
validationplot(pcr_fit)
names(pcr_fit)
defaultSummary(data.frame(obs=training$Salary,
pred= as.vector(pcr_fit$fitted.values)))
predict(pcr_fit, test_x)
defaultSummary(data.frame(obs = training$Salary,
pred = as.vector(predict(
pcr_fit, test_x, ncomp = 1:3
))))
# öbyle bir döngü ilew kaç bileşende ne kadar etki olduğunu görebiliriz.
for (i in c(1:19)) {
print(as.character(i))
print(defaultSummary(data.frame(obs = test_y$Salary,
pred = as.vector(predict(
pcr_fit, test_x, ncomp = i
)))))
i = i + 1
}
#ben sürekli yukarıdaki gibi bir döngüyle uğraşmak istemediğim için
# bir fonksiyon yazıp daha sonra da gerektiğinde kullanmak istiyorum
pcr_tune <- function(model, x, y) {
num_of_iter=length(x)
for (i in c(1:num_of_iter)) {
cat("COMPONENTS:", i)
cat("\n")
print(defaultSummary(data.frame(obs = y[,1],
pred = as.vector(predict(
model, x, ncomp = i
)))))
cat("\n")
i = i + 1
if (i=num_of_iter) {
remove(i)
}
}
}
ayarlar <- pcr_tune(model=pcr_fit, x=test_x, y = test_y)
#yazdığmız fonksiyon düzgün çalışıyor
kontrol <- trainControl(method = "CV", number = 10)
set.seed(100)
pcr_ayar <- train(train_x, train_y$Salary,
method="pcr",
trContol = kontrol,
tuneLength = 20,
preProc = c("center", "scale")
)
pcr_ayar
pcr_ayar$finalModel
plot(pcr_ayar)
# PLS - PARTIAL LEAST SQUARES
pls_fit <- plsr(Salary~., data=training)
summary(pls_fit)
validationplot(pls_fit, val.type = "MSEP")
defaultSummary(data.frame(obs=test_y$Salary, pred=as.vector(
predict(pls_fit, test_x)
)))
kontrol <- trainControl(method = "CV", number = 10)
set.seed(100)
pls_ayar <- train(train_x, train_y$Salary,
method="pls",
trContol = kontrol,
tuneLength = 20,
preProc = c("center", "scale")
)
pls_ayar
pls_ayar$finalModel
plot(pls_ayar)