Skip to content

Instantly share code, notes, and snippets.

@wboykinm
Last active November 13, 2023 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wboykinm/ac1984e0dd34cf8a87dd to your computer and use it in GitHub Desktop.
Save wboykinm/ac1984e0dd34cf8a87dd to your computer and use it in GitHub Desktop.
Sample Size Calculator
// Adapted from CRS: http://www.surveysystem.com/sscalc.htm
conInt = 0;
conLev=1;
zVal=1.96;
zValC=3.8416;
ss=0;
pop=0;
perc=0;
pf = 0;
function to(obj) {
if (obj.box.value == "") {
conInt=0
}
else {
conInt = eval(obj.box.value)
}
if (obj.popbox.value == "") {
pop=0
}
else {
pop = eval(obj.popbox.value)
}
if (conInt == 0) {
alert("You must enter a confidence interval between .1 and 50.")
}
else {
if (pop == 0) {
ss = ((zVal *zVal) * 0.25) / ((conInt / 100) *(conInt / 100))
}
else {
ss = ((zVal *zVal) * 0.25) / ((conInt / 100) *(conInt / 100));
ss=ss/(1+(ss-1)/pop)
}
obj.ssbox.value=parseInt(ss+.5)
}
}
function ConLevButF1(obj){
zVal=1.96
}
function ConLevButF2(obj){
zVal=2.58
}
function ConLevButFC1(obj){
zValC=3.8416 ;
}
function ConLevButFC2(obj){
zValC=6.6564 ;
}
function cler(obj, string) {
obj.box.value = string;
obj.popbox.value = string;
obj.ssbox.value = string;
}
<html>
<head>
<title>Sample Size Calculator</title>
<link href="//maxcdn.bootstrapcdn.com/bootswatch/3.3.0/paper/bootstrap.min.css" rel="stylesheet">
<script src="confidence.js"></script>
</head>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<div class="container">
<h1>Sample Size Calculator</h1>
<form id="calc" name="calc">
<table class="table">
<tr>
<td>Confidence Level:</td>
<td>
<label class="radio-inline"><input checked name="ConLevBut" onclick="ConLevButF1(this.form)" type="radio" value="1"> 95%</label>
<label class="radio-inline"><input name="ConLevBut" onclick="ConLevButF2(this.form)" type="radio" value="2"> 99%</label>
</td>
</tr>
<tr>
<td>Margin of Error:</td>
<td><input name="box" type="text"></td>
</tr>
<tr>
<td>Population:</td>
<td><input name="popbox" onkeyup="this.value=iVal(this.value)" type=
"text"></td>
</tr>
<tr>
<td><input class="btn btn-primary" name="Calculate" onclick="to(this.form)" type="button" value="Calculate"></td>
<td><input class="btn btn-default" name="Clear" onclick="cler(this.form,'')" type="button" value=" Clear "></td>
</tr>
<tr>
<td>Sample size needed:</td>
<td><input name="ssbox" type="text"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment