[rkward-cvs] SF.net SVN: rkward: [2363] branches/release_branch_0.4.9/rkward/plugins

tfry at users.sourceforge.net tfry at users.sourceforge.net
Tue Apr 15 09:50:50 UTC 2008


Revision: 2363
          http://rkward.svn.sourceforge.net/rkward/?rev=2363&view=rev
Author:   tfry
Date:     2008-04-15 02:50:50 -0700 (Tue, 15 Apr 2008)

Log Message:
-----------
Backmerge HP filter plugin

Modified Paths:
--------------
    branches/release_branch_0.4.9/rkward/plugins/analysis.pluginmap

Added Paths:
-----------
    branches/release_branch_0.4.9/rkward/plugins/analysis/time_series/hp_filter.php
    branches/release_branch_0.4.9/rkward/plugins/analysis/time_series/hp_filter.rkh
    branches/release_branch_0.4.9/rkward/plugins/analysis/time_series/hp_filter.xml

Added: branches/release_branch_0.4.9/rkward/plugins/analysis/time_series/hp_filter.php
===================================================================
--- branches/release_branch_0.4.9/rkward/plugins/analysis/time_series/hp_filter.php	                        (rev 0)
+++ branches/release_branch_0.4.9/rkward/plugins/analysis/time_series/hp_filter.php	2008-04-15 09:50:50 UTC (rev 2363)
@@ -0,0 +1,95 @@
+<?
+function preprocess () {
+}
+
+function calculate () {
+}
+
+function printout () {
+	if (getRK_val("custom") == 0)
+		$lambda = getRK_val ("lambda");
+	else
+		$lambda = getRK_val ("clambda");
+?>
+rk.header ("Hodrick-Prescott Filter", parameters=list("Lambda", <? echo $lambda; ?>))
+x <- get("<? getRK("x"); ?>", envir=globalenv())
+lambda <- <? echo $lambda . "\n"; ?>
+nas <- 0
+for (i in 1:length(x)) {
+	if (is.na(x[i])) {
+		nas <- nas + 1
+	}
+}
+if (nas == 0) {
+	i <- diag(length(x))
+	trend <- solve(i + lambda * crossprod(diff(i, lag=1, d=2)), x) # The HP Filter itself. Thanks to Grant V. Farnsworth
+	cycle <- x - trend
+	if (is.ts(x)) {
+		trend <- ts(trend,start(x),frequency=frequency(x))
+		cycle <- ts(cycle,start(x),frequency=frequency(x))
+	}
+<?
+	if (getRK_val("create_trend") == 1) {
+?>
+	assign("<? getRK("trend_name"); ?>", trend, envir=globalenv())
+<?
+	} 
+	if (getRK_val("create_cycle") == 1) {
+?>
+	assign("<? getRK("cycle_name"); ?>", cycle, envir=globalenv())
+<?
+	}
+
+	if (getRK_val("series_col.color") != "" & getRK_val("trend_col.color") != "")
+		$upcol = ", col=c(\"" . getRK_val("series_col.color") . "\", \"" . getRK_val("trend_col.color") . "\")";
+	elseif (getRK_val("series_col.color") != "")
+		$upcol = ", col=c(\"" . getRK_val("series_col.color") . "\", \"black\")";
+	elseif (getRK_val("trend_col.color") != "")
+		$upcol = ", col=c(\"black\", \"" . getRK_val("trend_col.color") . "\")";
+	else
+		$upcol = "";
+
+	if (getRK_val("series_lty") != "" & getRK_val("trend_lty") != "")
+		$uplty = ", lty=c(\"" . getRK_val("series_lty") . "\", \"" . getRK_val("trend_lty") . "\")";
+	elseif (getRK_val("series_lty") != "")
+		$uplty = ", lty=c(\"" . getRK_val("series_lty") . "\", \"solid\")";
+	elseif (getRK_val("trend_lty") != "")
+		$uplty = ", lty=c(\"solid\", \"" . getRK_val("trend_lty") . "\")";
+	else
+		$uplty = "";
+
+	if (getRK_val("uplab.text") == "")
+		$uplab = "\"" . getRK_val("x") . ", Trend\"";
+	else
+		if (getRK_val("uplabisquote") == 1)		
+			$uplab = "\"" . getRK_val("uplab") . "\"";
+		else
+			$uplab = getRK_val("uplab");
+?>
+	rk.graph.on ()
+	try({
+		par(mfrow=c(<?if (getRK_val("plot_cycle") == 1) echo 2; else echo 1;?>,1),mar=c(2,4,2,2)+0.1)
+		plot.ts(cbind(x, trend), ylab=<? echo $uplab; echo $upcol; ?>,lwd=c(<? getRK("series_lwd"); ?>,<? getRK("trend_lwd"); ?>)<? echo $uplty; ?>, plot.type="single")
+<?
+	if (getRK_val("plot_cycle") == 1) {
+		if (getRK_val("downlab.text") == "") 
+			$downlab = "\"Cycle\"";
+		else
+			if (getRK_val("downlabisquote") == 1)	
+				$downlab = "\"" . getRK_val("downlab") . "\"";
+			else
+				$downlab = getRK_val("downlab");
+?>
+		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") . "\""; ?>)
+<?
+	}
+?>
+	})
+	rk.graph.off ()
+}
+else {
+	warning("The series provided contains missing values")
+}
+<?
+}
+?>

