forked from MariekeDirk/ML_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLin_Reg.Rmd
More file actions
157 lines (105 loc) · 3.29 KB
/
Lin_Reg.Rmd
File metadata and controls
157 lines (105 loc) · 3.29 KB
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
---
title: "Lin_Reg"
author: "Eva Kleingeld"
date: "December 15, 2016"
output: html_document
---
First clear your working environment and install all necessary packages
```{r}
rm(list=ls())
# Install packages
#install.packages("caret")
```
Now load in the test and train sets and split them into target and predictor sets
```{r}
# Load train
load("/usr/people/kleingel/Projects/MLProject/Train_BIG_noCat.Rda")
# Load test
load("/usr/people/kleingel/Projects/MLProject/Test_BIG_noCat.Rda")
# Split train set into target and predictors
Target_Train <- Train_set$TRoad
Train_set <- subset(Train_set, select=-c(TRoad))
# Split test set into target and predictors
Target_Test <- Test_set$TRoad
Test_set <- subset(Test_set, select = -c(TRoad))
```
# Build a linear model
To test:
Can you supply mutiple metrics? metric = c("RMSE", "Rsquared")
Possible cross-validation options:
cv = K-fold cross validation
repeatedcv = K-fold cross validation, repeated a certain nr. of times (why do this?)
LGOCV = leave group out cross validation
boot = bootstrap
oob = out of bag (for Ranfom Forest)
```{r}
library(caret)
library(doParallel)
library(parallel)
cluster_1<-makeCluster(3)
registerDoParallel(cluster_1)
getDoParWorkers()
control<-trainControl(method="repeatedcv",repeats=5,number=10, allowParallel = TRUE)
LinearModel <- train(x = Train_set,
y = Target_Train,
method = "lm",
trControl = control,
tuneLength = 10
)
# Stop the cluster
stopCluster(cluster_1)
registerDoSEQ()
summary(LinearModel)
LinearModel$results
# Predict
LM_Predict <- extractPrediction(models = list(LinearModel), testX = Test_set, testY = Target_Test)
# Plot observed versus predicted and save to a PDF
png("LinReg_ObsPred")
plotObsVsPred(LM_Predict)
dev.off()
# Calculate the residuals for the train/test sets
LM_Residuals <- (LM_Predict$obs - LM_Predict$pred)
# Plot the residuals versus the observed values
png("LinReg_ResPlot")
plot(LM_Predict$obs,LM_Residuals,
ylab="Residuals", xlab="Observed road temperature",
main="Residual plot")
abline(0, 0)
dev.off()
#Save the model
save(LinearModel, file = "/usr/people/kleingel/Projects/MLProject/LinearModel.Rda")
```
```{r}
library(caret)
library(doParallel)
library(parallel)
#library(foreach)
library(kernlab)
#cl<-makeCluster(7)
registerDoParallel(3)
#getDoParWorkers()
sigmaRangeReduced<-sigest(as.matrix(Target_Train))[1]
svmRadialRGridReduced<-expand.grid(C=2^(seq(-4,4)))
control_2<-trainControl(method="cv", number=10, allowParallel = TRUE)
SVMradModel <- train(x = Train_set,
y = Target_Train,
method = "svmRadialCost",
trControl = control_2)
# tuneGrid = svmRadialRGridReduced)
# ,
# trControl = control)
stopCluster(cluster)
registerDoSEQ()
# control<-trainControl(method="repeatedcv",repeats=5,number=10)
# rfgridreduced <-expand.grid(mtry=2:3)
# length <- 10
#
# RFModel <- train(x = Train_set,
# y = Target_Train,
# method = "parRF",
# tuneGrid = rfgridreduced,
# importance = FALSE)
#
#
#
```