0
$\begingroup$

long time lurker, first time poster. I've recently been digging into time-series analysis and, for my master thesis one of the steps is to use an ARIMA model to be able to forecast short-term taxi demand. I'll give some context so I apologize in advance if it's too long.

I've built a clean database in MySQL with all the needed variables and extracted data and, after a spatiotemporal exploratory analysis of the case study, I'm now ready to start predicting/forecasting. I've been watching tutorial after tutorial and reading a lot in how properly use an ARIMA model but sadly, my journey stops even before reaching that point.

It's no secret that often R has some issues with table columns that aren't read as "dates" when they should. In my case, I exported a small sample to a .csv that has only 2 columns: 337 Days of 2016 (the remaining were not considered due to low counts) and taxi trips that occurred in each of those days.

When I analyze the structure of the date column i get:

str(Ano2016$DatasTaxis) Factor w/ 337 levels "1/1/2016","1/10/2016",..: 1 12 23 26 27 28 29 30 31 2 ...

So in order to convert it to a time-series, my guess is that I'll need to first convert these factors to dates. I've tried converting to Posix using anytime but it started filling some rows with "NA" and it keeps being Factor with levels

anytime(as.factor(Ano2016$DatasTaxis))

class(anytime(as.factor(Ano2016$DatasTaxis)))

          [1] "POSIXct" "POSIXt" 

str(Ano2016$DatasTaxis)

          Factor w/ 337 levels "1/1/2016","1/10/2016",..: 1 12 23 26 27 28 29 30 31 2 ...

I've tried using as.Date() as well and even stipulate the format, to no avail.

as.Date(as.character(Ano2016$DatasTaxis), format="%m/%d/%Y")

I've tried to convert to char as you can see first but I'm starting to feel a little frustrated.

If it helps, the data wasn't directly from MySQL. I went from MySQL to Tableau (data visualization software) to filter the year and count the observations.

Thanks in advance, my next step will be buying online courses, I guess...

$\endgroup$

closed as off-topic by Sycorax, kjetil b halvorsen, Michael Chernick, Juho Kokkala, mkt Jan 16 at 7:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Sycorax, kjetil b halvorsen, Michael Chernick, Juho Kokkala, mkt
If this question can be reworded to fit the rules in the help center, please edit the question.

1
$\begingroup$

Factors are stored as integers 1, 2, ... and mapped to a string representation of the actual value of the factor for display purposes, so your approach won't work. What you want is to convert the factor back to its string representation, then use as.Date or some equivalent function:

res <- as.Date(levels(Ano2016$DatasTaxis)[Ano2016$DatasTaxis])

The levels function returns the string representation of the original values of the factor in order by the integer value of the factor. When indexed by the factor itself, you get the string representations instead of the integers:

> x <- as.factor(c("2019-01-01", "2018-01-01"))
> x
[1] 2019-01-01 2018-01-01
Levels: 2018-01-01 2019-01-01

> tmp <- levels(x)[x]
> str(tmp)
 chr [1:2] "2019-01-01" "2018-01-01"

> as.Date(tmp)
[1] "2019-01-01" "2018-01-01"
$\endgroup$
  • $\begingroup$ Although my question was put on hold, I want to thank you a lot, for your explanation made it more clear! I had to add the format at the end, because your suggestion, for some reason made the dates appear 'weird'. $\endgroup$ – Kronnos Jan 16 at 16:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.