Skip to content

Instantly share code, notes, and snippets.

@richshaw
Last active April 5, 2016 19:00
Show Gist options
  • Save richshaw/1170b4a7c44530013cd998e9066c8901 to your computer and use it in GitHub Desktop.
Save richshaw/1170b4a7c44530013cd998e9066c8901 to your computer and use it in GitHub Desktop.
Confidence Interval of Proportions
<?php
/**
* Function to get confidence interval of proportions in PHP
* Based on https://onlinecourses.science.psu.edu/stat200/node/48
*/
/**
* Calculates CI, defaults to 95% z of 1.96
* for 99% use z of 2.58
*/
function ci($prop, $n, $z = 1.96) {
//Std Error
$se = sqrt($prop * (1 - $prop) / $n);
return [$prop + $z * $se,
$prop - $z * $se];
}
//Test
$ci = ci(0.640,1168);
//Returns [0.676,0.604]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment