-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPCR_PLS_Tree.R
More file actions
224 lines (194 loc) · 7.92 KB
/
PCR_PLS_Tree.R
File metadata and controls
224 lines (194 loc) · 7.92 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
data = read.csv("df_kickstarterFE.csv")
#remove nan
data = na.omit(data)
#remove char and state
rmvdData <- data[-c(1,6,8,13)]
#remove duplicated rows
library(dplyr)
deduped.data = unique(rmvdData [,1:ncol(rmvdData)])
#log values
deduped.data $usd_pledged = log1p(deduped.data $usd_pledged)
deduped.data $goal = log1p(deduped.data $goal)
newData=deduped.data
# Divide training and test sets
set.seed(1)
training_id = sample(seq_len(nrow(newData)), size = nrow(newData)*0.5)
ntrain_id = setdiff(1:nrow(newData), training_id)
validation_id = sample(ntrain_id, nrow(newData)*0.25)
test_id = setdiff(ntrain_id,validation_id)
training = newData[training_id,]
validation = newData[validation_id,]
test = newData[test_id,]
summary(training)
y=newData$usd_pledged
y.validation = y[validation_id]
y.state = data$state[validation_id]
##################### Principal Component Regression (PCR) Using 10-fold-CV ################################
install.packages('pcr')
library(pcr)
install.packages("pls")
library(pls)
pcr.fit = pcr(usd_pledged~., data= training, scale=T, validation = "CV", segments=10)
# plot cross-validation scores
validationplot(pcr.fit,val.type="MSEP") #MSEP = mean squared error of prediction
#here we see that best number of components = 8
#in red -- bias-corrected cross-validation error
#obtain predictions using # comps = 8
x = model.matrix(usd_pledged~., data= validation)[,-9] #deletes column "usd_pledged" from matrix
pcr.pred = predict(pcr.fit, x, ncomp=8)
# calculate prediction errors (mean squared error)
mean((pcr.pred - y.validation)^2) #14869.63
# calculate accuracy
validation$pcr = ifelse(pcr.pred>validation$goal,1,0)
mean(validation$pcr!= y.state) #0.5218444
############################calculate profits##########################
#senario1 where users are wrong and we also give wrong prediction
# this won't influnce user's gain/loss
s1 = (test$goal>=test$usd_pledged) & (pcr.pred>=test$usd_pledged)
#probability of s1
p1 = length(s1[s1 == TRUE])/nrow(test)
#senario2 where users are wrong and we give correct suggestion
s2=(test$goal>=test$usd_pledged) & (pcr.pred<test$usd_pledged)
#probability of s2
p2 = length(s2[s2 == TRUE])/nrow(test)
#the gain for users are the values predicted
gain1=sum(pcr.pred[s2])*1.064 #sum(pred_subset[gain])
#senario3 where users are correct and we give incorrect suggestion
s3=(test$goal<test$usd_pledged) & (pcr.pred>=test$usd_pledged)
#probability of s3
p3 = length(s3[s3 == TRUE])/nrow(test)
#the loss for users are the value actually funded
loss1=sum(test$usd_pledged[s3])
#senario4 where users are correct and we also predict correct direction
#but the value are lower than user's goal
s4=(test$goal<test$usd_pledged) & (pcr.pred<test$usd_pledged)
#probability of s4
p4 = length(s4[s4 == TRUE])/nrow(test)
#calculate loss
loss2=sum(test$usd_pledged[s4]-pcr.pred[s4]*1.064)
p1+p2+p3+p4
# #senario5 where users are correct and we also predict correct direction
#but the value are higher than user's goal
#s5=(test$goal<test$usd_pledged) & (pcr.pred>test$goal)
##probability of s5
#p5 = length(s5[s5 == TRUE])/nrow(test)
##calcultae gain
#gain2 = sum(pcr.pred[s5]-test$goal[s5])
#total gain and loss
gain_sum = gain1
loss_sum = loss1+loss2
total = gain_sum - loss_sum
total -145421.2
##################### Partial Least Squares (PLS) Using 10-fold-CV ########################################
pls.fit = plsr(usd_pledged~., data= training, scale=T, validation = "CV", segments=10)
validationplot(pls.fit,val.type="MSEP")
pls.pred = predict(pls.fit,validation,ncomp=3)
mean((pls.pred - y.validation)^2) #8.378075
# calculate accuracy
validation$pls = ifelse(pls.pred>validation$goal,1,0)
mean(validation$pls!= y.state) # 0.4212695
############################calculate profits##########################
#senario1 where users are wrong and we also give wrong prediction
# this won't influnce user's gain/loss
s1 = (test$goal>=test$usd_pledged) & (pls.pred>=test$usd_pledged)
#probability of s1
p1 = length(s1[s1 == TRUE])/nrow(test)
#senario2 where users are wrong and we give correct suggestion
s2=(test$goal>=test$usd_pledged) & (pls.pred<test$usd_pledged)
#probability of s2
p2 = length(s2[s2 == TRUE])/nrow(test)
#the gain for users are the values predicted
gain1=sum(pls.pred[s2])*1.064 #sum(pred_subset[gain])
#senario3 where users are correct and we give incorrect suggestion
s3=(test$goal<test$usd_pledged) & (pls.pred>=test$usd_pledged)
#probability of s3
p3 = length(s3[s3 == TRUE])/nrow(test)
#the loss for users are the value actually funded
loss1=sum(test$usd_pledged[s3])
#senario4 where users are correct and we also predict correct direction
#but the value are lower than user's goal
s4=(test$goal<test$usd_pledged) & (pls.pred<test$usd_pledged)
#probability of s4
p4 = length(s4[s4 == TRUE])/nrow(test)
#calculate loss
loss2=sum(test$usd_pledged[s4]-pls.pred[s4]*1.064)
p1+p2+p3+p4
# #senario5 where users are correct and we also predict correct direction
#but the value are higher than user's goal
#s5=(test$goal<test$usd_pledged) & (pcr.pred>test$goal)
##probability of s5
#p5 = length(s5[s5 == TRUE])/nrow(test)
##calcultae gain
#gain2 = sum(pcr.pred[s5]-test$goal[s5])
#total gain and loss
gain_sum = gain1
loss_sum = loss1+loss2
total = gain_sum - loss_sum
total #-26507.21
############################################ Decision Tree ###############################################
# Load rpart and rpart.plot
install.packages("tree")
library(MASS)
library(ISLR)
library(rpart)
tree.pldg=tree(usd_pledged~.,training)
cv.pldg=cv.tree(tree.pldg)
plot(cv.pldg$size,cv.pldg$dev,type='b')
prune.pldg=prune.tree(tree.pldg,best=cv.pldg$size[which.min(cv.pldg$dev)])
# Prediction
yhat=predict(prune.pldg,newdata=validation)
mean((yhat-y.validation)^2) #1.036756
# calculate accuracy
validation$tree = ifelse(yhat>validation$goal,1,0)
mean(validation$tree != y.state) #0.4681237
############################calculate profits##########################
#senario1 where users are wrong and we also give wrong prediction
# this won't influnce user's gain/loss
s1 = (test$goal>=test$usd_pledged) & (yhat>=test$usd_pledged)
#probability of s1
p1 = length(s1[s1 == TRUE])/nrow(test)
#senario2 where users are wrong and we give correct suggestion
s2=(test$goal>=test$usd_pledged) & (yhat<test$usd_pledged)
#probability of s2
p2 = length(s2[s2 == TRUE])/nrow(test)
#the gain for users are the values predicted
gain1=sum(yhat[s2])*1.064 #sum(pred_subset[gain])
#senario3 where users are correct and we give incorrect suggestion
s3=(test$goal<test$usd_pledged) & (yhat>=test$usd_pledged)
#probability of s3
p3 = length(s3[s3 == TRUE])/nrow(test)
#the loss for users are the value actually funded
loss1=sum(test$usd_pledged[s3])
#senario4 where users are correct and we also predict correct direction
#but the value are lower than user's goal
s4=(test$goal<test$usd_pledged) & (yhat<test$usd_pledged)
#probability of s4
p4 = length(s4[s4 == TRUE])/nrow(test)
#calculate loss
loss2=sum(test$usd_pledged[s4]-yhat[s4]*1.064)
p1+p2+p3+p4
# #senario5 where users are correct and we also predict correct direction
#but the value are higher than user's goal
#s5=(test$goal<test$usd_pledged) & (pcr.pred>test$goal)
##probability of s5
#p5 = length(s5[s5 == TRUE])/nrow(test)
##calcultae gain
#gain2 = sum(pcr.pred[s5]-test$goal[s5])
#total gain and loss
gain_sum = gain1
loss_sum = loss1+loss2
total = gain_sum - loss_sum
total #-78960.19
########################################## Random Forrest #################################################
install.packages("randomForest")
library(randomForest)
rf.pldg=randomForest(usd_pledged~.,data=training,mtry=6,ntree = 10, importance=TRUE)
# Prediction
yhat.rf = predict(rf.pldg,newdata=validation)
mean((yhat.rf-y.validation)^2)
# Calculate accuracy
validation$rf = ifelse(yhat.rf>validation$goal,1,0)
mean(validation$rf != y.state)
# Most important predictor
importance(rf.boston)
varImpPlot(rf.pldg)