-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNYFare.Rmd
233 lines (147 loc) · 4.95 KB
/
NYFare.Rmd
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
225
226
227
228
229
230
231
232
233
---
title: "New York Taxi fare - prediction"
---
Loading required libraries
```{r}
library(dplyr)
library(readr)
library(geosphere)
library(caret)
library(lubridate)
library(randomForest)
```
Loading train and test dataset
```{r cache=TRUE,cache.lazy = FALSE}
train=read_csv('datasets/train.csv')
test=read_csv('datasets/test.csv',col_types = list(key=col_character()))
```
Feature Engineering
```{r}
jfk_coord = c(40.639722, -73.778889)
ewr_coord = c(40.6925, -74.168611)
lga_coord = c(40.77725, -73.872611)
feature_engineer=function(df)
{
df=df %>%
rowwise() %>%
# casting as pickup date time
mutate(pickup_datetime=substring(pickup_datetime,1,nchar(pickup_datetime)-4)) %>%
mutate(pickup_datetime=ymd_hms(pickup_datetime,tz="UTC")) %>%
mutate(pickup_datetime_est=with_tz(pickup_datetime, tzone = "America/New_York")) %>%
mutate(pickup_year=year(pickup_datetime_est)) %>%
mutate(pickup_month=as.factor(month(pickup_datetime_est))) %>%
mutate(pickup_day=as.factor(day(pickup_datetime_est))) %>%
# adding week day
mutate(wday=wday(pickup_datetime_est,label=TRUE)) %>%
# adding pickup hour
mutate(pickup_hour_est=hour(pickup_datetime_est)) %>%
# Adding haversine distance
mutate(dist_haversine=distHaversine(c(pickup_latitude,pickup_longitude),c(dropoff_latitude,dropoff_longitude))*0.000621371) %>%
mutate(jfk_haversine=min(distHaversine(jfk_coord,c(pickup_latitude,pickup_longitude)),distHaversine(jfk_coord,c(dropoff_latitude,dropoff_longitude)))) %>%
mutate(ewr_haversine=min(distHaversine(ewr_coord,c(pickup_latitude,pickup_longitude)),distHaversine(ewr_coord,c(dropoff_latitude,dropoff_longitude)))) %>%
mutate(lga_haversine=min(distHaversine(lga_coord,c(pickup_latitude,pickup_longitude)),distHaversine(lga_coord,c(dropoff_latitude,dropoff_longitude))))
}
```
### Randomly select 50k rows from the train dataset
### Remove outliers
### Remove NAs
```{r}
train_1=train[sample(1:nrow(train),2000000,FALSE,NULL),] %>%
rowwise() %>%
#drop rows if any column has NA's
na.omit() %>%
#remove wrong latitude and longitude
filter (pickup_longitude > -80 & pickup_longitude < -70) %>%
filter(pickup_latitude>35 & pickup_latitude<45) %>%
filter(dropoff_longitude > -80 & dropoff_longitude < -70) %>%
filter(dropoff_latitude > 35 & dropoff_latitude < 45) %>%
#remove fare amounts less than minimun fare amount
filter(fare_amount > 2.5 ) %>%
#remove passenger count between 0 and 10 resonable range
filter(passenger_count>0 & passenger_count <=10)
```
```{r}
summary(train_1)
```
### Adding the new features / converting as date data type
```{r}
train_2=feature_engineer(train_1) %>% mutate(dist_haversine=ifelse(dist_haversine==0,(fare_amount-2.5)/1.56,dist_haversine))
test1=feature_engineer(test)
```
```{r}
train_2 =train_2 %>% filter(dist_haversine<=60)
```
```{r}
summary(train_2)
```
# select only required columns
```{r}
train_3=train_2 %>% select(fare_amount,dist_haversine,wday,pickup_hour_est,pickup_year,pickup_month,pickup_day,passenger_count,jfk_haversine,ewr_haversine,lga_haversine)
```
# creating train and test dataset
```{r}
index=createDataPartition(train_3$fare_amount,p=0.8,list=FALSE)
train_data=train_3[index,]
test_data=train_3[-index,]
```
#training the model - lm
```{r}
lm_model=train(fare_amount~.,data=train_data,method='lm')
```
Trainign set error
```{r}
sqrt(mean((predict(lm_model,train_data)-train_data$fare_amount)^2))
```
Test set error
```{r}
sqrt(mean((predict(lm_model,test_data)-test_data$fare_amount)^2))
```
#training the model - xgb
```{r}
xgb_model=train(fare_amount~.,data=train_data,method='xgbTree')
```
Trainign set error
```{r}
sqrt(mean((predict(xgb_model,train_data)-train_data$fare_amount)^2))
```
Test set error
```{r}
sqrt(mean((predict(xgb_model,test_data)-test_data$fare_amount)^2))
```
#training the model - random forest/ ranger
```{r}
rf_model=train(fare_amount~.,data=train_data,method='rf',ntree=5)
```
Trainign set error
```{r}
sqrt(mean((predict(rf_model,train_data)-train_data$fare_amount)^2))
```
Test set error
```{r}
sqrt(mean((predict(rf_model,test_data)-test_data$fare_amount)^2))
```
#training the model - random forest
```{r}
rf_model=randomForest(fare_amount~.,data=train_data,ntree=50,mtry=7)
```
Trainign set error
```{r}
sqrt(mean((predict(rf_model,train_data)-train_data$fare_amount)^2))
```
Test set error
```{r}
sqrt(mean((predict(rf_model,test_data)-test_data$fare_amount)^2))
```
# below code to write make prediction and write to csv file
# selecting only required column in test
```{r}
test_1=test1 %>% select(dist_haversine,wday,pickup_hour_est,pickup_year,pickup_month,pickup_day,passenger_count,jfk_haversine,ewr_haversine,lga_haversine)
```
```{r}
prediction=predict(rf_model,test_1)
```
writing to csv file for submission
```{r}
submission=data.frame(key=as.character(test$key),fare_amount=prediction)
write.csv(submission,file='rf_submissions.csv',row.names=FALSE)
```