# Tests for Linear Trends in Proportions

Sep 1st, 2021
1,592
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # Test for trends in proportions, based on scores
2. score.prop.test <- function(x) {
3.   colsum <- apply(x,2,sum)
4.   rowsum <- apply(x,1,sum)
5.   propcol <- x[1,]/colsum
6.   frate <- rowsum[1]/sum(rowsum)
7.   if(ncol(x)%%2 == 0) {
8.     scores <- seq(2,ncol(x)*2,by=2)-(ncol(x)+1)
9.   } else {
10.     scores <- 1:ncol(x)-ceiling(ncol(x)/2)
11.   }
12.   scoresMean <- sum(colsum*scores)/sum(colsum)
13.   Npxx <- sum(colsum*propcol*(scores-scoresMean))
14.   Nxx2 <- sum(colsum*(scores-scoresMean)^2)
15.   b <- Npxx/Nxx2
16.   Vb <- (frate*(1-frate))/Nxx2
17.   X2 <- (b^2)/Vb
18.   df <- ifelse(ncol(x) <= 2, 1, ncol(x)-2)
19.   pvalue <- 1-pchisq(X2,df)
20.   structure(list("b"=b, "V(b)"=Vb, "X squared"=X2, "df"=df, "p-value"=pvalue))
21. }
22.
23. # TEST
24.
25. # Relationship between nasal carrier rate for Streptococcus pyogenes and size of tonsils.
26. stcarriers <- matrix(c( 19, 29, 24,
27.                        497,560,269),
28.                      ncol=3, byrow=TRUE)
29.
30. score.prop.test(stcarriers)
31.
32. # Armitage, P. (1955). Tests for Linear Trends in Proportions and Frequencies.
33. # Biometrics, Vol. 11, No. 3 (Sep., 1955), pp. 375-386
34. # URL: http://www.jstor.org/stable/3001775