Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script src="//cdn.jsdelivr.net/jstat/latest/jstat.min.js" ></script>

<script id='fftable' type='x-template'>
  <div>
	<div  class="col-md-6">
    <p>Enter raw numbers in the following fourfold table:</p>
    <table class="fourfold_table">
      <tr><td rowspan="4"><div style="height: 20px;font-weight: bold; transform: rotate(-90deg);">Outcomes</div></td><th></th><th>Present</th><th>Absent</th><th></th></tr>
      <tr>
        <th>Present</th>
        <td><input type="text" class="form-control" v-model="aa" @input="()  => { this.totalA = +this.aa+ +this.ab }"></td>
        <td><input type="text" class="form-control" v-model="ab" @input="()  => { this.totalA = +this.aa+ +this.ab }"></td>
        <th><input  type="text" class="form-control"  v-model="totalA" @input="()  => { this.ab = +this.totalA- (+this.aa) }"></th>
      </tr>
      <tr>
        <th>Absent</th>
        <td><input type="text" class="form-control" v-model="ba" @input="()  => { this.totalB = +this.ba+ +this.bb }"></td>
        <td><input type="text" class="form-control" v-model="bb" @input="()  => { this.totalB = +this.ba+ +this.bb }"></td>
        <th><input  type="text" class="form-control"  v-model="totalB" @input="()  => { this.bb = +this.totalB- (+this.ba) }"></th>
      </tr>
      <tr>
        <th></th>
        <th>{{ Number(aa) + Number(ba) }}</th>
        <th>{{ Number(bb) + Number(ab) }}</th>
        <th>{{ Number(bb) + Number(ab) +Number(aa) + Number(ba) }}</th>
      </tr>
    </table>
    <hr>
    Null hypothesis to compare odds ratio: <br>
    <div class="input-group col-sm-6" >
      <span class="input-group-addon">w =</span>
      <input id="msg" type="text" class="form-control" name="msg" placeholder="" v-model="w">
    </div>
    <hr>
    <table class="keys_values">
  		<tr v-for="item in describe"><th>{{item.title}}</th><td>{{item.value}}</td></tr>
    </table>
		<hr class="hidden-sm-up">    
  </div>
  <div class="col-md-6">
  	<h3>Bayesian (Beta binomial conj. prior) </h3>
    <table class="keys_values">
    	<tr v-for="item in bayesian"><th>{{item.title}}</th><td>{{item.value}}</td></tr>
    </table>
    <hr>
    <h3>X2 test</h3>
    Tests independence between both properties.
  	<table class="keys_values">
    	<tr v-for="item in chisq_test"><th>{{item.title}}</th><td>{{item.value}}</td></tr>
    </table>
    <hr>
    <h3>Z score test of |p1 - p2|</h3>
    Tests whether probabilities are different.
  	<table class="keys_values">
    	<tr v-for="item in z_test"><th>{{item.title}}</th><td>{{item.value}}</td></tr>
    </table>
    
  </div>
  </div>
</script>

<div class="container">
  <div class="row">  
    <h1>Comparison of two proportions</h1>
    This is a fourfold widget with analytics based on some simple equations found on the book <b>Statistical Methods for Rates and Proportions</b> <i>Fleiss J.L.</i>. Odds ratios X2 test was found in chapter five, while X2 and Z tests were found in chapter 2.
    <hr>
    <div id="output"></div>
  </div>
</div>
              
            
!

CSS

              
                body {
  background: #023;
  color: white;
}
.fourfold_table {
  width: 25em;
}
.fourfold_table th{
  padding: 4px;
  text-align: center;
  min-width: 4em;
}
.fourfold_table input {
  width: 100%;
  margin: 0px;
  margin-right: -3px;
  display: inline-block;
  text-align: center;
}
.keys_values th, .keys_values td {
  padding: 4px;
}
              
            
!

