Difference between revisions of "Analyze data with the Statistics software R"

From JSXGraph Wiki
Jump to navigationJump to search
 
(85 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This litte application sends the coordinates of the points to the server. There, with the Statistics software R the data is analyzed. The return values are plotted and displayed.
+
<h1>Normal Location and Scale</h1>
<html>
+
This litte application sends the y-coordinates of the points which are normal distributed (pseudo-)random numbers to the server.<br />
<link rel="stylesheet" type="text/css" href="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraph.css" />
+
There, location and scale of the sample are estimated using the [http://www.r-project.org Statistics software R].<br />
<script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/prototype.js"></script>
+
The return values are plotted and displayed.<br /><br />
<script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraphcore.js"></script>
+
<b>The computed estimates are:</b><br />
<div id="jxgbox" class="jxgbox" style="width:700px; height:400px;" onmouseup="doIt();"></div>
+
* mean, standard deviation: red (non-robust!)<br />
</html>
+
* median and MAD: black (most-robust!)<br />  
 +
* radius-minimax estimator: green (optimally robust; cf. Rieder et al. (2008))<br /><br />
 +
By changing the y-position of the four movable points you should recognize the instability (non-robustness) of mean and standard deviation in contrast to the robust estimates; e.g., move one of the four movable points to the top of the plot.<br /><br />
 
===Online results:===
 
===Online results:===
 +
<html><script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/prototype.js"></script></html>
 
<pre id='output'>Statistics:<br></pre>
 
<pre id='output'>Statistics:<br></pre>
<html>
+
<jsxgraph width="700" height="400">
<script language="JavaScript">
+
         brd = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-0.15, 60, 11.15, -20],axis:true});
         brd = JXG.JSXGraph.initBoard('jxgbox', {originX: 20, originY: 300, unitX: 60, unitY: 10});
+
         brd.suspendUpdate();
        b1axisx = brd.createElement('axis', [[0,0], [1,0]], {ticksDelta:5, strokeColor:'#000000'});
+
         var graph1,graph2,graph3,graph4,graph5,graph6,graph7,graph8,graph9;
        b1axisy = brd.createElement('axis', [[0,0], [0,1]], {ticksDelta:5, strokeColor:'#000000'});
 
        b1axisx.ticksDelta = 5;
 
        b1axisy.ticksDelta = 5;
 
         brd.fullUpdate = true;
 
        brd.update();
 
        brd.fullUpdate = false;
 
       
 
         var graph1,graph2,graph3,graph4,graph5,graph6;
 
  
 
         var x = [];
 
         var x = [];
 
         var y = [];
 
         var y = [];
 
         var p = [];
 
         var p = [];
         for (var i=0;i<11;i++) {
+
        var r1,r2,rad,phi,z1,z2;
 +
         for (var i=0;i<6;i++) {
 
             x[i] = i;
 
             x[i] = i;
             y[i] = 10+(Math.random()-0.5)*10;
+
             // Box-Muller-Method for standard normal random numbers
             p[i] = brd.createElement('point', [x[i],y[i]],{name:' ',fixed:true,style:7});  
+
            r1 = Math.random();
 +
            r2 = Math.random();
 +
            rad = Math.sqrt(-2*Math.log(1-r1));
 +
            phi = 2*Math.PI*r2;
 +
            z1 = rad*Math.cos(phi);
 +
            z2 = rad*Math.sin(phi);
 +
            if(i < 5){
 +
                y[i] = 10+z1*3;
 +
                y[6+i] = 10+z2*3;
 +
                x[6+i] = 6+i;
 +
                p[i] = brd.createElement('point', [x[i],y[i]],{name:' ',fixed:true,size:2,face:'[]'});
 +
                p[6+i] = brd.createElement('point', [x[6+i],y[6+i]],{name:' ',fixed:true,size:2,face:'[]'});
 +
             }else{
 +
                y[i] = 10+z1*3;
 +
                p[i] = brd.createElement('point', [x[i],y[i]],{name:' ',fixed:true,size:2,face:'[]'});  
 +
            }
 
         }
 
         }
        var q = [];
 
 
         var hlow = [];
 
         var hlow = [];
 
         var hup = [];
 
         var hup = [];
