####### Week 7 itn Swedish diabetes
## Pub Table 2, baseline characteristics

> famdep_2 = matrix(c(.219*4815,.122*4815, .153*28785,.197*28785), nr = 2, dimnames = 
+             list("famsize" = c("none", "four+"),"deprivation" = c("Low", "High")))
> famdep_2
       deprivation
famsize      Low     High
  none  1054.485 4404.105
  four+  587.430 5670.645
> prop.table(round(famdep_2),1)
       deprivation
famsize        Low      High
  none  0.19311103 0.8068890
  four+ 0.09379994 0.9062001
> chisq.test(round(famdep_2))
        Pearson's Chi-squared test with Yates' continuity correction
data:  round(famdep_2)
X-squared = 237.9, df = 1, p-value < 2.2e-16

##### certainly not close to random, could be consequence systematic assignment

##diabetes outcome ("unadjusted")
> didep = matrix(c(4815 - 281,28785 - 2278,281,2278), nr = 2, dimnames = 
+             list("deprivation" = c("Low", "High"), "Diabetes" = c("N", "Y")))
> didep
           Diabetes
deprivation     N    Y
       Low   4534  281
       High 26507 2278
> margin.table(didep)
[1] 33600
> prop.table(didep, 1)
           Diabetes
deprivation         N          Y
       Low  0.9416407 0.05835929
       High 0.9208616 0.07913844
> prop.table(didep, 1)[2,2]/ prop.table(didep, 1)[1,2] 
[1] 1.356055
## relative risk ~35%, about 2 percentage point increase
> .15*(.05836) # 15% increase over advantaged in paper
[1] 0.008754
#############################################################
> install.packages("vcd") ##for odds ratio
> library(vcd)
> or = oddsratio(didep log = F) # in odds metric "unadjusted"
> summary(or)
z test of coefficients:
             Estimate Std. Error z value  Pr(>|z|)    
Low:High/N:Y 1.386655   0.090462  15.329 < 2.2e-16 ***

> confint(or)
                2.5 %   97.5 %
Low:High/N:Y 1.220219 1.575792
#######################################################################################################
# cautionary lesson--did this wrong first two times, cond'l tables are a bitch but 2.2e-16 is as low as it goes
> famdep = matrix(c(.219*10347,.122*11179, .153*10347,.197*11179), nr = 2, dimnames = 
+             list("famsize" = c("none", "four+"),"deprivation" = c("Low", "High")))
> famdep # could round to get perfect counts
       deprivation
famsize      Low     High
  none  2265.993 1583.091
  four+ 1363.838 2202.263

> chisq.test(round(famdep))
        Pearson's Chi-squared test with Yates' continuity correction
data:  round(famdep)
X-squared = 314.2, df = 1, p-value < 2.2e-16

> chisq.test(famdep)
        Pearson's Chi-squared test with Yates' continuity correction
data:  famdep
X-squared = 314.33, df = 1, p-value < 2.2e-16
