*~~~~~~~~~~~~~~~~~~~~~.
		*~~~~~~~~~~~~~~~~~~~~~.
		*~~~~~~~~~~~~~~~~~~~~~.
		*   Inference for Proportions.
		*~~~~~~~~~~~~~~~~~~~~~.
		*         20th March 2001         .
		*~~~~~~~~~~~~~~~~~~~~~.
		*~~~~~~~~~~~~~~~~~~~~~.

*see Moore and McCabe (2001) Intro to the Practice of Statistics, chapter 8.


*-------------------------------------------------------------------------------.
*-------------------------------------------------------------------------------.
* Large-Sample Significance Test for a Single Population Proportion.
* (see Moore and McCabe (2001) Intro to the Practice of Statistics, p. 588-591).
*-------------------------------------------------------------------------------.

MATRIX.
COMPUTE n = {4040}. /* Enter the sample size here (i.e. change the number in curly brackets)*/
COMPUTE x = {1992}. /* Enter the number of "successes" here (change the number in curly brackets)*/
COMPUTE p0 = {0.5}. /* Enter the hypothesised value for p (change the number in curly brackets)/*
*The remainder of the syntax calculates the z score and signficance levels given the values 
	for n, x and p0 which you have entered.
*NB you don't need to alter anything from here on.
COMPUTE p = x/n.
COMPUTE SE_p0 = SQRT((p0*(1-p0))/n).
COMPUTE z = (p - p0) /SE_p0.
COMPUTE SIGz_2TL = 2 * (1 - CDFNORM(ABS(Z))).
COMPUTE SIGz_LTL = CDFNORM(Z).
COMPUTE SIGz_UTL = 1 - CDFNORM(Z).
COMPUTE ANSWER = {n, p, SE_p0, z, SIGz_2TL, SIGz_LTL, SIGz_UTL}.
PRINT ANSWER / FORMAT "F10.3" / CLABELS = n, p, SE, z, SIGz_2TL, SIGz_LTL, SIGz_UTL.
END MATRIX.

*NB if you want to obtain values to a greater (lesser) number of decimal places, change the 
	format specified in the last but one line of the syntax.
*e.g. if you want only 3 decimal places, change the format to "F10.3".
*-------------------------------------------------------------------------------.



*-------------------------------------------------------------------------------.
*-------------------------------------------------------------------------------.
* Large-Sample Confidence Interval for a Single Population Proportion.
* (see Moore and McCabe (2001) Intro to the Practice of Statistics, p. 586-588).
*-------------------------------------------------------------------------------.
*For the inverse normal computation, I use the approximation used by 
	http://www.hpmuseum.org/software/67pacs/67ndist.htm adapted from Abramowitz and 
	Stegun, Handbook of Mathematical Functions, National Bureau of Standards 1970. 

MATRIX.
COMPUTE n = {4040}.       /* Enter the sample size here (change the number in curly brackets)*/
COMPUTE x = {2048}.       /* Enter the number of "successes"(change the number in curly brackets)*/
COMPUTE CONFID = {0.99}.  /* Enter the desired confidence level here */
*The remainder of the syntax calculates the Confidence Interval given the values for n and x 
	which you have entered above.
*NB you don't need to alter anything from here on.
COMPUTE Q = 0.5 * (1-CONFID).
COMPUTE A = ln(1/(Q**2)).
COMPUTE T_ = SQRT(A).
COMPUTE zstar = T_ - ((2.515517 + (0.802853*T_)  + (0.010328*T_**2))/
(1 + (1.432788*T_) + (0.189269*T_**2) + (0.001308*T_**3))).
COMPUTE phat = x/n.
COMPUTE SE_phat = SQRT((phat*(1-phat))/n).
COMPUTE m = zstar * SE_phat.
COMPUTE LOWER = phat - m.
COMPUTE UPPER = phat + m.
COMPUTE ANSWER = {n, phat, zstar, SE_phat, Lower, Upper}.
PRINT ANSWER / FORMAT "F10.5" 
	/Title = "Confidence Interval for a Single Population Proportion" 
	/ CLABELS = n, phat, zstar, SE, Lower, Upper.
END MATRIX.

*NB if you want to obtain values to a greater (lesser) number of decimal places, 
	change the format specified in the last but one line of the syntax.
*e.g. if you want only 3 decimal places, change the format to "F10.3".
*------------------------------------------------------------------------------.
*------------------------------------------------------------------------------.


