<?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>TheSpicyChicken &#187; math</title>
	<atom:link href="http://thespicychicken.com/tag/math/feed/" rel="self" type="application/rss+xml" />
	<link>http://thespicychicken.com</link>
	<description>n'th generation research</description>
	<lastBuildDate>Sun, 27 Nov 2011 19:17:19 +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>R Tip: Fitting Sigmoidal Data</title>
		<link>http://thespicychicken.com/2010/02/17/r-tip-fitting-sigmoidal-data/</link>
		<comments>http://thespicychicken.com/2010/02/17/r-tip-fitting-sigmoidal-data/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 07:12:54 +0000</pubDate>
		<dc:creator>Spicy</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Science]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[sigmoid]]></category>

		<guid isPermaLink="false">http://thespicychicken.com/?p=175</guid>
		<description><![CDATA[Using R to fit a sigmoid function to experimental data.]]></description>
			<content:encoded><![CDATA[<p>
  Sigmoid functions are our friends and sometimes you have data which you would like to fit with a sigmoid function. We can use R to find such a fit.  First let us look at a sigmoid function.
</p>
<div class="code">
  y = 1 / (1 + exp( -a*x + b) )
</div>
<p>
  Now let&#8217;s say you are given a vector x and y, say:
</p>
<div class="code">
  x = c(0.00,0.02,0.04,0.06,0.08,0.10,0.12,0.14,0.16,0.18,0.20,0.24,0.26,<br/><br />
0.28,0.30,0.34,0.40,0.42,0.48,0.54,0.56,0.64,1.00)<br/><br />
  y = c(0.409742,0.319277,0.530120,0.377778,0.357143,0.608696,0.315789,<br/><br />
0.692308,0.642857,0.636364,0.750000,0.000000,0.833333,1.000000,<br/><br />
0.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,<br/><br />
1.000000,1.000000)
</div>
<p>
  In this case the y values here represent probabilities and one thing you’ll notice is that we have probs of 1 and 0. Both of which are bad. So we apply a little “laplace smoothing” to them:
</p>
<div class="code">
  y[y==0] = 0.001<br/><br />
  y[y==1] = 0.999
</div>
<p>
Now let&#8217;s look at what the data looks like.
</p>
<div class="code">
plot(x, y)
</div>
<p><a href="http://thespicychicken.com/wp-content/uploads/2010/02/RTip_Sigmoid_Fig1.png"><img src="http://thespicychicken.com/wp-content/uploads/2010/02/RTip_Sigmoid_Fig1-300x279.png" alt="" title="RTip_Sigmoid_Fig1" width="300" height="279" class="alignnone size-medium wp-image-186" /></a></p>
<p>
Well, it may be sigmoidal, maybe not. For now let’s assume we think it is. Which we do for the most part.
</p>
<p>
Okay, now let’s solve for a line in our sigmoid function:
</p>
<div class="code">
y = 1 / (1 + exp( a*x + b) )<br/><br />
1 + exp( a*x + b) = 1/ y<br/><br />
a*x + b = log ( (1/ y) &#8211; 1 )<br/>
</div>
<p>
Now the left hand side of the equation is a line and the right hand side is some logarithm of the y data. We can plot x versus this right hand side:
</p>
<div class="code">
new_y = log( 1 / y &#8211; 1 )<br/><br />
plot(x, new_y)
</div>
<p><a href="http://thespicychicken.com/wp-content/uploads/2010/02/RTip_Sigmoid_Fig2.png"><img src="http://thespicychicken.com/wp-content/uploads/2010/02/RTip_Sigmoid_Fig2-300x272.png" alt="" title="RTip_Sigmoid_Fig2" width="300" height="272" class="alignnone size-medium wp-image-187" /></a></p>
<p>
Looks pretty interesting and hopefully at this point it also looks kinda linear, which it kinda does.
</p>
<p>
Now let’s fit it with a line:
</p>
<div class="code">
lm.res <- lm( new_y ~ x )<br/><br />
lm.res
</div>
<p>Which produces this output:</p>
<div class="code">
Coefficients:<br/><br />
(Intercept)            x<br/><br />
1.122      -11.647<br/>
</div>
<p>We can also test the significance of the fit with an ANOVA.</p>
<div class="code">
anova(lm.res)
</div>
<p>Which produces this output:</p>
<div class="code">
Analysis of Variance Table<br/><br />
Response: new_y<br/><br />
Df Sum Sq Mean Sq F value    Pr(>F)<br/><br />
x          1 172.80 172.802  14.641 0.0009834 ***<br/>
</div>
<p>
And we can plot the resulting fit in linear space:
</p>
<p><a href="http://thespicychicken.com/wp-content/uploads/2010/02/RTip_Sigmoid_Fig3.png"><img src="http://thespicychicken.com/wp-content/uploads/2010/02/RTip_Sigmoid_Fig3-300x276.png" alt="" title="RTip_Sigmoid_Fig3" width="300" height="276" class="alignnone size-medium wp-image-188" /></a></p>
<p>
Now let’s see how our fit looks back in normal space using our formula with our derived a and b values.
</p>
<div class="code">
a = -11.647<br/><br />
b = 1.122<br/><br />
plot(x, y)<br/><br />
sim_x = (1:101-1)/100<br/><br />
points(sim_x, 1/(1+exp(a*sim_x+b)), type=&#8221;l&#8221;)<br/>
</div>
<p><a href="http://thespicychicken.com/wp-content/uploads/2010/02/RTip_Sigmoid_Fig4.png"><img src="http://thespicychicken.com/wp-content/uploads/2010/02/RTip_Sigmoid_Fig4-300x271.png" alt="" title="RTip_Sigmoid_Fig4" width="300" height="271" class="alignnone size-medium wp-image-189" /></a></p>
<p>
Voila! We have fit a sigmoid function to our data.</p>
]]></content:encoded>
			<wfw:commentRss>http://thespicychicken.com/2010/02/17/r-tip-fitting-sigmoidal-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