Line 51: Line 61:
 
         brd.removeObject(p[9]);
 
         brd.removeObject(p[9]);
 
          
 
          
         p[0] = brd.createElement('glider', [x[0],y[0],l[0]],{name:' ',style:6});  
+
         p[0] = brd.createElement('glider', [x[0],y[0],l[0]],{name:' ',size:4,face:'o'});  
         p[10] = brd.createElement('glider', [x[10],y[10],l[1]],{name:' ',style:6});  
+
         p[10] = brd.createElement('glider', [x[10],y[10],l[1]],{name:' ',size:4,face:'o'});  
         p[8] = brd.createElement('glider', [x[8],y[8],l[2]],{name:' ',style:6});  
+
         p[8] = brd.createElement('glider', [x[8],y[8],l[2]],{name:' ',size:4,face:'o'});  
         p[9] = brd.createElement('glider', [x[9],y[9],l[3]],{name:' ',style:6});  
+
         p[9] = brd.createElement('glider', [x[9],y[9],l[3]],{name:' ',size:4,face:'o'});
 +
        brd.unsuspendUpdate();
  
 
         doIt();
 
         doIt();
Line 65: Line 76:
 
                 t += p[i].Y() + ';';
 
                 t += p[i].Y() + ';';
 
             }
 
             }
             new Ajax.Request('/~alfred/jsxgraph/branches/0.70/examples/rserv.php', {
+
             new Ajax.Request('/~alfred/jsxgraph/examples/rserv.php', {
 
                 method:'post',
 
                 method:'post',
 
                 parameters:'input='+escape(t),
 
                 parameters:'input='+escape(t),
Line 78: Line 89:
 
                         var med = a[2]*1.0;
 
                         var med = a[2]*1.0;
 
                         var mad = a[3]*1.0;
 
                         var mad = a[3]*1.0;
                         var mad = a[3]*1.0;
+
                         var est1 = a[4]*1.0;
                        var estimator1 = a[4]*1.0;
+
                         var est2 = a[5]*1.0;
                         var estimator2 = a[5]*1.0;
+
 
 
                         if (!graph2) {  
 
                         if (!graph2) {  
 
                             graph2 = brd.createElement('curve', [[x[0],x[x.length-1]],[m,m]], {strokecolor:'red'});  
 
                             graph2 = brd.createElement('curve', [[x[0],x[x.length-1]],[m,m]], {strokecolor:'red'});  
 
                             graph3 = brd.createElement('curve', [[x[0],x[x.length-1]],[m+sd,m+sd]], {strokecolor:'red',dash:2});  
 
                             graph3 = brd.createElement('curve', [[x[0],x[x.length-1]],[m+sd,m+sd]], {strokecolor:'red',dash:2});  
 
                             graph4 = brd.createElement('curve', [[x[0],x[x.length-1]],[m-sd,m-sd]], {strokecolor:'red',dash:2});  
 
                             graph4 = brd.createElement('curve', [[x[0],x[x.length-1]],[m-sd,m-sd]], {strokecolor:'red',dash:2});  
                             graph5 = brd.createElement('curve', [[x[0],x[x.length-1]],[med,med]], {strokecolor:'gray'});  
+
                             graph5 = brd.createElement('curve', [[x[0],x[x.length-1]],[med,med]], {strokecolor:'black'});  
                             graph1 = brd.createElement('curve', [[x[0],x[x.length-1]],[med-mad,med-mad]], {strokecolor:'gray',dash:3});  
+
                             graph1 = brd.createElement('curve', [[x[0],x[x.length-1]],[med-mad,med-mad]], {strokecolor:'black',dash:3});  
                             graph6 = brd.createElement('curve', [[x[0],x[x.length-1]],[med+mad,med+mad]], {strokecolor:'gray',dash:3});  
+
                             graph6 = brd.createElement('curve', [[x[0],x[x.length-1]],[med+mad,med+mad]], {strokecolor:'black',dash:3});
 +
                            graph8 = brd.createElement('curve', [[x[0],x[x.length-1]],[est1,est1]], {strokecolor:'green'});
 +
                            graph7 = brd.createElement('curve', [[x[0],x[x.length-1]],[est1-est2,est1-est2]], {strokecolor:'green',dash:4});
 +
                            graph9 = brd.createElement('curve', [[x[0],x[x.length-1]],[est1+est2,est1+est2]], {strokecolor:'green',dash:4});  
 
                         } else {
 
                         } else {
 
                             graph2.dataY = [m,m];
 
                             graph2.dataY = [m,m];
Line 95: Line 109:
 
                             graph1.dataY = [med-mad,med-mad];
 
                             graph1.dataY = [med-mad,med-mad];
 
                             graph6.dataY = [med+mad,med+mad];
 
                             graph6.dataY = [med+mad,med+mad];
 +
                            graph8.dataY = [est1,est1];
 +
                            graph7.dataY = [est1-est2,est1-est2];
 +
                            graph9.dataY = [est1+est2,est1+est2];
 
                         }
 
                         }
  
                         $('output').innerHTML = 'Statistics:<br>'+  
+
                         document.getElementById('output').innerHTML = '<b><font size="+1">Normal location and scale:</font></b><br /><br />'+
                                 'Mean value=' + brd.round(m,2) + '<br />' +
+
                                '<b>Estimates for location (true value = 10):</b><br />' +
                                 'sd=' + brd.round(sd,2) + '<br />' +
+
                                 'Mean = ' + Math.round(m,2) + '<br />' +
                                 'median=' + brd.round(med,2) + '<br />' +
+
                                 'Median = ' + Math.round(med,2) + '<br />' +
                                 'MAD=' + brd.round(mad,2) + '<br />' +  
+
                                 'Radius-minimax estimator = ' + Math.round(est1,2) + '<br /><br />' +
                                 'optimal-robust radius-minimax estimator for location=' + brd.round(estimator1,2) + '<br />' +
+
                                '<b>Estimates for scale (true value = 3):</b><br />' +
                                 'optimal-robust radius-minimax estimator for scala=' + brd.round(estimator2,2) + '<br />';
+
                                 'Standard deviation = ' + Math.round(sd,2) + '<br />' +
 +
                                 'MAD = ' + Math.round(mad,2) + '<br />' +  
 +
                                 'Radius-minimax estimator = ' + Math.round(est2,2) + '<br />';
 
                         brd.update();
 
                         brd.update();
 
                     };
 
                     };
 
                 }});
 
                 }});
 
         }
 
         }