*##############################################################################.


*------------------------------------------------------------------------------.
*------------------------------------------------------------------------------.

* Large-sample significance test for two population proportions.

MATRIX.
COMPUTE n1 = {7180}.  /* Enter the first sample size here (change the number in curly brackets)*/
COMPUTE n2 = {9916}.  /* Enter the second sample size here (change the number in curly brackets)*/
COMPUTE x1 = {1630}.  /* Enter the number of "successes" for sample 1 here (change the nb in curly brackets)*/
COMPUTE x2 = {1684}. /* Enter the number of "successes" for sample 2 here (change the nb in curly brackets)*/
*The remainder of the syntax calculates the z score and signficance levels given the values for n1, 
	n2, x1 and x2 which you have entered.
*NB you don't need to alter anything from here on.
COMPUTE p1 = x1/n1.
COMPUTE p2 = x2/n2.
COMPUTE phat = (x1 + x2) / (n1 + n2).
COMPUTE SE_phat = SQRT(phat * (1 - phat) * ((1/n1) + (1/n2))).
COMPUTE z = (p1 - p2) /SE_phat.
COMPUTE SIGz_2TL = 2 * (1 - CDFNORM(ABS(z))).
COMPUTE SIGz_LTL = CDFNORM(Z).
COMPUTE SIGz_UTL = 1 - CDFNORM(Z).
COMPUTE ANSWER = {p1, p2, SE_phat, z, SIGz_2TL, SIGz_LTL, SIGz_UTL}.
PRINT ANSWER / FORMAT "F10.5" / CLABELS = p1, p2, SE, z, SIGz_2TL, SIGz_LTL, SIGz_UTL.
END MATRIX.
*-------------------------------------------------------------------------------.



*-------------------------------------------------------------------------------.
*-------------------------------------------------------------------------------.
* Large-sample Confidence Intervals for Comparing for two population proportions.
* (see Moore and McCabe (2001) Intro to the Practice of Statistics, p. 602-604).
*-------------------------------------------------------------------------------.
*For the inverse normal computation, I use the approximation used by 
	http://www.hpmuseum.org/software/67pacs/67ndist.htm adapted from Abramowitz and Stegun, 
	Handbook of Mathematical Functions, National Bureau of Standards 1970. 

MATRIX.
COMPUTE n1 = {84}.  /* Enter the first sample size here (change the number in curly brackets)*/
COMPUTE n2 = {106}.  /* Enter the second sample size here (change the number in curly brackets)*/
COMPUTE x1 = {15}.  /* Enter the number of "successes" for sample 1 here (change the nb in curly brackets)*/
COMPUTE x2 = {21}. /* Enter the number of "successes" for sample 2 here (change the nb in curly brackets)*/
COMPUTE CONFID = {0.90}.  /* Enter the desired confidence level here */
*The remainder of the syntax calculates the Confidence Interval given the values for n and 
	x which you have entered above.
*NB you don't need to alter anything from here on.
COMPUTE Q = 0.5 * (1-CONFID).
COMPUTE A = ln(1/(Q**2)).
COMPUTE T_ = SQRT(A).
COMPUTE zstar = T_ - ((2.515517 + (0.802853*T_)  + (0.010328*T_**2))/
 (1 + (1.432788*T_) + (0.189269*T_**2) + (0.001308*T_**3))).
COMPUTE p1hat = x1/n1.
COMPUTE p2hat = x2/n2.
COMPUTE SE_phat = SQRT(((p1hat*(1-p1hat))/n1) + (p2hat*(1-p2hat))/n2)).
COMPUTE m = zstar * SE_phat.
COMPUTE LOWER = (p1hat - p2hat) - m.
COMPUTE UPPER = (p1hat - p2hat) + m.
COMPUTE diffp1p2 = p1hat - p2hat.
COMPUTE ANSWER = {n1, n2, diffp1p2, zstar, SE_phat, Lower, Upper}.
PRINT ANSWER / FORMAT "F10.5" 
	/Title = "Confidence Interval for Comparing 2 Proportions" 
	/ CLABELS = n1, n2, diffp1p2, zstar, SE, Lower, Upper.
END MATRIX.

*NB if you want to obtain values to a greater (lesser) number of decimal places, 
	change the format specified in the last but one line of the syntax.
*e.g. if you want only 3 decimal places, change the format to "F10.3".


*(c) Gwilym Pryce 2002.