Robert,<br><br>If you don&#39;t pass the set.seed, R will use a random seed already I believe.&nbsp; The reason I use it with the same seed is that I had a need to be able to run the query multiple times on the same data and be sure I would get the same results.&nbsp; I&#39;ve had great success using this approach with a number of different R functions.
<br><br>I&#39;m glad to see that you are getting some use out of it too.<br><br>David<br><br><div><span class="gmail_quote">On 1/25/07, <b class="gmail_sendername">Robert Burgholzer</b> &lt;<a href="mailto:rburghol@chesapeakebay.net">
rburghol@chesapeakebay.net</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">David/others,<br>I have finally revisited this thread, having forgotten all about it, and
<br>missing the finale. I wish I had realized you solved my problem already<br>(with your array_accum), as I came up with a less elegant solution --<br>after hours of struggle :).<br><br>Anyhow, I have modified your &quot;kmeans&quot; function slightly to make it a bit
<br>more robust (I think), allowing it to use decimals instead of integers,<br>and allowing you to pass in the seed value yourself (assuming that some<br>utility exists in being able to supply a random, rather than static,
<br>seed). That said, I don&#39;t really know if kmeans is supposed to act on<br>non-integer values, but it seems to behave OK.<br><br>CREATE OR REPLACE FUNCTION kmeans(double precision[], int4, int4)<br>&nbsp;&nbsp;RETURNS double precision[] AS&#39;
<br>set.seed(arg3)<br>km=kmeans(sort(arg1),arg2)<br>sort(unlist(tapply(sort(arg1),factor(match(km$cluster,order(km$centers))<br>),range)))<br>&#39; LANGUAGE &#39;plr&#39; VOLATILE STRICT;<br><br>I have posted this code, as well as an implementation of the &quot;quantile&quot;
<br>function (now using your more robust array_accum implementation) at:<br><br><a href="http://soulswimmer.dynalias.net/db/R/r_functions.01.sql">http://soulswimmer.dynalias.net/db/R/r_functions.01.sql</a><br><br>Comments, suggestions, and other R function implementations are most
<br>welcome.<br><br>r.b.<br><br><br>-----Original Message-----<br>From: <a href="mailto:postgis-users-bounces@postgis.refractions.net">postgis-users-bounces@postgis.refractions.net</a><br>[mailto:<a href="mailto:postgis-users-bounces@postgis.refractions.net">
postgis-users-bounces@postgis.refractions.net</a>] On Behalf Of<br>David Bitner<br>Sent: Thursday, March 02, 2006 12:45 PM<br>To: PostGIS Users Discussion<br>Subject: Re: [postgis-users] quantiles, quartiles, or jenks natural
<br><br>By the way, the set.seed call is so I get the same results with<br>subsequent calls on the dataset as I make one call with PHP and<br>PostgreSQL to the database to create my legend with the class<br>intervals and another to divvy up my dataset into predefined class
<br>styles in my mapfile with MapServer and I need subsequent calls to<br>come up with the same results.<br><br>On 3/2/06, David Bitner &lt;<a href="mailto:osgis.lists@gmail.com">osgis.lists@gmail.com</a>&gt; wrote:<br>&gt; The main function that I made is kmeans which takes an array of the
<br>&gt; values that you want to classify and the number of classes that you<br>&gt; want and spits out an array of the break points for the data.<br>&gt;<br>&gt; For example:<br>&gt; select<br>kmeans(array[1,1,1,1,4,3,5,6,45,3,5,7,8,6,4,3,2,1,32,6,7,5,6,7,8],4)
<br>&gt; returns<br>&gt; {1,2,3,4,5,8,32,45}<br>&gt; which can be interpreted as use these classes:<br>&gt; 1-2,3-4,5-8,32-45<br>&gt; which extending to have no gaps would be the same as either<br>&gt; 1-2,3-4,5-31,32-45 or
<br>&gt; 1-2,3-4,5-8,9-45<br>&gt;<br>&gt; I generally just call this using an array_accum aggregate like this:<br>&gt; select kmeans(array_accum(myintegercol),4)&nbsp;&nbsp;from mytable<br>&gt;<br>&gt; As I said before, I kept getting some parse errors that I haven&#39;t had
<br>&gt; time to look into when I tried writing the function to multiple lines,<br>&gt; so the function is all one line.<br>&gt;<br>&gt; CREATE OR REPLACE FUNCTION kmeans(_int8, int4)<br>&gt;&nbsp;&nbsp; RETURNS _int8 AS<br>&gt;<br>
&#39;set.seed(2007);km=kmeans(sort(arg1),arg2);sort(unlist(tapply(sort(arg1)<br>,factor(match(km$cluster<br>&gt; ,order(km$centers))),range)))&#39;<br>&gt;&nbsp;&nbsp; LANGUAGE &#39;plr&#39; VOLATILE STRICT;<br>&gt;<br>&gt; CREATE AGGREGATE array_accum(
<br>&gt;&nbsp;&nbsp; BASETYPE=anyelement,<br>&gt;&nbsp;&nbsp; SFUNC=array_append,<br>&gt;&nbsp;&nbsp; STYPE=anyarray,<br>&gt;&nbsp;&nbsp; INITCOND=&#39;{}&#39;<br>&gt; );<br>&gt;<br>&gt;<br>&gt;<br>&gt; On 3/2/06, Stephen Woodbridge &lt;<a href="mailto:woodbri@swoodbridge.com">
woodbri@swoodbridge.com</a>&gt; wrote:<br>&gt; &gt; David,<br>&gt; &gt;<br>&gt; &gt; Please post it to the listserv, I would be interested also. I have<br>yet<br>&gt; &gt; to jump into PL/R but it is on my list to do.<br>
&gt; &gt;<br>&gt; &gt; Thanks,<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;-Steve<br>&gt; &gt;<br>&gt; &gt; David Bitner wrote:<br>&gt; &gt; &gt; I ended up jumping into the PL/R world and just created an<br>aggregate<br>&gt; &gt; &gt; wrapper around kmeans to get my class values. They ended up being
<br>&gt; &gt; &gt; very, very close (identical in some cases) to classifications that<br>had<br>&gt; &gt; &gt; been done with Jenks Natural Breaks.&nbsp;&nbsp;If you want the same results<br>&gt; &gt; &gt; every time you run a classification on the same data, you need to
<br>set<br>&gt; &gt; &gt; the same seed value for the random number generator before each<br>run.<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; It&#39;s pretty basic and my code is ugly due to some R parser errors<br>that<br>&gt; &gt; &gt; I could only get passed by throwing all the code on one line with
<br>no<br>&gt; &gt; &gt; spaces (hey it worked and I didn&#39;t have time to look into the<br>parser<br>&gt; &gt; &gt; error), but I can throw the code up if anyone would like.<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; On 3/2/06, Robert Burgholzer &lt;
<a href="mailto:rburghol@chesapeakebay.net">rburghol@chesapeakebay.net</a>&gt; wrote:<br>&gt; &gt; &gt;<br>&gt; &gt; &gt;&gt;OK,<br>&gt; &gt; &gt;&gt;I&#39;m coming into this late, but I am a user of PL/R and PostGIS,<br>
and<br>&gt; &gt; &gt;&gt;would appreciate any progress on developing some classification<br>routines<br>&gt; &gt; &gt;&gt;to be posted to this lists, or I would be interested in being<br>notified<br>&gt; &gt; &gt;&gt;offline.
<br>&gt; &gt; &gt;&gt;<br>&gt; &gt; &gt;&gt;Thanks!<br>&gt; &gt; &gt;&gt;<br>&gt; &gt; &gt;&gt;r.b.<br>&gt; &gt; &gt;&gt;<br>&gt; &gt; &gt;&gt;-----Original Message-----<br>&gt; &gt; &gt;&gt;From: <a href="mailto:postgis-users-bounces@postgis.refractions.net">
postgis-users-bounces@postgis.refractions.net</a><br>&gt; &gt; &gt;&gt;[mailto:<a href="mailto:postgis-users-bounces@postgis.refractions.net">postgis-users-bounces@postgis.refractions.net</a>] On Behalf<br>Of Amit<br>&gt; &gt; &gt;&gt;Kulkarni
<br>&gt; &gt; &gt;&gt;Sent: Wednesday, March 01, 2006 1:20 PM<br>&gt; &gt; &gt;&gt;To: <a href="mailto:postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net</a><br>&gt; &gt; &gt;&gt;Subject: Re: [postgis-users] quantiles, quartiles, or jenks
<br>natural<br>&gt; &gt; &gt;&gt;<br>&gt; &gt; &gt;&gt;Sorry, I have been catching up on the past few months emails. I<br>just<br>&gt; &gt; &gt;&gt;want to add that I read that quantiles and minimum boundary error<br>are<br>
&gt; &gt; &gt;&gt;better than jenks. Also minimum boundary error takes into account<br>the<br>&gt; &gt; &gt;&gt;underlying topology.<br>&gt; &gt; &gt;&gt;<br>&gt; &gt; &gt;&gt;The two being better are mentioned in<br>&gt; &gt; &gt;&gt;
<br>&gt; &gt; &gt;&gt;Brewer, Cynthia A. &amp; Pickle, Linda (2002) Evaluation of Methods<br>for<br>&gt; &gt; &gt;&gt;Classifying Epidemiological Data on Choropleth Maps in Series.<br>&gt; &gt; &gt;&gt;Annals of the Association of American Geographers 92 (4), 662-681
<br>&gt; &gt; &gt;&gt;<br>&gt; &gt; &gt;&gt;And the minimum boundary algorithm is supposedly mentioned in<br>&gt; &gt; &gt;&gt;<br>&gt; &gt; &gt;&gt;Cromley, E. K. , and R. G. Cromley. 1996. An analysis of<br>alternative<br>
&gt; &gt; &gt;&gt;classification&nbsp;&nbsp;schemes&nbsp;&nbsp;for&nbsp;&nbsp;medical&nbsp;&nbsp;atlas mapping. European<br>Journal<br>&gt; &gt; &gt;&gt;of Cancer 32A (9): 1551 -- 59.<br>&gt; &gt; &gt;&gt;<br>&gt; &gt; &gt;&gt;Cromley, R. G. , and R. D. Mrozinski. 1999. The classification of
<br>&gt; &gt; &gt;&gt;ordinal data for choropleth mapping. The Cartographic Journal 36<br>(2):<br>&gt; &gt; &gt;&gt;101 -- 9.<br>&gt; &gt; &gt;&gt;<br>&gt; &gt; &gt;&gt;HTH,<br>&gt; &gt; &gt;&gt;amit<br>&gt; &gt; &gt;&gt;
<br>&gt; &gt; &gt;&gt;<br>&gt; &gt; &gt;&gt;Date: Tue, 14 Feb 2006 12:38:39 -0800<br>&gt; &gt; &gt;&gt;From: Paul Ramsey &lt;<a href="mailto:pramsey@refractions.net">pramsey@refractions.net</a>&gt;<br>&gt; &gt; &gt;&gt;<br>
&gt; &gt; &gt;&gt;I did some in PHP, but the algorithms are relatively braindead,<br>the<br>&gt; &gt; &gt;&gt;quantile stuff in particular.&nbsp;&nbsp;Jenks I did some research on but<br>never<br>&gt; &gt; &gt;&gt;really found a definitive description of the process.&nbsp;&nbsp;Some of the
<br>&gt; &gt; &gt;&gt;descriptions ended up sounding like a k-means clustering idea,<br>which<br>&gt; &gt; &gt;&gt;is not cheap!<br>&gt; &gt; &gt;&gt;<br>&gt; &gt; &gt;&gt;P.<br>&gt; &gt; &gt;&gt;<br>&gt; &gt; &gt;&gt;__________________________________________________
<br>&gt; &gt; &gt;&gt;Do You Yahoo!?<br>&gt; &gt; &gt;&gt;Tired of spam?&nbsp;&nbsp;Yahoo! Mail has the best spam protection around<br>&gt; &gt; &gt;&gt;<a href="http://mail.yahoo.com">http://mail.yahoo.com</a><br>&gt; &gt; &gt;&gt;_______________________________________________
<br>&gt; &gt; &gt;&gt;postgis-users mailing list<br>&gt; &gt; &gt;&gt;<a href="mailto:postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net</a><br>&gt; &gt; &gt;&gt;<a href="http://postgis.refractions.net/mailman/listinfo/postgis-users">
http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br>&gt; &gt; &gt;&gt;_______________________________________________<br>&gt; &gt; &gt;&gt;postgis-users mailing list<br>&gt; &gt; &gt;&gt;<a href="mailto:postgis-users@postgis.refractions.net">
postgis-users@postgis.refractions.net</a><br>&gt; &gt; &gt;&gt;<a href="http://postgis.refractions.net/mailman/listinfo/postgis-users">http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br>&gt; &gt; &gt;&gt;
<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; _______________________________________________<br>&gt; &gt; &gt; postgis-users mailing list<br>&gt; &gt; &gt; <a href="mailto:postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net
</a><br>&gt; &gt; &gt; <a href="http://postgis.refractions.net/mailman/listinfo/postgis-users">http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br>&gt; &gt; &gt;<br>&gt; &gt;<br>&gt; &gt; _______________________________________________
<br>&gt; &gt; postgis-users mailing list<br>&gt; &gt; <a href="mailto:postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net</a><br>&gt; &gt; <a href="http://postgis.refractions.net/mailman/listinfo/postgis-users">
http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br>&gt; &gt;<br>&gt;<br>_______________________________________________<br>postgis-users mailing list<br><a href="mailto:postgis-users@postgis.refractions.net">
postgis-users@postgis.refractions.net</a><br><a href="http://postgis.refractions.net/mailman/listinfo/postgis-users">http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br>_______________________________________________
<br>postgis-users mailing list<br><a href="mailto:postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net</a><br><a href="http://postgis.refractions.net/mailman/listinfo/postgis-users">http://postgis.refractions.net/mailman/listinfo/postgis-users
</a><br></blockquote></div><br><br clear="all"><br>-- <br>************************************<br>David William Bitner