Added: branches/release_branch_0.4.9/rkward/plugins/analysis/time_series/hp_filter.rkh
===================================================================
--- branches/release_branch_0.4.9/rkward/plugins/analysis/time_series/hp_filter.rkh	                        (rev 0)
+++ branches/release_branch_0.4.9/rkward/plugins/analysis/time_series/hp_filter.rkh	2008-04-15 09:50:50 UTC (rev 2363)
@@ -0,0 +1,27 @@
+<!DOCTYPE rkhelp>
+<document>
+	<summary>
+		This plugin extracts and plots the long term trend of a series and (optionally) its cyclical component.
+	</summary>
+
+	<usage>
+		The Hodrick-Prescott filter is used to separate the long term trend of a time series from its cyclical (short term) component.
+
+		In addition to the resulting series object(s), this plugin can plot them in relation with the original series provided so as to give a graphic view of the filtering operation.
+	</usage>
+
+	<settings>
+		
+		<setting id="x">Select the data to be computed. It must be a single Time Series object or a Numeric Vector. NA values are not admitted. </setting>
+		<setting id="frame_lambda">This is the smoothing parameter. You have a dropdown menu with the three most common values depending on the frequency of the data (<i>Annual</i>, <i>Quarterly</i> and <i>Monthly</i>). (Default: 1600)</setting>
+		<setting id="custom">Select if you want to specify a lambda value yourself in the spinbox below.</setting>
+		<setting id="output_frame">Select which resulting series should be created in the workspace. The trend of the original series is created by default. Write any name you want to use as identifier for the series. By default, it's <i>hp</i> followed by <i>trend</i> and/or <i>cycle</i>. <i>Tip: you can add the name of the original series to avoid overwriting other variables' trend/cycle (i.e. "myseries.hptrend").</i></setting>
+		<caption id="tab_plot"/>
+		<setting id="plot_cycle">Plot the cyclical component below the trend graph. (Default: YES)</setting>
+		<setting id="frame_lines">Here you can adjust the line width, type and colour of each series on the plot. (Default: blue series, red trend and dark green cycle)</setting>
+		<caption id="frame_labels"/>
+		<setting id="uplab">Here you can specify the text on the Y axis label of the trend plot (upper). If empty, the default label is the name of the original series plus <i>, Trend</i>.</setting>
+		<setting id="downlab">Here you can specify the text on the Y axis label of the cycle plot (lower - if applicable). If empty, the default label is <i>Cycle</i>.</setting>
+		<setting id="uplabisquote">Enable or disable the labels being evaluated as literal (quoted) strings. To achieve a line-break (in a quoted string), enter '\n'. (Default: YES)</setting>
+	</settings>
+</document> 

Added: branches/release_branch_0.4.9/rkward/plugins/analysis/time_series/hp_filter.xml
===================================================================
--- branches/release_branch_0.4.9/rkward/plugins/analysis/time_series/hp_filter.xml	                        (rev 0)
+++ branches/release_branch_0.4.9/rkward/plugins/analysis/time_series/hp_filter.xml	2008-04-15 09:50:50 UTC (rev 2363)
@@ -0,0 +1,125 @@
+<!DOCTYPE rkplugin>
+
+<document>
+	<code file="hp_filter.php"/>
+	<help file="hp_filter.rkh" />
+	<logic>
+		<connect client="lambda.enabled" governor="custom.state.not"/>
+		<connect client="clambda.enabled" governor="custom.state"/>
+		<connect client="trend_name.enabled" governor="create_trend.state"/>
+		<connect client="cycle_name.enabled" governor="create_cycle.state"/>
+		<connect client="cycle_frame.enabled" governor="plot_cycle.state"/>
+		<connect client="cycle_col.color.enabled" governor="plot_cycle.state"/>
+		<connect client="downlab.enabled" governor="plot_cycle.state"/>
+		<connect client="uplabisquote.enabled" governor="uplab_full"/>
+		<connect client="downlabisquote.enabled" governor="downlab_fullplot"/>
+		<connect client="interpret.enabled" governor="lab_full"/>
+		<set id="series_col.default_color" to="blue"/>
+		<set id="trend_col.default_color" to="red"/>
+		<set id="cycle_col.default_color" to="green4"/>
+		<convert id="uplab_full" mode="notequals" sources="uplab.text" standard=""/>
+		<convert id="downlab_full" mode="notequals" sources="downlab.text" standard=""/>
+		<convert id="downlab_fullplot" mode="and" sources="plot_cycle.state;downlab_full"/>
+		<convert id="lab_full" mode="or" sources="uplab_full;downlab_full"/>
+		
+	</logic>
+	<dialog label="Hodrick-Prescott Filter">
+		<tabbook>
+			<tab label="Select Variable" >
+				<row>
+					<column>
+						<varselector id="var"/>
+					</column>
+					<column>
+						<row>
+							<varslot classes="numeric ts" num_dimensions="1" multi="false" id="x" source="var" required="true" label="Variable:"/>
+						</row>
+						<row>
+							<frame label="Lambda" id="frame_lambda">
+								<dropdown id="lambda">
+									<option value="100" label="100 (Annual)"/>
+									<option value="1600" label="1600 (Quarterly)" checked="true"/>
+									<option value="14400" label="14400 (Monthly)"/>
+								</dropdown>
+								<checkbox id="custom" label="Custom value" value="1" value_unchecked="0"/>
+								<spinbox initial="1" min="1" id="clambda" type="integer"/>
+							</frame>
+						</row>
+						<row>
+							<frame label="Output series" id="output_frame">
+								<row>
+									<checkbox id="create_trend" label="Trend" value="1" value_unchecked="0" checked="true"/>
+									<saveobject id="trend_name" initial="hptrend"/>
+								</row>
+								<row>
+									<checkbox id="create_cycle" label="Cycle" value="1" value_unchecked="0"/>
+									<saveobject id="cycle_name" initial="hpcycle"/>
+								</row>
+							</frame>
+						</row>
+					</column>
+				</row>
+			</tab>
+			<tab label="Plot options" id="tab_plot">
+				<checkbox id="plot_cycle" label="Plot Cycle" value="1" value_unchecked="0" checked="true"/>
+				<frame label="Lines" id="frame_lines">
+					<row>
+						<frame label="Original series" id="original_frame">
+							<spinbox id="series_lwd" type="integer" label="Line width" min="1" initial="1"/>
+							<dropdown id="series_lty" label="Line type" >
+								<option value="" label="default" checked="true"/>
+								<option value="solid" label="Solid"/>
+								<option value="dashed" label="Dashed" />
+								<option value="dotted" label="Dotted" />
+								<option value="dotdash" label="Dot-Dash" />
+								<option value="longdash" label="Long-dash" />
+								<option value="twodash" label="Two-dash" />
+							</dropdown>
+							<embed id="series_col" component="rkward::color_chooser"/>
+							<stretch/>
+						</frame>
+						<frame label="Trend">
+							<spinbox id="trend_lwd" type="integer" label="Line width" min="1" initial="1"/>
+							<dropdown id="trend_lty" label="Line type" >
+								<option value="" label="default" checked="true"/>
+								<option value="solid" label="Solid"/>
+								<option value="dashed" label="Dashed" />
+								<option value="dotted" label="Dotted" />
+								<option value="dotdash" label="Dot-Dash" />
+								<option value="longdash" label="Long-dash" />
+								<option value="twodash" label="Two-dash" />
+							</dropdown>
+							<embed id="trend_col" component="rkward::color_chooser"/>
+							<stretch/>
+						</frame>
+						<frame label="Cycle" id="cycle_frame">
+							<spinbox id="cycle_lwd" type="integer" label="Line width" min="1" initial="1"/>
+							<dropdown id="cycle_lty" label="Line type" >
+								<option value="" label="default" checked="true"/>
+								<option value="solid" label="Solid"/>
+								<option value="dashed" label="Dashed" />
+								<option value="dotted" label="Dotted" />
+								<option value="dotdash" label="Dot-Dash" />
+								<option value="longdash" label="Long-dash" />
+								<option value="twodash" label="Two-dash" />
+							</dropdown>
+							<embed id="cycle_col" component="rkward::color_chooser"/>
+							<stretch/>
+						</frame>
+					</row>
+				</frame>
+				<frame label="Labels (leave empty for default)" id="frame_labels">
+					<row>
+						<input id="uplab" label="Original series and trend Y axis label"/>
+						<input id="downlab" label="Cycle Y axis label"/>
+					</row>
+					<text id="interpret">Interpret these labels as literal (quoted) strings?</text>
+					<row>
+						<checkbox id="uplabisquote" label="interpret as string" value="1" checked="true" />
+					        <checkbox id="downlabisquote" label="interpret as string" value="1" checked="true" />
+					</row>
+				</frame>
+			</tab>
+		</tabbook>
+	</dialog>
+</document>

