-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.R
190 lines (148 loc) · 6.57 KB
/
models.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
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
# External script to load vars needed for Netbook study case
## @knitr rfModels
#Set seed to remember the randomization
rfControl <- trainControl(method = "repeatedcv",
number = 10,
repeats = 3,
search="random")
set.seed(33)
#Creating RF model
rfModelNetbook <- train(Volume~., data = trainingNetbook,
method = "rf",
trControl=rfControl,
tuneLength=10,
importance=T)
set.seed(616)
#Creating RF model
rfModelPC <- train(Volume~., data = trainingPC,
method = "rf",
trControl=rfControl,
tuneLength=10,
importance=T)
set.seed(44)
#Creating RF model
rfModelLaptop <- train(Volume~., data = trainingLaptop,
method = "rf",
trControl=rfControl,
tuneLength=10,
importance=T)
set.seed(150)
#Creating RF model
rfModelSmartphone <- train(Volume~., data = trainingSmartphone,
method = "rf",
trControl=rfControl,
tuneLength=10,
importance=T)
#get predictions
predNetbook <- predict(rfModelNetbook, newdata = newSubNetbook)
predPC <- predict(rfModelPC, newdata = newSubPC)
predLaptop <- predict(rfModelLaptop, newdata = newSubLaptop)
predSmartphone <- predict(rfModelSmartphone, newdata = newSubSmartphone)
#combine predictions
predVolumes <- rbind(predNetbook, predPC, predLaptop, predSmartphone)
## @knitr rfNetbook
#Set seed to remember the randomization
set.seed(99)
#Creating subset of the dataset
existingData %>% select(c(Volume,
PositiveServiceReview,
NegativeServiceReview,
sumReviews,
ProductType.Netbook)) -> ExistingSub
#Normalise/scale attributes except the dependent feature
ExistingSub[,c(2:5)] <- lapply(ExistingSub[,c(2:5)] , scale)
#Splitting data into training set and testing set for cross validation
inTraining <- createDataPartition(ExistingSub$Volume, p = .75, list = FALSE)
trainingNetbook <- ExistingSub[inTraining,]
testingNetbook <- ExistingSub[-inTraining,]
#create subset
newData %>% select(c(PositiveServiceReview, NegativeServiceReview, sumReviews, ProductType.Netbook,Volume)) -> newSub
#Normalise/scale attributes except the dependent feature
newSub[,c(1:4)] <- lapply(newSub[,c(1:4)] , scale)
#filtering test set to include only Netbook products
newSubNetbook <- newSub %>%
dplyr::filter(newSub$ProductType.Netbook > 1)
## @knitr rfPC
#Set seed to remember the randomization
set.seed(666)
#Creating subset of the dataset
existingData %>% select(c(Volume,
PositiveServiceReview,
NegativeServiceReview,
sumReviews,
ProductType.PC)) -> ExistingSub
#Normalise/scale attributes except the dependent feature
ExistingSub[,c(2:5)] <- lapply(ExistingSub[,c(2:5)] , scale)
#Splitting data into training set and testing set for cross validation
inTraining <- createDataPartition(ExistingSub$Volume, p = .75, list = FALSE)
trainingPC <- ExistingSub[inTraining,]
testingPC <- ExistingSub[-inTraining,]
#create subset
newData %>% select(c(PositiveServiceReview, NegativeServiceReview, sumReviews, ProductType.PC,Volume)) -> newSub
#Normalise/scale attributes except the dependent feature
newSub[,c(1:4)] <- lapply(newSub[,c(1:4)] , scale)
#filtering test set to include only PC products
newSubPC <- newSub %>%
dplyr::filter(newSub$ProductType.PC > 1)
## @knitr rfLaptop
#Set seed to remember the randomization
set.seed(77)
#Creating subset of the dataset
existingData %>% select(c(Volume,
PositiveServiceReview,
NegativeServiceReview,
sumReviews,
ProductType.Laptop)) -> ExistingSub
#Normalise/scale attributes except the dependent feature
ExistingSub[,c(2:5)] <- lapply(ExistingSub[,c(2:5)] , scale)
#Splitting data into training set and testing set for cross validation
inTraining <- createDataPartition(ExistingSub$Volume, p = .75, list = FALSE)
trainingLaptop <- ExistingSub[inTraining,]
testingLaptop <- ExistingSub[-inTraining,]
#create subset
newData %>% select(c(PositiveServiceReview, NegativeServiceReview, sumReviews, ProductType.Laptop,Volume)) -> newSub
#Normalise/scale attributes except the dependent feature
newSub[,c(1:4)] <- lapply(newSub[,c(1:4)] , scale)
#filtering test set to include only Laptop products
newSubLaptop <- newSub %>%
dplyr::filter(newSub$ProductType.Laptop > 1)
## @knitr rfSmartphone
#Set seed to remember the randomization
set.seed(66)
#Creating subset of the dataset
existingData %>% select(c(Volume,
PositiveServiceReview,
NegativeServiceReview,
sumReviews,
ProductType.Smartphone)) -> ExistingSub
#Normalise/scale attributes except the dependent feature
ExistingSub[,c(2:5)] <- lapply(ExistingSub[,c(2:5)] , scale)
#Splitting data into training set and testing set for cross validation
inTraining <- createDataPartition(ExistingSub$Volume, p = .75, list = FALSE)
trainingSmartphone <- ExistingSub[inTraining,]
testingSmartphone <- ExistingSub[-inTraining,]
#create subset
newData %>% select(c(PositiveServiceReview, NegativeServiceReview, sumReviews, ProductType.Smartphone,Volume)) -> newSub
#Normalise/scale attributes except the dependent feature
newSub[,c(1:4)] <- lapply(newSub[,c(1:4)] , scale)
#filtering test set to include only Smartphone products
newSubSmartphone <- newSub %>%
dplyr::filter(newSub$ProductType.Smartphone > 1)
## @knitr modelNetbook
#Set seed to remember the randomization
set.seed(99)
#Creating subset of the dataset
existingData %>% select(c(Volume,
PositiveServiceReview,
NegativeServiceReview,
sumReviews,
ProductType.Netbook)) -> ExistingSub
#Normalise/scale attributes except the dependent feature
ExistingSub[,c(2:5)] <- lapply(ExistingSub[,c(2:5)] , scale)
#Splitting data into training set and testing set for cross validation
inTraining <- createDataPartition(ExistingSub$Volume, p = .75, list = FALSE)
training <- ExistingSub[inTraining,]
testing <- ExistingSub[-inTraining,]
#filtering test set to include only netbook products
testingLimited <- testing %>%
dplyr::filter(testing$ProductType.Netbook > 1)