-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.R
81 lines (69 loc) · 2.98 KB
/
train.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
library(tidyverse)
library(randomForest)
library(caret)
#index for partition: test and train
trainIndex = createDataPartition(dsOutRemoved$count, p = .80, list = FALSE)
#multiple regression model for continuous variables
#for casual
dsContCas <- dsOutRemoved[trainIndex,1:11]
dsContCasTest <- dsOutRemoved[-trainIndex,1:11]
#removing season due to feature selection chi-test
dsContCas$season <- NULL
dsContCasTest$season <- NULL
mlrmodelCas <- lm(casual ~., data = dsContCas)
summary(mlrmodelCas)
#for registered
dsContReg <- dsOutRemoved[trainIndex,1:10]
dsContRegTest <- dsOutRemoved[-trainIndex,1:10]
dsContReg$registered <- dsOutRemoved[trainIndex,13]
dsContRegTest$registered <- dsOutRemoved[-trainIndex,13]
mlrmodelReg <- lm(registered ~., data = dsContReg)
summary(mlrmodelReg)
#random forest for categorical variables
#for casual
dsCatCas <- dsOutRemoved[trainIndex,1:11]
dsCatCasTest <- dsOutRemoved[-trainIndex,1:11]
#removing season due to feature selection chi-test
dsCatCas$season <- NULL
dsCatCasTest$season <- NULL
#keeping categorical variables as categorical
dsCatCas <- transform(dsCatCas,
year = as.factor(year),
month = as.factor(month),
holiday = as.factor(holiday),
weekday = as.factor(weekday),
workingday = as.factor(workingday),
weatherSituation = as.factor(weatherSituation))
dsCatCasTest <- transform(dsCatCasTest,
year = as.factor(year),
month = as.factor(month),
holiday = as.factor(holiday),
weekday = as.factor(weekday),
workingday = as.factor(workingday),
weatherSituation = as.factor(weatherSituation))
rfmodelCas <- randomForest(casual ~ ., data = dsCatCas)
print(rfmodelCas)
#for registered
dsCatReg <- dsOutRemoved[trainIndex,1:10]
dsCatRegTest <- dsOutRemoved[-trainIndex,1:10]
dsCatReg$registered <- dsOutRemoved[trainIndex,13]
dsCatRegTest$registered <- dsOutRemoved[-trainIndex,13]
#keeping categorical variables as categorical
dsCatReg <- transform(dsCatReg,
season = as.factor(season),
year = as.factor(year),
month = as.factor(month),
holiday = as.factor(holiday),
weekday = as.factor(weekday),
workingday = as.factor(workingday),
weatherSituation = as.factor(weatherSituation))
dsCatRegTest <- transform(dsCatRegTest,
season = as.factor(season),
year = as.factor(year),
month = as.factor(month),
holiday = as.factor(holiday),
weekday = as.factor(weekday),
workingday = as.factor(workingday),
weatherSituation = as.factor(weatherSituation))
rfmodelReg <- randomForest(registered ~ ., data = dsCatReg)
print(rfmodelReg)