# Moving average: Smoothing until stable

#### trinker

##### ggplot2orBust
I am looking at a score in discourse over equally spaced intervals. Basically this is a time series. I want to view this as a line graph using the cumulative average. HEre's the ugly code that I used in R and the plot:

Code:
set.seed(10)
dat <- data.frame(value=rnorm(1000, sd=10), time=1:1000)

len <- nrow(dat)

#### trinker

##### ggplot2orBust
My problem is that the moving average is less stable in the beginning. How can I make it smoother?

PS thanks on the non-for loop

#### Dason

What is your goal? I mean the rolling average is of course going to be 'less stable' (higher variance) at the beginning - there aren't as many data points to go into the average. I don't see that there is anything we can do unless you allow yourself to use future information or prior information as well.

#### trinker

##### ggplot2orBust
OK I think that's the info I want. Thanks Dason. I was thinking there was a way to borrow from later data (influence the beginning) until it becomes stable.

#### Dason

Well if you can borrow from later data and your model says that all the data is independent, normals with the same mean and variance... then your best 'smooth' is just draw a line at the sample mean. Doesn't get any more smooth than that.

#### trinker

##### ggplot2orBust
True. Just for reference here was what I am doing:

Here's the example from the qdap vignette:

Code:
library(qdap)
poldat4 <- with(rajSPLIT, polarity(dialogue, act, constrain = TRUE))

polcount <- counts(poldat4)$polarity len <- length(polcount) cumpolarity_mean <- cumpolarity_median <- rep(NA, len) cummean <- function(x){cumsum(x)/seq_along(x)} cumpolarity <- data.frame(cum_mean = cummean(polcount), Time=1:len) ## Calculate background rectangles ends <- cumsum(rle(counts(poldat4)$act)\$lengths)
starts <- c(1, head(ends + 1, -1))
rects <- data.frame(xstart = starts, xend = ends,
Act = c("I", "II", "III", "IV", "V"))

library(ggplot2)
ggplot() + theme_bw() +
geom_rect(data = rects, aes(xmin = xstart, xmax = xend,
ymin = -Inf, ymax = Inf, fill = Act), alpha = 0.2) +
geom_smooth(data = cumpolarity, aes(y=cum_mean, x = Time)) +
geom_hline(y=mean(polcount), color="grey30", size=1, alpha=.3, linetype=2) +
annotate("text", x = mean(ends[1:2]), y = mean(polcount), color="grey30",
label = "Average Polarity", vjust = .3, size=3) +
geom_line(data = cumpolarity, aes(y=cum_mean, x = Time), size=1) +
ylab("Cumulative Average Polarity") + xlab("Duration") +
scale_x_continuous(expand = c(0,0)) +
geom_text(data=rects, aes(x=(xstart + xend)/2, y=-.04,
label=paste("Act", Act)), size=3) +
guides(fill=FALSE) +
scale_fill_brewer(palette="Set1")