<?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>Zhanggw&#039;s Blog</title>
	<atom:link href="http://zhanggw.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://zhanggw.wordpress.com</link>
	<description>Thinking,Reading,Writing</description>
	<lastBuildDate>Sun, 03 Oct 2010 23:26:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='zhanggw.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Zhanggw&#039;s Blog</title>
		<link>http://zhanggw.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://zhanggw.wordpress.com/osd.xml" title="Zhanggw&#039;s Blog" />
	<atom:link rel='hub' href='http://zhanggw.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Stepwise logistic regression using R(Ⅱ)</title>
		<link>http://zhanggw.wordpress.com/2010/10/03/stepwise-logistic-regression%e2%85%a1/</link>
		<comments>http://zhanggw.wordpress.com/2010/10/03/stepwise-logistic-regression%e2%85%a1/#comments</comments>
		<pubDate>Sun, 03 Oct 2010 16:44:07 +0000</pubDate>
		<dc:creator>zgw21cnn</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[Statistic]]></category>
		<category><![CDATA[Logistic regression]]></category>

		<guid isPermaLink="false">https://zhanggw.wordpress.com/2010/10/03/stepwise-logistic-regression%e2%85%a1/</guid>
		<description><![CDATA[Consider a clinical data set. You can download it from Here. The Diabetes data set consists nine variables. The variable Class is the response variable with a value of tested_positive and a value of tested_negative. The other eight variables are the explanatory variables. Stepwise logistic regression #read into data diabetes&#60;-read.csv(&#8220;d:/diabetes.csv&#8221;,head=TRUE) #get explanatory variables x&#60;-c(rep(1,768),diabetes[2:769,1],diabetes[2:769,2],diabetes[2:769,3],diabetes[2:769,4],diabetes[2:769,5],diabetes[2:769,6],diabetes[2:769,7],diabetes[2:769,8]) x&#60;-matrix(x,nrow=768) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zhanggw.wordpress.com&amp;blog=10167014&amp;post=217&amp;subd=zhanggw&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Consider a clinical data set. You can download it from <a href="http://repository.seasr.org/Datasets/UCI/csv/diabetes.csv">Here</a>. The Diabetes data set consists nine variables. The variable Class is the response variable with a value of tested_positive and a value of tested_negative. The other eight variables are the explanatory variables.</p>
<div class="rsplus" style="font-size:small;font-family:consolas,;">
<div style="font-weight:bold;color:#ffffff;background-color:#4bacc6;text-align:center;">Stepwise logistic regression</div>
<ol>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">#read into data</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">diabetes<span style="color:#080;">&lt;-</span><span style="font-weight:bold;color:#0000ff;">read.csv</span><span style="color:#080;">(</span><span style="color:#ff0000;">&#8220;d:/diabetes.csv&#8221;</span>,<span style="font-weight:bold;color:#0000ff;">head</span><span style="color:#080;">=</span>TRUE<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">#get explanatory variables</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">x<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="font-weight:bold;color:#0000ff;">c</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rep.html"><span style="font-weight:bold;color:#0000ff;">rep</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">1</span>,<span style="color:#ff0000;">768</span><span style="color:#080;">)</span>,diabetes<span style="color:#080;">[</span><span style="color:#ff0000;">2</span><span style="color:#080;">:</span><span style="color:#ff0000;">769</span>,<span style="color:#ff0000;">1</span><span style="color:#080;">]</span>,diabetes<span style="color:#080;">[</span><span style="color:#ff0000;">2</span><span style="color:#080;">:</span><span style="color:#ff0000;">769</span>,<span style="color:#ff0000;">2</span><span style="color:#080;">]</span>,diabetes<span style="color:#080;">[</span><span style="color:#ff0000;">2</span><span style="color:#080;">:</span><span style="color:#ff0000;">769</span>,<span style="color:#ff0000;">3</span><span style="color:#080;">]</span>,diabetes<span style="color:#080;">[</span><span style="color:#ff0000;">2</span><span style="color:#080;">:</span><span style="color:#ff0000;">769</span>,<span style="color:#ff0000;">4</span><span style="color:#080;">]</span>,diabetes<span style="color:#080;">[</span><span style="color:#ff0000;">2</span><span style="color:#080;">:</span><span style="color:#ff0000;">769</span>,<span style="color:#ff0000;">5</span><span style="color:#080;">]</span>,diabetes<span style="color:#080;">[</span><span style="color:#ff0000;">2</span><span style="color:#080;">:</span><span style="color:#ff0000;">769</span>,<span style="color:#ff0000;">6</span><span style="color:#080;">]</span>,diabetes<span style="color:#080;">[</span><span style="color:#ff0000;">2</span><span style="color:#080;">:</span><span style="color:#ff0000;">769</span>,<span style="color:#ff0000;">7</span><span style="color:#080;">]</span>,diabetes<span style="color:#080;">[</span><span style="color:#ff0000;">2</span><span style="color:#080;">:</span><span style="color:#ff0000;">769</span>,<span style="color:#ff0000;">8</span><span style="color:#080;">]</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">x<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span>x,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">=</span><span style="color:#ff0000;">768</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">#get response variable</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">y<span style="color:#080;">&lt;-</span>diabetes<span style="color:#080;">[</span><span style="color:#ff0000;">2</span><span style="color:#080;">:</span><span style="color:#ff0000;">769</span>,<span style="color:#ff0000;">9</span><span style="color:#080;">]</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">y<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ifelse.html"><span style="font-weight:bold;color:#0000ff;">ifelse</span></a><span style="color:#080;">(</span>y<span style="color:#080;">==</span><span style="color:#ff0000;">&#8220;tested_positive&#8221;</span>,<span style="color:#ff0000;">1</span>,<span style="color:#ff0000;">0</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">y<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span>y,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">=</span><span style="color:#ff0000;">768</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">#set explantory variable&#8217;s name</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">x_name<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="font-weight:bold;color:#0000ff;">c</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">&#8220;INTERCEPT&#8221;</span>,<span style="color:#ff0000;">&#8220;PREGNANT&#8221;</span>,<span style="color:#ff0000;">&#8220;PLASMA&#8221;</span>,<span style="color:#ff0000;">&#8220;PRESSURE&#8221;</span>,<span style="color:#ff0000;">&#8220;TRICEPS&#8221;</span>,<span style="color:#ff0000;">&#8220;INSULIN&#8221;</span>,<span style="color:#ff0000;">&#8220;INDEX&#8221;</span>,<span style="color:#ff0000;">&#8220;PEDIGREE&#8221;</span>,<span style="color:#ff0000;">&#8220;AGE&#8221;</span><span style="color:#080;">)</span></div>
</li>
</ol>
<ol> The logistic regression R code is as follows.</ol>
</div>
<div class="rsplus" style="font-size:small;font-family:consolas,;">
<div style="font-weight:bold;color:#ffffff;background-color:#4bacc6;text-align:center;">Stepwise logistic regression</div>
<ol>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">pi_fun<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="font-weight:bold;color:#0000ff;">function</span></a><span style="color:#080;">(</span>x,Beta<span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Beta<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="font-weight:bold;color:#0000ff;">as.vector</span></a><span style="color:#080;">(</span>Beta<span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="font-weight:bold;color:#0000ff;">ncol</span></a><span style="color:#080;">=</span><span style="color:#ff0000;">1</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">x<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="font-weight:bold;color:#0000ff;">as.vector</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="font-weight:bold;color:#0000ff;">ncol</span></a><span style="color:#080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>Beta<span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">g_fun<span style="color:#080;">&lt;-</span>x<span style="color:#080;">%*%</span>Beta</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/exp.html"><span style="font-weight:bold;color:#0000ff;">exp</span></a><span style="color:#080;">(</span>g_fun<span style="color:#080;">)</span><span style="color:#080;">/</span><span style="color:#080;">(</span><span style="color:#ff0000;">1</span><span style="color:#080;">+</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/exp.html"><span style="font-weight:bold;color:#0000ff;">exp</span></a><span style="color:#080;">(</span>g_fun<span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">funs<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="font-weight:bold;color:#0000ff;">function</span></a><span style="color:#080;">(</span>x,y,Beta<span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">y<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="font-weight:bold;color:#0000ff;">as.vector</span></a><span style="color:#080;">(</span>y<span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="font-weight:bold;color:#0000ff;">ncol</span></a><span style="color:#080;">=</span><span style="color:#ff0000;">1</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">x<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="font-weight:bold;color:#0000ff;">as.vector</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>y<span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Beta<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="font-weight:bold;color:#0000ff;">c</span></a><span style="color:#080;">(</span>Beta<span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="font-weight:bold;color:#0000ff;">ncol</span></a><span style="color:#080;">=</span><span style="color:#ff0000;">1</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">pi_value<span style="color:#080;">&lt;-</span> pi_fun<span style="color:#080;">(</span>x,Beta<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">U<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/t.html"><span style="font-weight:bold;color:#0000ff;">t</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">%*%</span><span style="color:#080;">(</span>y<span style="color:#080;">-</span>pi_value<span style="color:#080;">)</span><span style="color:#080;">;</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">uni_matrix<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rep.html"><span style="font-weight:bold;color:#0000ff;">rep</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">1</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>pi_value<span style="color:#080;">)</span><span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>pi_value<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">;</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">H<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/t.html"><span style="font-weight:bold;color:#0000ff;">t</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">%*%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/diag.html"><span style="font-weight:bold;color:#0000ff;">diag</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="font-weight:bold;color:#0000ff;">as.vector</span></a><span style="color:#080;">(</span>pi_value<span style="color:#080;">*</span><span style="color:#080;">(</span> uni_matrix <span style="color:#080;">-</span>pi_value<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">%*%</span>x</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="font-weight:bold;color:#0000ff;">list</span></a><span style="color:#080;">(</span>U<span style="color:#080;">=</span>U,H<span style="color:#080;">=</span>H<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Newtons<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="font-weight:bold;color:#0000ff;">function</span></a><span style="color:#080;">(</span>fun,x,y,ep<span style="color:#080;">=</span>1e<span style="color:#080;">-</span>8,it_max<span style="color:#080;">=</span><span style="color:#ff0000;">100</span><span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">x<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="font-weight:bold;color:#0000ff;">as.vector</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>y<span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Beta<span style="color:#080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rep.html"><span style="font-weight:bold;color:#0000ff;">rep</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">0</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="font-weight:bold;color:#0000ff;">ncol</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="font-weight:bold;color:#0000ff;">ncol</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Index<span style="color:#080;">&lt;-</span><span style="color:#ff0000;">0</span><span style="color:#080;">;</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">k<span style="color:#080;">&lt;-</span><span style="color:#ff0000;">1</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/while.html"><span style="font-weight:bold;color:#0000ff;">while</span></a><span style="color:#080;">(</span>k<span style="color:#080;">&lt;=</span>it_max<span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">x1<span style="color:#080;">&lt;-</span>Beta<span style="color:#080;">;</span>obj<span style="color:#080;">&lt;-</span>fun<span style="color:#080;">(</span>x,y,Beta<span style="color:#080;">)</span><span style="color:#080;">;</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Beta<span style="color:#080;">&lt;-</span>Beta<span style="color:#080;">+</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/solve.html"><span style="font-weight:bold;color:#0000ff;">solve</span></a><span style="color:#080;">(</span>obj$H,obj$U<span style="color:#080;">)</span><span style="color:#080;">;</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">objTemp<span style="color:#080;">&lt;-</span>fun<span style="color:#080;">(</span>x,y,Beta<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="font-weight:bold;color:#0000ff;">if</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/any.html"><span style="font-weight:bold;color:#0000ff;">any</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/is.nan.html"><span style="font-weight:bold;color:#0000ff;">is.nan</span></a><span style="color:#080;">(</span>objTemp$H<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Beta<span style="color:#080;">&lt;-</span>x1</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">&#8220;Warning:The maximum likelihood estimate does not exist.&#8221;</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="font-weight:bold;color:#0000ff;">paste</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">&#8220;The LOGISTIC procedure continues in spite of the above warning.&#8221;</span>,</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#ff0000;">&#8220;Results shown are based on the last maximum likelihood iteration. Validity of the model fit is questionable.&#8221;</span><span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">;</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/break.html"><span style="font-weight:bold;color:#0000ff;">break</span></a></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">norm<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sqrt.html"><span style="font-weight:bold;color:#0000ff;">sqrt</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/t.html"><span style="font-weight:bold;color:#0000ff;">t</span></a><span style="color:#080;">(</span><span style="color:#080;">(</span>Beta<span style="color:#080;">-</span>x1<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">%*%</span><span style="color:#080;">(</span>Beta<span style="color:#080;">-</span>x1<span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="font-weight:bold;color:#0000ff;">if</span></a><span style="color:#080;">(</span>norm<span style="color:#080;">&lt;</span>ep<span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">index<span style="color:#080;">&lt;-</span><span style="color:#ff0000;">1</span><span style="color:#080;">;</span>break</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">k<span style="color:#080;">&lt;-</span>k<span style="color:#080;">+</span><span style="color:#ff0000;">1</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">obj<span style="color:#080;">&lt;-</span>fun<span style="color:#080;">(</span>x,y,Beta<span style="color:#080;">)</span><span style="color:#080;">;</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="font-weight:bold;color:#0000ff;">list</span></a><span style="color:#080;">(</span>Beta<span style="color:#080;">=</span>Beta,it<span style="color:#080;">=</span>k,U<span style="color:#080;">=</span>obj$U,H<span style="color:#080;">=</span>obj$H<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">CheckZero<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="font-weight:bold;color:#0000ff;">function</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/for.html"><span style="font-weight:bold;color:#0000ff;">for</span></a><span style="color:#080;">(</span>i <span style="font-weight:bold;color:#0000ff;">in</span> <span style="color:#ff0000;">1</span><span style="color:#080;">:</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="font-weight:bold;color:#0000ff;">length</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="font-weight:bold;color:#0000ff;">if</span></a><span style="color:#080;">(</span>x<span style="color:#080;">[</span>i<span style="color:#080;">]</span><span style="color:#080;">==</span><span style="color:#ff0000;">0</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">x<span style="color:#080;">[</span>i<span style="color:#080;">]</span><span style="color:#080;">&lt;-</span>1e<span style="color:#080;">-</span>300</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">x</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">ModelFitStat<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="font-weight:bold;color:#0000ff;">function</span></a><span style="color:#080;">(</span>x,y,Beta<span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">x<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="font-weight:bold;color:#0000ff;">as.vector</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>y<span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Beta<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="font-weight:bold;color:#0000ff;">as.vector</span></a><span style="color:#080;">(</span>Beta<span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="font-weight:bold;color:#0000ff;">ncol</span></a><span style="color:#080;">=</span><span style="color:#ff0000;">1</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">uni_matrix<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rep.html"><span style="font-weight:bold;color:#0000ff;">rep</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">1</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>y<span style="color:#080;">)</span><span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>y<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">;</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">LOGL<span style="color:#080;">&lt;&#8211;</span><span style="color:#ff0000;">2</span><span style="color:#080;">*</span><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/t.html"><span style="font-weight:bold;color:#0000ff;">t</span></a><span style="color:#080;">(</span>y<span style="color:#080;">)</span><span style="color:#080;">%*%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/log.html"><span style="font-weight:bold;color:#0000ff;">log</span></a><span style="color:#080;">(</span>pi_fun<span style="color:#080;">(</span>x,Beta<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">+</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/t.html"><span style="font-weight:bold;color:#0000ff;">t</span></a><span style="color:#080;">(</span>uni_matrix<span style="color:#080;">-</span>y<span style="color:#080;">)</span><span style="color:#080;">%*%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/log.html"><span style="font-weight:bold;color:#0000ff;">log</span></a><span style="color:#080;">(</span>CheckZero<span style="color:#080;">(</span>uni_matrix<span style="color:#080;">-</span>pi_fun<span style="color:#080;">(</span>x,Beta<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">#print(&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#8221;)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">#print(LOGL)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">AIC<span style="color:#080;">&lt;-</span>LOGL<span style="color:#080;">+</span><span style="color:#ff0000;">2</span><span style="color:#080;">*</span><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="font-weight:bold;color:#0000ff;">ncol</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">SC<span style="color:#080;">&lt;-</span>LOGL<span style="color:#080;">+</span><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="font-weight:bold;color:#0000ff;">ncol</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">*</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/log.html"><span style="font-weight:bold;color:#0000ff;">log</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="font-weight:bold;color:#0000ff;">list</span></a><span style="color:#080;">(</span>LOGL<span style="color:#080;">=</span>LOGL,<span style="font-weight:bold;color:#0000ff;">AIC</span><span style="color:#080;">=</span><span style="font-weight:bold;color:#0000ff;">AIC</span>,SC<span style="color:#080;">=</span>SC<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">GlobalNullTest<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="font-weight:bold;color:#0000ff;">function</span></a><span style="color:#080;">(</span>x,y,Beta,BetaIntercept<span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">y<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="font-weight:bold;color:#0000ff;">as.vector</span></a><span style="color:#080;">(</span>y<span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="font-weight:bold;color:#0000ff;">ncol</span></a><span style="color:#080;">=</span><span style="color:#ff0000;">1</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">x<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="font-weight:bold;color:#0000ff;">as.vector</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>y<span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Beta<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="font-weight:bold;color:#0000ff;">as.vector</span></a><span style="color:#080;">(</span>Beta<span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="font-weight:bold;color:#0000ff;">ncol</span></a><span style="color:#080;">=</span><span style="color:#ff0000;">1</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">pi_value<span style="color:#080;">&lt;-</span> pi_fun<span style="color:#080;">(</span>x,Beta<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">df<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>Beta<span style="color:#080;">)</span><span style="color:#080;">-</span><span style="color:#ff0000;">1</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">##compute Likelihood ratio</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">MF<span style="color:#080;">&lt;-</span>ModelFitStat<span style="color:#080;">(</span>x,y,Beta<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">n1<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sum.html"><span style="font-weight:bold;color:#0000ff;">sum</span></a><span style="color:#080;">(</span>y<span style="color:#080;">[</span>y<span style="color:#080;">&gt;</span><span style="color:#ff0000;">0</span><span style="color:#080;">]</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">n<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>y<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">LR<span style="color:#080;">&lt;&#8211;</span><span style="color:#ff0000;">2</span><span style="color:#080;">*</span><span style="color:#080;">(</span>n1<span style="color:#080;">*</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/log.html"><span style="font-weight:bold;color:#0000ff;">log</span></a><span style="color:#080;">(</span>n1<span style="color:#080;">)</span><span style="color:#080;">+</span><span style="color:#080;">(</span>n<span style="color:#080;">-</span>n1<span style="color:#080;">)</span><span style="color:#080;">*</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/log.html"><span style="font-weight:bold;color:#0000ff;">log</span></a><span style="color:#080;">(</span>n<span style="color:#080;">-</span>n1<span style="color:#080;">)</span><span style="color:#080;">-</span>n<span style="color:#080;">*</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/log.html"><span style="font-weight:bold;color:#0000ff;">log</span></a><span style="color:#080;">(</span>n<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">-</span>MF$LOGL</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">LR_p_value<span style="color:#080;">&lt;-</span><span style="font-weight:bold;color:#0000ff;">pchisq</span><span style="color:#080;">(</span>LR,<span style="font-weight:bold;color:#0000ff;">df</span>,lower.tail<span style="color:#080;">=</span>FALSE<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">LR_Test<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="font-weight:bold;color:#0000ff;">list</span></a><span style="color:#080;">(</span>LR<span style="color:#080;">=</span>LR,DF<span style="color:#080;">=</span><span style="font-weight:bold;color:#0000ff;">df</span>,LR_p_value<span style="color:#080;">=</span>LR_p_value<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">##compute Score</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">BetaIntercept<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="font-weight:bold;color:#0000ff;">c</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="font-weight:bold;color:#0000ff;">as.vector</span></a><span style="color:#080;">(</span>BetaIntercept<span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rep.html"><span style="font-weight:bold;color:#0000ff;">rep</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">0</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="font-weight:bold;color:#0000ff;">ncol</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">-</span><span style="color:#ff0000;">1</span><span style="color:#080;">)</span><span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="font-weight:bold;color:#0000ff;">ncol</span></a><span style="color:#080;">=</span><span style="color:#ff0000;">1</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">obj<span style="color:#080;">&lt;-</span>funs<span style="color:#080;">(</span>x,y,BetaIntercept<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Score<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/t.html"><span style="font-weight:bold;color:#0000ff;">t</span></a><span style="color:#080;">(</span>obj$U<span style="color:#080;">)</span><span style="color:#080;">%*%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/solve.html"><span style="font-weight:bold;color:#0000ff;">solve</span></a><span style="color:#080;">(</span>obj$H<span style="color:#080;">)</span><span style="color:#080;">%*%</span>obj$U</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Score_p_value<span style="color:#080;">&lt;-</span><span style="font-weight:bold;color:#0000ff;">pchisq</span><span style="color:#080;">(</span>Score,<span style="font-weight:bold;color:#0000ff;">df</span>,lower.tail<span style="color:#080;">=</span>FALSE<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Score_Test<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="font-weight:bold;color:#0000ff;">list</span></a><span style="color:#080;">(</span>Score<span style="color:#080;">=</span>Score,DF<span style="color:#080;">=</span><span style="font-weight:bold;color:#0000ff;">df</span>,Score_p_value<span style="color:#080;">=</span>Score_p_value<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">##compute Wald test </span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">obj<span style="color:#080;">&lt;-</span>funs<span style="color:#080;">(</span>x,y,Beta<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">I_Diag<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/diag.html"><span style="font-weight:bold;color:#0000ff;">diag</span></a><span style="color:#080;">(</span><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/solve.html"><span style="font-weight:bold;color:#0000ff;">solve</span></a><span style="color:#080;">(</span>obj$H<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Q<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="font-weight:bold;color:#0000ff;">c</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rep.html"><span style="font-weight:bold;color:#0000ff;">rep</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">0</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>Beta<span style="color:#080;">)</span><span style="color:#080;">-</span><span style="color:#ff0000;">1</span><span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="font-weight:bold;color:#0000ff;">as.vector</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/diag.html"><span style="font-weight:bold;color:#0000ff;">diag</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rep.html"><span style="font-weight:bold;color:#0000ff;">rep</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">1</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>Beta<span style="color:#080;">)</span><span style="color:#080;">-</span><span style="color:#ff0000;">1</span><span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>Beta<span style="color:#080;">)</span><span style="color:#080;">-</span><span style="color:#ff0000;">1</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Wald<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/t.html"><span style="font-weight:bold;color:#0000ff;">t</span></a><span style="color:#080;">(</span>Q<span style="color:#080;">%*%</span>Beta<span style="color:#080;">)</span><span style="color:#080;">%*%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/solve.html"><span style="font-weight:bold;color:#0000ff;">solve</span></a><span style="color:#080;">(</span>Q<span style="color:#080;">%*%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/diag.html"><span style="font-weight:bold;color:#0000ff;">diag</span></a><span style="color:#080;">(</span>I_Diag<span style="color:#080;">)</span><span style="color:#080;">%*%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/t.html"><span style="font-weight:bold;color:#0000ff;">t</span></a><span style="color:#080;">(</span>Q<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">%*%</span><span style="color:#080;">(</span>Q<span style="color:#080;">%*%</span>Beta<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Wald_p_value<span style="color:#080;">&lt;-</span><span style="font-weight:bold;color:#0000ff;">pchisq</span><span style="color:#080;">(</span>Wald,<span style="font-weight:bold;color:#0000ff;">df</span>,lower.tail<span style="color:#080;">=</span>FALSE<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Wald_Test<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="font-weight:bold;color:#0000ff;">list</span></a><span style="color:#080;">(</span>Wald<span style="color:#080;">=</span>Wald,DF<span style="color:#080;">=</span><span style="font-weight:bold;color:#0000ff;">df</span>,Wald_p_value<span style="color:#080;">=</span>Wald_p_value<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="font-weight:bold;color:#0000ff;">list</span></a><span style="color:#080;">(</span>LR_Test<span style="color:#080;">=</span>LR_Test,Score_Test<span style="color:#080;">=</span>Score_Test,Wald_Test<span style="color:#080;">=</span>Wald_Test<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">WhichEqual1<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="font-weight:bold;color:#0000ff;">function</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">a<span style="color:#080;">&lt;-</span>NULL</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/for.html"><span style="font-weight:bold;color:#0000ff;">for</span></a><span style="color:#080;">(</span>i <span style="font-weight:bold;color:#0000ff;">in</span> <span style="color:#ff0000;">1</span><span style="color:#080;">:</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="font-weight:bold;color:#0000ff;">length</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="font-weight:bold;color:#0000ff;">if</span></a><span style="color:#080;">(</span>x<span style="color:#080;">[</span>i<span style="color:#080;">]</span><span style="color:#080;">==</span><span style="color:#ff0000;">1</span><span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">a<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="font-weight:bold;color:#0000ff;">c</span></a><span style="color:#080;">(</span>a,i<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">a</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">CheckOut<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="font-weight:bold;color:#0000ff;">function</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/source.html"><span style="font-weight:bold;color:#0000ff;">source</span></a>,check<span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/for.html"><span style="font-weight:bold;color:#0000ff;">for</span></a><span style="color:#080;">(</span>j <span style="font-weight:bold;color:#0000ff;">in</span> <span style="color:#ff0000;">1</span><span style="color:#080;">:</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="font-weight:bold;color:#0000ff;">length</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/source.html"><span style="font-weight:bold;color:#0000ff;">source</span></a><span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/for.html"><span style="font-weight:bold;color:#0000ff;">for</span></a><span style="color:#080;">(</span>k <span style="font-weight:bold;color:#0000ff;">in</span> <span style="color:#ff0000;">1</span><span style="color:#080;">:</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="font-weight:bold;color:#0000ff;">length</span></a><span style="color:#080;">(</span>check<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="font-weight:bold;color:#0000ff;">if</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/source.html"><span style="font-weight:bold;color:#0000ff;">source</span></a><span style="color:#080;">[</span>j<span style="color:#080;">]</span><span style="color:#080;">==</span>check<span style="color:#080;">[</span>k<span style="color:#080;">]</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/source.html"><span style="font-weight:bold;color:#0000ff;">source</span></a><span style="color:#080;">[</span>j<span style="color:#080;">]</span><span style="color:#080;">&lt;-</span><span style="color:#ff0000;">0</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/source.html"><span style="font-weight:bold;color:#0000ff;">source</span></a><span style="color:#080;">[</span>source<span style="color:#080;">&gt;</span><span style="color:#ff0000;">0</span><span style="color:#080;">]</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">NegativeCheck<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="font-weight:bold;color:#0000ff;">function</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/for.html"><span style="font-weight:bold;color:#0000ff;">for</span></a><span style="color:#080;">(</span>i <span style="font-weight:bold;color:#0000ff;">in</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="font-weight:bold;color:#0000ff;">length</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">:</span><span style="color:#ff0000;">1</span><span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="font-weight:bold;color:#0000ff;">if</span></a><span style="color:#080;">(</span>x<span style="color:#080;">[</span>i<span style="color:#080;">]</span><span style="color:#080;">&gt;</span><span style="color:#ff0000;">0</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/break.html"><span style="font-weight:bold;color:#0000ff;">break</span></a></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">i</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">CycleCheck<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="font-weight:bold;color:#0000ff;">function</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">NegativeFlg<span style="color:#080;">&lt;-</span>NegativeCheck<span style="color:#080;">(</span>x<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="font-weight:bold;color:#0000ff;">if</span></a><span style="color:#080;">(</span>NegativeFlg<span style="color:#080;">==</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="font-weight:bold;color:#0000ff;">length</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="font-weight:bold;color:#0000ff;">return</span></a><span style="color:#080;">(</span>FALSE<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">NegVec<span style="color:#080;">&lt;-</span>x<span style="color:#080;">[</span><span style="color:#080;">(</span>NegativeFlg<span style="color:#080;">+</span><span style="color:#ff0000;">1</span><span style="color:#080;">)</span><span style="color:#080;">:</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="font-weight:bold;color:#0000ff;">length</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">]</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">PosVec<span style="color:#080;">&lt;-</span>x<span style="color:#080;">[</span><span style="color:#080;">(</span><span style="color:#ff0000;">2</span><span style="color:#080;">*</span>NegativeFlg<span style="color:#080;">-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="font-weight:bold;color:#0000ff;">length</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">+</span><span style="color:#ff0000;">1</span><span style="color:#080;">)</span><span style="color:#080;">:</span>NegativeFlg<span style="color:#080;">]</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">NegVec<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sort.html"><span style="font-weight:bold;color:#0000ff;">sort</span></a><span style="color:#080;">(</span><span style="color:#080;">-</span><span style="color:#ff0000;">1</span><span style="color:#080;">*</span>NegVec<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">PosVec<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sort.html"><span style="font-weight:bold;color:#0000ff;">sort</span></a><span style="color:#080;">(</span>PosVec<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="font-weight:bold;color:#0000ff;">if</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/all.html"><span style="font-weight:bold;color:#0000ff;">all</span></a><span style="color:#080;">(</span><span style="color:#080;">(</span>NegVec<span style="color:#080;">-</span>PosVec<span style="color:#080;">)</span><span style="color:#080;">==</span><span style="color:#ff0000;">0</span><span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="font-weight:bold;color:#0000ff;">return</span></a><span style="color:#080;">(</span>TRUE<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="font-weight:bold;color:#0000ff;">return</span></a><span style="color:#080;">(</span>FALSE<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">##stepwise</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Stepwise<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="font-weight:bold;color:#0000ff;">function</span></a><span style="color:#080;">(</span>x,y,checkin_pvalue<span style="color:#080;">=</span><span style="color:#ff0000;">0.05</span>,checkout_pvalue<span style="color:#080;">=</span><span style="color:#ff0000;">0.05</span><span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">##as matrix</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">x<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="font-weight:bold;color:#0000ff;">as.vector</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>y<span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">##indication of variable</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">indict<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rep.html"><span style="font-weight:bold;color:#0000ff;">rep</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">0</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="font-weight:bold;color:#0000ff;">ncol</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">)</span> <span style="color:#228b22;">##which column enter</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">##intercept entered</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">indict<span style="color:#080;">[</span><span style="color:#ff0000;">1</span><span style="color:#080;">]</span><span style="color:#080;">&lt;-</span><span style="color:#ff0000;">1</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Beta<span style="color:#080;">&lt;-</span>NULL</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">&#8220;Intercept Entered&#8230;&#8221;</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Result<span style="color:#080;">&lt;-</span>Newtons<span style="color:#080;">(</span>funs,x<span style="color:#080;">[</span>,<span style="color:#ff0000;">1</span><span style="color:#080;">]</span>,y<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Beta<span style="color:#080;">&lt;-</span>Result$Beta</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">BetaIntercept<span style="color:#080;">&lt;-</span>Result$Beta</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">uni_matrix<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="font-weight:bold;color:#0000ff;">matrix</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rep.html"><span style="font-weight:bold;color:#0000ff;">rep</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">1</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>y<span style="color:#080;">)</span><span style="color:#080;">)</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="font-weight:bold;color:#0000ff;">nrow</span></a><span style="color:#080;">(</span>y<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">;</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">LOGL<span style="color:#080;">&lt;&#8211;</span><span style="color:#ff0000;">2</span><span style="color:#080;">*</span><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/t.html"><span style="font-weight:bold;color:#0000ff;">t</span></a><span style="color:#080;">(</span>y<span style="color:#080;">)</span><span style="color:#080;">%*%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/log.html"><span style="font-weight:bold;color:#0000ff;">log</span></a><span style="color:#080;">(</span>pi_fun<span style="color:#080;">(</span>x<span style="color:#080;">[</span>,<span style="color:#ff0000;">1</span><span style="color:#080;">]</span>,Beta<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">+</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/t.html"><span style="font-weight:bold;color:#0000ff;">t</span></a><span style="color:#080;">(</span>uni_matrix<span style="color:#080;">-</span>y<span style="color:#080;">)</span><span style="color:#080;">%*%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/log.html"><span style="font-weight:bold;color:#0000ff;">log</span></a><span style="color:#080;">(</span>uni_matrix<span style="color:#080;">-</span>pi_fun<span style="color:#080;">(</span>x<span style="color:#080;">[</span>,<span style="color:#ff0000;">1</span><span style="color:#080;">]</span>,Beta<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="font-weight:bold;color:#0000ff;">paste</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">&#8220;-2Log=&#8221;</span>,LOGL<span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">indexVector<span style="color:#080;">&lt;-</span>WhichEqual1<span style="color:#080;">(</span>indict<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">##check other variable</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">VariableFlg<span style="color:#080;">&lt;-</span>NULL</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Terminate<span style="color:#080;">&lt;-</span>FALSE</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/repeat.html"><span style="font-weight:bold;color:#0000ff;">repeat</span></a><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="font-weight:bold;color:#0000ff;">if</span></a><span style="color:#080;">(</span>Terminate<span style="color:#080;">==</span>TRUE<span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">&#8220;Model building terminates because the last effect entered is removed by the Wald statistic criterion. &#8220;</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/break.html"><span style="font-weight:bold;color:#0000ff;">break</span></a></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">pvalue<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rep.html"><span style="font-weight:bold;color:#0000ff;">rep</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">1</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="font-weight:bold;color:#0000ff;">ncol</span></a><span style="color:#080;">(</span>x<span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">k<span style="color:#080;">&lt;-</span><span style="color:#ff0000;">2</span><span style="color:#080;">:</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="font-weight:bold;color:#0000ff;">length</span></a><span style="color:#080;">(</span>indict<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">k<span style="color:#080;">&lt;-</span>CheckOut<span style="color:#080;">(</span>k,indexVector<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/for.html"><span style="font-weight:bold;color:#0000ff;">for</span></a><span style="color:#080;">(</span>i <span style="font-weight:bold;color:#0000ff;">in</span> k<span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">obj<span style="color:#080;">&lt;-</span>funs<span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="font-weight:bold;color:#0000ff;">c</span></a><span style="color:#080;">(</span>x<span style="color:#080;">[</span>,indexVector<span style="color:#080;">]</span>,x<span style="color:#080;">[</span>,i<span style="color:#080;">]</span><span style="color:#080;">)</span>,y,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="font-weight:bold;color:#0000ff;">c</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="font-weight:bold;color:#0000ff;">as.vector</span></a><span style="color:#080;">(</span>Beta<span style="color:#080;">)</span>,<span style="color:#ff0000;">0</span><span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Score<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/t.html"><span style="font-weight:bold;color:#0000ff;">t</span></a><span style="color:#080;">(</span>obj$U<span style="color:#080;">)</span><span style="color:#080;">%*%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/solve.html"><span style="font-weight:bold;color:#0000ff;">solve</span></a><span style="color:#080;">(</span>obj$H,obj$U<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Score_pvalue<span style="color:#080;">&lt;-</span><span style="font-weight:bold;color:#0000ff;">pchisq</span><span style="color:#080;">(</span>Score,<span style="color:#ff0000;">1</span>,lower.tail<span style="color:#080;">=</span>FALSE<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">pvalue<span style="color:#080;">[</span>i<span style="color:#080;">]</span><span style="color:#080;">&lt;-</span>Score_pvalue</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">#print(&#8220;Score pvalue for variable enter&#8221;)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">#print(pvalue)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">pvalue_min<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/min.html"><span style="font-weight:bold;color:#0000ff;">min</span></a><span style="color:#080;">(</span>pvalue<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="font-weight:bold;color:#0000ff;">if</span></a><span style="color:#080;">(</span>pvalue_min<span style="color:#080;">&lt;</span>checkin_pvalue<span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">j<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/which.min.html"><span style="font-weight:bold;color:#0000ff;">which.min</span></a><span style="color:#080;">(</span>pvalue<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="font-weight:bold;color:#0000ff;">paste</span></a><span style="color:#080;">(</span>x_name<span style="color:#080;">[</span>j<span style="color:#080;">]</span>,<span style="color:#ff0000;">&#8221; entered:&#8221;</span><span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">##set indication of variable</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">indict<span style="color:#080;">[</span>j<span style="color:#080;">]</span><span style="color:#080;">&lt;-</span><span style="color:#ff0000;">1</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">VariableFlg<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="font-weight:bold;color:#0000ff;">c</span></a><span style="color:#080;">(</span>VariableFlg,j<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">indexVector<span style="color:#080;">&lt;-</span>WhichEqual1<span style="color:#080;">(</span>indict<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">&#8220;indexVector&#8211;test&#8221;</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span>indexVector<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Result<span style="color:#080;">&lt;-</span>Newtons<span style="color:#080;">(</span>funs,x<span style="color:#080;">[</span>,indexVector<span style="color:#080;">]</span>,y<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Beta<span style="color:#080;">&lt;-</span>NULL</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Beta<span style="color:#080;">&lt;-</span>Result$Beta</div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">##compute model fit statistics</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">&#8220;Model Fit Statistics&#8221;</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">MFStat<span style="color:#080;">&lt;-</span>ModelFitStat<span style="color:#080;">(</span>x<span style="color:#080;">[</span>,indexVector<span style="color:#080;">]</span>,y,Beta<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span>MFStat<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">##test globel null hypothesis:Beta=0 </span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">&#8220;Testing Global Null Hypothese:Beta=0&#8243;</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">GNTest<span style="color:#080;">&lt;-</span>GlobalNullTest<span style="color:#080;">(</span>x<span style="color:#080;">[</span>,indexVector<span style="color:#080;">]</span>,y,Beta,BetaIntercept<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span>GNTest<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/repeat.html"><span style="font-weight:bold;color:#0000ff;">repeat</span></a><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">##compute Wald test in order to remove variable</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">indexVector<span style="color:#080;">&lt;-</span>WhichEqual1<span style="color:#080;">(</span>indict<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">obj<span style="color:#080;">&lt;-</span>funs<span style="color:#080;">(</span>x<span style="color:#080;">[</span>,indexVector<span style="color:#080;">]</span>,y,Beta<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">H_Diag<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sqrt.html"><span style="font-weight:bold;color:#0000ff;">sqrt</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/diag.html"><span style="font-weight:bold;color:#0000ff;">diag</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/solve.html"><span style="font-weight:bold;color:#0000ff;">solve</span></a><span style="color:#080;">(</span>obj$H<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">WaldChisq<span style="color:#080;">&lt;-</span><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="font-weight:bold;color:#0000ff;">as.vector</span></a><span style="color:#080;">(</span>Beta<span style="color:#080;">)</span><span style="color:#080;">/</span>H_Diag<span style="color:#080;">)</span><span style="color:#080;">^</span><span style="color:#ff0000;">2</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">WaldChisqPvalue<span style="color:#080;">&lt;-</span><span style="font-weight:bold;color:#0000ff;">pchisq</span><span style="color:#080;">(</span>WaldChisq,<span style="color:#ff0000;">1</span>,lower.tail<span style="color:#080;">=</span>FALSE<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">WaldChisqTest<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="font-weight:bold;color:#0000ff;">list</span></a><span style="color:#080;">(</span>WaldChisq<span style="color:#080;">=</span>WaldChisq,WaldChisqPvalue<span style="color:#080;">=</span>WaldChisqPvalue<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">&#8220;Wald chisq pvalue for variable remove&#8221;</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span>WaldChisqPvalue<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">##check wald to decide to which variable to be removed</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">pvalue_max<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/max.html"><span style="font-weight:bold;color:#0000ff;">max</span></a><span style="color:#080;">(</span>WaldChisqPvalue<span style="color:#080;">[</span><span style="color:#ff0000;">2</span><span style="color:#080;">:</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="font-weight:bold;color:#0000ff;">length</span></a><span style="color:#080;">(</span>WaldChisqPvalue<span style="color:#080;">)</span><span style="color:#080;">]</span><span style="color:#080;">)</span><span style="color:#228b22;">##not check for intercept</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="font-weight:bold;color:#0000ff;">if</span></a><span style="color:#080;">(</span>pvalue_max<span style="color:#080;">&gt;</span>checkout_pvalue<span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">n<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/which.max.html"><span style="font-weight:bold;color:#0000ff;">which.max</span></a><span style="color:#080;">(</span>WaldChisqPvalue<span style="color:#080;">[</span><span style="color:#ff0000;">2</span><span style="color:#080;">:</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="font-weight:bold;color:#0000ff;">length</span></a><span style="color:#080;">(</span>WaldChisqPvalue<span style="color:#080;">)</span><span style="color:#080;">]</span><span style="color:#080;">)</span><span style="color:#228b22;">##not check for intercept</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="font-weight:bold;color:#0000ff;">paste</span></a><span style="color:#080;">(</span>x_name<span style="color:#080;">[</span>indexVector<span style="color:#080;">[</span>n<span style="color:#080;">+</span><span style="color:#ff0000;">1</span><span style="color:#080;">]</span><span style="color:#080;">]</span>,<span style="color:#ff0000;">&#8221; is removed:&#8221;</span><span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">##set indication of variable</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">indict<span style="color:#080;">[</span>indexVector<span style="color:#080;">[</span>n<span style="color:#080;">+</span><span style="color:#ff0000;">1</span><span style="color:#080;">]</span><span style="color:#080;">]</span><span style="color:#080;">&lt;-</span><span style="color:#ff0000;">0</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">m<span style="color:#080;">&lt;-</span> indexVector<span style="color:#080;">[</span>n<span style="color:#080;">+</span><span style="color:#ff0000;">1</span><span style="color:#080;">]</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">VariableFlg<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="font-weight:bold;color:#0000ff;">c</span></a><span style="color:#080;">(</span>VariableFlg,<span style="color:#080;">-</span>m<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">##renew Beta</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">indexVector<span style="color:#080;">&lt;-</span>WhichEqual1<span style="color:#080;">(</span>indict<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Result<span style="color:#080;">&lt;-</span>Newtons<span style="color:#080;">(</span>funs,x<span style="color:#080;">[</span>,indexVector<span style="color:#080;">]</span>,y<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Beta<span style="color:#080;">&lt;-</span>NULL</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Beta<span style="color:#080;">&lt;-</span>Result$Beta</div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="font-weight:bold;color:#0000ff;">if</span></a><span style="color:#080;">(</span>CycleCheck<span style="color:#080;">(</span>VariableFlg<span style="color:#080;">)</span><span style="color:#080;">==</span>TRUE<span style="color:#080;">)</span><span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Terminate<span style="color:#080;">&lt;-</span>TRUE</div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/break.html"><span style="font-weight:bold;color:#0000ff;">break</span></a></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="font-weight:bold;color:#0000ff;">else</span> <span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="font-weight:bold;color:#0000ff;">paste</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">&#8220;No (additional) effects met the&#8221;</span> ,checkout_pvalue,<span style="color:#ff0000;">&#8220;significance level for removal from the model.&#8221;</span><span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/break.html"><span style="font-weight:bold;color:#0000ff;">break</span></a><span style="color:#080;">;</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span><span style="color:#228b22;">##repeat end</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="font-weight:bold;color:#0000ff;">else</span> <span style="color:#080;">{</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="font-weight:bold;color:#0000ff;">paste</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">&#8220;No effects met the&#8221;</span> ,checkin_pvalue,<span style="color:#ff0000;">&#8221; significance level for entry into the model&#8221;</span><span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/break.html"><span style="font-weight:bold;color:#0000ff;">break</span></a></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span><span style="color:#228b22;">##repeat end </span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#228b22;">##</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">&#8220;Beta&#8221;</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span>Beta<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span><span style="color:#ff0000;">&#8220;Analysis of Maximum Likelihood Estimates&#8221;</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">obj<span style="color:#080;">&lt;-</span>funs<span style="color:#080;">(</span>x<span style="color:#080;">[</span>,indexVector<span style="color:#080;">]</span>,y,Beta<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">H_Diag<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sqrt.html"><span style="font-weight:bold;color:#0000ff;">sqrt</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/diag.html"><span style="font-weight:bold;color:#0000ff;">diag</span></a><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/solve.html"><span style="font-weight:bold;color:#0000ff;">solve</span></a><span style="color:#080;">(</span>obj$H<span style="color:#080;">)</span><span style="color:#080;">)</span><span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">WaldChisq<span style="color:#080;">&lt;-</span><span style="color:#080;">(</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="font-weight:bold;color:#0000ff;">as.vector</span></a><span style="color:#080;">(</span>Beta<span style="color:#080;">)</span><span style="color:#080;">/</span>H_Diag<span style="color:#080;">)</span><span style="color:#080;">^</span><span style="color:#ff0000;">2</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">WaldChisqPvalue<span style="color:#080;">&lt;-</span><span style="font-weight:bold;color:#0000ff;">pchisq</span><span style="color:#080;">(</span>WaldChisq,<span style="color:#ff0000;">1</span>,lower.tail<span style="color:#080;">=</span>FALSE<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">WaldChisqTest<span style="color:#080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="font-weight:bold;color:#0000ff;">list</span></a><span style="color:#080;">(</span>WaldChisq<span style="color:#080;">=</span>WaldChisq,WaldChisqPvalue<span style="color:#080;">=</span>WaldChisqPvalue<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="font-weight:bold;color:#0000ff;">print</span></a><span style="color:#080;">(</span>WaldChisqTest<span style="color:#080;">)</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;"><span style="color:#080;">}</span></div>
</li>
<li style="font-weight:normal;vertical-align:top;"></li>
</ol>
</div>
<p>Finally, revoke the stepwise function.</p>
<div class="rsplus" style="font-size:small;font-family:consolas,;">
<div style="font-weight:bold;color:#ffffff;background-color:#4bacc6;text-align:center;">Stepwise logistic regression</div>
<ol>
<li style="font-weight:normal;vertical-align:top;">
<div style="background:none transparent scroll repeat 0 0;font:1em/1.2em monospace;vertical-align:top;margin:0;padding:0;">Stepwise<span style="color:#080;">(</span>x,y<span style="color:#080;">)</span></div>
</li>
</ol>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zhanggw.wordpress.com/217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zhanggw.wordpress.com/217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/zhanggw.wordpress.com/217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/zhanggw.wordpress.com/217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/zhanggw.wordpress.com/217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/zhanggw.wordpress.com/217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/zhanggw.wordpress.com/217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/zhanggw.wordpress.com/217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/zhanggw.wordpress.com/217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/zhanggw.wordpress.com/217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/zhanggw.wordpress.com/217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/zhanggw.wordpress.com/217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/zhanggw.wordpress.com/217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/zhanggw.wordpress.com/217/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zhanggw.wordpress.com&amp;blog=10167014&amp;post=217&amp;subd=zhanggw&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zhanggw.wordpress.com/2010/10/03/stepwise-logistic-regression%e2%85%a1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3c8fa1e1b19522820e87f78d1785d1f8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Stentor</media:title>
		</media:content>
	</item>
		<item>
		<title>Stepwise Logistic Regression Using R(Ⅰ)</title>
		<link>http://zhanggw.wordpress.com/2010/10/03/stepwise-logistic-regression%e2%85%a0/</link>
		<comments>http://zhanggw.wordpress.com/2010/10/03/stepwise-logistic-regression%e2%85%a0/#comments</comments>
		<pubDate>Sun, 03 Oct 2010 14:48:01 +0000</pubDate>
		<dc:creator>zgw21cnn</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[Statistic]]></category>
		<category><![CDATA[Logistic regression]]></category>

		<guid isPermaLink="false">http://zhanggw.wordpress.com/2010/10/03/stepwise-logistic-regression%e2%85%a0/</guid>
		<description><![CDATA[1. Logic regression model We assume that we have p explanatory variables . The logic regression model is , where is the probability of and . The right-hand side is usually referred to logic function, which can be pictured as follow. We can also define , which means the probability of ,in other words, the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zhanggw.wordpress.com&amp;blog=10167014&amp;post=149&amp;subd=zhanggw&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2><strong>1. Logic regression model<br />
</strong></h2>
<p style="text-align:justify;">We assume that we have p explanatory variables <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog110.png?w=600" alt="" />. The logic regression model is</p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog210.png?w=600" alt="" />,</p>
<p>where <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog310.png?w=600" alt="" /> is the probability of <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog410.png?w=600" alt="" /> and <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog510.png?w=600" alt="" />. The right-hand side is usually referred to logic function, which can be pictured as follow.</p>
<p style="text-align:center;margin-left:5pt;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog65.png?w=600" alt="" /></p>
<p style="margin-left:5pt;">We can also define</p>
<p style="text-align:center;margin-left:5pt;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog71.png?w=600" alt="" />,</p>
<p style="margin-left:5pt;">which means the probability of <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog81.png?w=600" alt="" />,in other words, the probability of an event that not occurs.</p>
<p style="margin-left:5pt;">The odds of an event that occurs with probability p is defined as</p>
<p style="text-align:center;margin-left:5pt;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog91.png?w=600" alt="" />.</p>
<p style="margin-left:5pt;">Using log function on both sides, we can get a linear function</p>
<p style="text-align:center;margin-left:5pt;"><img class="aligncenter" src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog101.png?w=600" alt="" />.<strong> </strong></p>
<h2 style="text-align:left;margin-left:5pt;"><strong>2. Maximum likelihood estimation<br />
</strong></h2>
<p>The next goal is to estimate the p+1 parameters <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog111.png?w=600" alt="" />. It can be achieved by maximum likelihood estimation. Since the probability of an event that occurs is <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog121.png?w=600" alt="" />, the probability of <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog131.png?w=600" alt="" />that  occurs can be writhed as <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog141.png?w=600" alt="" />. Likewise, the probability of <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog151.png?w=600" alt="" />that occurs can be writhed as <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog161.png?w=600" alt="" />. Thus ,we can get a likelihood function</p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog171.png?w=600" alt="" /></p>
<p>Replacing <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog181.png?w=600" alt="" /> with <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog191.png?w=600" alt="" />, it can be rewritten as</p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog201.png?w=600" alt="" /></p>
<p>In order to maximize the likelihood function, we can use log function on both sides of likelihood function.<span style="font-family:MS Mincho;"><br />
</span></p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog211.png?w=600" alt="" /></p>
<p>To maximize <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog221.png?w=600" alt="" />, we set the first order partial derivatives of <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog231.png?w=600" alt="" /> to zero.</p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog241.png?w=600" alt="" /><span style="font-family:MS Mincho;"><br />
</span></p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog251.png?w=600" alt="" /><span style="font-family:MS Mincho;"><br />
</span></p>
<p>For j=1,2,&#8230; p. <strong> </strong></p>
<h2><strong>3. Newton-Raphson iteration method<br />
</strong></h2>
<p>In order to solve the set of p+1 nonlinear equations <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog261.png?w=600" alt="" /> ,for j=1,2,…p. we can use the Newton-Raphson iteration method . The matrix of second derivatives, called the Hessian, is</p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog271.png?w=600" alt="" /></p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog281.png?w=600" alt="" /></p>
<p>In matrix form, let <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog291.png?w=600" alt="" /> denotes Hessian matrix, and</p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog301.png?w=600" alt="" /><span style="font-family:MS Mincho;"><br />
</span></p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog311.png?w=600" alt="" /></p>
<p>Where <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog321.png?w=600" alt="" /> is an n by n diagonal matrix with element <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog331.png?w=600" alt="" />. We can get</p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog341.png?w=600" alt="" /></p>
<p>If</p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog351.png?w=600" alt="" /></p>
<p>We have New-Raphson update formula</p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog361.png?w=600" alt="" /></p>
<p>In fact, we don&#8217;t need to compute <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog371.png?w=600" alt="" /> directly. Since <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog381.png?w=600" alt="" />is a positive definite matrices, we can get it by Cholesky decomposition on <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog391.png?w=600" alt="" />.</p>
<p>Let <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog401.png?w=600" alt="" /> is denoted as <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog411.png?w=600" alt="" />,this matrix is called the <strong><em>observed information matrix</em></strong>. The variances and covariances of the estimated coefficients can be obtained from the inverse of <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog421.png?w=600" alt="" />, which represent as <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog431.png?w=600" alt="" />.that is to say, the variance of <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog441.png?w=600" alt="" /> is the <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog451.png?w=600" alt="" />diagonal element of matrix <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog461.png?w=600" alt="" />, and the covariance of <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog471.png?w=600" alt="" /> and <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog481.png?w=600" alt="" />, which can be denoted by <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog491.png?w=600" alt="" />, is the off-diagonal element. So, we can the estimated standard errors of the estimated coefficients as</p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog501.png?w=600" alt="" />, for  j=0,1,2,…,p <strong> </strong></p>
<h2 style="text-align:left;"><strong>4. Significance test<br />
</strong></h2>
<p>A Wald test is used to test the statistical significance of each coefficient in the model. The hypothesis of test is <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog511.png?w=600" alt="" />：<img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog521.png?w=600" alt="" />＝0. We can calculate</p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog531.png?w=600" alt="" /></p>
<p>Where <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog541.png?w=600" alt="" /> is not included in <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog551.png?w=600" alt="" /> and the relevant row and column is not included in <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog561.png?w=600" alt="" />. This value is distributed as chi-square with <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog571.png?w=600" alt="" /> degrees of freedom.</p>
<p>However, there are some problems with the use of the Wald statistic. Large coefficients and inflated standard error will lead to lower the Wald statistic value, and is easy to fail to reject the null when the hypothesis(TypeⅡerror). So the likelihood ratio test is more reliable for small sample sizes often used in practice.</p>
<p>The likelihood-ratio test uses the ratio of the maximized value of the likelihood function for unrestricted <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog581.png?w=600" alt="" /> (L1) over the maximized value of the likelihood function when <img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog591.png?w=600" alt="" /> (L0). The test statistic,</p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog601.png?w=600" alt="" /></p>
<h3><span style="font-size:10pt;"><br />
</span></h3>
<p>yields a chi-squared distribution.</p>
<p>Score test is be introduced to test the hypothesis:</p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog611.png?w=600" alt="" /></p>
<h3><span style="font-size:10pt;">:<img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog621.png?w=600" alt="" /><br />
</span></h3>
<p>The score test statistic is</p>
<p style="text-align:center;"><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog631.png?w=600" alt="" /></p>
<p>Which yelds a chi-squared distribution with df=1. The score test is often used in forward selection to add variable to model. <strong></strong></p>
<h2><strong>5. Variable selection: stepwise</strong></h2>
<p>A stepwise procedure for selection or deletion of variables from a model is based on a statistical algorithm that checks for the &#8220;importance&#8221; of variables. The &#8220;importance&#8221; of a variable is defined in terms of a measure of the statistical significance of the coefficient for the variable.    Here is the flow:</p>
<p><img src="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog641.png?w=600" alt="" /></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zhanggw.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zhanggw.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/zhanggw.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/zhanggw.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/zhanggw.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/zhanggw.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/zhanggw.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/zhanggw.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/zhanggw.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/zhanggw.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/zhanggw.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/zhanggw.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/zhanggw.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/zhanggw.wordpress.com/149/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zhanggw.wordpress.com&amp;blog=10167014&amp;post=149&amp;subd=zhanggw&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zhanggw.wordpress.com/2010/10/03/stepwise-logistic-regression%e2%85%a0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3c8fa1e1b19522820e87f78d1785d1f8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Stentor</media:title>
		</media:content>

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog110.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog210.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog310.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog410.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog510.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog65.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog71.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog81.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog91.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog101.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog111.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog121.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog131.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog141.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog151.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog161.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog171.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog181.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog191.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog201.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog211.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog221.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog231.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog241.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog251.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog261.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog271.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog281.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog291.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog301.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog311.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog321.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog331.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog341.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog351.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog361.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog371.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog381.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog391.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog401.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog411.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog421.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog431.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog441.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog451.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog461.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog471.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog481.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog491.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog501.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog511.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog521.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog531.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog541.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog551.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog561.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog571.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog581.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog591.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog601.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog611.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog621.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog631.png" medium="image" />

		<media:content url="http://zhanggw.files.wordpress.com/2010/10/100310_1446_stepwiselog641.png" medium="image" />
	</item>
		<item>
		<title>Optimization Schaffer F6 function using basic genetic algorithm</title>
		<link>http://zhanggw.wordpress.com/2010/09/25/optimization-schaffer-f6-function-using-basic-genetic-algorithm-2/</link>
		<comments>http://zhanggw.wordpress.com/2010/09/25/optimization-schaffer-f6-function-using-basic-genetic-algorithm-2/#comments</comments>
		<pubDate>Sat, 25 Sep 2010 15:03:18 +0000</pubDate>
		<dc:creator>zgw21cnn</dc:creator>
				<category><![CDATA[GeneticAlgorithm]]></category>
		<category><![CDATA[GA]]></category>
		<category><![CDATA[Optimization]]></category>

		<guid isPermaLink="false">https://zhanggw.wordpress.com/2010/09/25/optimization-schaffer-f6-function-using-basic-genetic-algorithm-2/</guid>
		<description><![CDATA[Schaffer F6 function, as well and its graph,is pictured here. if set y=0,you can get a 2D graph. &#160; These figures illustrate the fact that the Schaffer F6 function has many local optimizations. The basic genetic algorithm is as follows.The matplotlib is included for plot the function graph. import random from math import sin, sqrt, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zhanggw.wordpress.com&amp;blog=10167014&amp;post=69&amp;subd=zhanggw&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class="mycode"></div>
<div class="mycode"></div>
</p>
<div class="mycode"></div>
</p>
<div class="mycode"></div>
</p>
<div class="mycode"></div>
</p>
<div class="mycode"></div>
<p>Schaffer F6 function, as well and its graph,is pictured here.</p>
<p><a href="http://zhanggw.files.wordpress.com/2010/09/clip_image002_thumb.gif"><img title="clip_image002_thumb" style="display:inline;border-width:0;" height="67" alt="clip_image002_thumb" src="http://zhanggw.files.wordpress.com/2010/09/clip_image002_thumb_thumb.gif?w=229&#038;h=67" width="229" border="0" /></a></p>
<p><a href="http://zhanggw.files.wordpress.com/2010/09/schafferf6_thumb.png"><img title="schafferf6_thumb" style="display:block;float:none;margin-left:auto;margin-right:auto;border-width:0;" height="181" alt="schafferf6_thumb" src="http://zhanggw.files.wordpress.com/2010/09/schafferf6_thumb_thumb.png?w=240&#038;h=181" width="240" border="0" /></a>if set y=0,you can get a 2D graph.</p>
<p><a href="http://zhanggw.files.wordpress.com/2010/09/schafferf62d_thumb.png"><img title="schafferf62d_thumb" style="display:block;float:none;margin-left:auto;margin-right:auto;border-width:0;" height="181" alt="schafferf62d_thumb" src="http://zhanggw.files.wordpress.com/2010/09/schafferf62d_thumb_thumb.png?w=240&#038;h=181" width="240" border="0" /></a>&#160; </p>
<p>These figures illustrate the fact that the Schaffer F6 function has many local optimizations.</p>
<p>The basic genetic algorithm is as follows.The matplotlib is included for plot the function graph.</p>
</p>
</p>
<div>
<pre><span style="color:#0000ff;">import</span> random
from math <span style="color:#0000ff;">import</span> sin, sqrt, pow
from mpl_toolkits.mplot3d <span style="color:#0000ff;">import</span> Axes3D
from matplotlib <span style="color:#0000ff;">import</span> cm
<span style="color:#0000ff;">import</span> matplotlib.pyplot as plt
<span style="color:#0000ff;">import</span> numpy as np
from pylab <span style="color:#0000ff;">import</span> *

MAXIMIZE, MINIMIZE = 11, 22

<span style="color:#0000ff;">def</span> SchafferF6(x, y):
    temp1 = sin(sqrt(x * x + y * y))
    temp2 = 1 + 0.001 * (x * x + y * y)
    <span style="color:#0000ff;">return</span> 0.5 + (temp1 * temp1 - 0.5) / (temp2 * temp2)

#
#plot the 3D figure
#
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-100, 100, 1)
Y = np.arange(-100, 100, 1)
X, Y = np.meshgrid(X, Y)

R = np.sin(np.sqrt(X * X + Y * Y))
Q = 1 + 0.001 * (X * X + Y * Y)
Z = 0.5 + (R * R - 0.5) / (Q * Q)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet)

plt.show()
#<span style="color:#0000ff;">end</span>

#
#plot the 2D figure
#

t = arange(-100, 100, 1)
s = 0.5 + (sin(t) * sin(t) - 0.5) / (1 + 0.001 * (t * t))
plot(t, s, linewidth=1.0)

xlabel('x')
ylabel('y')
title('Schaffer F6 2D figure')
grid(True)
show()

<span style="color:#0000ff;">class</span> Individual:
    alleles = (0, 1)
    seperator = ' '
    optimization = MINIMIZE
    length = 20
    chromosome = None
    score = None
    <span style="color:#0000ff;">def</span> __init__(self, chromosome=None):
        self.chromosome = chromosome or self._makechromosome()
        self.score = None # set during evaluation

    <span style="color:#0000ff;">def</span> _makechromosome(self):
        &quot;<span style="color:#8b0000;">makes a chromosome from randomly selected alleles.</span>&quot;
        <span style="color:#0000ff;">return</span> [random.choice(self.alleles) for gene \

                 in range(0, self.length)]

    <span style="color:#0000ff;">def</span> evaluate(self):
        gene_x = self.chromosome[0: int(self.length / 2) + 1]
        gene_y = self.chromosome[int(self.length / 2) + 1:]
        x = 0
        y = 0
        for i in range(0, len(gene_x)):
            x += gene_x[i] * pow(2, i - 2)
        for j in range(0, len(gene_y)):
            y += gene_y[j] * pow(2, j - 2)
        self.score = SchafferF6(x, y)   

    <span style="color:#0000ff;">def</span> crossover(self, other):
       <span style="color:#0000ff;">return</span> self._twopoint(other)

    <span style="color:#0000ff;">def</span> mutate(self, gene):
       self.chromosome[gene] = 1 - self.chromosome[gene]

    # sample crossover method
    <span style="color:#0000ff;">def</span> _twopoint(self, other):
        &quot;<span style="color:#8b0000;">creates offspring via two-point crossover between mates.</span>&quot;
        left, right = self._pickpivots()
        <span style="color:#0000ff;">def</span> mate(p0, p1):
            chromosome = p0.chromosome[:]
            chromosome[left:right] = p1.chromosome[left:right]
            child = p0.__class__(chromosome)
            <span style="color:#0000ff;">return</span> child
        <span style="color:#0000ff;">return</span> mate(self, other), mate(other, self)  

    <span style="color:#0000ff;">def</span> _pickpivots(self):
        left = random.randrange(1, self.length - 2)
        right = random.randrange(left, self.length - 1)
        <span style="color:#0000ff;">return</span> left, right

    <span style="color:#0000ff;">def</span> __repr__(self):
        &quot;<span style="color:#8b0000;">returns string representation of self</span>&quot;
        <span style="color:#0000ff;">return</span> '&lt;%s chromosome=&quot;<span style="color:#8b0000;">%s</span>&quot; score=%s&gt;' % \
               (self.__class__.__name__,
                self.seperator.<span style="color:#0000ff;">join</span>(map(str, self.chromosome)),\

                self.score)

    <span style="color:#0000ff;">def</span> __cmp__(self, other):
        <span style="color:#0000ff;">if</span> self.optimization == MINIMIZE:
            <span style="color:#0000ff;">return</span> cmp(self.score, other.score)
        <span style="color:#0000ff;">else</span>: # MAXIMIZE
            <span style="color:#0000ff;">return</span> cmp(other.score, self.score)

    <span style="color:#0000ff;">def</span> <span style="color:#0000ff;">copy</span>(self):
        twin = self.__class__(self.chromosome[:])
        twin.score = self.score
        <span style="color:#0000ff;">return</span> twin

<span style="color:#0000ff;">class</span> Environment(object):
    <span style="color:#0000ff;">def</span> __init__(self, individual, population=None, size=100,\

                 maxgenerations=100,crossover_rate=0.90,\

                 mutation_rate=0.1, optimum=0):
        self.individual = individual
        self.size = size
        self.optimum = optimum
        self.population = population or self._makepopulation()
        for individual in self.population:
            individual.evaluate()
        self.crossover_rate = crossover_rate
        self.mutation_rate = mutation_rate
        self.maxgenerations = maxgenerations
        self.generation = 0

        self.population.sort()
        self.best = self.population[0]
        self.report()

    <span style="color:#0000ff;">def</span> _makepopulation(self):
        <span style="color:#0000ff;">return</span> [self.individual() for i in range(self.size)]

    <span style="color:#0000ff;">def</span> run(self):
        while self.generation &lt; self.maxgenerations or \
              self.best.score &lt;= self.optimum * 1.0:
            next_population = [self.best.<span style="color:#0000ff;">copy</span>()]
            while len(next_population) &lt; self.size:
                mate1 = self._select()
                <span style="color:#0000ff;">if</span> random.random() &lt; self.crossover_rate:
                    mate2 = self._select()
                    offspring = mate1.crossover(mate2)
                <span style="color:#0000ff;">else</span>:
                    offspring = [mate1.<span style="color:#0000ff;">copy</span>()]
                for individual in offspring:
                    for gene in range(individual.length):
                        <span style="color:#0000ff;">if</span> random.random() &lt; self.mutation_rate:
                            individual.mutate(gene)
                    individual.evaluate()
                    next_population.append(individual)
            self.population = next_population[:self.size]

            self.population.sort()
            self.best = self.population[0]
            self.generation += 1
            self.report()
        self.report()

    <span style="color:#0000ff;">def</span> _select(self):
        &quot;<span style="color:#8b0000;">override this to use your preferred selection method</span>&quot;
        <span style="color:#0000ff;">return</span> self._tournament()

    #
    # sample selection method
    #
    <span style="color:#0000ff;">def</span> _tournament(self, size=8, choosebest=0.90):
        competitors = [random.choice(self.population) \

                       for i in range(size)]
        competitors.sort()
        <span style="color:#0000ff;">if</span> random.random() &lt; choosebest:
            <span style="color:#0000ff;">return</span> competitors[0]
        <span style="color:#0000ff;">else</span>:
            <span style="color:#0000ff;">return</span> random.choice(competitors[1:])

    <span style="color:#0000ff;">def</span> report(self):
        <span style="color:#0000ff;">print</span> &quot;<span style="color:#8b0000;">=</span>&quot; * 70
        <span style="color:#0000ff;">print</span> &quot;<span style="color:#8b0000;">generation: </span>&quot;, self.generation
        <span style="color:#0000ff;">print</span> &quot;<span style="color:#8b0000;">best:       </span>&quot;, self.best

<span style="color:#0000ff;">if</span> __name__ == &quot;<span style="color:#8b0000;">__main__</span>&quot;:
    env = Environment(Individual, maxgenerations=1000)
    env.run()</pre>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zhanggw.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zhanggw.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/zhanggw.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/zhanggw.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/zhanggw.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/zhanggw.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/zhanggw.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/zhanggw.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/zhanggw.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/zhanggw.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/zhanggw.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/zhanggw.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/zhanggw.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/zhanggw.wordpress.com/69/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zhanggw.wordpress.com&amp;blog=10167014&amp;post=69&amp;subd=zhanggw&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zhanggw.wordpress.com/2010/09/25/optimization-schaffer-f6-function-using-basic-genetic-algorithm-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3c8fa1e1b19522820e87f78d1785d1f8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Stentor</media:title>
		</media:content>

		<media:content url="http://zhanggw.files.wordpress.com/2010/09/clip_image002_thumb_thumb.gif" medium="image">
			<media:title type="html">clip_image002_thumb</media:title>
		</media:content>

		<media:content url="http://zhanggw.files.wordpress.com/2010/09/schafferf6_thumb_thumb.png" medium="image">
			<media:title type="html">schafferf6_thumb</media:title>
		</media:content>

		<media:content url="http://zhanggw.files.wordpress.com/2010/09/schafferf62d_thumb_thumb.png" medium="image">
			<media:title type="html">schafferf62d_thumb</media:title>
		</media:content>
	</item>
		<item>
		<title>Plot correlation matrices in SAS</title>
		<link>http://zhanggw.wordpress.com/2010/05/27/plot-correlation-matrices-in-sas/</link>
		<comments>http://zhanggw.wordpress.com/2010/05/27/plot-correlation-matrices-in-sas/#comments</comments>
		<pubDate>Thu, 27 May 2010 08:18:45 +0000</pubDate>
		<dc:creator>zgw21cnn</dc:creator>
				<category><![CDATA[SAS]]></category>
		<category><![CDATA[Correlation matrices]]></category>

		<guid isPermaLink="false">https://zhanggw.wordpress.com/2010/05/27/plot-correlation-matrices-in-sas/</guid>
		<description><![CDATA[I have developed a simple SAS program to show representations of correlation matrices. A paper which introduces a set of techniques that depicts the value of a correlation can be found in Corrgrams (Corrgrams: Exploratory displays for correlation matrices，Friendly,2002). The procedure can be divided into two steps. At first, I use the gplot procedure to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zhanggw.wordpress.com&amp;blog=10167014&amp;post=61&amp;subd=zhanggw&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have developed a simple SAS program to show representations of correlation matrices. A paper which introduces a set of techniques that depicts the value of a correlation can be found in Corrgrams (Corrgrams: Exploratory displays for correlation matrices，Friendly,2002).</p>
<p>The procedure can be divided into two steps. At first, I use the gplot procedure to show the center of circle. The next step I use the annotate system to plot the circles and sectors.</p>
<p>The result is like this.Color encodes the sign of correlation value.Red color is represented for positive value, and blue color is represented for negative value.</p>
<p><a href="http://zhanggw.files.wordpress.com/2010/05/gplot2.gif"><img title="gplot" style="border-right:0;border-top:0;display:block;float:none;margin-left:auto;border-left:0;margin-right:auto;border-bottom:0;" height="240" alt="gplot" src="http://zhanggw.files.wordpress.com/2010/05/gplot_thumb.gif?w=240&#038;h=240" width="240" border="0" /></a> </p>
<p>SAS program is as follows.</p>
<p>%macro&#160;&#160;&#160;&#160; plotgraph(corrdata);   <br />%let radius=8; </p>
<p>data corrmatrix(drop= _TYPE_ _NAME_);   <br />&#160;&#160;&#160; set &amp;corrdata(firstobs=4);    <br />run; </p>
<p>proc sql noprint;   <br />select count(name) into :nvars    <br />&#160;&#160;&#160; from dictionary.columns    <br />&#160;&#160;&#160; where libname=upcase(&quot;work&quot;) and memname=upcase(&quot;corrmatrix&quot;);    <br />select name into :name1-:name%left(&amp;nvars)    <br />&#160;&#160;&#160;&#160;&#160;&#160; from dictionary.columns    <br />&#160;&#160;&#160; where libname=upcase(&quot;work&quot;) and memname=upcase(&quot;corrmatrix&quot;);    <br />Quit; </p>
<p>data corrdata(keep=corr);   <br />&#160;&#160;&#160; set corrmatrix;    <br />&#160;&#160;&#160; %do i=1 %to &amp;nvars;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; corr=&amp;&amp;name&amp;i;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; output;    <br />&#160;&#160;&#160; %end;    <br />run; </p>
<p>%annomac;   <br />data annodata;    <br />&#160;&#160;&#160; set corrdata;    <br />&#160;&#160;&#160; retain xsys ysys &#8217;2&#8242; hsys &#8217;3&#8242;;    <br />&#160;&#160;&#160; center_x=mod(_N_-1,&amp;nvars);    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; center_y=&amp;nvars-ceil(_N_/&amp;nvars);    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; if _N_&lt;=&amp;nvars then do;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; function=&#8217;label&#8217;;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; color=&#8217;black&#8217;;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; x=center_x;y=center_y+0.8;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; style=&#8217;SIMPLEX&#8217;;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; size=3;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; rotate=0;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; text=symget(&#8216;name&#8217;||left(_N_));    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; output;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; function=&#8217;label&#8217;;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; color=&#8217;black&#8217;;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; x=-0.8;y=&amp;nvars-_N_;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; style=&#8217;SIMPLEX&#8217;;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; size=3;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; rotate=0;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; text=symget(&#8216;name&#8217;||left(_N_));    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; output;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; end;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; %circle(center_x,center_y,&amp;radius,black);&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; if corr &gt;0 then do;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; x=center_x;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; y=center_y;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; angle=0;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; rotate=corr*360;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; size=&amp;radius;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; color=&#8217;red&#8217;;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; style=&#8217;p3n45&#8242;;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; line=3;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; output;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; end;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; else do;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; x=center_x;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; y=center_y;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; angle=0;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; rotate=abs(corr)*360;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; size=&amp;radius;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; color=&#8217;blue&#8217;;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; style=&#8217;p3n45&#8242;;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; line=3;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; output;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; end;    <br />run; </p>
<p>/*remove duplicates*/   <br />proc sql noprint;    <br />create table plotdata as     <br />&#160;&#160;&#160; select distinct center_x,center_y    <br />&#160;&#160;&#160; from annodata;    <br />quit; </p>
<p>goption reset=all;   <br />goption device=gif hsize=400 pt vsize=400 pt;    <br />symbol1 v=point;    <br />AXIS1&#160; ORDER =(-1 TO &amp;nvars BY 1) origin=(10 pct,10 pct)     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; COLOR=white;/*disappear the axis*/    <br />proc gplot data=plotdata;    <br />plot center_y*center_x/ haxis=axis1 vaxis=axis1    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; nolegend     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; annotate=annodata;    <br />run;    <br />quit;    <br />%mend;    <br />%macro corrgraph(libname,dataset);    <br />proc sql noprint;    <br />&#160;&#160;&#160; select count(name) into :nvars    <br />&#160;&#160;&#160; from dictionary.columns    <br />&#160;&#160;&#160; where libname=upcase(&quot;&amp;libname&quot;) and memname=upcase(&quot;&amp;dataset&quot;);    <br />&#160;&#160;&#160;&#160;&#160;&#160; select name into :varname separated by &#8216; &#8216;    <br />&#160;&#160;&#160; from dictionary.columns    <br />&#160;&#160;&#160; where libname=upcase(&quot;&amp;libname&quot;) and memname=upcase(&quot;&amp;dataset&quot;);&#160;&#160;&#160; <br />quit;    <br />proc corr data=&amp;dataset pearson spearman hoeffding    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; outp=Pearson_data outs=Spearman_data    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; outh=Hoeffding_data;    <br />var &amp;varname;    <br />run;    <br />/*can add other correlation graph*/    <br />%plotgraph(Pearson_data);    <br />/*%plotgraph(Spearman_data);    <br />%plotgraph(Hoeffding_data);*/    <br />%mend;</p>
<p>The example data is listed here.</p>
<p>data fitness;   <br />&#160;&#160; input Age Weight Runtime Oxygen @@;    <br />&#160;&#160; datalines;    <br />57 73.37 12.63 39.407&#160;&#160; 54 79.38 11.17 46.080    <br />52 76.32 9.63&#160; 45.441&#160;&#160; 50 70.87 8.92&#160;&#160;&#160; .    <br />51 67.25 11.08 45.118&#160;&#160; 54 91.63 12.88 39.203    <br />51 73.71 10.47 45.790&#160;&#160; 57 59.08 9.93&#160; 50.545    <br />49 76.32&#160; .&#160;&#160;&#160; 48.673&#160;&#160; 48 61.24 11.5&#160; 47.920    <br />52 82.78 10.5&#160; 47.467&#160;&#160; 44 73.03 10.13 50.541    <br />45 87.66 14.03 37.388&#160;&#160; 45 66.45 11.12 44.754    <br />47 79.15 10.6&#160; 47.273&#160;&#160; 54 83.12 10.33 51.855    <br />49 81.42 8.95&#160; 40.836&#160;&#160; 51 77.91 10.00 46.672    <br />48 91.63 10.25 46.774&#160;&#160; 49 73.37 10.08 50.388    <br />44 89.47 11.37 44.609&#160;&#160; 40 75.07 10.07 45.313    <br />44 85.84 8.65&#160; 54.297&#160;&#160; 42 68.15 8.17&#160; 59.571    <br />38 89.02 9.22&#160; 49.874&#160;&#160; 47 77.45 11.63 44.811    <br />40 75.98 11.95 45.681&#160;&#160; 43 81.19 10.85 49.091    <br />44 81.42 13.08 39.442&#160;&#160; 38 81.87 8.63&#160; 60.055    <br />;    <br />run;</p>
<p>Finally,invoke the macro.</p>
<p> %corrgraph(work,fitness);  </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zhanggw.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zhanggw.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/zhanggw.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/zhanggw.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/zhanggw.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/zhanggw.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/zhanggw.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/zhanggw.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/zhanggw.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/zhanggw.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/zhanggw.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/zhanggw.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/zhanggw.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/zhanggw.wordpress.com/61/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zhanggw.wordpress.com&amp;blog=10167014&amp;post=61&amp;subd=zhanggw&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zhanggw.wordpress.com/2010/05/27/plot-correlation-matrices-in-sas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3c8fa1e1b19522820e87f78d1785d1f8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Stentor</media:title>
		</media:content>

		<media:content url="http://zhanggw.files.wordpress.com/2010/05/gplot_thumb.gif" medium="image">
			<media:title type="html">gplot</media:title>
		</media:content>
	</item>
		<item>
		<title>A Simple Genetic Programming in Python</title>
		<link>http://zhanggw.wordpress.com/2009/11/08/a-simple-genetic-programming-in-python-4/</link>
		<comments>http://zhanggw.wordpress.com/2009/11/08/a-simple-genetic-programming-in-python-4/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 11:04:44 +0000</pubDate>
		<dc:creator>zgw21cnn</dc:creator>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[GeneticAlgorithm]]></category>
		<category><![CDATA[Genetic programming]]></category>
		<category><![CDATA[GP]]></category>

		<guid isPermaLink="false">http://zhanggw.wordpress.com/2009/11/08/a-simple-genetic-programming-in-python-4/</guid>
		<description><![CDATA[You can download a free guide to genetic programming here.http://www.gp-field-guide.org.uk/ This example aims to reconstruct a simple mathematic function,which can been refined in def examplefun(x, y). I have given x * x + x + 2 * y + 1 to it. Nodes in a tree can be classified to three kinds:function,variable and constant, so [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zhanggw.wordpress.com&amp;blog=10167014&amp;post=35&amp;subd=zhanggw&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>You can download a free guide to genetic programming here.<a href="http://www.gp-field-guide.org.uk/">http://www.gp-field-guide.org.uk/</a></p>
<p>This example aims to reconstruct a simple mathematic function,which can been refined in <em>def examplefun(x, y).</em> I have given <em>x * x + x + 2 * y + 1 </em>to it.</p>
<p>Nodes in a tree can be classified to three kinds:function,variable and constant, so I have built three wrappers:<em>funwrapper</em>,<em>variable </em>and <em>constant.</em></p>
<p>The fitness function is adding the difference between  result of the tree and  correct result of the  function.Roulette wheel selection is selected as selection method . A final result can be shown like this.</p>
<p><a href="http://zhanggw.files.wordpress.com/2009/11/tree.png"><img style="display:inline;border:0;" title="tree" src="http://zhanggw.files.wordpress.com/2009/11/tree_thumb.png?w=229&#038;h=244" border="0" alt="tree" width="229" height="244" /></a></p>
<p>The code is as follows.</p>
<pre>'''
Created on 2009-10-30

@author: Administrator
'''
from random <span style="color:#0000ff;">import</span> random, randint, choice
from <span style="color:#0000ff;">copy</span> <span style="color:#0000ff;">import</span> deepcopy
from PIL <span style="color:#0000ff;">import</span> Image, ImageDraw

<span style="color:#0000ff;">class</span> funwrapper:
  <span style="color:#0000ff;">def</span> __init__(self, <span style="color:#0000ff;">function</span>, childcount, <span style="color:#0000ff;">name</span>):
    self.<span style="color:#0000ff;">function</span> = <span style="color:#0000ff;">function</span>
    self.childcount = childcount
    self.<span style="color:#0000ff;">name</span> = <span style="color:#0000ff;">name</span>

<span style="color:#0000ff;">class</span> variable:
  <span style="color:#0000ff;">def</span> __init__(self, var, value=0):
    self.var = var
    self.value = value
    self.<span style="color:#0000ff;">name</span> = str(var)
    self.type = "<span style="color:#8b0000;">variable</span>"  

  <span style="color:#0000ff;">def</span> evaluate(self):
    <span style="color:#0000ff;">return</span> self.varvalue

  <span style="color:#0000ff;">def</span> setvar(self, value):
    self.value = value

  <span style="color:#0000ff;">def</span> display(self, indent=0):
    <span style="color:#0000ff;">print</span> '%s%s' % (' '*indent, self.var)

<span style="color:#0000ff;">class</span> const:
  <span style="color:#0000ff;">def</span> __init__(self, value):
    self.value = value
    self.<span style="color:#0000ff;">name</span> = str(value)
    self.type = "<span style="color:#8b0000;">constant</span>"   

  <span style="color:#0000ff;">def</span> evaluate(self):
    <span style="color:#0000ff;">return</span> self.value

  <span style="color:#0000ff;">def</span> display(self, indent=0):
    <span style="color:#0000ff;">print</span> '%s%d' % (' '*indent, self.value) 

<span style="color:#0000ff;">class</span> node:
  <span style="color:#0000ff;">def</span> __init__(self, type, children, funwrap, var=None, const=None):
    self.type = type
    self.children = children
    self.funwrap = funwrap
    self.variable = var
    self.const = const
    self.depth = self.refreshdepth()
    self.value = 0
    self.fitness = 0

  <span style="color:#0000ff;">def</span> eval(self):
    <span style="color:#0000ff;">if</span> self.type == "<span style="color:#8b0000;">variable</span>":
      <span style="color:#0000ff;">return</span> self.variable.value
    elif self.type == "<span style="color:#8b0000;">constant</span>":
      <span style="color:#0000ff;">return</span> self.const.value
    <span style="color:#0000ff;">else</span>:
      for c in self.children:
        result = [c.eval() for c in self.children]
      <span style="color:#0000ff;">return</span> self.funwrap.<span style="color:#0000ff;">function</span>(result)  

  <span style="color:#0000ff;">def</span> getfitness(self, checkdata):#checkdata like {"<span style="color:#8b0000;">x</span>":1,"<span style="color:#8b0000;">result</span>":3"<span style="color:#8b0000;">}</span>
    diff = 0
    #set variable value
    for <span style="color:#0000ff;">data</span> in checkdata:
      self.setvariablevalue(<span style="color:#0000ff;">data</span>)
      diff += <span style="color:#0000ff;">abs</span>(self.eval() - <span style="color:#0000ff;">data</span>["<span style="color:#8b0000;">result</span>"])
    self.fitness = diff      

  <span style="color:#0000ff;">def</span> setvariablevalue(self, value):
    <span style="color:#0000ff;">if</span> self.type == "<span style="color:#8b0000;">variable</span>":
      <span style="color:#0000ff;">if</span> value.has_key(self.variable.var):
        self.variable.setvar(value[self.variable.var])
      <span style="color:#0000ff;">else</span>:
        <span style="color:#0000ff;">print</span> "<span style="color:#8b0000;">There is no value for variable:</span>", self.variable.var
        <span style="color:#0000ff;">return</span>
    <span style="color:#0000ff;">if</span> self.type == "<span style="color:#8b0000;">constant</span>":
      pass
    <span style="color:#0000ff;">if</span> self.children:#<span style="color:#0000ff;">function</span> node
      for child in self.children:
        child.setvariablevalue(value)            

  <span style="color:#0000ff;">def</span> refreshdepth(self):
    <span style="color:#0000ff;">if</span> self.type == "<span style="color:#8b0000;">constant</span>" or self.type == "<span style="color:#8b0000;">variable</span>":
      <span style="color:#0000ff;">return</span> 0
    <span style="color:#0000ff;">else</span>:
      depth = []
      for c in self.children:
        depth.append(c.refreshdepth())
      <span style="color:#0000ff;">return</span> max(depth) + 1

  <span style="color:#0000ff;">def</span> __cmp__(self, other):
        <span style="color:#0000ff;">return</span> cmp(self.fitness, other.fitness)  

  <span style="color:#0000ff;">def</span> display(self, indent=0):
    <span style="color:#0000ff;">if</span> self.type == "<span style="color:#8b0000;">function</span>":
      <span style="color:#0000ff;">print</span> ('  '*indent) + self.funwrap.<span style="color:#0000ff;">name</span>
    elif self.type == "<span style="color:#8b0000;">variable</span>":
      <span style="color:#0000ff;">print</span> ('  '*indent) + self.variable.<span style="color:#0000ff;">name</span>
    elif self.type == "<span style="color:#8b0000;">constant</span>":
      <span style="color:#0000ff;">print</span> ('  '*indent) + self.const.<span style="color:#0000ff;">name</span>
    <span style="color:#0000ff;">if</span> self.children:
      for c in self.children:
        c.display(indent + 1)
  ##for draw node
  <span style="color:#0000ff;">def</span> getwidth(self):
    <span style="color:#0000ff;">if</span> self.type == "<span style="color:#8b0000;">variable</span>" or self.type == "<span style="color:#8b0000;">constant</span>":
      <span style="color:#0000ff;">return</span> 1
    <span style="color:#0000ff;">else</span>:
      result = 0
      for i in range(0, len(self.children)):
        result += self.children[i].getwidth()
      <span style="color:#0000ff;">return</span> result
  <span style="color:#0000ff;">def</span> drawnode(self, draw, x, y):
    <span style="color:#0000ff;">if</span> self.type == "<span style="color:#8b0000;">function</span>":
      allwidth = 0
      for c in self.children:
        allwidth += c.getwidth()*100
      left = x - allwidth / 2
      #draw the <span style="color:#0000ff;">function</span> <span style="color:#0000ff;">name</span>
      draw.text((x - 10, y - 10), self.funwrap.<span style="color:#0000ff;">name</span>, (0, 0, 0))
      #draw the children
      for c in self.children:
        wide = c.getwidth()*100
        draw.line((x, y, left + wide / 2, y + 100), fill=(255, 0, 0))
        c.drawnode(draw, left + wide / 2, y + 100)
        left = left + wide
    elif self.type == "<span style="color:#8b0000;">variable</span>":
      draw.text((x - 5 , y), self.variable.<span style="color:#0000ff;">name</span>, (0, 0, 0))
    elif self.type == "<span style="color:#8b0000;">constant</span>":
      draw.text((x - 5 , y), self.const.<span style="color:#0000ff;">name</span>, (0, 0, 0))

  <span style="color:#0000ff;">def</span> drawtree(self, jpeg="<span style="color:#8b0000;">tree.png</span>"):
    w = self.getwidth()*100
    h = self.depth * 100 + 120

    img = Image.<span style="color:#0000ff;">new</span>('RGB', (w, h), (255, 255, 255))
    draw = ImageDraw.Draw(img)
    self.drawnode(draw, w / 2, 20)
    img.save(jpeg, 'PNG')

<span style="color:#0000ff;">class</span> enviroment:
  <span style="color:#0000ff;">def</span> __init__(self, funwraplist, variablelist, constantlist, checkdata, \
               minimaxtype="<span style="color:#8b0000;">min</span>", population=None, size=10, maxdepth=10, \
               maxgen=100, crossrate=0.9, mutationrate=0.1, newbirthrate=0.6):
    self.funwraplist = funwraplist
    self.variablelist = variablelist
    self.constantlist = constantlist
    self.checkdata = checkdata
    self.minimaxtype = minimaxtype
    self.maxdepth = maxdepth
    self.population = population or self._makepopulation(size)
    self.size = size
    self.maxgen = maxgen
    self.crossrate = crossrate
    self.mutationrate = mutationrate
    self.newbirthrate = newbirthrate

    self.besttree = self.population[0]
    for i in range(0, self.size):
      self.population[i].depth=self.population[i].refreshdepth()
      self.population[i].getfitness(checkdata)
      <span style="color:#0000ff;">if</span> self.minimaxtype == "<span style="color:#8b0000;">min</span>":
        <span style="color:#0000ff;">if</span> self.population[i].fitness &lt; self.besttree.fitness:
          self.besttree = self.population[i]
      elif self.minimaxtype == "<span style="color:#8b0000;">max</span>":
        <span style="color:#0000ff;">if</span> self.population[i].fitness &gt; self.besttree.fitness:
          self.besttree = self.population[i]    

  <span style="color:#0000ff;">def</span> _makepopulation(self, popsize):
    <span style="color:#0000ff;">return</span> [self._maketree(0) for i in range(0, popsize)]     

  <span style="color:#0000ff;">def</span> _maketree(self, startdepth):
    <span style="color:#0000ff;">if</span> startdepth == 0:
      #make a <span style="color:#0000ff;">new</span> tree
      nodepattern = 0#<span style="color:#0000ff;">function</span>
    elif startdepth == self.maxdepth:
      nodepattern = 1#variable or constant
    <span style="color:#0000ff;">else</span>:
      nodepattern = randint(0, 1)
    <span style="color:#0000ff;">if</span> nodepattern == 0:
      childlist = []
      selectedfun = randint(0, len(self.funwraplist) - 1)
      for i in range(0, self.funwraplist[selectedfun].childcount):
        child = self._maketree(startdepth + 1)
        childlist.append(child)
      <span style="color:#0000ff;">return</span> node("<span style="color:#8b0000;">function</span>", childlist, self.funwraplist[selectedfun])
    <span style="color:#0000ff;">else</span>:
      <span style="color:#0000ff;">if</span> randint(0, 1) == 0:#variable
        selectedvariable = randint(0, len(self.variablelist) - 1)
        <span style="color:#0000ff;">return</span> node("<span style="color:#8b0000;">variable</span>", None, None, \
               variable(self.variablelist[selectedvariable]), None)
      <span style="color:#0000ff;">else</span>:
        selectedconstant = randint(0, len(self.constantlist) - 1)
        <span style="color:#0000ff;">return</span> node("<span style="color:#8b0000;">constant</span>", None, None, None,\
               const(self.constantlist[selectedconstant]))

  <span style="color:#0000ff;">def</span> mutate(self, tree, probchange=0.1, startdepth=0):
    <span style="color:#0000ff;">if</span> random() &lt; probchange:
      <span style="color:#0000ff;">return</span> self._maketree(startdepth)
    <span style="color:#0000ff;">else</span>:
      result = deepcopy(tree)
      <span style="color:#0000ff;">if</span> result.type == "<span style="color:#8b0000;">function</span>":
        result.children = [self.mutate(c, probchange, startdepth + 1) \
                           for c in tree.children]
    <span style="color:#0000ff;">return</span> result

  <span style="color:#0000ff;">def</span> crossover(self, tree1, tree2, probswap=0.8, top=1):
    <span style="color:#0000ff;">if</span> random() &lt; probswap and not top:
      <span style="color:#0000ff;">return</span> deepcopy(tree2)
    <span style="color:#0000ff;">else</span>:
      result = deepcopy(tree1)
      <span style="color:#0000ff;">if</span> tree1.type == "<span style="color:#8b0000;">function</span>" and tree2.type == "<span style="color:#8b0000;">function</span>":
        result.children = [self.crossover(c, choice(tree2.children), \
                           probswap, 0) for c in tree1.children]
    <span style="color:#0000ff;">return</span> result

  <span style="color:#0000ff;">def</span> envolve(self, maxgen=100, crossrate=0.9, mutationrate=0.1):
    for i in range(0, maxgen):
      <span style="color:#0000ff;">print</span> "<span style="color:#8b0000;">generation no.</span>", i
      child = []
      for j in range(0, int(self.size * self.newbirthrate / 2)):
        parent1, p1 = self.roulettewheelsel()
        parent2, p2 = self.roulettewheelsel()
        newchild = self.crossover(parent1, parent2)
        child.append(newchild)#generate <span style="color:#0000ff;">new</span> tree
        parent, p3 = self.roulettewheelsel()
        newchild = self.mutate(parent, mutationrate)
        child.append(newchild)
      #refresh all tree's fitness
      for j in range(0, int(self.size * self.newbirthrate)):
        replacedtree, replacedindex = self.roulettewheelsel(reverse=True)
        #<span style="color:#0000ff;">replace</span> bad tree with child
        self.population[replacedindex] = child[j]

      for k in range(0, self.size):
        self.population[k].getfitness(self.checkdata)
        self.population[k].depth=self.population[k].refreshdepth()
        <span style="color:#0000ff;">if</span> self.minimaxtype == "<span style="color:#8b0000;">min</span>":
          <span style="color:#0000ff;">if</span> self.population[k].fitness &lt; self.besttree.fitness:
            self.besttree = self.population[k]
        elif self.minimaxtype == "<span style="color:#8b0000;">max</span>":
          <span style="color:#0000ff;">if</span> self.population[k].fitness &gt; self.besttree.fitness:
            self.besttree = self.population[k]
      <span style="color:#0000ff;">print</span> "<span style="color:#8b0000;">best tree's fitbess..</span>",self.besttree.fitness
    self.besttree.display()
    self.besttree.drawtree()  

  <span style="color:#0000ff;">def</span> gettoptree(self, choosebest=0.9, reverse=False):
    <span style="color:#0000ff;">if</span> self.minimaxtype == "<span style="color:#8b0000;">min</span>":
      self.population.sort()
    elif self.minimaxtype == "<span style="color:#8b0000;">max</span>":
      self.population.sort(reverse=True)  

    <span style="color:#0000ff;">if</span> reverse == False:
      <span style="color:#0000ff;">if</span> random() &lt; choosebest:
        i = randint(0, self.size * self.newbirthrate)
        <span style="color:#0000ff;">return</span> self.population[i], i
      <span style="color:#0000ff;">else</span>:
        i = randint(self.size * self.newbirthrate, self.size - 1)
        <span style="color:#0000ff;">return</span> self.population[i], i
    <span style="color:#0000ff;">else</span>:
      <span style="color:#0000ff;">if</span> random() &lt; choosebest:
        i = self.size - randint(0, self.size * self.newbirthrate) - 1
        <span style="color:#0000ff;">return</span> self.population[i], i
      <span style="color:#0000ff;">else</span>:
        i = self.size - randint(self.size * self.newbirthrate,\
            self.size - 1)
        <span style="color:#0000ff;">return</span> self.population[i], i

  <span style="color:#0000ff;">def</span> roulettewheelsel(self, reverse=False):
    <span style="color:#0000ff;">if</span> reverse == False:
      allfitness = 0
      for i in range(0, self.size):
        allfitness += self.population[i].fitness
      randomnum = random()*(self.size - 1)
      check = 0
      for i in range(0, self.size):
        check += (1.0 - self.population[i].fitness / allfitness)
        <span style="color:#0000ff;">if</span> check &gt;= randomnum:
          <span style="color:#0000ff;">return</span> self.population[i], i
    <span style="color:#0000ff;">if</span> reverse == True:
      allfitness = 0
      for i in range(0, self.size):
        allfitness += self.population[i].fitness
      randomnum = random()
      check = 0
      for i in range(0, self.size):
        check += self.population[i].fitness * 1.0 / allfitness
        <span style="color:#0000ff;">if</span> check &gt;= randomnum:
          <span style="color:#0000ff;">return</span> self.population[i], i

  <span style="color:#0000ff;">def</span> listpopulation(self):
    for i in range(0, self.size):
      self.population[i].display()   

#############################################################

<span style="color:#0000ff;">def</span> <span style="color:#0000ff;">add</span>(ValuesList):
    sumtotal = 0
    for val in ValuesList:
      sumtotal = sumtotal + val
    <span style="color:#0000ff;">return</span> sumtotal

<span style="color:#0000ff;">def</span> <span style="color:#0000ff;">sub</span>(ValuesList):
    <span style="color:#0000ff;">return</span> ValuesList[0] - ValuesList[1]

<span style="color:#0000ff;">def</span> <span style="color:#0000ff;">mul</span>(ValuesList):
    <span style="color:#0000ff;">return</span> ValuesList[0] * ValuesList[1]

<span style="color:#0000ff;">def</span> <span style="color:#0000ff;">div</span>(ValuesList):
    <span style="color:#0000ff;">if</span> ValuesList[1] == 0:
        <span style="color:#0000ff;">return</span> 1
    <span style="color:#0000ff;">return</span> ValuesList[0] / ValuesList[1]

addwrapper = funwrapper(<span style="color:#0000ff;">add</span>, 2, "<span style="color:#8b0000;">Add</span>")
subwrapper = funwrapper(<span style="color:#0000ff;">sub</span>, 2, "<span style="color:#8b0000;">Sub</span>")
mulwrapper = funwrapper(<span style="color:#0000ff;">mul</span>, 2, "<span style="color:#8b0000;">Mul</span>")
divwrapper = funwrapper(<span style="color:#0000ff;">div</span>, 2, "<span style="color:#8b0000;">Div</span>")

<span style="color:#0000ff;">def</span> examplefun(x, y):
  <span style="color:#0000ff;">return</span> x * x + x + 2 * y + 1
<span style="color:#0000ff;">def</span> constructcheckdata(<span style="color:#0000ff;">count</span>=10):
  checkdata = []
  for i in range(0, <span style="color:#0000ff;">count</span>):
    dic = {}
    x = randint(0, 10)
    y = randint(0, 10)
    dic['x'] = x
    dic['y'] = y
    dic['result'] = examplefun(x, y)
    checkdata.append(dic)
  <span style="color:#0000ff;">return</span> checkdata

<span style="color:#0000ff;">if</span> __name__ == "<span style="color:#8b0000;">__main__</span>":
  checkdata = constructcheckdata()
  <span style="color:#0000ff;">print</span> checkdata
  env = enviroment([addwrapper, subwrapper, mulwrapper], ["<span style="color:#8b0000;">x</span>", "<span style="color:#8b0000;">y</span>"],
                  [-3, -2, -1, 1, 2, 3], checkdata)
  env.envolve()</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zhanggw.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zhanggw.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/zhanggw.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/zhanggw.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/zhanggw.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/zhanggw.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/zhanggw.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/zhanggw.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/zhanggw.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/zhanggw.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/zhanggw.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/zhanggw.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/zhanggw.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/zhanggw.wordpress.com/35/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zhanggw.wordpress.com&amp;blog=10167014&amp;post=35&amp;subd=zhanggw&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zhanggw.wordpress.com/2009/11/08/a-simple-genetic-programming-in-python-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3c8fa1e1b19522820e87f78d1785d1f8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Stentor</media:title>
		</media:content>

		<media:content url="http://zhanggw.files.wordpress.com/2009/11/tree_thumb.png" medium="image">
			<media:title type="html">tree</media:title>
		</media:content>
	</item>
		<item>
		<title>Resolve TSP (Traveling Saleman Problem) in Genetic Algorithm</title>
		<link>http://zhanggw.wordpress.com/2009/11/07/resolve-tsp-traveling-saleman-problem-in-genetic-algorithm/</link>
		<comments>http://zhanggw.wordpress.com/2009/11/07/resolve-tsp-traveling-saleman-problem-in-genetic-algorithm/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 15:02:54 +0000</pubDate>
		<dc:creator>zgw21cnn</dc:creator>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[GeneticAlgorithm]]></category>
		<category><![CDATA[GA]]></category>
		<category><![CDATA[Genetic algorithm]]></category>
		<category><![CDATA[TSP]]></category>

		<guid isPermaLink="false">http://zhanggw.wordpress.com/2009/11/07/resolve-tsp-traveling-saleman-problem-in-genetic-algorithm/</guid>
		<description><![CDATA[A random route can be shown like this, After iteration,the result can be like this. Method of generation routines is got from here: http://www.psychicorigami.com/2007/04/17/tackling-the-travelling-salesman-problem-part-one/ ''' Created on 2009-10-29 @author: Administrator ''' import sys, random from math import sqrt from pickle import * PIL_SUPPORT = None try: from PIL import Image, ImageDraw, ImageFont PIL_SUPPORT = True [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zhanggw.wordpress.com&amp;blog=10167014&amp;post=29&amp;subd=zhanggw&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A random route can be shown like this,</p>
<p><a href="http://zhanggw.files.wordpress.com/2009/11/tspstart.png"><img style="display:inline;border-width:0;" title="TSPstart" src="http://zhanggw.files.wordpress.com/2009/11/tspstart_thumb.png?w=240&#038;h=190" border="0" alt="TSPstart" width="240" height="190" /></a></p>
<p>After iteration,the result can be like this.</p>
<p><a href="http://zhanggw.files.wordpress.com/2009/11/tspresult.png"><img style="display:inline;border-width:0;" title="TSPresult" src="http://zhanggw.files.wordpress.com/2009/11/tspresult_thumb.png?w=240&#038;h=190" border="0" alt="TSPresult" width="240" height="190" /></a></p>
<p>Method of generation routines is got from here:</p>
<p><a href="http://www.psychicorigami.com/2007/04/17/tackling-the-travelling-salesman-problem-part-one/">http://www.psychicorigami.com/2007/04/17/tackling-the-travelling-salesman-problem-part-one/</a></p>
<pre>'''
Created on 2009-10-29
@author: Administrator
'''
<span style="color:#0000ff;">import</span> <span style="color:#0000ff;">sys</span>, random
from math <span style="color:#0000ff;">import</span> sqrt
from <span style="color:#0000ff;">pickle</span> <span style="color:#0000ff;">import</span> *

PIL_SUPPORT = None
try:
   from PIL <span style="color:#0000ff;">import</span> Image, ImageDraw, ImageFont
   PIL_SUPPORT = True
except:
   PIL_SUPPORT = False 

<span style="color:#0000ff;">def</span> cartesian_matrix(coords):
   """ A distance matrix """
   matrix = {}
   for i, (x1, y1) in enumerate(coords):
      for j, (x2, y2) in enumerate(coords):
         dx, dy = x1 - x2, y1 - y2
         dist = sqrt(dx * dx + dy * dy)
         matrix[i, j] = dist
   <span style="color:#0000ff;">return</span> matrix 

<span style="color:#0000ff;">def</span> tour_length(matrix, tour):
   """ Returns the total length of the tour """
   total = 0
   num_cities = len(tour)
   for i in range(num_cities):
      j = (i + 1) % num_cities
      city_i = tour[i]
      city_j = tour[j]
      total += matrix[city_i, city_j]
   <span style="color:#0000ff;">return</span> total

<span style="color:#0000ff;">def</span> write_tour_to_img(coords, tour, img_file):
   """ The <span style="color:#0000ff;">function</span> to plot the graph """
   padding = 20
   coords = [(x + padding, y + padding) for (x, y) in coords]
   maxx, maxy = 0, 0
   for x, y in coords:
      maxx = max(x, maxx)
      maxy = max(y, maxy)
   maxx += padding
   maxy += padding
   img = Image.<span style="color:#0000ff;">new</span>("<span style="color:#8b0000;">RGB</span>", (int(maxx), int(maxy)),\
         color=(255, 255, 255))
   font = ImageFont.load_default()
   d = ImageDraw.Draw(img);
   num_cities = len(tour)
   for i in range(num_cities):
      j = (i + 1) % num_cities
      city_i = tour[i]
      city_j = tour[j]
      x1, y1 = coords[city_i]
      x2, y2 = coords[city_j]
      d.line((int(x1), int(y1), int(x2), int(y2)), fill=(0, 0, 0))
      d.text((int(x1) + 7, int(y1) - 5), str(i), \
        font=font, fill=(32, 32, 32)) 

   for x, y in coords:
      x, y = int(x), int(y)
      d.ellipse((x - 5, y - 5, x + 5, y + 5), outline=(0, 0, 0),\
                fill=(196, 196, 196))
   del d
   img.save(img_file, "<span style="color:#8b0000;">PNG</span>")
   <span style="color:#0000ff;">print</span> "<span style="color:#8b0000;">The plot was saved into the %s file.</span>" % (img_file,)  

cm = []
coords = [] 

<span style="color:#0000ff;">def</span> eval_func(chromosome):
   """ The evaluation <span style="color:#0000ff;">function</span> """
   global cm
   <span style="color:#0000ff;">return</span> tour_length(cm, chromosome) 

<span style="color:#0000ff;">def</span> cities_random(cities, xmax=800, ymax=600):
   """ get random cities/positions """
   coords = []
   for i in xrange(cities):
      x = random.randint(0, xmax)
      y = random.randint(0, ymax)
      coords.append((float(x), float(y)))
   <span style="color:#0000ff;">return</span> coords

#Individuals
<span style="color:#0000ff;">class</span> Individual:
    score = 0
    length = 30
    seperator = ' '
    <span style="color:#0000ff;">def</span> __init__(self, chromosome=None, length=30):
        self.chromosome = chromosome or self._makechromosome()
        self.length = length
        self.score = 0  # set during evaluation  

    <span style="color:#0000ff;">def</span> _makechromosome(self):
        "<span style="color:#8b0000;">makes a chromosome from randomly selected alleles.</span>"
        chromosome = []
        lst = [i for i in xrange(self.length)]
        for i in xrange(self.length):
            choice = random.choice(lst)
            lst.remove(choice)
            chromosome.append(choice)
        <span style="color:#0000ff;">return</span> chromosome

    <span style="color:#0000ff;">def</span> evaluate(self, optimum=None):
        self.score = eval_func(self.chromosome)

    <span style="color:#0000ff;">def</span> crossover(self, other):
        left, right = self._pickpivots()
        p1 = Individual()
        p2 = Individual()
        c1 = [ c for c in self.chromosome \
               <span style="color:#0000ff;">if</span> c not in other.chromosome[left:right + 1]]
        p1.chromosome = c1[:left] + other.chromosome[left:right + 1]\
                         + c1[left:]
        c2 = [ c for c in other.chromosome <span style="color:#0000ff;">\
               if</span> c not in self.chromosome[left:right + 1]]
        p2.chromosome = c2[:left] + self.chromosome[left:right + 1] \
                        + c2[left:]
        <span style="color:#0000ff;">return</span> p1, p2

    <span style="color:#0000ff;">def</span> mutate(self):
        "<span style="color:#8b0000;">swap two element</span>"
        left, right = self._pickpivots()
        temp = self.chromosome[left]
        self.chromosome[left] = self.chromosome[right]
        self.chromosome[right] = temp   

    <span style="color:#0000ff;">def</span> _pickpivots(self):
        left = random.randint(0, self.length - 2)
        right = random.randint(left, self.length - 1)
        <span style="color:#0000ff;">return</span> left, right    

    <span style="color:#0000ff;">def</span> __repr__(self):
        "<span style="color:#8b0000;">returns string representation of self</span>"
        <span style="color:#0000ff;">return</span> '&lt;%s chromosome="<span style="color:#8b0000;">%s</span>" score=%s&gt;' % \
               (self.__class__.__name__,
                self.seperator.<span style="color:#0000ff;">join</span>(map(str, self.chromosome)), self.score)  

    <span style="color:#0000ff;">def</span> <span style="color:#0000ff;">copy</span>(self):
        twin = self.__class__(self.chromosome[:])
        twin.score = self.score
        <span style="color:#0000ff;">return</span> twin

    <span style="color:#0000ff;">def</span> __cmp__(self, other):
        <span style="color:#0000ff;">return</span> cmp(self.score, other.score)

<span style="color:#0000ff;">class</span> Environment:
    size = 0
    <span style="color:#0000ff;">def</span> __init__(self, population=None, size=100, maxgenerations=1000,\
                 newindividualrate=0.6,crossover_rate=0.90,\
                 mutation_rate=0.1):
        self.size = size
        self.population = self._makepopulation()
        self.maxgenerations = maxgenerations
        self.newindividualrate = newindividualrate
        self.crossover_rate = crossover_rate
        self.mutation_rate = mutation_rate
        for individual in self.population:
            individual.evaluate()
        self.generation = 0
        self.minscore = <span style="color:#0000ff;">sys</span>.<span style="color:#0000ff;">maxint</span>
        self.minindividual = None
        #self._printpopulation()
        <span style="color:#0000ff;">if</span> PIL_SUPPORT:
            write_tour_to_img(coords, self.population[0].chromosome,\
                              "<span style="color:#8b0000;">TSPstart.png</span>")
        <span style="color:#0000ff;">else</span>:
            <span style="color:#0000ff;">print</span> "<span style="color:#8b0000;">No PIL detected,can not plot the graph</span>"

    <span style="color:#0000ff;">def</span> _makepopulation(self):
        <span style="color:#0000ff;">return</span> [Individual() for i in range(0, self.size)]

    <span style="color:#0000ff;">def</span> run(self):
        for i in range(1, self.maxgenerations + 1):
            <span style="color:#0000ff;">print</span> "<span style="color:#8b0000;">Generation no:</span>" + str(i)
            for j in range(0, self.size):
                self.population[j].evaluate()
                curscore = self.population[j].score
                <span style="color:#0000ff;">if</span> curscore &lt; self.minscore:
                    self.minscore = curscore
                    self.minindividual = self.population[j]
            <span style="color:#0000ff;">print</span> "<span style="color:#8b0000;">Best individual:</span>", self.minindividual
            <span style="color:#0000ff;">if</span> random.random() &lt; self.crossover_rate:
                children = []
                newindividual = int(self.newindividualrate * self.size / 2)
                for i in range(0, newindividual):
                    selected1 = self._selectrank()
                    selected2 = self._selectrank()
                    parent1 = self.population[selected1]
                    parent2 = self.population[selected2]
                    child1, child2 = parent1.crossover(parent2)
                    child1.evaluate()
                    child2.evaluate()
                    children.append(child1)
                    children.append(child2)
                for i in range(0, newindividual):
                    #replce with child
                    totalscore = 0
                    for k in range(0, self.size):
                        totalscore += self.population[k].score
                    randscore = random.random()
                    addscore = 0
                    for j in range(0, self.size):
                        addscore += (self.population[j].score / totalscore)
                        <span style="color:#0000ff;">if</span> addscore &gt;= randscore:
                            self.population[j] = children[i]
                            break
            <span style="color:#0000ff;">if</span> random.random() &lt; self.mutation_rate:
                selected = self._select()
                self.population[selected].mutate()
        #<span style="color:#0000ff;">end</span> loop
        for i in range(0, self.size):
                self.population[i].evaluate()
                curscore = self.population[i].score
                <span style="color:#0000ff;">if</span> curscore &lt; self.minscore:
                    self.minscore = curscore
                    self.minindividual = self.population[i]
        <span style="color:#0000ff;">print</span> "<span style="color:#8b0000;">..................Result.........................</span>"
        <span style="color:#0000ff;">print</span> self.minindividual
        #self._printpopulation()

    <span style="color:#0000ff;">def</span> _select(self):
        totalscore = 0
        for i in range(0, self.size):
            totalscore += self.population[i].score
        randscore = random.random()*(self.size - 1)
        addscore = 0
        selected = 0
        for i in range(0, self.size):
            addscore += (1 - self.population[i].score / totalscore)
            <span style="color:#0000ff;">if</span> addscore &gt;= randscore:
                selected = i
                break
        <span style="color:#0000ff;">return</span> selected

    <span style="color:#0000ff;">def</span> _selectrank(self, choosebest=0.9):
        self.population.sort()
        <span style="color:#0000ff;">if</span> random.random() &lt; choosebest:
            <span style="color:#0000ff;">return</span> random.randint(0, self.size * self.newindividualrate)
        <span style="color:#0000ff;">else</span>:
            <span style="color:#0000ff;">return</span> random.randint(self.size * self.newindividualrate,\
                   self.size - 1)

    <span style="color:#0000ff;">def</span> _printpopulation(self):
        for i in range(0, self.size):
            <span style="color:#0000ff;">print</span> "<span style="color:#8b0000;">Individual </span>", i, self.population[i]      

<span style="color:#0000ff;">def</span> main_run():
    global cm, coords
    #get cities's coords
    coords =cities_random(30)
    cm = cartesian_matrix(coords)
    ev = Environment()
    ev.run()
    <span style="color:#0000ff;">if</span> PIL_SUPPORT:
        write_tour_to_img(coords, ev.minindividual.chromosome, \
                          "<span style="color:#8b0000;">TSPresult.png</span>")
    <span style="color:#0000ff;">else</span>:
        <span style="color:#0000ff;">print</span> "<span style="color:#8b0000;">No PIL detected,can not plot the graph</span>"
<span style="color:#0000ff;">if</span> __name__ == "<span style="color:#8b0000;">__main__</span>":
    main_run()</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zhanggw.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zhanggw.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/zhanggw.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/zhanggw.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/zhanggw.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/zhanggw.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/zhanggw.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/zhanggw.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/zhanggw.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/zhanggw.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/zhanggw.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/zhanggw.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/zhanggw.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/zhanggw.wordpress.com/29/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zhanggw.wordpress.com&amp;blog=10167014&amp;post=29&amp;subd=zhanggw&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zhanggw.wordpress.com/2009/11/07/resolve-tsp-traveling-saleman-problem-in-genetic-algorithm/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3c8fa1e1b19522820e87f78d1785d1f8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Stentor</media:title>
		</media:content>

		<media:content url="http://zhanggw.files.wordpress.com/2009/11/tspstart_thumb.png" medium="image">
			<media:title type="html">TSPstart</media:title>
		</media:content>

		<media:content url="http://zhanggw.files.wordpress.com/2009/11/tspresult_thumb.png" medium="image">
			<media:title type="html">TSPresult</media:title>
		</media:content>
	</item>
	</channel>
</rss>
