<?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/"
	>

<channel>
	<title>Kornels Welt &#187; R</title>
	<atom:link href="http://www.kornels-welt.de/blog/tag/r/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kornels-welt.de/blog</link>
	<description></description>
	<lastBuildDate>Thu, 15 Dec 2011 23:21:04 +0000</lastBuildDate>
	<language>de</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Small timer for R</title>
		<link>http://www.kornels-welt.de/blog/2008/12/09/small-timer-for-r/</link>
		<comments>http://www.kornels-welt.de/blog/2008/12/09/small-timer-for-r/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 20:46:45 +0000</pubDate>
		<dc:creator>Kornel</dc:creator>
				<category><![CDATA[Computer]]></category>
		<category><![CDATA[Programmieren]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Unsinn]]></category>

		<guid isPermaLink="false">http://www.kornels-welt.de/blog/?p=900</guid>
		<description><![CDATA[My coworker told me that pie charts never make sense. To prove him wrong I proudly present a fantastic R function that reminds you to spend not too much time on coffee drinking, rss feed reading or whatever else is stealing your time:
GeSHi Error: GeSHi could not find the language r (using path /www/htdocs/w006f92c/blog/wp-content/plugins/codecolorer/lib/geshi/) (code [...]]]></description>
			<content:encoded><![CDATA[<p>My coworker told me that pie charts never make sense. To prove him wrong I proudly present a fantastic <a href="http://www.r-project.org/">R</a> function that reminds you to spend not too much time on coffee drinking, rss feed reading or whatever else is stealing your time:</p>
<div class="codecolorer-container r default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><br /><strong>GeSHi Error:</strong> GeSHi could not find the language r (using path /www/htdocs/w006f92c/blog/wp-content/plugins/codecolorer/lib/geshi/) (code 2)<br /></div>
<p>So <tt>timer(<em>someNumber</em>)</tt> will result in the following graphical output and the red part will take approximate <em>someNumber</em> seconds to fill the whole pie:</p>
<p align="center"><img src="http://www.kornels-welt.de/blog/pictures/computer/animatedtimer.gif" alt="Animated Timer" /></p>
<p>You are still reading? Hmmm&#8230; then here also is the code that produced the animated gif above:</p>
<div class="codecolorer-container r default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><br /><strong>GeSHi Error:</strong> GeSHi could not find the language r (using path /www/htdocs/w006f92c/blog/wp-content/plugins/codecolorer/lib/geshi/) (code 2)<br /></div>
<p>Execute the code above in R. Then open a shell and produce an animated gif out of the one hundred png files with <a href="http://www.imagemagick.org/">ImageMagick</a>:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><br /><strong>GeSHi Error:</strong> GeSHi could not find the language r (using path /www/htdocs/w006f92c/blog/wp-content/plugins/codecolorer/lib/geshi/) (code 2)<br /></div>
<p>P.S.: My wife tells me that she likes pie charts because of the pie&#8230; <img src='http://www.kornels-welt.de/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.kornels-welt.de/blog/2008/12/09/small-timer-for-r/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Vortrag in Dortmund</title>
		<link>http://www.kornels-welt.de/blog/2008/08/18/vortrag-in-dortmund/</link>
		<comments>http://www.kornels-welt.de/blog/2008/08/18/vortrag-in-dortmund/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 19:30:59 +0000</pubDate>
		<dc:creator>Kornel</dc:creator>
				<category><![CDATA[Unterwegs]]></category>
		<category><![CDATA[Arbeiten]]></category>
		<category><![CDATA[Mathe]]></category>
		<category><![CDATA[Programme]]></category>
		<category><![CDATA[Programmieren]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Statistik]]></category>

		<guid isPermaLink="false">http://www.kornels-welt.de/blog/?p=761</guid>
		<description><![CDATA[Wir haben in der letzten Woche auf der

 in Dortmund einen Vortrag gehalten. Einen Vortrag an den man sich erinnern wird&#8230; &#8211; vielleicht nicht wegen dem Inhalt, sondern weil sich Bernds vorbereiteter Computer im Talk vor uns ausgeschaltet hat. Von unseren 15 Minuten Vortragszeit warteten wir 10 Minuten darauf, dass der Computer startete. Emule, Antivir, [...]]]></description>
			<content:encoded><![CDATA[<p>Wir haben in der letzten Woche auf der
<p align="center"><a href="http://www.statistik.uni-dortmund.de/useR-2008/"><img src="http://www.kornels-welt.de/blog/pictures/computer/useR.png" alt="useR" /></a></p>
<p> in Dortmund einen <a href="http://www.statistik.uni-dortmund.de/useR-2008/abstracts/Bischl+Rohmeyer_1.pdf">Vortrag</a> gehalten. Einen Vortrag an den man sich erinnern wird&#8230; &#8211; vielleicht nicht wegen dem Inhalt, sondern weil sich Bernds vorbereiteter Computer im Talk vor uns ausgeschaltet hat. Von unseren 15 Minuten Vortragszeit warteten wir 10 Minuten darauf, dass der Computer startete. Emule, Antivir, Windows Sicherheitscenter, diverse Mediacenter und andere Programme mussten sich erst starten und mit Informationen versorgen, bevor wir endlich mit Folien weitermachen konnten.</p>
<p>Trotz allem haben wir zu drei Leuten neuen, vielversprechenden Kontakt bekommen und inhaltlich war der Vortrag trotz allem gut. An uns ran kamen von der Präsentation sonst nur zwei Vorträge: In dem einen wurde nach 3 Minuten Einleitung einfach ein Präsentationsvideo abgespielt und im anderen Vortrag hatte der Vortragende sich so erkältet, dass er die Folien ohne Kommentar durchging.</p>
<p>Btw.: Wo kann man in Dortmund sinnvoll übernachten? Ich habe 70€/Nacht für ein Hotel mit dreckigen Handtüchern und ohne WLAN bezahlt! (Wenn ich es selber zahlen müsste, hätte ich sonst in der Uni übernachtet &#8211; Ich konnte nicht bei meinem Kollegen übernachten, da wegen Wasserschäden schon seine Freundin notdürftig bei ihm untergekommen war&#8230;) Das Hostel in München für 20€/Nacht war in jeder Hinsicht (nagut, es gab kein Frühstück) um Meilen angenehmer&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kornels-welt.de/blog/2008/08/18/vortrag-in-dortmund/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Projekt vorbei</title>
		<link>http://www.kornels-welt.de/blog/2008/07/14/projekt-vorbei/</link>
		<comments>http://www.kornels-welt.de/blog/2008/07/14/projekt-vorbei/#comments</comments>
		<pubDate>Mon, 14 Jul 2008 19:06:41 +0000</pubDate>
		<dc:creator>Kornel</dc:creator>
				<category><![CDATA[Computer]]></category>
		<category><![CDATA[Wissenschaft]]></category>
		<category><![CDATA[Arbeiten]]></category>
		<category><![CDATA[Mathe]]></category>
		<category><![CDATA[Programme]]></category>
		<category><![CDATA[Programmieren]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Statistik]]></category>

		<guid isPermaLink="false">http://www.kornels-welt.de/blog/?p=726</guid>
		<description><![CDATA[Das große Projekt bei uns am Institut ist nun vorbei.

Vorstellen werden wir das Framework des GUI-Teilprojektes unter dem Titel &#8220;Towards a Java Framework for Rapid Development of Graphical User Interfaces for Statistical Applications based on R&#8221; auf der UseR im August.
Jemand Interesse?

]]></description>
			<content:encoded><![CDATA[<p>Das <a href="http://dr-ibs.biostat.uni-hannover.de:8080/rjavaclient/deploy/toxicology/">große Projekt</a> bei uns am Institut ist nun vorbei.</p>
<p align="center"><a title="Unser GUI" href="#" onclick="window.open('/blog/wp-content/plugins/popup-images/popup.php?z=http://www.kornels-welt.de/blog/pictures/computer/eu/JDesktopPane.png&#038;width=1280&#038;height=750&#038;title=Unser%20GUI&#038;persistent=1','imagepopup','width=1280,height=750,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=no,screenx=150,screeny=150');return false" onmouseover="window.status='image popup: Unser GUI';return true" onmouseout="window.status='';return true"><img src="http://www.kornels-welt.de/blog/pictures/computer/eu/JDesktopPane-thumbnail.png" width="450" height="264" alt="Unser GUI" title="Unser GUI" /></a></p>
<p><a href="http://www.statistik.uni-dortmund.de/useR-2008/abstracts/Bischl+Rohmeyer_1.pdf">Vorstellen werden wir</a> das Framework des GUI-Teilprojektes unter dem Titel &#8220;<em>Towards a Java Framework for Rapid Development of Graphical User Interfaces for Statistical Applications based on R</em>&#8221; auf der <a href="http://www.statistik.uni-dortmund.de/useR-2008/">UseR</a> im August.</p>
<p>Jemand Interesse?</p>
<p align="center"><a title="Die Hilfe" href="#" onclick="window.open('/blog/wp-content/plugins/popup-images/popup.php?z=http://www.kornels-welt.de/blog/pictures/computer/eu/help.png&#038;width=1166&#038;height=694&#038;title=Die%20Hilfe&#038;persistent=1','imagepopup','width=1166,height=694,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=no,screenx=150,screeny=150');return false" onmouseover="window.status='image popup: Die Hilfe';return true" onmouseout="window.status='';return true"><img src="http://www.kornels-welt.de/blog/pictures/computer/eu/help-thumbnail.png" width="450" height="268" alt="Die Hilfe" title="Die Hilfe" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kornels-welt.de/blog/2008/07/14/projekt-vorbei/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kein Spaß zu Googlen</title>
		<link>http://www.kornels-welt.de/blog/2008/04/13/spas-beim-googlen/</link>
		<comments>http://www.kornels-welt.de/blog/2008/04/13/spas-beim-googlen/#comments</comments>
		<pubDate>Sun, 13 Apr 2008 10:37:00 +0000</pubDate>
		<dc:creator>Kornel</dc:creator>
				<category><![CDATA[Computer]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Sprache]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Web-Fund]]></category>

		<guid isPermaLink="false">http://www.kornels-welt.de/blog/?p=603</guid>
		<description><![CDATA[Es gibt Namen, die sind schwer zu googlen. Wer zum Beispiel die Sprache R mit .net benutzen möchte, hat es nicht einfach, Informationen zu finden. Hier mal Googles Ergebnisse zu: R und .net.
Viel Werbung und Suche könnte man sich sparen, wenn man andere Namen gewählt hätte&#8230;  
Btw.: Ob es eine weitere einfache Möglichkeit außer [...]]]></description>
			<content:encoded><![CDATA[<p>Es gibt Namen, die sind schwer zu googlen. Wer zum Beispiel die Sprache <a href="http://www.r-project.org/">R</a> mit <a href="http://de.wikipedia.org/wiki/.NET">.net</a> benutzen möchte, hat es nicht einfach, Informationen zu finden. Hier mal Googles Ergebnisse zu: <a href="http://www.google.com/search?q=R+.net">R und .net</a>.</p>
<p>Viel Werbung und Suche könnte man sich sparen, wenn man andere Namen gewählt hätte&#8230; <img src='http://www.kornels-welt.de/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Btw.: Ob es eine weitere einfache Möglichkeit außer über <a href="http://de.wikipedia.org/wiki/DCOM">DCOM</a> gibt, konnte ich bisher nicht herausfinden&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kornels-welt.de/blog/2008/04/13/spas-beim-googlen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random Correlation Matrices</title>
		<link>http://www.kornels-welt.de/blog/2008/02/19/random-correlation-matrices/</link>
		<comments>http://www.kornels-welt.de/blog/2008/02/19/random-correlation-matrices/#comments</comments>
		<pubDate>Tue, 19 Feb 2008 18:22:00 +0000</pubDate>
		<dc:creator>Kornel</dc:creator>
				<category><![CDATA[Mathe]]></category>
		<category><![CDATA[Programmieren]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Statistik]]></category>

		<guid isPermaLink="false">http://www.kornels-welt.de/blog/2008/02/19/random-correlation-matrices/</guid>
		<description><![CDATA[One of my colleagues has implemented a new method for evaluating the cumulative distribution function of a multivariate normal distribution and wanted to compare the speed of his method with that of randomized quasi-Monte Carlo methods. A few days ago, while we were going to lunch, he asked me how to generate random correlation matrices, [...]]]></description>
			<content:encoded><![CDATA[<P>One of my colleagues has implemented a new method for evaluating the cumulative distribution function of a multivariate normal distribution and wanted to compare the speed of his method with that of randomized quasi-Monte Carlo methods. A few days ago, while we were going to lunch, he asked me how to generate random correlation matrices, because the speed of his method strongly depends on the <a href="http://en.wikipedia.org/wiki/Correlation#Correlation_matrices">correlation matrix</a> and he wanted to have some sort of average.

<P>
<strong>But what is a random correlation matrix?</strong>

<P>
Let's first give a characterization of correlation matrices.

<P>
It is well known that for a matrix 
<SPAN CLASS="MATH"><IMG WIDTH="183" HEIGHT="35" ALIGN="MIDDLE" BORDER="0" SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img1.png" ALT="$C:=(c_{i,j})_{1\leq i,j\leq n}\in\ensuremath{\mathbb{R}}\xspace ^{n\times n}$"></SPAN> there exist (multivariate normal distributed) random variables <SPAN CLASS="MATH"><IMG
 WIDTH="38" HEIGHT="30" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img2.png"
 ALT="$X,Y$"></SPAN> with
<BR><P></P>
<DIV ALIGN="CENTER" CLASS="mathdisplay">

<IMG WIDTH="334" HEIGHT="57" BORDER="0" SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img3b.png" ALT="">
</DIV><BR CLEAR="ALL">
<P>if and only if</P>
<OL>
<LI>
<SPAN CLASS="MATH"><IMG WIDTH="97" HEIGHT="30" ALIGN="MIDDLE" BORDER="0" SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img4.png" ALT="$-1\leq c_{i,j}\leq 1$"></SPAN> for all <SPAN CLASS="MATH"><IMG WIDTH="113" HEIGHT="32" ALIGN="MIDDLE" BORDER="0" SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img5.png" ALT="$i,j\in\{0,\ldots,n\}$"></SPAN>,
</LI>
<LI><SPAN CLASS="MATH"><IMG
 WIDTH="54" HEIGHT="30" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img6.png"
 ALT="$c_{i,i}= 1$"></SPAN> for all <!-- MATH
 $i\in\{0,\ldots,n\}$
 -->
<SPAN CLASS="MATH"><IMG
 WIDTH="98" HEIGHT="32" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img7.png"
 ALT="$i\in\{0,\ldots,n\}$"></SPAN>,
</LI>
<LI><SPAN CLASS="MATH"><IMG
 WIDTH="17" HEIGHT="14" ALIGN="BOTTOM" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img8.png"
 ALT="$C$"></SPAN> is symmetric (therefore all eigenvalues <!-- MATH
 $\lambda_1,\ldots,\lambda_n$
 -->
<SPAN CLASS="MATH"><IMG
 WIDTH="74" HEIGHT="30" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img9.png"
 ALT="$\lambda_1,\ldots,\lambda_n$"></SPAN> of <SPAN CLASS="MATH"><IMG
 WIDTH="17" HEIGHT="14" ALIGN="BOTTOM" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img10.png"
 ALT="$C$"></SPAN> are real)
</LI>
<LI>and all eigenvalues of <SPAN CLASS="MATH"><IMG
 WIDTH="17" HEIGHT="14" ALIGN="BOTTOM" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img11.png"
 ALT="$C$"></SPAN> are greater or equal to zero.
</LI>
</OL>

<P>
But what is the right notion of randomness for these matrices?
For example let's look at the orthogonal matrices. In many numerical applications we need <a href="http://en.wikipedia.org/wiki/Orthogonal_matrix#Randomization">uniformly distributed random orthogonal matrices in terms of the Haar measure</A>.
<P>
Unfortunately in our case there is no clear, natural notion of randomness. <img src='http://www.kornels-welt.de/blog/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> 

<P>
<strong>Method 1 - Try and Error:</strong> We generate a matrix fulfilling no. 1, 2 and 3 of the characterization (these matrices are called pseudo correlation matrices) by generating independent pseudo-random numbers uniformly distributed between -1 and 1 for the entries <!-- MATH
 $c_{i,j}=c_{j,i}$
 -->
<SPAN CLASS="MATH"><IMG
 WIDTH="69" HEIGHT="30" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img12.png"
 ALT="$c_{i,j}=c_{j,i}$"></SPAN>, <!-- MATH
 $1\leq i<j\leq n$
 -->
<SPAN CLASS="MATH"><IMG
 WIDTH="99" HEIGHT="29" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img13.png"
 ALT="$1\leq i&lt;j\leq n$"></SPAN>.

<P>
If this random symmetric matrix is positive semidefinite (i.e. all eigenvalues of <SPAN CLASS="MATH"><IMG
 WIDTH="17" HEIGHT="14" ALIGN="BOTTOM" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img14.png"
 ALT="$C$"></SPAN> are greater or equal to zero), we have the desired result. Otherwise we try again. Here is the corresponding R code:

<P>
<PRE>
random.pseudo.correlation.matrix &lt;-
function(n) {
  a &lt;- diag(n)
  for(i in 1:(n-1)) {
    for(j in (i+1):n) {
      a[i,j] &lt;- a[j,i] &lt;- runif(1,-1,1)
    }
  }
  return(a)
}

random.correlation.matrix.try.and.error &lt;-
function(n) {
  repeat {
    a &lt;- random.pseudo.correlation.matrix(n)
    if (min(eigen(a)$values)&gt;=0) return(a)
  }
}
</PRE>

<P>
This approach is only reasonable for very small dimensions (try it with <SPAN CLASS="MATH"><IMG
 WIDTH="73" HEIGHT="30" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img15.png"
 ALT="$n=6,7,8$"></SPAN>).

<P>
<strong>Method 2 - Lift the Diagonal:</strong>

<P>
We denote by <SPAN CLASS="MATH"><IMG
 WIDTH="13" HEIGHT="14" ALIGN="BOTTOM" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img16.png"
 ALT="$I$"></SPAN> the identity matrix. If <SPAN CLASS="MATH"><IMG
 WIDTH="17" HEIGHT="14" ALIGN="BOTTOM" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img17.png"
 ALT="$C$"></SPAN> has the eigenvalues <!-- MATH
 $\lambda_1\leq\ldots\leq\lambda_n$
 -->
<SPAN CLASS="MATH"><IMG
 WIDTH="100" HEIGHT="30" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img18.png"
 ALT="$\lambda_1\leq\ldots\leq\lambda_n$"></SPAN> then <SPAN CLASS="MATH"><IMG
 WIDTH="77" HEIGHT="32" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img19.png"
 ALT="$(C+a\cdot I)$"></SPAN> 
has the eigenvalues 
<SPAN CLASS="MATH"><IMG
 WIDTH="156" HEIGHT="30" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img20.png"
 ALT="$\lambda_1+a\leq\ldots\leq\lambda_n+a$"></SPAN> since <SPAN CLASS="MATH"><IMG
 WIDTH="14" HEIGHT="13" ALIGN="BOTTOM" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img21.png"
 ALT="$x$"></SPAN>
 is a solution of 
<SPAN CLASS="MATH"><IMG
 WIDTH="129" HEIGHT="32" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img22.png"
 ALT="$\det(C-x\cdot I)=0$"></SPAN> if
and only if <SPAN CLASS="MATH"><IMG
 WIDTH="41" HEIGHT="30" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img23.png"
 ALT="$x+a$"></SPAN> 
is a solution of 
<SPAN CLASS="MATH"><IMG
 WIDTH="333" HEIGHT="32" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img24.png"
 ALT="$\det(C+a\cdot I-x\cdot I)=\det(C-(x-a)\cdot I)=0$"></SPAN>.

<P>
So we start again with a pseudo correlation matrix 
<SPAN CLASS="MATH"><IMG
 WIDTH="17" HEIGHT="14" ALIGN="BOTTOM" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img25.png"
 ALT="$C$"></SPAN>, 
but instead of retrying if <SPAN CLASS="MATH"><IMG
 WIDTH="17" HEIGHT="14" ALIGN="BOTTOM" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img26.png"
 ALT="$C$"></SPAN> has negative eigen values, we lift the diagonal by <SPAN CLASS="MATH"><IMG
 WIDTH="21" HEIGHT="30" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img27.png"
 ALT="$\lambda_1$"></SPAN> and obtain <!-- MATH
 $C+\lambda_1\cdot I$
 -->
<SPAN CLASS="MATH"><IMG
 WIDTH="73" HEIGHT="30" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img28.png"
 ALT="$C+\lambda_1\cdot I$"></SPAN>, which is always positive semidefinite. After dividing by <SPAN CLASS="MATH"><IMG
 WIDTH="48" HEIGHT="30" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img29.png"
 ALT="$1+\lambda_1$"></SPAN> we have a correlation matrix which is ``some kind of random''. <img src='http://www.kornels-welt.de/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> 

<P>
Unfortunately the diagonal is accentuated and the smallest eigen value is always zero. The second problem we could avoid by adding <SPAN CLASS="MATH"><IMG
 WIDTH="47" HEIGHT="30" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img30.png"
 ALT="$\lambda_1+b$"></SPAN> where <SPAN CLASS="MATH"><IMG
 WIDTH="11" HEIGHT="14" ALIGN="BOTTOM" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img31.png"
 ALT="$b$"></SPAN> is some random number, but the first remains.

<P>
<PRE>
make.positive.semi.definite &lt;-
function(a, offset=0) {
  (a + (diag(dim(a)[1]) * (abs(min(eigen(a)$values))+offset))) /
    (1+(abs(min(eigen(a)$values))+offset))
}
</PRE>

<P>
<PRE>
random.correlation.matrix.lift.diagonal &lt;-
function(n, offset=0) {
  a &lt;- random.pseudo.correlation.matrix(n)
  make.positive.semi.definite(offset)
}
</PRE>

<P>
<strong>Method 3 - Gramian matrix - my favorite:</strong> Holmes [<A
 HREF="#holmes"><SPAN  CLASS="textit">Holmes</SPAN>1991</A>] discusses two principal methods for generating random correlation matrices.
One of them is to generate <SPAN CLASS="MATH"><IMG
 WIDTH="14" HEIGHT="13" ALIGN="BOTTOM" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img32.png"
 ALT="$n$"></SPAN> independent pseudo-random vectors <!-- MATH
 $t_1,\ldots,t_n$
 -->
<SPAN CLASS="MATH"><IMG
 WIDTH="67" HEIGHT="30" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img33.png"
 ALT="$t_1,\ldots,t_n$"></SPAN> distributed uniformly on the unit sphere <SPAN CLASS="MATH"><IMG
 WIDTH="40" HEIGHT="16" ALIGN="BOTTOM" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img34.png"
 ALT="$S^{n-1}$"></SPAN> in <!-- MATH
 $\ensuremath{\mathbb{R}}\xspace ^n$
 -->
<SPAN CLASS="MATH"><IMG
 WIDTH="25" HEIGHT="14" ALIGN="BOTTOM" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img35.png"
 ALT="$\ensuremath{\mathbb{R}}\xspace ^n$"></SPAN> and to use the <a href="http://en.wikipedia.org/wiki/Gramian_matrix">Gram matrix</a> <SPAN CLASS="MATH"><IMG
 WIDTH="33" HEIGHT="19" ALIGN="BOTTOM" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img36.png"
 ALT="$T^tT$"></SPAN>, where <!-- MATH
 $T:=(t_1,\ldots,t_n)$
 -->
<SPAN CLASS="MATH"><IMG
 WIDTH="117" HEIGHT="32" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img37.png"
 ALT="$T:=(t_1,\ldots,t_n)$"></SPAN> has <SPAN CLASS="MATH"><IMG
 WIDTH="16" HEIGHT="30" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img38.png"
 ALT="$t_i$"></SPAN> as <SPAN CLASS="MATH"><IMG
 WIDTH="10" HEIGHT="17" ALIGN="BOTTOM" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img39.png"
 ALT="$i$"></SPAN>-th column and <SPAN CLASS="MATH"><IMG
 WIDTH="22" HEIGHT="19" ALIGN="BOTTOM" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img40.png"
 ALT="$T^t$"></SPAN> is the transpose of <SPAN CLASS="MATH"><IMG
 WIDTH="16" HEIGHT="14" ALIGN="BOTTOM" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img41.png"
 ALT="$T$"></SPAN>.

<P>
To create the <SPAN CLASS="MATH"><IMG
 WIDTH="16" HEIGHT="30" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img42.png"
 ALT="$t_i$"></SPAN> in R, we load the package <a href="http://cran.r-project.org/web/packages/mvtnorm/index.html">mvtnorm</a>, generate <!-- MATH
 $\tau_i\sim\mathcal{N}(0,I)$
 -->
<SPAN CLASS="MATH"><IMG
 WIDTH="89" HEIGHT="32" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img43.png"
 ALT="$\tau_i\sim\mathcal{N}(0,I)$"></SPAN> and set <!-- MATH
 $t_i:=\tau_i/||\tau_i||$
 -->
<SPAN CLASS="MATH"><IMG
 WIDTH="91" HEIGHT="32" ALIGN="MIDDLE" BORDER="0"
 SRC="http://www.kornels-welt.de/blog/pictures/mathe/correlation/img44.png"
 ALT="$t_i:=\tau_i/\vert\vert\tau_i\vert\vert$"></SPAN>:

<P>
<PRE>
random.correlation.matrix.sphere &lt;-
function(n) {
  require("mvtnorm")
  t &lt;- rmvnorm(n,rep(0,n),diag(n))
  for (i in 1:n) {
    t[i,] &lt;- t[i,]/sqrt(t(t[i,])%*%t[i,])
  }
  t%*%t(t)
}
</PRE>

<P>
<strong>Conclusion:</strong> There are futher methods (like e.g. to generate a random spectrum and then construct the correlation matrix), which are not as easy to implement as the ones described above. But just as the three given methods, they all are unsatisfactory in some way because we don't really know how random correlation matrices should be distributed.

<P>
For my colleague an average of calculation time does only make sense when he knows which kind of correlation matrices occurs in the applications. He decided to describe and compare the different cases individually.

<P>
But does it perhaps make sense to use random correlation matrices as test cases or are the special cases more important? For example, random correlation matrices generated with method 1 and 3 are only singular with probability zero.

<P>
Any critique, comments, suggestions or questions are welcome!

<P>
<b>And for the next time:</b> Given a correlation matrix C. How do we generate tuples of pseudo-random numbers following a given multivariate distribution with correlation matrix C?

<P>
 
<b>Bibliography</b>
<DL COMPACT><DD><P></P><DT><A NAME="holmes"><SPAN  CLASS="textit">Holmes</SPAN>1991</A>
<DD>
Holmes, R.&nbsp;B. 1991.
<BR><EM>On random correlation matrices.</EM>
<BR>Siam J. Matrix Anal. Appl., Vol. 12 No. 2: 239-272.
</DL>]]></content:encoded>
			<wfw:commentRss>http://www.kornels-welt.de/blog/2008/02/19/random-correlation-matrices/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Solve double integrals with R numerically</title>
		<link>http://www.kornels-welt.de/blog/2008/02/06/solve-integrals-with-r-numerally/</link>
		<comments>http://www.kornels-welt.de/blog/2008/02/06/solve-integrals-with-r-numerally/#comments</comments>
		<pubDate>Wed, 06 Feb 2008 21:04:35 +0000</pubDate>
		<dc:creator>Kornel</dc:creator>
				<category><![CDATA[Computer]]></category>
		<category><![CDATA[Mathe]]></category>
		<category><![CDATA[Programmieren]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://www.kornels-welt.de/blog/2008/02/06/solve-integrals-with-r-numerally/</guid>
		<description><![CDATA[The goal of this post is to show how to evaluate in R terms like

with

The R function &#8220;integrate&#8221;:
We use the R function integrate and cite some arguments from its help:
integrate(f, lower, upper, &#8230;)
       f: an R function taking a numeric first argument and       [...]]]></description>
			<content:encoded><![CDATA[<p>The goal of this post is to show how to evaluate in <a href="http://www.r-project.org/">R</a> terms like<br />
<DIV ALIGN="CENTER" CLASS="mathdisplay"><img WIDTH="140" HEIGHT="56" BORDER="0" SRC="http://www.kornels-welt.de/blog/pictures/r_integrals/img1.png" ALT="\begin{displaymath }\int_a^b\int_c^d f(x,y)\; dx\; dy \end{displaymath}"></DIV><br />
with<br />
<DIV ALIGN="CENTER" CLASS="mathdisplay"><IMG WIDTH="252" HEIGHT="37" BORDER="0" SRC="http://www.kornels-welt.de/blog/pictures/r_integrals/img2.png" ALT="\begin{displaymath}a,b,c,d\in[\infty,\infty]=:\overline{\ensuremath{\mathbb{R}}\...mathbb{R}}\xspace ^2\rightarrow\ensuremath{\mathbb{R}}\xspace .\end{displaymath}"></DIV><br />
<b>The R function &#8220;integrate&#8221;:</b><br />
We use the R function <TT>integrate</TT> and cite some arguments from its help:<br />
<PRE>integrate(f, lower, upper, &#8230;)</p>
<p>       f: an R function taking a numeric first argument and <br />          returning a numeric vector of the same length.  <br />          Returning a non-finite element will generate an error.</p>
<p>lower, upper: the limits of integration.  Can be infinite.</p>
<p>     &#8230;: additional arguments to be passed to &#8216;f&#8217;.</PRE><br />
There are further arguments, but see ?integrate for the complete and correct signature of this function.</p>
<p><b>Examples:</b><br />
<DIV ALIGN="CENTER" CLASS="mathdisplay"><IMG WIDTH="92" HEIGHT="55" BORDER="0" SRC="http://www.kornels-welt.de/blog/pictures/r_integrals/img3.png" ALT="\begin{displaymath}\int_0^3x^2\;dx = 9\end{displaymath}"></DIV><br />
<PRE>&gt; integrate(function(x){x^2},0,3)<br />9 with absolute error &lt; 1e-13</PRE><br />
<DIV ALIGN="CENTER" CLASS="mathdisplay"><IMG WIDTH="124" HEIGHT="55" BORDER="0" SRC="http://www.kornels-welt.de/blog/pictures/r_integrals/img4.png" ALT="\begin{displaymath}\int_0^{2\pi}\sin(x)\;dx = 0\end{displaymath}"></DIV><br />
<PRE>&gt; integrate(sin,0,2*pi)<br />2.032977e-16 with absolute error &lt; 4.4e-14</PRE><br />
<DIV ALIGN="CENTER" CLASS="mathdisplay"><br />
<IMG WIDTH="102" HEIGHT="53" BORDER="0" SRC="http://www.kornels-welt.de/blog/pictures/r_integrals/img5.png" ALT="\begin{displaymath}\int_1^{\infty}\frac{1}{x^2}\;dx = 1\end{displaymath}"></DIV><br />
<PRE>&gt; integrate(function(x){1/x^2},1,Inf)<br />1 with absolute error &lt; 1.1e-14</PRE><br />
<b>Double integral:</b><br />
To evaluate<br />
<DIV ALIGN="CENTER" CLASS="mathdisplay"><IMG WIDTH="140" HEIGHT="56" BORDER="0" SRC="http://www.kornels-welt.de/blog/pictures/r_integrals/img6.png" ALT="\begin{displaymath}\int_a^b\int_c^d f(x,y)\; dx\; dy\end{displaymath}"></DIV><br />
we define:<br />
<DIV ALIGN="CENTER" CLASS="mathdisplay"><IMG WIDTH="153" HEIGHT="56" BORDER="0" SRC="http://www.kornels-welt.de/blog/pictures/r_integrals/img7.png" ALT="\begin{displaymath}g:\;y \mapsto \int_c^d f(x,y)\; dx\end{displaymath}"></DIV><br />
<PRE>g &lt;- function(y) {integrate(function(x) {f(x,y)},c,d)$value}</PRE><br />
and then integrate over <em>g</em>:</p>
<p><DIV ALIGN="CENTER" CLASS="mathdisplay"><IMG WIDTH="236" HEIGHT="56" BORDER="0" SRC="http://www.kornels-welt.de/blog/pictures/r_integrals/img8.png" ALT="\begin{displaymath}\int_a^b\int_c^d f(x,y)\; dx\; dy=\int_a^b g(y)\; dy\end{displaymath}"></DIV><PRE>integrate(Vectorize(g),a,b)$value</PRE><br />
<b>Example:</b><br />
<IMG WIDTH="262" HEIGHT="34" ALIGN="MIDDLE" BORDER="0" SRC="http://www.kornels-welt.de/blog/pictures/r_integrals/img9.png" ALT="$f(x,y)=x^2\cdot y,\;a=c=0,\;b=d=1$">,<br />
<DIV ALIGN="CENTER" CLASS="mathdisplay"><IMG BORDER="0" SRC="http://www.kornels-welt.de/blog/pictures/r_integrals/img10.png" ALT="\begin{displaymath}\int_0^1\int_0^1 x^2\cdot y\; dx\; dy=...\end{displaymath}"></DIV><br />
<strong>R-Code:</strong><PRE>&gt; f &lt;- function(x,y) { x^2*y }<br />&gt; g &lt;- (function(y) {integrate(function(x) <br />{f(x,y)},0,1)$value})<br />&gt; integrate(Vectorize(g),0,1)<br />0.1666667 with absolute error &lt; 1.9e-15</PRE><br />
<b>Warning</b><br />
Not all functions are suited: &#8220;<em>Like all numerical integration routines, these evaluate the function on a finite set of points.  If the function is approximately constant (in particular, zero) over nearly all its range it is possible that the result and error estimate may be seriously wrong</em>&#8221; (from the integrate help &#8211; see ?integrate).</p>
<p>To demonstrate this, we integrate over the indicator function of ]1,3[:<DIV ALIGN="CENTER"><IMG SRC="http://www.kornels-welt.de/blog/pictures/r_integrals/testfunktion.png" alt="plot"></DIV><br />
<PRE>&gt; f&lt;-function(x){ifelse(x&lt;1,0,ifelse(x&lt;3,1,0))}<br />&gt; integrate(f,-20,20)<br />2 with absolute error &lt; 2.2e-15<br />&gt; integrate(f,-30,30)<br />0 with absolute error &lt; 0</PRE></p>
<p>On the other hand the last integral<br />
<DIV ALIGN="CENTER" CLASS="mathdisplay"><IMG WIDTH="455" HEIGHT="55" BORDER="0" SRC="http://www.kornels-welt.de/blog/pictures/r_integrals/img11.png" ALT="\begin{displaymath}\int_0^\infty\left(\int_{-\infty}^\infty\binom{m}{j}[\Phi(b...\d t\right)f_\nu(s)\d s\end{displaymath}"></DIV><br />
we were interested in was no problem to evaluate&#8230; <img src='http://www.kornels-welt.de/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Since LaTeX2HTML2Wordpress is a stressful conversion, I defer the theoretical background indefinitely.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kornels-welt.de/blog/2008/02/06/solve-integrals-with-r-numerally/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