</script>
+
brd.addHook(doIt, 'mouseup');
</html>
+
</jsxgraph>
  
=== The JavaScript code of the client ===
+
=== The underlying source code ===
<source lang="javascript">
+
[[The underlying JavaScript and PHP code]]
        brd = JXG.JSXGraph.initBoard('jxgbox', {originX: 20, originY: 300, unitX: 60, unitY: 10});
 
        b1axisx = brd.createElement('axis', [[0,0], [1,0]], {ticksDelta:5, strokeColor:'#000000'});
 
        b1axisy = brd.createElement('axis', [[0,0], [0,1]], {ticksDelta:5, strokeColor:'#000000'});
 
        b1axisx.ticksDelta = 5;
 
        b1axisy.ticksDelta = 5;
 
        brd.fullUpdate = true;
 
        brd.update();
 
        brd.fullUpdate = false;
 
       
 
        var graph1,graph2,graph3,graph4,graph5,graph6;
 
  
        var x = [];
+
The R script can be [http://jsxgraph.uni-bayreuth.de/~alfred/jsxgraph/branches/0.70/examples/LokSkala.R downloaded here].
        var y = [];
 
        var p = [];
 
        for (var i=0;i<11;i++) {
 
            x[i] = i;
 
            y[i] = 10+(Math.random()-0.5)*10;
 
            p[i] = brd.createElement('point', [x[i],y[i]],{name:' ',fixed:true,style:7});
 
        }
 
        var q = [];
 
        var hlow = [];
 
        var hup = [];
 
        var l = [];
 
        hlow[0] = brd.createElement('point', [x[0],0],{name:' ',visible:false});
 
        hlow[1] = brd.createElement('point', [x[10],0],{name:' ',visible:false});
 
        hlow[2] = brd.createElement('point', [x[8],0],{name:' ',visible:false});
 
        hlow[3] = brd.createElement('point', [x[9],0],{name:' ',visible:false});
 
        hup[0] = brd.createElement('point', [x[0],20],{name:' ',visible:false});
 
        hup[1] = brd.createElement('point', [x[10],20],{name:' ',visible:false});
 
        hup[2] = brd.createElement('point', [x[8],20],{name:' ',visible:false});
 
        hup[3] = brd.createElement('point', [x[9],20],{name:' ',visible:false});
 
        l[0] = brd.createElement('line', [hlow[0],hup[0]],{visible:false});
 
        l[1] = brd.createElement('line', [hlow[1],hup[1]],{visible:false});
 
        l[2] = brd.createElement('line', [hlow[2],hup[2]],{visible:false});
 
        l[3] = brd.createElement('line', [hlow[3],hup[3]],{visible:false});
 
 
 
        brd.removeObject(p[0]);
 
        brd.removeObject(p[10]);
 
        brd.removeObject(p[8]);
 
        brd.removeObject(p[9]);
 
       
 
        p[0] = brd.createElement('glider', [x[0],y[0],l[0]],{name:' ',style:6});
 
        p[10] = brd.createElement('glider', [x[10],y[10],l[1]],{name:' ',style:6});
 
        p[8] = brd.createElement('glider', [x[8],y[8],l[2]],{name:' ',style:6});
 
        p[9] = brd.createElement('glider', [x[9],y[9],l[3]],{name:' ',style:6});
 
 
 
        doIt();
 
       
 
        function doIt() {
 
            brd.update();
 
            // Generate a ";"-separated list
 
            var t = '';
 
            for (var i=0;i<p.length;i++) {
 
                t += p[i].Y() + ';';
 
            }
 
            new Ajax.Request('/~alfred/jsxgraph/branches/0.70/examples/rserv.php', {
 
                method:'post',
 
                parameters:'input='+escape(t),
 
                onComplete: function(transport) {
 
                    if (200 == transport.status) {
 
                        var t = transport.responseText;
 
                        var res = t.match(/"(.*)"/gi);
 
                        res = RegExp.$1;
 
                        var a = res.split(";");
 
                        var m = a[0]*1.0;
 
                        var sd = a[1]*1.0;
 
                        var med = a[2]*1.0;
 
                        var mad = a[3]*1.0;
 
                        var mad = a[3]*1.0;
 
                        var estimator1 = a[4]*1.0;
 
                        var estimator2 = a[5]*1.0;
 
                        if (!graph2) {
 
                            graph2 = brd.createElement('curve', [[x[0],x[x.length-1]],[m,m]], {strokecolor:'red'});
 
                            graph3 = brd.createElement('curve', [[x[0],x[x.length-1]],[m+sd,m+sd]], {strokecolor:'red',dash:2});
 
                            graph4 = brd.createElement('curve', [[x[0],x[x.length-1]],[m-sd,m-sd]], {strokecolor:'red',dash:2});
 
                            graph5 = brd.createElement('curve', [[x[0],x[x.length-1]],[med,med]], {strokecolor:'gray'});
 
                            graph1 = brd.createElement('curve', [[x[0],x[x.length-1]],[med-mad,med-mad]], {strokecolor:'gray',dash:3});
 
                            graph6 = brd.createElement('curve', [[x[0],x[x.length-1]],[med+mad,med+mad]], {strokecolor:'gray',dash:3});
 
                        } else {
 
                            graph2.dataY = [m,m];
 
                            graph3.dataY = [m+sd,m+sd];
 
                            graph4.dataY = [m-sd,m-sd];
 
                            graph5.dataY = [med,med];
 
                            graph1.dataY = [med-mad,med-mad];
 
                            graph6.dataY = [med+mad,med+mad];
 
                        }
 
  
                        $('output').innerHTML = 'Statistics:<br>'+
+
=== References ===
                                'Mean value=' + brd.round(m,2) + '<br />' +
+
* ''The Costs of not Knowing the Radius'', Helmut Rieder, Matthias Kohl and Peter Ruckdeschel, Statistical Methods and Application 2008 Feb; 17(1): p.13-40; cf. also [http://www.stamats.de/RRlong.pdf] for an extended version.
                                'sd=' + brd.round(sd,2) + '<br />' +
+
* ''Robust Asymptotic Statistics'', Helmut Rieder, Springer, 1994.
                                'median=' + brd.round(med,2) + '<br />' +
+
* ''Numerical Contributions to the Asymptotic Theory of Robustness'', Matthias Kohl, PhD-Thesis, University of Bayreuth, 2005; cf. also [http://www.stamats.de/ThesisMKohl.pdf].
                                'MAD=' + brd.round(mad,2) + '<br />' +
 
                                'optimal-robust radius-minimax estimator for location=' + brd.round(estimator1,2) + '<br />' +
 
                                'optimal-robust radius-minimax estimator for scala=' + brd.round(estimator2,2) + '<br />';
 
                        brd.update();
 
                    };
 
                }});
 
        }
 
</source>
 
  
=== The PHP code of the server (rserv.php) ===
+
=== External links ===
<source lang="php">
+
* [http://www.r-project.org The R Project for statistical computing.]
<?php
+
* [https://r-forge.r-project.org/projects/robast/ RobASt project on R-Forge]
$input = $_POST["input"];
+
* [http://en.wikipedia.org/wiki/Robust_statistics Wikipedia on Robust Statistics]
$cmd = "/usr/bin/Rscript LokSkala.R '" . $input ."'";
 
passthru($cmd);
 
?>
 
</source>
 
  
 
[[Category:Examples]]
 
[[Category:Examples]]
 +
[[Category:Statistics]]

Latest revision as of 12:35, 21 February 2013

Normal Location and Scale

This litte application sends the y-coordinates of the points which are normal distributed (pseudo-)random numbers to the server.
There, location and scale of the sample are estimated using the Statistics software R.
The return values are plotted and displayed.

The computed estimates are:

  • mean, standard deviation: red (non-robust!)
  • median and MAD: black (most-robust!)
  • radius-minimax estimator: green (optimally robust; cf. Rieder et al. (2008))

By changing the y-position of the four movable points you should recognize the instability (non-robustness) of mean and standard deviation in contrast to the robust estimates; e.g., move one of the four movable points to the top of the plot.

Online results:

Statistics:<br>

The underlying source code

The underlying JavaScript and PHP code

The R script can be downloaded here.

References

  • The Costs of not Knowing the Radius, Helmut Rieder, Matthias Kohl and Peter Ruckdeschel, Statistical Methods and Application 2008 Feb; 17(1): p.13-40; cf. also [1] for an extended version.
  • Robust Asymptotic Statistics, Helmut Rieder, Springer, 1994.
  • Numerical Contributions to the Asymptotic Theory of Robustness, Matthias Kohl, PhD-Thesis, University of Bayreuth, 2005; cf. also [2].

External links