<?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>R-bloggers &#187; Observations</title>
	<atom:link href="http://www.r-bloggers.com/tag/observations/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.r-bloggers.com</link>
	<description>R news and tutorials contributed by (300) R bloggers</description>
	<lastBuildDate>Tue, 07 Feb 2012 10:27:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Quick Review of Matrix Algebra in R</title>
		<link>http://www.r-bloggers.com/quick-review-of-matrix-algebra-in-r/</link>
		<comments>http://www.r-bloggers.com/quick-review-of-matrix-algebra-in-r/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 03:29:09 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[R bloggers]]></category>
		<category><![CDATA[Observations]]></category>
		<category><![CDATA[statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3707</guid>
		<description><![CDATA[Lately, I&#8217;ve been running a series of fMRI experiments on visual perception. In the interests of understanding the underlying properties of the images I&#8217;m using as stimuli, I&#8217;ve been trying to learn more about the matrix transformations commonly used for image compression and image manipulation. Thankfully, R provides simple-to-use implementations for all of the matrix [...]]]></description>
			<content:encoded><![CDATA[<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;">
(This article was first published on  <strong><a href="http://www.johnmyleswhite.com/notebook/2009/12/16/quick-review-of-matrix-algebra-in-r/"> John Myles White: Die Sudelbücher » Statistics</a></strong>, and kindly contributed to <a href="http://www.r-bloggers.com/">R-bloggers)</a>      
</div></p>
<p>Lately, I&#8217;ve been running a series of fMRI experiments on visual perception. In the interests of understanding the underlying properties of the images I&#8217;m using as stimuli, I&#8217;ve been trying to learn more about the matrix transformations commonly used for image compression and image manipulation. Thankfully, R provides simple-to-use implementations for all of the matrix operations I wanted to play around with, so it&#8217;s been quite easy to get started. For the next few posts, I thought that I&#8217;d review the standard matrix techniques for image compression and editing, giving full examples of their implementation in R and demonstrations of their real-world value.</p>
<p>Before I start, I should make sure that you&#8217;re familiar with basic matrix operations in R. I imagine almost every R user knows a little bit about matrix algebra and probably knows the basics of using R to perform matrix algebra, but here&#8217;s a quick review to make sure I don&#8217;t leave anyone in the dark:</p>
<h3>Building Matrices</h3>
<p>You can build a matrix in R using the <code>matrix</code> function:</p>

<div class="wp_codebox"><table><tr id="p3707145"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p3707code145"><pre class="c" style="font-family:monospace;">m <span style="color: #339933;">&lt;-</span> matrix<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> nrow <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> byrow <span style="color: #339933;">=</span> TRUE<span style="color: #009900;">&#41;</span>
&nbsp;
m
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    1    0</span>
<span style="color: #339933;"># [2,]    0    1</span></pre></td></tr></table></div>

<p>You can test whether an item you&#8217;ve been given is a matrix using <code>is.matrix</code> and you can convert appropriate objects to matrices using <code>as.matrix</code>:</p>

<div class="wp_codebox"><table><tr id="p3707146"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p3707code146"><pre class="c" style="font-family:monospace;">m <span style="color: #339933;">&lt;-</span> matrix<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> nrow <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> byrow <span style="color: #339933;">=</span> TRUE<span style="color: #009900;">&#41;</span>
&nbsp;
is.<span style="color: #202020;">matrix</span><span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># TRUE</span>
&nbsp;
as.<span style="color: #202020;">matrix</span><span style="color: #009900;">&#40;</span>data.<span style="color: #202020;">frame</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">=</span> c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> y <span style="color: #339933;">=</span> c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;">#      x y</span>
<span style="color: #339933;"># [1,] 1 0</span>
<span style="color: #339933;"># [2,] 0 1</span></pre></td></tr></table></div>

<h3>Diagonal Matrices</h3>
<p>The matrix I&#8217;ve been building in the examples above is a diagonal matrix, so you could also construct it using <code>diag</code>:</p>

<div class="wp_codebox"><table><tr id="p3707147"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p3707code147"><pre class="c" style="font-family:monospace;">m <span style="color: #339933;">&lt;-</span> diag<span style="color: #009900;">&#40;</span>nrow <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span>
&nbsp;
m
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    1    0</span>
<span style="color: #339933;"># [2,]    0    1</span></pre></td></tr></table></div>

<p>In general, you can generate the n by n identity matrix as:</p>

<div class="wp_codebox"><table><tr id="p3707148"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p3707code148"><pre class="c" style="font-family:monospace;">n <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">10</span>
&nbsp;
diag<span style="color: #009900;">&#40;</span>nrow <span style="color: #339933;">=</span> n<span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> n<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>I&#8217;ll be exploiting a more interesting use of <code>diag</code> in my next post, so let&#8217;s see how you can build matrices other than the identity with <code>diag</code> by specifying a vector of entries along the diagonal:</p>

<div class="wp_codebox"><table><tr id="p3707149"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p3707code149"><pre class="c" style="font-family:monospace;">m <span style="color: #339933;">&lt;-</span> diag<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> nrow <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span>
&nbsp;
m
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    2    0</span>
<span style="color: #339933;"># [2,]    0    1</span></pre></td></tr></table></div>

<p>This form of <code>diag</code> turns out to be extremely useful, as you&#8217;ll see once I cover the SVD&#8217;s syntax in R.</p>
<h3>Matrix Algebra: Addition, Scalar Multiplication, Matrix Multiplication</h3>
<p>The three core operations that can be performed on matrices are addition, scalar multiplication and matrix multiplication. These are easy to work with in R:</p>

<div class="wp_codebox"><table><tr id="p3707150"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p3707code150"><pre class="c" style="font-family:monospace;">m <span style="color: #339933;">&lt;-</span> matrix<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> nrow <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> byrow <span style="color: #339933;">=</span> TRUE<span style="color: #009900;">&#41;</span>
&nbsp;
m <span style="color: #339933;">+</span> m
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    0    4</span>
<span style="color: #339933;"># [2,]    2    0</span>
&nbsp;
<span style="color: #0000dd;">2</span> <span style="color: #339933;">*</span> m
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    0    4</span>
<span style="color: #339933;"># [2,]    2    0</span>
&nbsp;
m <span style="color: #339933;">%*%</span> m
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    2    0</span>
<span style="color: #339933;"># [2,]    0    2</span></pre></td></tr></table></div>

<p>Be careful with the <code>*</code> operator: it does not perform matrix multiplication, but rather an entry-wise multiplication:</p>

<div class="wp_codebox"><table><tr id="p3707151"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p3707code151"><pre class="c" style="font-family:monospace;">m <span style="color: #339933;">*</span> m
&nbsp;
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    0    4</span>
<span style="color: #339933;"># [2,]    1    0</span></pre></td></tr></table></div>

<h3>Matrix Transposes and Inverses</h3>
<p>The next few matrix operations are a little more complex: transposition and inversion. Transposition is the easier of the two. To get the transpose of a matrix, you simply call the <code>t</code> function:</p>

<div class="wp_codebox"><table><tr id="p3707152"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p3707code152"><pre class="c" style="font-family:monospace;">t<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    0    1</span>
<span style="color: #339933;"># [2,]    2    0</span></pre></td></tr></table></div>

<p>In contrast, inversion is a little more complex, partly because the function you&#8217;d want to use has a non-obvious name: <code>solve</code>.</p>

<div class="wp_codebox"><table><tr id="p3707153"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p3707code153"><pre class="c" style="font-family:monospace;">solve<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]  0.0    1</span>
<span style="color: #339933;"># [2,]  0.5    0</span></pre></td></tr></table></div>

<p>The reason that <code>solve</code> is called <code>solve</code> is that it&#8217;s a general purpose function you can use to solve matrix equations without wasting time computing the full inverse, which is often inefficient. If you want to know more about the computational efficiency issues, you should look into the ideas behind the even faster variant, <code>qr.solve</code>.</p>
<p>Now, you probably know this already, but the definition of a matrix&#8217;s inverse is that the product of the matrix and its inverse is the identity matrix, if the inverse exists. I always find this a good way to make sure that I&#8217;m correctly computing the inverse of a matrix:</p>

<div class="wp_codebox"><table><tr id="p3707154"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p3707code154"><pre class="c" style="font-family:monospace;">solve<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span> <span style="color: #339933;">%*%</span> m <span style="color: #339933;">==</span> diag<span style="color: #009900;">&#40;</span>nrow <span style="color: #339933;">=</span> nrow<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> ncol<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,] TRUE TRUE</span>
<span style="color: #339933;"># [2,] TRUE TRUE</span></pre></td></tr></table></div>

<h3>Further Matrix Operations</h3>
<p>The <code>car</code> package defines an <code>inv</code> function, which is simply a new name for <code>solve</code>:</p>

<div class="wp_codebox"><table><tr id="p3707155"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p3707code155"><pre class="c" style="font-family:monospace;">library<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'car'</span><span style="color: #009900;">&#41;</span>
inv
&nbsp;
<span style="color: #339933;"># function (x) </span>
<span style="color: #339933;"># solve(x)</span>
<span style="color: #339933;"># &lt;environment: namespace:car&gt;</span></pre></td></tr></table></div>

<p>I think this is pretty clever, though I&#8217;m loathe to import a package just for this one snippet.</p>
<p>More interestingly, the <code>MASS</code> package defines a <code>ginv</code> function, which gives the matrix pseudoinverse, a generalization of matrix inversion that works for all matrices:</p>

<div class="wp_codebox"><table><tr id="p3707156"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p3707code156"><pre class="c" style="font-family:monospace;">library<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'MASS'</span><span style="color: #009900;">&#41;</span>
&nbsp;
m <span style="color: #339933;">&lt;-</span> matrix<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> nrow <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span>
&nbsp;
solve<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># Error in solve.default(m) : </span>
<span style="color: #339933;">#  Lapack routine dgesv: system is exactly singular</span>
&nbsp;
ginv<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]  0.1  0.1</span>
<span style="color: #339933;"># [2,]  0.2  0.2</span></pre></td></tr></table></div>

<p>In practice, you can usually get around using the pseudoinverse, but it&#8217;s nice to know that it&#8217;s at hand all the time. </p>
<h3>Eigenvalues and Eigenvectors</h3>
<p>Eigenvectors are surely the bane of every starting student of linear algebra, though their considerable power to simplify problems makes them the darling of every applied mathematician. Thankfully, R makes it easy to get these for every matrix:</p>

<div class="wp_codebox"><table><tr id="p3707157"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p3707code157"><pre class="c" style="font-family:monospace;">m <span style="color: #339933;">&lt;-</span> diag<span style="color: #009900;">&#40;</span>nrow <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span>
&nbsp;
eigen<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># $values</span>
<span style="color: #339933;"># [1] 1 1</span>
<span style="color: #339933;">#</span>
<span style="color: #339933;"># $vectors</span>
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    0   -1</span>
<span style="color: #339933;"># [2,]    1    0</span></pre></td></tr></table></div>

<h3>Matrix Metadata</h3>
<p>Last, but not least, you can get metadata about the shape of a matrix using <code>dim</code>, <code>nrow</code> and <code>ncol</code>:</p>

<div class="wp_codebox"><table><tr id="p3707158"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p3707code158"><pre class="c" style="font-family:monospace;">m <span style="color: #339933;">&lt;-</span> diag<span style="color: #009900;">&#40;</span>nrow <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span>
&nbsp;
dim<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] 2 2</span>
&nbsp;
nrow<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] 2</span>
&nbsp;
ncol<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] 2</span></pre></td></tr></table></div>

<p>Hopefully those operations were already familiar to any readers out there, as I doubt that they&#8217;ll be clear from this short explanation without prior knowledge. I just felt compelled to review them before using any of them in my next set of posts. Tomorrow, I&#8217;ll start going through more interesting matrix algorithms in R, beginning with the SVD.</p>

<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;">
<div style="text-align: center;">To <strong>leave a comment</strong> for the author, please follow the link and comment on his blog: <strong><a href="http://www.johnmyleswhite.com/notebook/2009/12/16/quick-review-of-matrix-algebra-in-r/"> John Myles White: Die Sudelbücher » Statistics</a></strong>.</div>
<hr />
<a href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: visualization (<a title="ggplot and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" href="http://www.r-bloggers.com/?s=time+series">time series</a>,<a title="ecdf" href="http://www.r-bloggers.com/?s=ecdf">ecdf</a>, <a title="finance trading" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
</div></p>]]></content:encoded>
			<wfw:commentRss>http://www.r-bloggers.com/quick-review-of-matrix-algebra-in-r/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

