Biology, images, analysis, design...
Use/Abuse Principles How To Related
"It has long been an axiom of mine that the little things are infinitely the most important" (Sherlock Holmes)

Search this site



Just a note

  • a=0.1 puts 0.1 into variable a.
  • z=y[1] copies whatever is in the first element of variable y to variable z. If variable y does not exist you get an error message. If variable z does not exist, it is created. But if variable z does already exist, this instruction causes any information it contained to be overwritten and lost.

  • while(i < length(y)){i=i+1
    z[i] = a*y[i] + (1-a)*z[i-1]}
    tells R to work its way along the elements of variables y and z, in ascending order, applying the exponential smoothing formula you have specified.

    Because this function smoothes from left to right, any consistent trend biases the line. Hence if numbers just increase, the smoothed line will track below the level of the observations. In effect, unlike an ordinary moving average, this asymmetrically weighted moving average is subject to a noticeable lag. The lower is 'a', the smoother the line, and the greater is this effect.

  • plot(y) instructs R to plot out the points in order as a scatterplot. Then the two commands separated by a semicolon n=length(y); Since this is the only item of information, it automatically goes into the first (and then the only) element of variable z.

  • points(z) adds the smoothed line.