<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Rforge &#187; Statistics</title>
	<atom:link href="http://rforge.org/category/statistics/feed/" rel="self" type="application/rss+xml" />
	<link>http://rforge.org</link>
	<description>open source tools and statistical computing ++</description>
	<lastBuildDate>Thu, 12 Jan 2012 11:03:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='rforge.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Rforge &#187; Statistics</title>
		<link>http://rforge.org</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://rforge.org/osd.xml" title="Rforge" />
	<atom:link rel='hub' href='http://rforge.org/?pushpress=hub'/>
		<item>
		<title>R function to transform continuous variable to categorical factor cut at n-tiles</title>
		<link>http://rforge.org/2011/11/06/r-function-to-transform-continuous-variable-to-categorical-factor-cut-at-n-tiles/</link>
		<comments>http://rforge.org/2011/11/06/r-function-to-transform-continuous-variable-to-categorical-factor-cut-at-n-tiles/#comments</comments>
		<pubDate>Sun, 06 Nov 2011 20:45:13 +0000</pubDate>
		<dc:creator>rforge</dc:creator>
				<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Use R!]]></category>
		<category><![CDATA[categorical]]></category>
		<category><![CDATA[continuous]]></category>
		<category><![CDATA[n-tiles]]></category>
		<category><![CDATA[quantiles]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[transform]]></category>

		<guid isPermaLink="false">http://rforge.org/2011/11/06/r-function-to-transform-continuous-variable-to-categorical-factor-cut-at-n-tiles/</guid>
		<description><![CDATA[The cut() function can be used to transform a continuous variable into a categorical factor variable. The syntax is quite lengthy and if one wishes to cut at quartiles, quintiles or other n-tiles one has to include the quantile() function into the call. This is not very newbee friendly and if included into a model-call [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=715&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The <tt>cut()</tt> function can be used to transform a continuous variable into a categorical factor variable. The syntax is quite lengthy and if one wishes to cut at quartiles, quintiles or other n-tiles one has to include the <tt>quantile()</tt> function into the call.</p>
<p>This is not very newbee friendly and if included into a model-call nearly ufnreadable.</p>
<p>The function in the code box <tt>cut.at.n.tile()</tt> does the job.</p>
<p><code><br />
cut.at.n.tile &lt;- function(X , n = 4){ cut( X , breaks = quantile( X , probs = (0:n)/n , na.rm = TRUE ) , include.lowest = TRUE )}<br />
</code></p>
<p>In order to cut the continuous variable <tt>Creatinine</tt> in the dataset <tt>Patients</tt> into deciles (n=10) the syntax is:<br />
<code>cut.at.n.tile( Patients$Creatinine , n = 10 )</code></p>
<p>No big deal, but maybe useful&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rforge.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rforge.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rforge.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rforge.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rforge.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rforge.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rforge.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rforge.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rforge.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rforge.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rforge.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rforge.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rforge.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rforge.wordpress.com/715/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=715&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rforge.org/2011/11/06/r-function-to-transform-continuous-variable-to-categorical-factor-cut-at-n-tiles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2a4cc3be2c3e1c3b1442535ef7a46f42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rforge</media:title>
		</media:content>
	</item>
		<item>
		<title>Mathematical expressions in R plot</title>
		<link>http://rforge.org/2011/03/03/mathematical-expressions-in-r-plot/</link>
		<comments>http://rforge.org/2011/03/03/mathematical-expressions-in-r-plot/#comments</comments>
		<pubDate>Thu, 03 Mar 2011 10:09:01 +0000</pubDate>
		<dc:creator>rforge</dc:creator>
				<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Use R!]]></category>
		<category><![CDATA[axis]]></category>
		<category><![CDATA[expression]]></category>
		<category><![CDATA[label]]></category>
		<category><![CDATA[main]]></category>
		<category><![CDATA[plot]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[title]]></category>

		<guid isPermaLink="false">http://rforge.org/?p=648</guid>
		<description><![CDATA[Mathematical anotations to R plots can be formated LaTeX style with the expression function. The expression() can be included in plot titles title( expression(...)) axis anotations plot( ... , xlab = expression(...)) or the plot panel itself text( x , y , expression(...)) An example: plot( 0 , 0 , type = "n" , xlab [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=648&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Mathematical anotations to R plots can be formated LaTeX style with the <em>expression</em> function. The expression() can be included in</p>
<ul>
<li>plot titles
<pre>title( expression(...))</pre>
</li>
<li>axis anotations
<pre>plot( ... , xlab = expression(...))</pre>
<p> or</li>
<li>the plot panel itself
<pre>text( x , y , expression(...))</pre>
</li>
</ul>
<p>An example:<br />
<code><br />
plot( 0 , 0 , type = "n" , xlab = expression( "Nothing" * (mu*mol/l)) )<br />
text( 0 , 0 , expression(beta&gt;=0.2))<br />
title( expression( "Only an expression() demo " * theta^(2*pi)))<br />
</code>
</p>
<p>It is even possible to update the plot anotation from a variable (found on the <a href="https://stat.ethz.ch/pipermail/r-help/2000-December/009413.html">R forum</a>)</p>
<h3>[R] Expression in plot text</h3>
<blockquote><p>
Roger Koenker roger at ysidro.econ.uiuc.edu<br />
Wed Dec 6 20:22:05 CET 2000 </p>
<pre>expression(paste(hat(theta),'= ',that)))</pre>
<p>We&#8217;ve been here before. To get an expression either use parse on your<br />
pasted character string or substitute on an expression.  There are worked<br />
examples in the list achives. The neatest is</p>
<pre>title(substitute(hat(theta) == that, list(that=that)))</pre>
<p>(note it is == not =)</p>
</blockquote>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rforge.wordpress.com/648/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rforge.wordpress.com/648/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rforge.wordpress.com/648/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rforge.wordpress.com/648/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rforge.wordpress.com/648/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rforge.wordpress.com/648/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rforge.wordpress.com/648/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rforge.wordpress.com/648/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rforge.wordpress.com/648/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rforge.wordpress.com/648/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rforge.wordpress.com/648/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rforge.wordpress.com/648/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rforge.wordpress.com/648/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rforge.wordpress.com/648/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=648&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rforge.org/2011/03/03/mathematical-expressions-in-r-plot/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2a4cc3be2c3e1c3b1442535ef7a46f42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rforge</media:title>
		</media:content>
	</item>
		<item>
		<title>Violinplots with R</title>
		<link>http://rforge.org/2011/02/07/violinplots-with-r/</link>
		<comments>http://rforge.org/2011/02/07/violinplots-with-r/#comments</comments>
		<pubDate>Mon, 07 Feb 2011 10:31:42 +0000</pubDate>
		<dc:creator>rforge</dc:creator>
				<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Use R!]]></category>

		<guid isPermaLink="false">http://rforge.org/?p=633</guid>
		<description><![CDATA[Just for the record a nice alternative to boxplot() from the R introduction on MOAC@University of Warwick: Violin plots are essentially pretty versions of box plots, where the width is set by the local density. For skewed distributions, you get things that look a bit like &#8220;violins&#8221;, hence the name. The catch is that we [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=633&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Just for the record a nice alternative to boxplot() from the R introduction on <a href="http://www2.warwick.ac.uk/fac/sci/moac/degrees/modules/ch923/r_introduction/boxplot/">MOAC@University of Warwick:</a></p>
<blockquote><p>
Violin plots are essentially pretty versions of box plots, where the width is set by the local density. For skewed distributions, you get things that look a bit like &#8220;violins&#8221;, hence the name.</p>
<p>The catch is that we need to install and load the package <em>UsingR</em> to get the violin plot function. [...]<br />
<code>library(UsingR)<br />
simple.violinplot(Age ~ Gender, data=Santa, col = "red")</code>
</p></blockquote>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rforge.wordpress.com/633/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rforge.wordpress.com/633/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rforge.wordpress.com/633/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rforge.wordpress.com/633/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rforge.wordpress.com/633/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rforge.wordpress.com/633/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rforge.wordpress.com/633/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rforge.wordpress.com/633/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rforge.wordpress.com/633/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rforge.wordpress.com/633/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rforge.wordpress.com/633/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rforge.wordpress.com/633/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rforge.wordpress.com/633/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rforge.wordpress.com/633/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=633&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rforge.org/2011/02/07/violinplots-with-r/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2a4cc3be2c3e1c3b1442535ef7a46f42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rforge</media:title>
		</media:content>
	</item>
		<item>
		<title>Plot Function For Additive Cox Proportional Hazard Regression</title>
		<link>http://rforge.org/2009/10/30/plot-function-for-additive-cox-proportional-hazard-regression/</link>
		<comments>http://rforge.org/2009/10/30/plot-function-for-additive-cox-proportional-hazard-regression/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 14:14:08 +0000</pubDate>
		<dc:creator>rforge</dc:creator>
				<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Use R!]]></category>
		<category><![CDATA[additive model]]></category>
		<category><![CDATA[cox]]></category>
		<category><![CDATA[coxph]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[GAM]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[smoothing splines]]></category>

		<guid isPermaLink="false">http://rforge.wordpress.com/2009/10/30/plot-function-for-additive-cox-proportional-hazard-regression/</guid>
		<description><![CDATA[Usually a Cox-regression is achieved in R by library(survival) model &#60;- coxph ( SuvivalObject ~ Covariate1 + Covariate2 + Factor1 + Factor2 , data = Dataset ) The covariates can be enclosed in other funtions: factors should be enclosed by factor() strata, which allow to adjust for a factor without getting an estimate, should be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=466&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Usually a Cox-regression is achieved in R by<br />
<code>library(survival)<br />
model &lt;- coxph ( SuvivalObject ~ Covariate1 + Covariate2 + Factor1 + Factor2 , data = Dataset )</code></p>
<p>The covariates can be enclosed in other funtions:</p>
<ul>
<li>factors should be enclosed by <strong>factor()</strong></li>
<li>strata, which allow to adjust for a factor without getting an estimate, should be enclosed by <strong>strata()</strong></li>
<li>non-log linear continuous terms can be enclosed by<br />
<strong>pspline()</strong></li>
</ul>
<p>In the latter case the model might look like<br />
<code>model &lt;- coxph (SurvivalObject ~ pspline(Covariate1) + Covariate2 + factor(Factor1) + strata(Factor2) , data = Dataset )</code></p>
<p>The functional form of the covariates (including the factors) can now be plotted with<br />
<code>termplot(model)</code></p>
<p>Though the termplot() function fails with plotting just one covariate and leaves no cusomization.</p>
<p>The function <a href="http://bit.ly/6rv2nH"><strong>plotHR()</strong></a> plots the functional form of the desired term: <code>plotHR(model)</code><br />
plots the first term in the model by default but other terms can be accessed by calling their number (e.g. the second one):<br />
<code>plotHR(model , terms = 2)</code></p>
<p><img alt="" src="http://lh3.ggpht.com/_pxm6IBN55sE/SxOmXsC1TAI/AAAAAAAAB68/_7AmxpPHvcA/s800/Exp_COX_GAM.jpg" title="Exampleplot of Additive Proportional Hazard Model" class="alignnone" width="480" height="480" /></p>
<p>In order to use the function you have to &#8220;source&#8221; it into R. It is the same procedure as calling a package, but using &#8220;source&#8221; instead of &#8220;library&#8221;.</p>
<p>Paste the function syntax into a textfile and safe it (as plot.HR.R) on your harddisk, remember the path and include<br />
<code>source("C:Path/to/plotHR_0.6.R")</code><br />
before using the function.</p>
<h1><a href="http://db.tt/FRf1ME">Download</a> plotHR()</h1>
<p>Note: I have rewritten the function several times since I wrote the initial post &#8230; using version numbers now &#8230;</p>
<ul>
<li>
V0.6 &#8211; removed the y.log option, since the scale should be logaritmic anyway. Later I will also rewrite the x.log option, since the feature is already incorporated in the plot.default() function. I also removed the dottet line at HR=1 level, since some complained about it overstating the importance of the log(HR) intercept. I included it, since it gives a hint about the significance of the smooth term, in case the confidence intervalls  cross over the line&#8230; Those who miss it can add manually <code>lines( h = 0 , type = 2 )</code></p>
<p>I rewrote the &#8220;rugs&#8221; option. Try <code>rugs = "density"</code> It is still &#8220;beta&#8221;ish, but some like it.</li>
<li>V0.5 &#8211; bug fix for the y-scale and slight adjustment of the default plotting colors (paler CI shade and stronger term-line)</li>
<li>V0.4 &#8211; the y-scale should be logarithmic; a HR of 0.5 (50% reduced Hazard) should show the same distance from HR = 1 as a doubled Hazard (HR = 2); this is now default. The linear scale I used initially is biased in this concern (Hat-tip: Arve Ulvik, Eva Pedersen and Roy Nilsen). The option <strong>y.log</strong> allows both ways (linear and log-scale); the axis labels denote <strong>Hazard Ratio</strong> instead of log(HR).</li>
</ul>
<p>Usage:<br />
<code>plotHR( model , terms = 1 , se = TRUE , rug = "ticks" , x.log = FALSE , xlab = "" , ylab = "Hazard Ratio" , main = NULL , xlim = NULL , ylim = NULL, col.term = "#08519C", lwd.term = 3, col.se = "#DEEBF7", cex = 1 , bty = "n" , axes = TRUE )</code></p>
<ul>
<li>model &#8211; a coxph model</li>
<li>terms &#8211; integer; the number of the term to plot</li>
<li>se &#8211; logical TRUE/FALSE; plotting the CI</li>
<li>rug &#8211; &#8220;ticks&#8221; or &#8220;density&#8221;; rug plot or density plot at x-axis. Any other value for &#8220;rug&#8221; will omit the rugplot.</li>
<li>x.log &#8211; logical TRUE/FALSE; log-transformed exposure variable</li>
<li>xlab &#8211; character; x-axis label</li>
<li>ylab &#8211; character; y-axis label</li>
<li>main &#8211; character; main plot title</li>
<li>xlim &#8211; 2&#215;1 column vector; x-range of plot</li>
<li>ylim &#8211; 2&#215;1 column vector; y-range of plot</li>
<li>col.term &#8211; color of HR-curve</li>
<li>lwd.term &#8211; line width of HR-curve</li>
<li>col.se &#8211; color of CI (if plotted)</li>
<li>cex &#8211; numeric; size factor of labels</li>
<li>bty &#8211; specifies the boxtype around the plot. See <code>?plot.default</code></li>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rforge.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rforge.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rforge.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rforge.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rforge.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rforge.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rforge.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rforge.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rforge.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rforge.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rforge.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rforge.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rforge.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rforge.wordpress.com/466/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=466&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rforge.org/2009/10/30/plot-function-for-additive-cox-proportional-hazard-regression/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2a4cc3be2c3e1c3b1442535ef7a46f42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rforge</media:title>
		</media:content>

		<media:content url="http://lh3.ggpht.com/_pxm6IBN55sE/SxOmXsC1TAI/AAAAAAAAB68/_7AmxpPHvcA/s800/Exp_COX_GAM.jpg" medium="image">
			<media:title type="html">Exampleplot of Additive Proportional Hazard Model</media:title>
		</media:content>
	</item>
		<item>
		<title>Matrix Operations in R</title>
		<link>http://rforge.org/2009/08/21/matrix-operations-in-r/</link>
		<comments>http://rforge.org/2009/08/21/matrix-operations-in-r/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 14:13:37 +0000</pubDate>
		<dc:creator>rforge</dc:creator>
				<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Use R!]]></category>
		<category><![CDATA[inversion]]></category>
		<category><![CDATA[MASS]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[multiplication]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://rforge.wordpress.com/?p=420</guid>
		<description><![CDATA[How2 use R to do matrix calculus<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=420&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>R can do all kinds of matrix calculations, like multiplication, tranposing and calculating the inverse. The following manual was created by <a href="http://www.gseis.ucla.edu/courses/ed231a1/notes/matr.html">Phil Ender</a>.</p>
<p>Note: R wants the data to be entered by columns starting with column one.</p>
<h4>The Matrix</h4>
<pre><b># the matrix function
# 1st arg: c(2,3,-2,1,2,2) the values of the elements filling the columns
# 2nd arg: 3 the number of rows
# 3rd arg: 2 the number of columns

&gt; A &lt;- matrix(c(2,3,-2,1,2,2),3,2)
&gt; A</b>

[,1] [,2]
[1,]    2    1
[2,]    3    2
[3,]   -2    2</pre>
<h4>Is Something a Matrix</h4>
<pre><b>&gt; is.matrix(A)</b>

[1] TRUE

<b>&gt; is.vector(A)</b>

[1] FALSE</pre>
<h4>Multiplication by a Scalar</h4>
<pre><b>&gt; c &lt;- 3
&gt; c*A</b>

[,1] [,2]
[1,]    6    3
[2,]    9    6
[3,]   -6    6</pre>
<h4>Matrix Addition &amp; Subtraction</h4>
<pre><b>&gt; B &lt;- matrix(c(1,4,-2,1,2,1),3,2)
&gt; B</b>

[,1] [,2]
[1,]    1    1
[2,]    4    2
[3,]   -2    1

<b>&gt; C &lt;- A + B
&gt; C</b>

[,1] [,2]
[1,]    3    2
[2,]    7    4
[3,]   -4    3

<b>&gt; D &lt;- A - B
&gt; D</b>

[,1] [,2]
[1,]    1    0
[2,]   -1    0
[3,]    0    1</pre>
<h4>Matrix Multiplication</h4>
<pre><b>&gt; D &lt;- matrix(c(2,-2,1,2,3,1),2,3)
&gt; D</b>

[,1] [,2] [,3]
[1,]    2    1    3
[2,]   -2    2    1

<b>&gt; C &lt;- D %*% A
&gt; C</b>

[,1] [,2]
[1,]    1   10
[2,]    0    4

<b>&gt; C &lt;- A %*% D
&gt; C</b>

[,1] [,2] [,3]
[1,]    2    4    7
[2,]    2    7   11
[3,]   -8    2   -4

<b>&gt; D &lt;- matrix(c(2,1,3),1,3)
&gt; D</b>

[,1] [,2] [,3]
[1,]    2    1    3

<b>&gt; C &lt;- D %*% A
&gt; C</b>

[,1] [,2]
[1,]    1   10

<b>&gt; C &lt;- A %*% D</b>

Error in A %*% D : non-conformable arguments</pre>
<h4>Transpose of a Matrix</h4>
<pre><b>&gt; AT &lt;- t(A)
&gt; AT</b>

[,1] [,2] [,3]
[1,]    2    3   -2
[2,]    1    2    2

<b>&gt; ATT &lt;- t(AT)
&gt;ATT</b>

[,1] [,2]
[1,]    2    1
[2,]    3    2
[3,]   -2    2</pre>
<h2>Common Vectors</h2>
<h2>Unit Vector</h2>
<pre><b>&gt; U &lt;- matrix(1,3,1)
&gt; U</b>

[,1]
[1,]    1
[2,]    1
[3,]    1</pre>
<h2>Common Matrices</h2>
<h2>Unit Matrix</h2>
<p> <strong>Using Stata</strong></p>
<pre><b>&gt; U &lt;- matrix(1,3,2)
&gt; U</b>

[,1] [,2]
[1,]    1    1
[2,]    1    1
[3,]    1    1</pre>
<h2>Diagonal Matrix</h2>
<pre><b>&gt; S &lt;- matrix(c(2,3,-2,1,2,2,4,2,3),3,3)
&gt; S</b>

[,1] [,2] [,3]
[1,]    2    1    4
[2,]    3    2    2
[3,]   -2    2    3

<b>&gt; D &lt;- diag(S)
&gt; D</b>

[1] 2 2 3

<b>&gt; D &lt;- diag(diag(S))
&gt; D</b>

[,1] [,2] [,3]
[1,]    2    0    0
[2,]    0    2    0
[3,]    0    0    3</pre>
<h2>Identity Matrix</h2>
<pre><b>&gt; I &lt;- diag(c(1,1,1))
&gt; I</b>

[,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0
[3,]    0    0    1</pre>
<h2>Symmetric Matrix</h2>
<pre><b>&gt; C &lt;- matrix(c(2,1,5,1,3,4,5,4,-2),3,3)
&gt; C</b>

[,1] [,2] [,3]
[1,]    2    1    5
[2,]    1    3    4
[3,]    5    4   -2

<b>&gt; CT &lt;- t(C)
&gt; CT</b>

[,1] [,2] [,3]
[1,]    2    1    5
[2,]    1    3    4
[3,]    5    4   -2</pre>
<h4>Inverse of a Matrix</h4>
<pre><b>&gt; A &lt;- matrix(c(4,4,-2,2,6,2,2,8,4),3,3)
&gt; A</b>

[,1] [,2] [,3]
[1,]    4    2    2
[2,]    4    6    8
[3,]   -2    2    4

<b>&gt; # using MASS package

&gt; library(MASS)

&gt; AI &lt;- ginv(A)
&gt; AI</b>

[,1] [,2] [,3]
[1,]  1.0 -0.5  0.5
[2,] -4.0  2.5 -3.0
[3,]  2.5 -1.5  2.0

<b>&gt; # using car package

&gt; library(car)

&gt; AI &lt;- inv(A)
&gt; AI</b>

[,1] [,2] [,3]
[1,]  1.0 -0.5  0.5
[2,] -4.0  2.5 -3.0
[3,]  2.5 -1.5  2.0

<b>&gt; A %*% AI</b>

[,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0
[3,]    0    0    1

<b>&gt; AI %*% A</b>

[,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0
[3,]    0    0    1</pre>
<h4>Inverse &amp; Determinant of a Matrix</h4>
<pre><b>&gt; C &lt;- matrix(c(2,1,6,1,3,4,6,4,-2),3,3)
&gt; C</b>

[,1] [,2] [,3]
[1,]    2    1    6
[2,]    1    3    4
[3,]    6    4   -2

<b>&gt; CI &lt;- inv(C)
CI</b>

[,1]        [,2]        [,3]
[1,]  0.2156863 -0.25490196  0.13725490
[2,] -0.2549020  0.39215686  0.01960784
[3,]  0.1372549  0.01960784 -0.04901961

<b>&gt; d &lt;- det(C)
&gt; d</b>

[1] -102</pre>
<h4>Number of Rows &amp; Columns</h4>
<pre><b>&gt; X &lt;- matrix(c(3,2,4,3,2,-2,6,1),4,2)
&gt; X</b>

[,1] [,2]
[1,]    3    2
[2,]    2   -2
[3,]    4    6
[4,]    3    1

<b>&gt; dim(X)</b>

[1] 4 2

<b>&gt; r &lt;- nrow(X)
&gt; r</b>

[1] 4

<b>&gt; c &lt;- ncol(X)
&gt; c</b>

[1] 2</pre>
<h4>Computing Column &amp; Row Sums</h4>
<pre><b># note the uppercase S

&gt; A &lt;- matrix(c(2,3,-2,1,2,2),3,2)
&gt; A</b>

[,1] [,2]
[1,]    2    1
[2,]    3    2
[3,]   -2    2

<b>&gt; c &lt;- colSums(A)
&gt; c</b>

[1] 3 5

<b>&gt; r &lt;- rowSums(A)
&gt; r</b>

[1] 3 5 0

<b>&gt; a &lt;- sum(A)
&gt; a</b>

[1] 8</pre>
<h4>Computing Column &amp; Row Means</h4>
<pre><b># note the uppercase M

&gt; cm &lt;- colMeans(A)
&gt; cm</b>

[1] 1.000000 1.666667

<b>&gt; rm &lt;- rowMeans(A)
&gt; rm</b>

[1] 1.5 2.5 0.0

<b>&gt; m &lt;- mean(A)
&gt; m</b>

[1] 1.333333</pre>
<h4>Horizontal Concatenation</h4>
<pre><b>&gt; A
&gt; A</b>

[,1] [,2]
[1,]    2    1
[2,]    3    2
[3,]   -2    2

<b>&gt; B &lt;- matrix(c(1,3,2,1,4,2),3,2)
&gt; B</b>

[,1] [,2]
[1,]    1    1
[2,]    3    4
[3,]    2    2

<b>&gt; C &lt;- cbind(A,B)
&gt; C</b>

[,1] [,2] [,3] [,4]
[1,]    2    1    1    1
[2,]    3    2    3    4
[3,]   -2    2    2    2</pre>
<h4>Vertical Concatenation (Appending)</h4>
<pre><b>&gt; C &lt;- rbind(A,B)
&gt; C</b>

[,1] [,2]
[1,]    2    1
[2,]    3    2
[3,]   -2    2
[4,]    1    1
[5,]    3    4
[6,]    2    2</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rforge.wordpress.com/420/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rforge.wordpress.com/420/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rforge.wordpress.com/420/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rforge.wordpress.com/420/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rforge.wordpress.com/420/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rforge.wordpress.com/420/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rforge.wordpress.com/420/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rforge.wordpress.com/420/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rforge.wordpress.com/420/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rforge.wordpress.com/420/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rforge.wordpress.com/420/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rforge.wordpress.com/420/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rforge.wordpress.com/420/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rforge.wordpress.com/420/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=420&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rforge.org/2009/08/21/matrix-operations-in-r/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2a4cc3be2c3e1c3b1442535ef7a46f42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rforge</media:title>
		</media:content>
	</item>
		<item>
		<title>Densityplot Variations</title>
		<link>http://rforge.org/2009/08/18/densityplot-variations/</link>
		<comments>http://rforge.org/2009/08/18/densityplot-variations/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 08:31:13 +0000</pubDate>
		<dc:creator>rforge</dc:creator>
				<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Use R!]]></category>
		<category><![CDATA[density]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[plot]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://rforge.wordpress.com/?p=395</guid>
		<description><![CDATA[Variation I Just playing around the other day to get the default plot.density() function a bit more like publishing quality. Above is my favorite so far. Further down are two more spartanic versions. In this connection I also wrote my first function. When I get more customed to R I will package my ideas into [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=395&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Variation I</h2>
<div id="attachment_372" class="wp-caption alignnone" style="width: 410px"><img src="http://rforge.files.wordpress.com/2009/08/densityplot3.jpg?w=510" alt="Densityplot with filled area, quartiles and mean" title="Densityplot3"   class="size-full wp-image-372" /><p class="wp-caption-text">Densityplot with filled area, quartiles and mean</p></div>
<p>Just playing around the other day to get the default <em>plot.density()</em> function a bit more like publishing quality. Above is my favorite so far. Further down are two more spartanic versions.</p>
<p>In this connection I also wrote my first function. When I get more customed to R I will package my ideas into an R library &#8211; but later&#8230;</p>
<p>There is the possibility to call R-functions without embedding the code into your analysis script, but I did not look that up yet, so embedding the following code into your script (at the beginning) and then using<br />
<code>densityplot(Dataset$Coviate)</code><br />
will do the job, where <strong>Dataset</strong> and <strong>Covariate</strong> have to be replaced by the according values of course.</p>
<p>Now the function code:<br />
<code># function densityplot<br />
densityplot &lt;- function(x , digits = 1 , xlab = &quot;&quot; , ylab = &quot;&quot; , main = &quot;Density&quot; , col = &quot;blue&quot;){<br />
dens &lt;- density(x , na.rm = T)<br />
bins &lt;- as.numeric(cut(dens$x , breaks = fivenum(x)))<br />
i1 &lt;- bins == 1 &amp; !is.na(bins)<br />
i2 &lt;- bins == 2 &amp; !is.na(bins)<br />
i3 &lt;- bins == 3 &amp; !is.na(bins)<br />
i4 &lt;- bins == 4 &amp; !is.na(bins)<br />
x.p1 &lt;- dens$x[i1]; x.p1 &lt;- c(x.p1,max(x.p1),min(x.p1))<br />
x.p2 &lt;- dens$x[i2]; x.p2 &lt;- c(x.p2,max(x.p2),min(x.p2))<br />
x.p3 &lt;- dens$x[i3]; x.p3 &lt;- c(x.p3,max(x.p3),min(x.p3))<br />
x.p4 &lt;- dens$x[i4]; x.p4 &lt;- c(x.p4,max(x.p4),min(x.p4))<br />
y.p1 &lt;- dens$y[i1]; y.p1 &lt;- c(y.p1,0,0)<br />
y.p2 &lt;- dens$y[i2]; y.p2 &lt;- c(y.p2,0,0)<br />
y.p3 &lt;- dens$y[i3]; y.p3 &lt;- c(y.p3,0,0)<br />
y.p4 &lt;- dens$y[i4]; y.p4 &lt;- c(y.p4,0,0)<br />
plot(dens , type = &quot;n&quot; , axes = F, main = main, xlab = xlab , ylab = ylab)<br />
polygon(x.p1,y.p1 , border = F , col = col)<br />
polygon(x.p2,y.p2 , border = F , col = col)<br />
polygon(x.p3,y.p3 , border = F , col = col)<br />
polygon(x.p4,y.p4 , border = F , col = col)<br />
axis(side = 1 , line = 0 , at = fivenum(x, na.rm = T) , label = c(&quot;Minimum&quot;,&quot;Quartile 1&quot;, &quot;Median&quot;, &quot;Quartile 3&quot;, &quot;Maximum&quot;), lwd = 0, cex.axis = 0.6)<br />
axis(side = 1 , line = 1 , at = fivenum(x, na.rm = T))<br />
axis(side = 1 , line = 1 , at = round(mean(x , na.rm = T) , digits = digits) , tcl = 0.4 , label = F)<br />
axis(side = 1 , line = -1.5 , at = round(mean(x , na.rm = T) , digits = digits) , tick = F , cex.axis = 0.6)<br />
axis(side = 1 , line = -2.0 , at = round(mean(x , na.rm = T) , digits = digits) , label = &quot;Mean&quot; , tick = F , cex.axis = 0.6)<br />
}</code></p>
<h2>Variation II</h2>
<p>Some might find the colored area to much, although IMHO it puts the focus on the fact that one is looking at areas when ploting a density. But then something similar without the color fill. Not a function just a few lines of code to embed and adjust to the script:<br />
<div id="attachment_371" class="wp-caption alignnone" style="width: 410px"><img src="http://rforge.files.wordpress.com/2009/08/densityplot2.jpg?w=510" alt="Densityplot with quartiles and mean" title="Densityplot2"   class="size-full wp-image-371" /><p class="wp-caption-text">Densityplot with quartiles and mean</p></div></p>
<p>&#8230; and the source&#8230;<br />
<code>plot(density(angio$PE_ALDER , na.rm = T), axes = F, main = "Basic densityplot", xlab = "" , ylab = "")<br />
# Add Quartiles<br />
axis(side = 1 , line = 1 , at = fivenum(angio$PE_ALDER, na.rm = T) , label = c("Minimum","Quartile 1", "Median", "Quartile 3", "Maximum"), lwd = 0, cex.axis = 0.6)<br />
axis(side = 1 , line = 2 , at = fivenum(angio$PE_ALDER, na.rm = T))<br />
abline(v = fivenum(angio$PE_ALDER, na.rm = T)[2:4] , lty = 3)<br />
# Mean<br />
axis(side = 1 , line = 2 , at = round(mean(angio$PE_ALDER , na.rm = T) , digits = 2) , tcl = 0.4 , label = F)<br />
axis(side = 1 , line = -0.5 , at = round(mean(angio$PE_ALDER , na.rm = T) , digits = 2) , tick = F)<br />
axis(side = 1 , line = -1.4 , at = round(mean(angio$PE_ALDER , na.rm = T) , digits = 2) , label = "Mean" , tick = F , cex.axis = 0.6)<br />
abline(v = mean(angio$PE_ALDER , na.rm = T) , lty = 4)</code></p>
<h2>Variation III</h2>
<p>&#8230; finally an even more stripped down version without the mean:<br />
<img src="http://rforge.files.wordpress.com/2009/08/densityplot1.jpg?w=510" alt="Densityplot1" title="Densityplot1"   class="alignnone size-full wp-image-370" /><br />
which was achieved by:<br />
<code>plot(density(angio$PE_ALDER , na.rm = T), axes = F, main = "Basic densityplot", xlab = "Age")<br />
abline(v = fivenum(angio$PE_ALDER, na.rm = T)[2:4] , lty = 3)<br />
axis(side = 1 , line = -1 , at = fivenum(angio$PE_ALDER, na.rm = T) , label = c("Minimum","Quartile 1", "Median", "Quartile 3", "Maximum"), lwd = 0, cex.axis = 0.6)<br />
axis(side = 1 , at = fivenum(angio$PE_ALDER, na.rm = T))</code></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rforge.wordpress.com/395/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rforge.wordpress.com/395/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rforge.wordpress.com/395/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rforge.wordpress.com/395/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rforge.wordpress.com/395/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rforge.wordpress.com/395/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rforge.wordpress.com/395/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rforge.wordpress.com/395/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rforge.wordpress.com/395/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rforge.wordpress.com/395/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rforge.wordpress.com/395/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rforge.wordpress.com/395/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rforge.wordpress.com/395/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rforge.wordpress.com/395/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=395&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rforge.org/2009/08/18/densityplot-variations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2a4cc3be2c3e1c3b1442535ef7a46f42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rforge</media:title>
		</media:content>

		<media:content url="http://rforge.files.wordpress.com/2009/08/densityplot3.jpg" medium="image">
			<media:title type="html">Densityplot3</media:title>
		</media:content>

		<media:content url="http://rforge.files.wordpress.com/2009/08/densityplot2.jpg" medium="image">
			<media:title type="html">Densityplot2</media:title>
		</media:content>

		<media:content url="http://rforge.files.wordpress.com/2009/08/densityplot1.jpg" medium="image">
			<media:title type="html">Densityplot1</media:title>
		</media:content>
	</item>
		<item>
		<title>GAM Plot with 95% Confidence Shade</title>
		<link>http://rforge.org/2009/08/11/gam-plot-with-95-confidence-shade/</link>
		<comments>http://rforge.org/2009/08/11/gam-plot-with-95-confidence-shade/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 13:11:49 +0000</pubDate>
		<dc:creator>rforge</dc:creator>
				<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Use R!]]></category>
		<category><![CDATA[additive model]]></category>
		<category><![CDATA[GAM]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://rforge.wordpress.com/?p=345</guid>
		<description><![CDATA[The lightblue shade denoting the 95% pointwise confidence limits of the GAM estimate is a polygon() object in R. Usually one would plot the GAM model with the default termplot() function and specifiy se=T to get the confidence limits as lines. I showed in a recent post how to plot the fitted GAM smooth and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=345&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><img src="http://rforge.files.wordpress.com/2009/08/aphm_ccte_cishade.jpg?w=510" alt="GAM plot with conficence &quot;shade&quot; and customized rugs" title="GAM plot with conficence &quot;shade&quot; and customized rugs"   class="alignnone size-full wp-image-344" /><br />
The lightblue shade denoting the 95% pointwise confidence limits of the GAM estimate is a <em>polygon()</em> object in R. </p>
<p>Usually one would plot the GAM model with the default termplot() function and specifiy se=T to get the confidence limits as lines. I showed in a <a href="http://rforge.wordpress.com/2009/06/16/how2plot-nicer-gam-curves/">recent post</a> how to plot the fitted GAM smooth and the confidence limits manually.</p>
<p>The same approach can be used to have the confidence limits as a shade. In order to achieve this:</p>
<ol>
<li>Fit the model</li>
<p><code>library(mgcv)<br />
model &lt;- gam( MyResponse ~ MyCovariate , data = MyDataset)</code></p>
<li>get the estimated values of the fitted smoothing spline</li>
<p><code>fit &lt;- predict( model , se = TRUE )$fit</code></p>
<li>and pointwise standard errors</li>
<p><code>se &lt;- predict( model , se = TRUE)$se.fit</code></p>
<li>calculate the values of the upper and lower 95%-confidence limits</li>
<p><code>lcl &lt;- fit - 1.96 * se<br />
ucl &lt;- fit + 1.96 * se</code></p>
<li>plot an empty coordinate system with right scaling, labels , titles and so on. I prefer to include axes = FALSE and add the axes manually. See <a href="http://rforge.wordpress.com/2009/08/10/fancy-rugs-in-regression-plots/">here</a> for an example.</li>
<p><code>plot( 0 , type = "n" , bty = "n" , xlab = "The x-axis lable" , ylab = "The y-axis lable" , main = "The Title" , xlim = c(0,5) , ylim = c(0,3) )</code></p>
<li>No it gets a bit tricky with sorting the coordinates in the right order. R provides a very effective way to achieve this, though it is not easily understandable at once. We create two indices on the dataset <strong>i.for</strong> and <strong>i.back</strong> which number the dataset according to the independend variable on the x-axis &#8211; forward and backward respectively:</li>
<p><code>i.for &lt;- order( MyDataset$MyCovariate )<br />
i.back &lt;- order( MyDataset$MyCovariate , decreasing = TRUE )</code></p>
<li>The points of the shade polygon follow here first the upper confidence line (ucl) forward and then the lower confidence line backward. The coordinates of the shade-polygon are</li>
<p><code>x.polygon &lt;- c( MyDataset$MyCovariate[i.for] , MyDataset$MyCovariate[i.back] )<br />
y.polygon &lt;- c( ucl[i.for] , lcl[i.back] )</code></p>
<li>First plot the polygon with the confidence shade &#8211; otherwise the GAM plot vanishes behind it.</li>
<p><code>polygon( x.polygon , y.polygon , col = "#A6CEE3" , border = NA )</code></p>
<li>now the mainplot <strong>afterwards</strong></li>
<p><code>lines( MyDataset$MyCovariate[i.for] , fit[i.for], col = "#1F78B4" , lwd = 3 )</code></p>
<li> add a horizontal line marking the intercept ( the mean of the covariate) &#8211; note: univariate only. I will post a remark on this later&#8230;</li>
<p><code>abline( h = mean(MyDataset$MyCovariate) , lty = 2 )</code></p>
<ol>
<p>Hope no typos. Comment if you find some.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rforge.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rforge.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rforge.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rforge.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rforge.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rforge.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rforge.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rforge.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rforge.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rforge.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rforge.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rforge.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rforge.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rforge.wordpress.com/345/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=345&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rforge.org/2009/08/11/gam-plot-with-95-confidence-shade/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2a4cc3be2c3e1c3b1442535ef7a46f42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rforge</media:title>
		</media:content>

		<media:content url="http://rforge.files.wordpress.com/2009/08/aphm_ccte_cishade.jpg" medium="image">
			<media:title type="html">GAM plot with conficence &#34;shade&#34; and customized rugs</media:title>
		</media:content>
	</item>
		<item>
		<title>Fancy Rugs in Regression Plots</title>
		<link>http://rforge.org/2009/08/10/fancy-rugs-in-regression-plots/</link>
		<comments>http://rforge.org/2009/08/10/fancy-rugs-in-regression-plots/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 08:44:09 +0000</pubDate>
		<dc:creator>rforge</dc:creator>
				<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Use R!]]></category>
		<category><![CDATA[GAM]]></category>
		<category><![CDATA[plot]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[regression]]></category>
		<category><![CDATA[rugs]]></category>

		<guid isPermaLink="false">http://rforge.wordpress.com/?p=269</guid>
		<description><![CDATA[Rugplots along the axes show the distribution of the underlying data in regression model plots. This is particulary useful in connection with additive (nonparametric) models where the plotted smooth function is the exclusive representation of the model in order to assess how much data contributed to the model fit at the different values of the exlanatory variable. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=269&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="attachment_321" class="wp-caption alignnone" style="width: 410px"><img class="size-full wp-image-321" title="FancyRugs" src="http://rforge.files.wordpress.com/2009/08/fancyrugs.jpg?w=510" alt="Additive Regression Model with Customized Rug"   /><p class="wp-caption-text">Additive Regression Model with Customized Rug</p></div>
<p>Rugplots along the axes show the distribution of the underlying data in regression model plots. This is particulary useful in connection with additive (nonparametric) models where the plotted smooth function is the exclusive representation of the model in order to assess how much data contributed to the model fit at the different values of the exlanatory variable.</p>
<p>The custom plot.gam() function includes the possibility of such <em>rugs</em> and pointwise <em>conficence intervalls</em> by default.</p>
<p>Adding quartiles to the rugs requires some customization, though. I included the complete code to produce the above plot underneath.</p>
<p>The example for this GAM is borrowed from the excellent book of Alain Zuur et. al. <a href="http://www.amazon.co.uk/gp/reader/0387874577/ref=sib_dp_pt#reader-link">Mixed Effects Models and Extensions in Ecology with R</a> p.55ff. The ISIT data to run the code above is included in the R package <a href="http://www.highstat.com/AED_1.0.zip"><em>AED</em></a> which can be downloaded from the <a href="http://www.highstat.com">books website</a>.</p>
<p><strong>Note: the package AED is needed for the example dataset only. It is NOT necessary to use the example code on ones own dataset.</strong></p>
<p>The main points concerning the rugs and quantile lables on the x-axis are:</p>
<ol>
<li>Plot the coordinate system <strong>without</strong> lables</li>
<p><code>plot( ... , axes = FALSE )</code></p>
<li>Plot the x-axis with x-lables</li>
<p><code>axis(side = 1 , line = 0.3 , at = 0:5*1000 , tick = TRUE)</code></p>
<li>Plot rugs &#8211; the jitter() is necessary since a lot of datapoints sit on the same values of SampleDepth &#8211; so shake them a bit.</li>
<p><code>axis(side = 1 , line = -0.9 , at = jitter(ISIT$SampleDepth) , labels = F , tick = T , tcl = 0.8 , lwd.ticks = 0.1 , lwd = 0)</code></p>
<li>Print the lables &#8220;1Q&#8221;, &#8220;Median&#8221; and &#8220;3Q&#8221; or whatever you like to call them on the right position. The <strong>line</strong> and <strong>padj</strong> parameter set the position of the text and <strong>cex.axis</strong> the textsize.</li>
<p><code>axis(side = 1 , line = -0.8 , at = fivenum(ISIT$SampleDepth)[2:4], lwd = 0 , tick = F , labels = c(&quot;1Q&quot;,&quot;median&quot;,&quot;3Q&quot;), cex.axis = 0.7, col.axis = &quot;black&quot; , padj = -2.8)</code></p>
<li>Plot thick tickmarks crossing through the rug cloud at 1Q, median and 3Q</li>
<p><code>axis(side = 1 , line = -0.8 , at = fivenum(ISIT$SampleDepth)[2:4], lwd = 0 , tick = T, tcl = 1.1 , lwd.ticks = 1 , col.ticks = &quot;black&quot;, labels = FALSE)</code></p>
<li>and finally short thick tickmarks under the text touching the x-axis</li>
<p><code>axis(side = 1 , line = 0.3, at = fivenum(ISIT$SampleDepth)[2:4], lwd = 0 , tick = T, tcl = 0.2 , lwd.ticks = 1 , col.ticks = &quot;black&quot;, labels = FALSE)</code>
</ol>
<p>Here goes the complete code:<br />
<code>library(mgcv)<br />
library(AED)<br />
data(ISIT)<br />
#<br />
# Fit a univariate GAM model<br />
model &lt;- gam(Sources ~ s(SampleDepth) , data = ISIT)<br />
fit &lt;- predict(model, se = T)$fit<br />
se &lt;- predict(model, se = T)$se.fit<br />
lcl &lt;- fit - 1.96 * se<br />
ucl &lt;- fit + 1.96 * se<br />
#<br />
# open a jpeg<br />
jpeg(&quot;FancyRugs.jpg&quot; , width=400, height=400)<br />
#<br />
# set plotting options: 1 plot per page, horizontal labels and textsize<br />
par(mfrow = c(1,1) , las = 1 , cex = 1)<br />
#<br />
# plot coordinatesystem and labels<br />
plot(0 , bty = &quot;n&quot; , type = &quot;n&quot; , xlim = c(0,5000) , ylim = c(-10,50) , xlab = &quot;Depth (m)&quot; , ylab = expression(paste(&quot;Number of sources (&quot; , m^-3 , &quot;)&quot;)) , axes = FALSE)<br />
#<br />
title(main=&quot;Association between number of sources of\nbioluminescent organisms and ocean depth&quot; , cex.main = 0.8)<br />
#<br />
## _____ X-AXIS ______<br />
# x-axis values<br />
axis(side = 1 , line = 0.3 , at = 0:5*1000 , tick = TRUE)<br />
#<br />
# rugs at datapoints<br />
axis(side = 1 , line = -0.9 , at = jitter(ISIT$SampleDepth) , labels = F , tick = T , tcl = 0.8 , lwd.ticks = 0.1 , lwd = 0)<br />
#<br />
# labels at 1Q, median and 3Q<br />
axis(side = 1 , line = -0.8 , at = fivenum(ISIT$SampleDepth)[2:4], lwd = 0 , tick = F , labels = c(&quot;1Q&quot;,&quot;median&quot;,&quot;3Q&quot;), cex.axis = 0.7, col.axis = &quot;black&quot; , padj = -2.8)<br />
#<br />
# tick marks at 1Q, median and 3Q<br />
axis(side = 1 , line = 0.3, at = fivenum(ISIT$SampleDepth)[2:4], lwd = 0 , tick = T, tcl = 0.2 , lwd.ticks = 1 , col.ticks = &quot;black&quot;, labels = FALSE)<br />
#<br />
axis(side = 1 , line = -0.8 , at = fivenum(ISIT$SampleDepth)[2:4], lwd = 0 , tick = T, tcl = 1.1 , lwd.ticks = 1 , col.ticks = &quot;black&quot;, labels = FALSE)<br />
#<br />
## _____ Y-AXIS ______<br />
# y-axis values<br />
axis(side = 2 , at = 0:5*10)<br />
#<br />
# rugs at datapoints<br />
axis(side = 2 , line = -0.9 , at = jitter(ISIT$Sources) , labels = F , tick = T , tcl = 0.8 , lwd.ticks = 0.1 , lwd = 0)<br />
#<br />
# labels at 1Q, median and 3Q<br />
axis(side = 2 , line = -0.7 , at = fivenum(ISIT$Sources)[2:4], lwd = 0 , tick = F , labels = c(&quot;1Q&quot;,&quot;median&quot;,&quot;3Q&quot;), cex.axis = 0.7, col.axis = &quot;black&quot;)<br />
#<br />
# thicker tick marks at 1Q, median and 3Q<br />
axis(side = 2 , line = 0.3, at = fivenum(ISIT$Sources)[2:4], lwd = 0 , tick = T, tcl = 0.3 , lwd.ticks = 1 , col.ticks = &quot;black&quot;, labels = FALSE , padj = -2)<br />
axis(side = 2 , line = -0.7 , at = fivenum(ISIT$Sources)[2:4], lwd = 0 , tick = T, tcl = 1.1 , lwd.ticks = 1 , col.ticks = &quot;black&quot; , labels = FALSE)<br />
#<br />
# horizontal line marking the intercept = mean(Sources) (for univariate model only)<br />
abline(h=mean(ISIT$Sources), lty=3)<br />
#<br />
# Scatterplot<br />
lines(ISIT$SampleDepth , ISIT$Source , type = &quot;p&quot; , cex = 0.4 , lwd = 0.2 , col = &quot;grey&quot;)<br />
#<br />
# plot main figure<br />
lines(ISIT$SampleDepth[order(ISIT$SampleDepth)] , fit[order(ISIT$SampleDepth)] , col = &quot;black&quot; , lwd = 2)<br />
#<br />
# plot lower confidence limit (lcl)<br />
lines(ISIT$SampleDepth[order(ISIT$SampleDepth)] , lcl[order(ISIT$SampleDepth)] , col = &quot;grey&quot; , lwd = 1)<br />
#<br />
# plot upper confidence limit (ucl)<br />
lines(ISIT$SampleDepth[order(ISIT$SampleDepth)] , ucl[order(ISIT$SampleDepth)] , col = &quot;grey&quot; , lwd = 1)<br />
#<br />
# closing the jpg file<br />
dev.off()</code> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rforge.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rforge.wordpress.com/269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rforge.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rforge.wordpress.com/269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rforge.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rforge.wordpress.com/269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rforge.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rforge.wordpress.com/269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rforge.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rforge.wordpress.com/269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rforge.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rforge.wordpress.com/269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rforge.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rforge.wordpress.com/269/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=269&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rforge.org/2009/08/10/fancy-rugs-in-regression-plots/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2a4cc3be2c3e1c3b1442535ef7a46f42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rforge</media:title>
		</media:content>

		<media:content url="http://rforge.files.wordpress.com/2009/08/fancyrugs.jpg" medium="image">
			<media:title type="html">FancyRugs</media:title>
		</media:content>
	</item>
		<item>
		<title>Correlation matrices</title>
		<link>http://rforge.org/2009/08/06/correlation-matrices/</link>
		<comments>http://rforge.org/2009/08/06/correlation-matrices/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 14:00:43 +0000</pubDate>
		<dc:creator>kjetil</dc:creator>
				<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Use R!]]></category>

		<guid isPermaLink="false">http://rforge.wordpress.com/?p=292</guid>
		<description><![CDATA[Correlation matrices are a great way to visualize covariation in large datasets. Thanks to Sarkar (2008) and Friendly (2002), we can borrow these examples. First we need to load a few packages: library(lattice) library(ellipse) We then go on to importing our data: MyData &#60;- read.table("/FILE/LOCATION/FILE.txt", header=TRUE,sep='\t',quote='') Then on for some tedious work. We&#8217;ll have to manually [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=292&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Correlation matrices are a great way to visualize covariation in large datasets. Thanks to Sarkar (2008) and Friendly (2002), we can borrow these examples. First we need to load a few packages:</p>
<p><code>library(lattice)<br />
library(ellipse)</code></p>
<p>We then go on to importing our data:</p>
<p><code>MyData &lt;- read.table("/FILE/LOCATION/FILE.txt", header=TRUE,sep='\t',quote='')</code></p>
<p>Then on for some tedious work. We&#8217;ll have to manually type in the name (as it named i the dataset!) of each of the variables we&#8217;re interested in (unless off course you&#8217;ll want to analyze the whole shebang &#8211; which often isn&#8217;t desirable).</p>
<p><code>var &lt;- c("var1","var2","var3")</code></p>
<p>We then proceed to make a correlation matrix with the selected variables vector &#8220;var&#8221;:</p>
<p><code>cor.MyData &lt;- cor(MyData[,var],use="pair")</code></p>
<p>And here&#8217;s the money shot; setting the order of the variables in the matrix based on the result of a cluster analysis. This is for easier visual interpretation.</p>
<p><code>ord &lt;- order.dendrogram(as.dendrogram(hclust(dist(cor.MyData))))</code></p>
<p>We then can make our Corrgrams. First an easy one:</p>
<p><code>print(levelplot(cor.MyData[ord,ord],xlab=NULL,ylab=NULL,<br />
at=do.breaks(c(-1.01,1.01),101),scales=list(x=list(rot=90)),<br />
colorkey=list(space="top"),<br />
col.regions=colorRampPalette(c("red","white","blue"))))</code></p>
<p><img src="http://rforge.files.wordpress.com/2009/08/enkel.jpg?w=510" alt="Simple correlation matrix" title="Simple correlation matrix"   class="aligncenter size-full wp-image-405" /></p>
<p>Then on to the more elaborate ones, first a informative one using the ellipse package. We&#8217;ll have to write our own panel function here (again, thanks to Sarkar 2008), but this is completly generic, so cut &#8216;n&#8217; paste should suffice.</p>
<p><code>panel.corrgram&lt;-function(x,y,z,subscripts,at,<br />
level=0.9,label=FALSE,...<br />
{require('ellipse',quietly=TRUE)<br />
x&lt;-as.numeric(x)[subscripts]<br />
y&lt;-as.numeric(y)[subscripts]<br />
z&lt;-as.numeric(z)[subscripts]<br />
zcol&lt;-level.colors(z,at=at,...)<br />
for(i in seq(along=z)){<br />
ell&lt;-ellipse(z[i],level=level,npoints=50,<br />
scale=c(.2,.2),centre=c(x[i],y[i]))<br />
panel.polygon(ell,col=zcol[i],border=zcol[i],...)<br />
}<br />
if (label)<br />
panel.text(x=x,y=y,lab=100*round(z,2),<br />
cex=0.8,col=ifelse(z&lt;0,'white','black'))<br />
}</code></p>
<p>To create a *.pdf of your output we&#8217;ll run following code (remember your working directory!):</p>
<p><code>pdf('corrgram_ellipse.pdf',10,10)<br />
levelplot(cor.MyData[ord,ord],<br />
at=do.breaks(c(-1.01,1.01),20),xlab=NULL,<br />
ylab=NULL,colorkey=list(space='top'),<br />
scales=list(x=list(rot=90)),<br />
panel=panel.corrgram,label=TRUE)<br />
dev.off()</code></p>
<p><img src="http://rforge.files.wordpress.com/2009/08/ellipse.jpg?w=510" alt="Correlation matrix - ellipse" title="Correlation matrix - ellipse"   class="aligncenter size-full wp-image-406" /></p>
<p>However, if you&#8217;re more in the pacman thing, this next one might be a better choice. We&#8217;ll start by writing a new panel function:</p>
<p><code>panel.corrgram.2&lt;-function(x,y,z,<br />
subscripts,at=pretty(z),scale=0.8,...)<br />
{<br />
require('grid',quietly=TRUE)<br />
x&lt;-as.numeric(x)[subscripts]<br />
y&lt;-as.numeric(y)[subscripts]<br />
z&lt;-as.numeric(z)[subscripts]<br />
zcol&lt;-level.colors(z,at=at,...)<br />
for(i in seq(along=z))<br />
{<br />
lims&lt;-range(0,z[i])<br />
tval&lt;-2*base::pi*<br />
seq(from=lims[1],to=lims[2],by=0.01)<br />
grid.polygon(x=x[i]+.5*scale*c(0,sin(tval)),<br />
y=y[i]+.5*scale*c(0,cos(tval)),<br />
default.units='native',<br />
gp=gpar(fill=zcol[i]))<br />
grid.circle(x=x[i],y=y[i],r=.5*scale,<br />
default.units='native')<br />
}<br />
}</code></p>
<p>Which we&#8217;ll export:</p>
<p><code>pdf('corrgram_pacman.pdf',10,10)<br />
levelplot(cor.MyData[ord,ord],<br />
at=do.breaks(c(-1.01,1.01),101),xlab=NULL,<br />
ylab=NULL,colorkey=list(space='top'),<br />
scales=list(x=list(rot=90)),<br />
panel=panel.corrgram.2,<br />
col.regions=colorRampPalette(c('red','white','blue')))<br />
dev.off()</code></p>
<p><img src="http://rforge.files.wordpress.com/2009/08/pacman.jpg?w=510&#038;h=510" alt="Correlation matrix - pacman" title="Correlation matrix - pacman" width="510" height="510" class="aligncenter size-full wp-image-410" /></p>
<p>Of course, changing the output size (and type) is easy. Changing pdf &#8211;&gt; png (or the format of your choosing). Size were in these examples 10 by 10 inches.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rforge.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rforge.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rforge.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rforge.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rforge.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rforge.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rforge.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rforge.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rforge.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rforge.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rforge.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rforge.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rforge.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rforge.wordpress.com/292/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=292&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rforge.org/2009/08/06/correlation-matrices/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/25aa54194d54c76ba9c845bb5ead462d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kjetil</media:title>
		</media:content>

		<media:content url="http://rforge.files.wordpress.com/2009/08/enkel.jpg" medium="image">
			<media:title type="html">Simple correlation matrix</media:title>
		</media:content>

		<media:content url="http://rforge.files.wordpress.com/2009/08/ellipse.jpg" medium="image">
			<media:title type="html">Correlation matrix - ellipse</media:title>
		</media:content>

		<media:content url="http://rforge.files.wordpress.com/2009/08/pacman.jpg" medium="image">
			<media:title type="html">Correlation matrix - pacman</media:title>
		</media:content>
	</item>
		<item>
		<title>How2plot nicer GAM curves</title>
		<link>http://rforge.org/2009/06/16/how2plot-nicer-gam-curves/</link>
		<comments>http://rforge.org/2009/06/16/how2plot-nicer-gam-curves/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 20:35:32 +0000</pubDate>
		<dc:creator>rforge</dc:creator>
				<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Use R!]]></category>
		<category><![CDATA[GAM]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[regression]]></category>

		<guid isPermaLink="false">http://rforge.wordpress.com/?p=31</guid>
		<description><![CDATA[The default plot method for GAM models is very useful and mostly sufficient. Though there are situations where are more fancy, customized solution is appropriate ...<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=31&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Generalized additive models are an established tool to model correlations for nonlinear covariates without too much hazzle with the form of the assosiation between predictor and response. It is a great straightforward tool, which leaves most of the work to the computer. </p>
<p>The default plotting method produces clean plots for all covariates in the model (or a selection) but: They do not have presentation quality by any means! To achieve this one needs customization (colors, understandable axes-labels, scaling).</p>
<p>This example shows a customization variant for a additive regression model with one covariate. The goal was to display the absolute value of the response variable on the y-axis and not the &#8220;difference from intercept&#8221; which is default.</p>
<p>This is only meaningful for <em>a single</em> covariate in the model.</p>
<h2>1 The default <em>plot.gam()</em> method</h2>
<p><code>MyGAM1&lt;- with(MyData[MyData$Strata==1,], gam(Y ~ s(Covariate)))<br />
MyGAM0&lt;- with(MyData[MyData$Strata==0,], gam(Y ~ s(Covariate)))</code></p>
<p><code>par( mfcol=c(1,2))<br />
plot(MyGAM0)<br />
plot(MyGAM1)</code></p>
<p>This is the resulting plot:</p>
<div id="attachment_30" class="wp-caption alignnone" style="width: 346px"><img class="size-full wp-image-30" title="plot.gam" src="http://rforge.files.wordpress.com/2009/06/defaultplot.jpg?w=510" alt="Stratified Additive Regression Model"   /><p class="wp-caption-text">Stratified Additive Regression Model</p></div>
<h2>2 The fancy way</h2>
<p>1. Extract the values of the model response from the GAM object:</p>
<p><code>response1 &lt;- predict(MyGAM1, type="response", se.fit=T)<br />
response0 &lt;- predict(MyGAM0, type="response", se.fit=T)</code></p>
<p>2. Print the response values against the covariate (note: this works just with one covariate)</p>
<p><code>par(mfcol=c(1,1))</p>
<p>plot(0, type="n", bty="n", main="Fancy GAM plot", xlab="MyCovariate", ylab="MyResponse", lwd=3,ylim=c(0,60), xlim=c(0,200))<br />
legend("bottomright", bty="n", lwd=5, col=c("green","red"), legend=c("Strata = 0", "Strata = 1"))</p>
<p>lines(sm.spline(MyGAM1$model$Covariate , response1$fit) , lwd = 3 , col = "red")<br />
lines(sm.spline(MyGAM1$model$Covariate , response1$fit+1.96*response1$se) , lty = 3 , lwd = 2 , col = "red")<br />
lines(sm.spline(MyGAM1$model$Covariate , response1$fit-1.96*response1$se) , lty = 3 , lwd = 2 , col = "red")</p>
<p>lines(sm.spline(MyGAM0$model$Covariate , response0$fit) ,  lwd = 3 , col = "green")<br />
lines(sm.spline(MyGAM0$model$Covariate, response0$fit + 1.96 * response0$se) , lty = 3 , lwd = 2, col = "green")<br />
lines(sm.spline(MyGAM0$model$Covariate, response0$fit - 1.96 * response0$se) , lty = 3 , lwd = 2 , col = "green")</p>
<p>abline(h=gam.dm1$coefficients[1], lty=2, lwd=1, col="red")<br />
abline(h=gam.dm0$coefficients[1], lty=2, lwd=1, col="green")</code></p>
<div id="attachment_32" class="wp-caption alignleft" style="width: 346px"><img class="size-full wp-image-32" title="Fancy Plot" src="http://rforge.files.wordpress.com/2009/06/fancyplot.jpg?w=510" alt="Stratified Additive Regression Model on Response Scale"   /><p class="wp-caption-text">Stratified Additive Regression Model on Response Scale</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rforge.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rforge.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rforge.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rforge.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rforge.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rforge.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rforge.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rforge.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rforge.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rforge.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rforge.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rforge.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rforge.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rforge.wordpress.com/31/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rforge.org&amp;blog=7577563&amp;post=31&amp;subd=rforge&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rforge.org/2009/06/16/how2plot-nicer-gam-curves/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2a4cc3be2c3e1c3b1442535ef7a46f42?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rforge</media:title>
		</media:content>

		<media:content url="http://rforge.files.wordpress.com/2009/06/defaultplot.jpg" medium="image">
			<media:title type="html">plot.gam</media:title>
		</media:content>

		<media:content url="http://rforge.files.wordpress.com/2009/06/fancyplot.jpg" medium="image">
			<media:title type="html">Fancy Plot</media:title>
		</media:content>
	</item>
	</channel>
</rss>
