which is a standard linear model. The yt is the observed Milk sales units at time t and the xt,i is the indicator variable for weekday i at time t. As per usual β0 serves as our intercept. A small sample of the data set looks like this
which, for the response variable y, looks like the distributional plot below.
For those of you wth modeling experience you will see that a mere intra-weekly seasonality will not be enough for capturing all the interesting parts of this particular series but for the point I’m trying to make it will work just fine sticking with seasonality + and intercept.
mylm <- lm(y~WDay1+WDay2+WDay3+WDay4+WDay5+WDay6+WDay7, data=ourdata)
data {
int< lower = 0 > N; // Number of data points
vector[N] y; // The response variable
matrix[N, 7] xweekday; // The weekdays variables
}
parameters {
real b0; // The intercept
vector[7] bweekday; // The weekday regression parameters
real< lower = 0 > sigma; // The standard deviation
}
transformed parameters {
vector[N] mu; // Declaration
mu = b0 + xweekday*bweekday; // The mean prediction each timestep
}
model {
y ~ normal(mu, sigma); // Likelihood
}
generated quantities {
vector[N] yhat;
yhat = b0 + xweekday * bweekdayhat;
}
require(brms)
mybrms <- brm(bf(y~WDay1+WDay2+WDay3+WDay4+WDay5+WDay6+WDay7), data=ourdata, cores = 2, chains = 4)
data {
int< lower = 0 > N; // Number of data points
vector[N] y; // The response variable
matrix[N, 7] xweekday; // The weekdays variables
}
parameters {
real< lower = 0.01 > b0; // The intercept
vector[7 - 1] bweekday; // The weekday regression parameters
real< lower = 0 > sigma; // The standard deviation
}
transformed parameters {
// Declarations
vector[N] mu;
vector[7] bweekdayhat;
// The weekday part
bweekdayhat[1] = 0;
for (i in 1:(7 - 1) ) bweekdayhat[i + 1] = bweekday[i];
// The mean prediction each timestep
mu = b0 + xweekday*bweekdayhat;
}
model {
// Priors
b0 ~ normal(mean(y), sd(y));
bweekday ~ normal(0, sd(y));
// Likelihood
y ~ normal(mu, sigma);
}
generated quantities {
vector[N] yhat;
yhat = b0 + xweekday * bweekdayhat;
}