Modified: branches/release_branch_0.4.9/rkward/plugins/analysis.pluginmap
===================================================================
--- branches/release_branch_0.4.9/rkward/plugins/analysis.pluginmap	2008-04-15 09:47:09 UTC (rev 2362)
+++ branches/release_branch_0.4.9/rkward/plugins/analysis.pluginmap	2008-04-15 09:50:50 UTC (rev 2363)
@@ -42,6 +42,7 @@
 		<!-- time series -->
 		<component type="standard" id="Box_test" file="analysis/time_series/Box_test.xml" label="Box-Pierce or Ljung-Box Tests" />
 		<component type="standard" id="kpss_test" file="analysis/time_series/kpss_test.xml" label="KPSS Test for Stationarity" />
+		<component type="standard" id="hp_filter" file="analysis/time_series/hp_filter.xml" label="Hodrick-Prescott Filter" />
 
 		<!-- regression -->
 		<component type="standard" id="linear_regression" file="analysis/regression/linear_regression.xml" label="Linear Regression" />
@@ -97,6 +98,7 @@
 			<menu id="time_series" label="Time Series" index="10">
 				<entry component="Box_test"/>
 				<entry component="kpss_test"/>
+				<entry component="hp_filter"/>
 			</menu>
 			<menu id="wilcoxon_test" label="Wilcoxon Tests" index="11">
 				<entry component="wilcoxon_test" />


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the rkward-tracker mailing list