2
function preprocess () {
5
function calculate () {
9
if (getRK_val("custom") == 0)
10
$lambda = getRK_val ("lambda");
12
$lambda = getRK_val ("clambda");
14
rk.header ("Hodrick-Prescott Filter", parameters=list("Lambda", <? echo $lambda; ?>))
15
x <- get("<? getRK("x"); ?>", envir=globalenv())
16
lambda <- <? echo $lambda . "\n"; ?>
18
for (i in 1:length(x)) {
25
trend <- solve(i + lambda * crossprod(diff(i, lag=1, d=2)), x) # The HP Filter itself. Thanks to Grant V. Farnsworth
28
trend <- ts(trend,start(x),frequency=frequency(x))
29
cycle <- ts(cycle,start(x),frequency=frequency(x))
32
if (getRK_val("create_trend") == 1) {
34
assign("<? getRK("trend_name"); ?>", trend, envir=globalenv())
37
if (getRK_val("create_cycle") == 1) {
39
assign("<? getRK("cycle_name"); ?>", cycle, envir=globalenv())
43
if (getRK_val("series_col.color") != "" & getRK_val("trend_col.color") != "")
44
$upcol = ", col=c(\"" . getRK_val("series_col.color") . "\", \"" . getRK_val("trend_col.color") . "\")";
45
elseif (getRK_val("series_col.color") != "")
46
$upcol = ", col=c(\"" . getRK_val("series_col.color") . "\", \"black\")";
47
elseif (getRK_val("trend_col.color") != "")
48
$upcol = ", col=c(\"black\", \"" . getRK_val("trend_col.color") . "\")";
52
if (getRK_val("series_lty") != "" & getRK_val("trend_lty") != "")
53
$uplty = ", lty=c(\"" . getRK_val("series_lty") . "\", \"" . getRK_val("trend_lty") . "\")";
54
elseif (getRK_val("series_lty") != "")
55
$uplty = ", lty=c(\"" . getRK_val("series_lty") . "\", \"solid\")";
56
elseif (getRK_val("trend_lty") != "")
57
$uplty = ", lty=c(\"solid\", \"" . getRK_val("trend_lty") . "\")";
61
if (getRK_val("uplab.text") == "")
62
$uplab = "\"" . getRK_val("x") . ", Trend\"";
64
if (getRK_val("uplabisquote") == 1)
65
$uplab = "\"" . getRK_val("uplab") . "\"";
67
$uplab = getRK_val("uplab");
71
par(mfrow=c(<?if (getRK_val("plot_cycle") == 1) echo 2; else echo 1;?>,1),mar=c(2,4,2,2)+0.1)
72
plot.ts(cbind(x, trend), ylab=<? echo $uplab; echo $upcol; ?>,lwd=c(<? getRK("series_lwd"); ?>,<? getRK("trend_lwd"); ?>)<? echo $uplty; ?>, plot.type="single")
74
if (getRK_val("plot_cycle") == 1) {
75
if (getRK_val("downlab.text") == "")
76
$downlab = "\"Cycle\"";
78
if (getRK_val("downlabisquote") == 1)
79
$downlab = "\"" . getRK_val("downlab") . "\"";
81
$downlab = getRK_val("downlab");
83
plot.ts(cycle, ylab=<? echo $downlab; if (getRK_val("cycle_col.color") != "") echo ", col=\"" . getRK_val("cycle_col.color") . "\""; ?>, lwd=<? getRK("cycle_lwd"); if (getRK_val("cycle_lty") != "") echo ", lty=\"" . getRK_val("cycle_lty") . "\""; ?>)
91
warning("The series provided contains missing values")