Published
Edited
Apr 14, 2018
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
sample_dataset_emails_per_day
Insert cell
Insert cell
Insert cell
function sampleMean( x ) {
var sum = 0;
for ( var i = 0; i < x.length; i++ ) {
sum += x[ i ];
}
return sum/x.length;
}
Insert cell
function e3_sample( x, n ) {
var out = new Array( n );
var idx;
for ( var i = 0; i < n; i++ ) {
idx = stdlib.base.random.discreteUniform( 0, x.length-1 );
out[ i ] = x[ idx ][ 2 ];
}
return out;
}
Insert cell
sampleMean( e3_sample( sample_dataset_emails_per_day.slice( 1 ), 10 ) )
Insert cell
Insert cell
{
// Pick a random starting index and take 10 consecutive entries...
var idx = stdlib.base.random.discreteUniform( 0, sample_dataset_emails_per_day.length-11 );
return sampleMean( stdlib.pluck( sample_dataset_emails_per_day.slice( idx, idx+10 ), 2 ) );
}
Insert cell
Insert cell
Insert cell
e5_x = {
// Return a set of sample sizes...
return stdlib.linspace( 1, 1000, 1000 );
}
Insert cell
e5_samples = {
// For each sample size, generate a new random sample...
var out = new Array( e5_x.length );
var d = sample_dataset_emails_per_day.slice( 1 ) ;
for ( var i = 0; i < out.length; i++ ) {
out[ i ] = e3_sample( d, e5_x[i] );
}
return out;
}
Insert cell
e5_means = {
// For each sample, compute the sample mean...
var out = new Array( e5_samples.length );
for ( var i = 0; i < out.length; i++ ) {
out[ i ] = sampleMean( e5_samples[ i ] );
}
return out;
}
Insert cell
plt5 = stdlib.plot({
'x': [ e5_x ], // e.g., 'x': [ n ]
'y': [ e5_means ], // e.g., 'y': [ mu ]
'xLabel': 'n',
'yLabel': 'sample mean',
'yMin': 0.0,
'width': 600
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
e6_x = {
// Generate a linearly-spaced array whose elements correspond to the "indices" of the simulated numbers...
return stdlib.linspace( 1, 10000, 10000 );
}
Insert cell
e6_y = {
// For each element in `x`, generate a normallyd-distributed pseudorandom number:
return stdlib.inmap( new Array( e6_x.length ), stdlib.base.random.normal.factory( 10.0, 3.0 ) );
}
Insert cell
plt6 = stdlib.plot({
'x': [ e6_x ], // e.g., 'x': [ stdlib.linspace(1,10000,10000) ]
'y': [ e6_y ], // e.g., 'y': [ rvs ]
'xLabel': 'x',
'yLabel': 'y',
'yMin': -10.0,
'yMax': 30.0,
'width': 600
})
Insert cell
Insert cell
Insert cell
e7_y = stdlib.inmap( e6_y.slice(), stdlib.incrmean() )
Insert cell
Insert cell
plt8 = stdlib.plot({
'x': [ stdlib.linspace( 1, e7_y.length, e7_y.length ) ], // e.g., 'x': [ n ]
'y': [ e7_y ], // e.g., 'y': [ mu ]
'xLabel': 'n',
'yLabel': 'sample mean',
'width': 600,
'yMin': 9.0,
'yMax': 11.0
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof mu = html`<input type=range min=-10.0 max=10.0 step=0.1 value=0.0>`
Insert cell
viewof sigma = html`<input type=range min=0.1 max=5.0 step=0.1 value=1.0>`
Insert cell
e9_x = stdlib.linspace( -10.0, 10.0, 1000 )
Insert cell
e9_y = stdlib.inmap( e9_x.slice(), stdlib.base.dists.normal.pdf.factory( mu, sigma ) )
Insert cell
plt9 = stdlib.plot({
'x': [ e9_x ], // e.g., 'x': [ x ]
'y': [ e9_y ], // e.g., 'y': [ pdf ]
'xLabel': 'x',
'yLabel': 'pdf',
'yMin': 0.0,
'yMax': 0.5,
'width': 600
})
Insert cell
Insert cell
Insert cell
'<your_code_here>'
Insert cell
Insert cell
viewof nu = html`<input type=range min=0.1 max=10 step=0.1 value=1>`
Insert cell
e11_x = stdlib.linspace( -10.0, 10.0, 500 )
Insert cell
e11_y = stdlib.inmap( e11_x.slice(), stdlib.base.dists.t.pdf.factory( nu ) )
Insert cell
plt11 = stdlib.plot({
'x': [ e11_x ], // e.g., 'x': [ x ]
'y': [ e11_y ], // e.g., 'y': [ pdf ]
'xLabel': 'x',
'yLabel': 'pdf',
'yMin': 0.0,
'yMax': 0.5,
'width': 600
})
Insert cell
Insert cell
Insert cell
'<your_code_here>'
Insert cell
Insert cell
'<your_code_here>'
Insert cell
Insert cell
'<your_code_here>'
Insert cell
Insert cell
'<your_code_here>'
Insert cell
Insert cell
'<your_code_here>'
Insert cell
Insert cell
'<your_code_here>'
Insert cell
Insert cell
'<your_code_here>'
Insert cell
Insert cell
Insert cell
dataset_page1 = [
1, 0, 0, 0, 0, 0, 1, 0, 0, 1,
0, 0, 0, 1, 0, 0, 0, 1, 0, 0,
0, 1, 1, 0, 0, 0, 0, 0, 0, 0
]
Insert cell
dataset_page2 = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0
]
Insert cell
Insert cell
e19_p1 = sampleMean( dataset_page1 )
Insert cell
e19_p2 = sampleMean( dataset_page2 )
Insert cell
Insert cell
Insert cell
stdlib.ttest2( dataset_page1, dataset_page2 ).print()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
stdlib.ttest2( dataset_page1, dataset_page2, {'alternative': 'less'} ).print()
Insert cell
stdlib.ttest2( dataset_page1, dataset_page2, {'alternative': 'greater'} ).print()
Insert cell
Insert cell
Insert cell
viewof p1 = html`<input type=range min=0.0 max=1.0 step=0.01 value=0.5>`
Insert cell
viewof n1 = html`<input type=range min=1 max=1000 step=1 value=10>`
Insert cell
viewof p2 = html`<input type=range min=0.0 max=1.0 step=0.01 value=0.5>`
Insert cell
viewof n2 = html`<input type=range min=1 max=1000 step=1 value=10>`
Insert cell
function randomBinarySequence( n, p ) {
var out = new Array( n );
var i;
for ( i = 0; i < n; i++ ) {
if ( stdlib.base.random.randu() < p ) {
out[ i ] = 1;
} else {
out[ i ] = 0;
}
}
return out;
}
Insert cell
function c3_sort_descending( a, b ) {
if ( a > b ) {
return -1;
}
if ( a === b ) {
return 0;
}
return 1;
}
Insert cell
pltc3 = stdlib.plot({
'x': [ stdlib.linspace( 0, 1, n1 ), stdlib.linspace( 0, 1, n2 ) ], // e.g., 'x': [ stdlib.linspace(0,1,n1), stdlib.linspace(0,1,n2) ]
'y': [ randomBinarySequence( n1, p1 ).sort( c3_sort_descending ), randomBinarySequence( n2, p2 ).sort( c3_sort_descending ) ], // e.g., 'y': [ sorted_dataset1, sorted_dataset2 ]
'xLabel': 'proportion',
'yLabel': 'converted',
'yMin': -0.1,
'yMax': 1.1,
'width': 600
})
Insert cell
Insert cell
Insert cell
c4_p1 = 0.1
Insert cell
c4_p2 = stdlib.linspace( c4_p1+0.1, 1.0, 100 )
Insert cell
c4_ns = {
var maxN = 1000;
var out;
var res;
var n;
var i;
out = new Array( c4_p2.length );
for ( i = 0; i < c4_p2.length; i++ ) {
n = 1;
while ( true ) {
res = stdlib.ttest2( randomBinarySequence( n, c4_p1 ), randomBinarySequence( n, c4_p2[ i ] ) );
if ( res.rejected ) {
out[ i ] = n;
break;
}
n += 1;
if ( n >= maxN ) {
break;
}
}
}
return out;
}
Insert cell
c4_dp = {
var out = new Array( c4_p2.length );
var i;
for ( i = 0; i < out.length; i++ ) {
out[ i ] = c4_p2[ i ] - c4_p1;
}
return out;
}
Insert cell
pltc4 = stdlib.plot({
'x': [ c4_dp ], // e.g., 'x': [ delta_p ]
'y': [ c4_ns ], // e.g., 'y': [ n ]
'xLabel': 'Δp',
'yLabel': 'n',
'xMin': 0.0,
'xMax': 1.0,
'yMin': 0,
'width': 600,
'lineStyle': [ 'none' ],
'symbols': [ 'open-circle' ]
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
'<your_code_here>'
Insert cell
plt22 = stdlib.plot({
'x': [ /* insert your `x` values */ ], // e.g., 'x': [ stdlib.linspace(-10,10,n) ]
'y': [ /* insert your pseudorandom numbers */ ], // e.g., 'y': [ rvs ]
'xLabel': 'x',
'yLabel': 'y',
'width': 600
})
Insert cell
Insert cell
Insert cell
'<your_code_here>'
Insert cell
Insert cell
Insert cell
'<your_code_here>'
Insert cell
pltc5 = stdlib.plot({
'x': [ /* insert your `n` values */ ], // e.g., 'x': [ stdlib.linspace(1,n,n) ]
'y': [ /* insert your proportions */ ], // e.g., 'y': [ y ]
'xLabel': 'n',
'yLabel': 'proportion',
'width': 600,
'yMin': 0.0,
'yMax': 1.1,
'lineStyle': [ 'none' ],
'symbols': [ 'open-circle' ]
})
Insert cell
Insert cell
Insert cell
Insert cell
'<your_code_here>'
Insert cell
Insert cell
Insert cell
'<your_code_here>'
Insert cell
plt25 = stdlib.plot({
'x': [ /* insert your `x` values */ ], // e.g., 'x': [ stdlib.linspace(-10,10,n) ]
'y': [ /* insert your pseudorandom numbers */ ], // e.g., 'y': [ rvs ]
'xLabel': 'x',
'yLabel': 'y',
'width': 600
})
Insert cell
Insert cell
Insert cell
'<your_code_here>'
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
'<your_code_here>'
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
nutrition_data
Insert cell
Insert cell
Insert cell
Insert cell
e29_groups = stdlib.bifurcateBy( nutrition_data, ( v ) => v.diabetes === 'Yes' )
Insert cell
Insert cell
e30_g1 = stdlib.inmap( e29_groups[ 0 ].slice(), ( v ) => parseInt( v.GREENSALADFREQ ) )
Insert cell
e30_g2 = stdlib.inmap( e29_groups[ 1 ].slice(), ( v ) => parseInt( v.GREENSALADFREQ ) )
Insert cell
Insert cell
stdlib.ttest2( e30_g1, e30_g2 ).print()
Insert cell
Insert cell
Insert cell
Insert cell
{
var keys = Object.keys( nutrition_data[ 0 ] ).slice( 27 );
var res;
var out;
var g2;
var g1;
var k;
var i;
out = [];
for ( i = 0; i < keys.length; i++ ) {
k = keys[ i ];
g1 = stdlib.inmap( e29_groups[ 0 ].slice(), ( v ) => parseInt( v[ k ] ) );
g2 = stdlib.inmap( e29_groups[ 1 ].slice(), ( v ) => parseInt( v[ k ] ) );
res = stdlib.ttest2( g1, g2 );
if ( res.rejected ) {
if ( res.xmean < res.ymean ) {
out.push( 'those with diabetes are less likely: '+k );
} else {
out.push( 'those with diabetes are more likely: '+k );
}
}
}
return out;
}
Insert cell
Insert cell
Insert cell
'<your_code_here>'
Insert cell
Insert cell
Insert cell
'<your_code_here>'
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
0.05/1066
Insert cell
Insert cell
Insert cell
'<your_code_here>'
Insert cell
plt34 = stdlib.plot({
'x': [ /* insert your `x` values */ ], // e.g., 'x': [ stdlib.linspace(1,1066,1066), stdlib.linspace(1,1066,1066) ]
'y': [ /* insert your y values and p-values */ ], // e.g., 'y': [ line_with_slope_alpha_over_m, pvals ]
'xLabel': 'x',
'yLabel': 'P_k',
'width': 600
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more