-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitTester.R
38 lines (30 loc) · 1.15 KB
/
fitTester.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
library(RMySQL) # will load DBI as well
library(randomForest)
library(DBI)
args <- commandArgs(trailingOnly = TRUE)
stationID <-as.integer(args[1])
from <- as.integer(args[2])
to <- as.integer(args[3])
user <- Sys.getenv("MYSQL_RAW_DATA_USER")
dbname <- Sys.getenv("MYSQL_RAW_DATA_NAME")
password <- Sys.getenv("MYSQL_RAW_DATA_PASSWORD")
host <- Sys.getenv("MYSQL_RAW_DATA_HOST")
mydb <- dbConnect(MySQL(), user=user, dbname=dbname, password=password, host=host)
query <- sprintf(
"SELECT bikes, slots, weather_type, UNIX_TIMESTAMP(updatetime) as updatetime FROM fit_precalculation_2 WHERE id=%d AND updatetime >= FROM_UNIXTIME(%d) AND updatetime <= FROM_UNIXTIME(%d)",
stationID,
from,
to
)
data <- dbGetQuery(mydb, query)
isCalm <- function(weather) {
return(weather >= 3 && weather <= 8)
}
data$weather <- isCalm(data$weather)
data$dayMoment <- data$updatetime %% 86400
data$weekday <- as.POSIXlt(as.POSIXct(data$updatetime, origin="1970-01-01"))$wday
objectID <- sprintf("/tmp/station/bike/%d.fit", stationID)
fit <- readRDS(objectID)
data$prediction <- predict(fit, data)
write.csv(data, sprintf("station%d.csv", stationID))
dbDisconnect(mydb)