JS

              
                   var app = new Vue({
      // The `el` option can be a node, an ID, or a CSS selector.
      el: '#output',

      // We could pass in a string, but for the sake of convenience
      // we're passing the ID of the <script> tag above.
      template: '#fftable',

      // Here, we're passing in some initial data
     data: { aa:10,bb:135, ab:40, ba:15, w:5,
           totalA: 50, totalB: 150},
     computed: {
       describe: function () {
         aa = Number(this.aa);
         ab = Number(this.ab);
         ba = Number(this.ba);
         bb = Number(this.bb);
         w = this.w;
         var p1 = aa / (aa+ab),
             p2 = ba / (ba+bb),
             p1_2 = p1-p2,
             or = (aa+0.5)*(bb+0.5)/((ab+0.5)*(ba+0.5)),
             or_se = or*Math.sqrt(1.0/(aa+0.5) +
                                  1.0/(ab+0.5) + 1.0/(ba+0.5) +
                                  1.0/(bb+0.5)),
             L_ = Math.log(or),
             X2w_5_47 = Math.pow(L_-Math.log(w), 2)/(or_se*or_se)
         ;
         return [
           {"title": "p1 = ", "value": p1},
           {"title": "p2 = ", "value": p2},
           {"title": "p1-p2 = ", "value": p1_2},
           {"title": "OR (s.e.) = ", "value": or + " ("+or_se+")" },
           {"title": "P(d|H0: OR=w) [Eq. 5.47] = ", "value": window.jStat.chisquare.cdf(1-X2w_5_47, 1) }
         ];
       },
       z_test: function () {
         aa = Number(this.aa);
         ab = Number(this.ab);
         ba = Number(this.ba);
         bb = Number(this.bb);
         w = this.w;
         if (aa>0 && ab>0 && ba>0 && bb>0) {
           var p1 = aa / (aa + ab),
               p2 = ba / (ba + bb),
               n1 = aa + ab,
               n2 = ba + bb,
               n = n1 + n2,
               p_ = (aa + ba) / n;
           var z = (Math.abs(p2-p1)-0.5*(1.0/n1 + 1.0/n2))/Math.sqrt(p_*(1-p_)*(1.0/n1 + 1.0/n2));
           var p = 1 - window.jStat.normal.cdf(z,0,1);
           return [{"title": "p-value", "value": p.toFixed(5)},
                   {"title": "Z score", "value": z.toFixed(5)}];
         }
         return [{"title": "p-value", "value": ""},
                 {"title": "Z score", "value": ""}];
       },
       chisq_test: function () {
         aa = Number(this.aa);
         ab = Number(this.ab);
         ba = Number(this.ba);
         bb = Number(this.bb);
         w = this.w;
         if (aa>0 && ab>0 && ba>0 && bb>0) {
           var p = 0, chisq = 0;
           var n = aa+ab+ba+bb;
           var p1 = (aa+ab)*(aa+ba)/(n*n),
               p2 = (aa+ab)*(ab+bb)/(n*n),
               p3 = (aa+ba)*(bb+ba)/(n*n),
               p4 = (ab+bb)*(bb+ba)/(n*n);
           chisq = n * ( 
             Math.pow(Math.abs(aa/n - p1) - 1.0/(2*n), 2)/p1 + 
             Math.pow(Math.abs(ab/n - p2) - 1.0/(2*n), 2)/p2 + 
             Math.pow(Math.abs(ba/n - p3) - 1.0/(2*n), 2)/p3 + 
             Math.pow(Math.abs(bb/n - p4) - 1.0/(2*n), 2)/p4
           );
           p = 1 - window.jStat.chisquare.cdf(chisq, 1)
           return [{"title": "p-value", "value": p.toFixed(5)},
                   {"title": "X2", "value": chisq.toFixed(5)}];
         }
         return [{"title": "p-value", "value": ""},
                 {"title": "X2", "value": ""}];
       },
       bayesian: function () {
         aa = Number(this.aa);
         ab = Number(this.ab);
         ba = Number(this.ba);
         bb = Number(this.bb);
         return [
           {
             "title": "SD 1",
             "value": betaSD(0.5,2, aa+ab, aa)
           },
           {
             "title": "SD 2",
             "value": betaSD(0.5,2, bb+ba, bb)
           }
         ];
       }
     }
   });

function betaSD(m,n,N,Y){
  a = Y + (n*m) -1;
  b = N - Y + (n*(1-m)) - 1;
  return Math.sqrt((a*b)/(Math.pow((a+b),2)*(a+b+1)))
}
              
            
!
999px

Console