/**** Calculates Logit of probability of cancer ****/ -- Cancer is the treatment. It is also response/dependent variable in analysis. -- Death is the outcome and this routine does not examine it. DROP TABLE #Prob SELECT SUM(CAST(Cancer as Float))/Cast(Count(distinct [column 0]) as float) AS Prob , count(distinct [column 0]) as n ,[I305 1] ,[I309 81] ,[I311 ] ,[IE849 7] ,[I150 9] ,[I276 1] ,[I276 8] ,[I530 81] ,[I263 9] ,[I276 51] ,[IV15 82] ,[I511 9] ,[I401 9] ,[I787 20] ,[I564 00] ,[I272 4] ,[I280 9] ,[I285 9] ,[I496 ] ,[I458 9] ,[I486 ] ,[IV58 61] ,[I197 7] ,[I578 9] ,[I584 9] ,[IV66 7] ,[I244 9] ,[I414 01] ,[I599 0] ,[I414 00] ,[I585 9] ,[I600 00] ,[I428 0] ,[I427 31] ,[I403 90] INTO #PROB FROM [Cancer].[dbo].[StomachCancer] GROUP BY [I305 1] ,[I309 81] ,[I311 ] ,[IE849 7] ,[I150 9] ,[I276 1] ,[I276 8] ,[I530 81] ,[I263 9] ,[I276 51] ,[IV15 82] ,[I511 9] ,[I401 9] ,[I787 20] ,[I564 00] ,[I272 4] ,[I280 9] ,[I285 9] ,[I496 ] ,[I458 9] ,[I486 ] ,[IV58 61] ,[I197 7] ,[I578 9] ,[I584 9] ,[IV66 7] ,[I244 9] ,[I414 01] ,[I599 0] ,[I414 00] ,[I585 9] ,[I600 00] ,[I428 0] ,[I427 31] ,[I403 90] HAVING Count(distinct [column 0])>9 -- Next we calculate the logit -- Logit cannot be calculated for probability 0 or 1. -- We want the approximation of logit to show how confident we are about these numbers -- We want the estimates to be in order of confidence, de facto in order of sample size SELECT CASE WHEN Prob=0 THEN log(1/(cast(n as FLOAT)+1.) ) WHEN Prob=1 Then Log(Cast(n as FLOAT)+1.) ELSE log(Prob/(1-prob)) END AS Logit , * -- gets the remaining values FROM #Prob