I have the following table in R
df <- structure(list(x = structure(c(12458, 12633, 12692, 12830, 13369,
13455, 13458, 13515), class = "Date"), y = c(6080, 6949, 7076,
7818, 0, 0, 10765, 11153)), .Names = c("x", "y"), row.names = c("1",
"2", "3", "4", "5", "6", "8", "9"), class = "data.frame")
> df
x y
1 2004-02-10 6080
2 2004-08-03 6949
3 2004-10-01 7076
4 2005-02-16 7818
5 2006-08-09 0
6 2006-11-03 0
8 2006-11-06 10765
9 2007-01-02 11153
I can plot the points and a Tukey's linear fitting (line
function in R
) via
plot(data=df, y ~ x)
lines(df$x, line(df$x, df$y)$fitted.values)
which produces:
All fine. The above plot shows energy consumption values, expected only to increase, so I'm happy with the fit not passing through those two points (which will be subsequently flagged as outliers).
However, "just" removing the last point and replot again
df <- df[-nrow(df),]
plot(data=df, y ~ x)
lines(df$x, line(df$x, df$
)$fitted.values)
The result is completely different.
My need is to have ideally the same result in both scenarios above. R doesn't seem to have ready to use function for monotonic regression, besides isoreg
which however is piecewise constant.
EDIT:
As @Glen_b pointed out the outliers-to-sample size ratio is too big (~28%) for the regression technique used above. However, I believe there might be something else to consider. If I add the points at the beginning of the table:
df <- rbind(data.frame(x=c(as.Date("2003-10-01"), as.Date("2003-12-01")), y=c(5253,5853)), df)
and recalculate again like above plot(data=df, y ~ x); lines(df$x, line(df$x,df$y)$fitted.values)
I get the same result, with a ration of ~22%
line
. You can have more details by typing?line
in the r console $\endgroup$nnls
package (non-negative least squares). That should help you with the positivity constraints, but not with the outliers. $\endgroup$