0

I'm new in R and i'm trying to do some prognosis using GA connection using forecast library.

I have an input data like this:

 day month year visits
1  01    01 2013  21821
2  02    01 2013  17865
3  03    01 2013  25300
4  04    01 2013  41184
5  05    01 2013  48953
6  06    01 2013  64135

It grabs the visits for the days of each month and year.

When i try to use ts function i go like this:

visits.ts=ts(ga.data$visits, start=c(2013,1), end=c(2014,1), frequency=12)

Given the output like this:

     Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
2013 
2014 

Here is my question- how can i split the month to days and create an output like this:

      01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
January 2013
February 2013
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Keithx
  • 2,994
  • 15
  • 42
  • 71
  • so i need to create prognosis not for a month but for a day – Keithx Oct 14 '15 at 13:39
  • see the [comments here](https://stackoverflow.com/questions/8437620/analyzing-daily-weekly-data-using-ts-in-r) - you are better off using `zoo` or `xts` for daily data – jeremycg Oct 14 '15 at 13:51

1 Answers1

1

First create some test data, DF. (In the future please provide test data.)

# create data set for testing
tt0 <- seq(as.Date("2013-01-01"), as.Date("2014-12-31"), by = "day")
lt <- as.POSIXlt(tt0)
DF <- data.frame(year = lt$year + 1900, month = lt$mon + 1, day = lt$mday, visits = 1:730)

The following depends crucially on every year containing the exact same number of days (i.e. no leap years). Fortunately, that is the case in the data shown in the question. Using DF convert it to "ts" class:

# convert to ts
tser <- ts(DF$visits, start = 2013, freq = 365)

If we did have leap years we might want to use "zoo" class from the zoo package or "xts" class from the xts package.

For the second question, first append a year_month column and then use dcast to create a 2d display:

library(reshape2)
DF2 <- transform(DF, year_month = I(sprintf("%d-%02d", year, month)))
dcast(DF2, year_month ~ day, value.var = "visits")

or using as.yearmon from the zoo package:

library(reshape2)
library(zoo)
DF2 <- transform(DF, year_month = as.yearmon(paste(year, month, sep = "-")))
dcast(DF2, year_month ~ day, value.var = "visits")

and here is a third alternative. This one does not use any external packages although it's a bit slow:

 DF2 <- transform(DF, day = factor(day), year_month = sprintf("%d-%02d", year, month))
 xtabs(visits ~ year_month + day, DF2, sparse = TRUE)

Omitting sparse=TRUE it will be faster but will fill the unused spots with 0.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341