R - Daily data and Time Series by year and week - r

Hi I am new to R and this time series forecasting.
I have a sample data of sales by day for past 3 years and I would like to use this data set to produce plot to find seasonality and pattern.
My daily data format is like eg..
Date, Sales
2010-01-01, 5
2010-01-03, 3
2010-01-04, 2
..
2011-12-01, 4
..
2014-11-01, 1
What I want to see is similar to below plot but by week and year using ts function. Also, due to leap year some year has 53 weeks and some 52 weeks, any idea how this taken into account when plotting ?
Playing with this ts function is not easy to me so it will be great if someone could help with this ..

You should start by creating a ts object. Check ?ts for the syntax, but assuming your data above were stored in `data', it's basically
tsData <- ts(data, start=c(2010,1), frequency=365)
where start refers to the (year, month) and frequency is the number of samples per year. Then you can use plot.ts() to plot the entire time series
plot.ts(tsData)
To extract seasonal patterns or trends, you can use the decompose() function.
decompose(tsData)

Here is an sample
x <- 1:10
y <- 11:20
plot(x, y)
lines(x,y)
On your data sales and date.
You can replace y with date and x with sales. If you still have issue, plz post it to me.

Related

Time Series & Forecast - Daily Data for 7.5 months

I have daily sales data between 1/1/2017 and 10/15/2017. My first question is that I was trying to set the time series with the following command:
marketing = ts(df$Total_Sales_Spend, start=c(2017,1,1), frequency = 365)
However, when I try to decompose the TS, I am getting the following error:
time-series has no or less than 2 periods
I understand that it's because I don't have at least two years of data. So in this case what can I do?
Also, I'd like to forecast the daily sales numbers for the second half of October (16 days). How may I do that in this case? I wasn't able to set it up with zoo and arima.
Thank you.

timeseries object frequency

Hi I'm new to timeseries so excuse I'm asking trivial questions, I have two questions:
I have hourly dataset (solar radiation) 7am - 7pm (12 hours for each day) what can I choose frequency for ts object?
I did this xts_2014.ts <- ts(xts_2014, frequency=12) but I think it recognize my data as monthly I know it's wrong but I don't know if 24 works when I don't have full 24 hours just daylight hours.
I have the raw data in black (which exhibits seasonal daily pattern and as far as I can tell yearly pattern), I tried moving average for removing noise of order 13 (hours) I don't know if I can proceed in trying to make data stationary?
plot

time series in R with sales prediction with only date values

i have a data with date(2015)with mm/dd/yy format and sales. I need to predict sales for 2016 with the given data. I just know, I need to use time series forecasting. However no idea. Since, many examples have only year like(1960,1970,..) my data has only one year with several months. Don't know how to plot too. can you give me a clear structure how to proceed?
Assuming that the date is in string and in the format mm/dd/yy
convert string into date by using this code
a <- "07/23/15"
b <- as.Date(a, format = "%m/%d/%y")
fullYear <- format(b,'%Y') // to get 2015 as year
halfYear <- format(b, '%y') //to get 15 as year
After this you can work on
I have found the solution. Converted sales figure into time series format.
plotted the data and seen whether there is any trend/Seasonality.
Since the data has only trend applied holts exponential smoothing under forecast package. Sales of 2016 has been found and plotted.

weekly time series in r, arima

I have a data frame with the following column names.
"week" "demand" "product-id"
The problem is to convert it into a time series object.
week is a number like 3,4,5,6,7,8,9 etc., and demand is in units and product-id is unique.
I want to convert the week column into time series, so as to prepare for modeling.
I want to predict weeks 10 and 11 demand by using an ARIMA model. How do I do that?
myTS <- ts(mydataframe[-1], frequency = 52)
will convert your demand and productId to a timeseries of 52 observations per year. For more elaborate timeseries, check package xts. Also compare this post on weekly data with ts.

How do I add periods to time series in R after aggregation

I have a two variable dataframe (df) in R of daily sales for a ten year period from 2004-07-09 through 2014-12-31. Not every single date is represented in the ten year period, but pretty much most days Monday through Friday.
My objective is to aggregate sales by quarter, convert to a time series object, and run a seasonal decomposition and other time series forecasting.
I am having trouble with the conversion, as ulitmately I receive a error:
time series has no or less than 2 periods
Here's the structure of my code.
# create a time series object
library(xts)
x <- xts(df$amount, df$date)
# create a time series object aggregated by quarter
q.x <- apply.quarterly(x, sum)
When I try to run
fit <- stl(q.x, s.window = "periodic")
I get the error message
series is not periodic or has less than two periods
When I try to run
q.x.components <- decompose(q.x)
# or
decompose(x)
I get the error message
time series has no or less than 2 periods
So, how do I take my original dataframe, with a date variable and an amount variable (sales), aggregate that quarterly as a time series object, and then run a time series analysis?
I think I was able to answer my own question. I did this. Can anyone confirm if this structure makes sense?
library(lubridate)
# add a new variable indicating the calendar year.quarter (i.e. 2004.3) of each observation
df$year.quarter <- quarter(df$date, with_year = TRUE)
library(plyr)
# summarize gift amount by year.quarter
new.data <- ddply(df, .(year.quarter), summarize,
sum = round(sum(amount), 2))
# convert the new data to a quarterly time series object beginning
# in July 2004 (2004, Q3) and ending in December 2014 (2014, Q4)
nd.ts <- ts(new.data$sum, start = c(2004,3), end = c(2014,4), frequency = 4)

Resources