
R version 3.1.0 (2014-04-10) -- "Spring Dance"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin13.1.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(survey)

Attaching package: 'survey'

The following object is masked from 'package:graphics':

    dotchart

> 
> ab<-expand.grid(a=factor(1:4),b=factor(1:3))
> 
> kaltonsample<-ab[rep(1:12,c(20,50,100,30,40,140,50,100,40,310,50,70)),]
> 
> kaltonpop<-ab[rep(1:12,c(80,60,170,55,40,150,60,165,55,340,200,125)),]
> 
> jointpop<-colSums(model.matrix(~a*b,kaltonpop))
> marginalpop<-colSums(model.matrix(~a+b,kaltonpop))
> gregpop<-colSums(model.matrix(~as.numeric(a)+as.numeric(b),kaltonpop))
> 
> dkalton<-svydesign(id=~1,data=kaltonsample)
Warning message:
In svydesign.default(id = ~1, data = kaltonsample) :
  No weights or probabilities supplied, assuming equal probability
> 
> dps<-postStratify(dkalton,~a+b,xtabs(~a+b,kaltonpop))
> 
> drake<-rake(dkalton, list(~a,~b),list(xtabs(~a,kaltonpop),xtabs(~b,kaltonpop)),control=list(epsilon=0.0001))
> 
> dcalps<-calibrate(dkalton, ~a*b, jointpop)
> dcalrake<-calibrate(dkalton,~a+b, marginalpop, calfun="raking")
Loading required package: MASS
> dlinear<-calibrate(dkalton, ~a+b, marginalpop)
> 
> dtrunclinear<-calibrate(dkalton, ~a+b, marginalpop,bounds=c(0.5,2.2))
> 
> dlogit<-calibrate(dkalton, ~a+b, marginalpop,bounds=c(0.5,2.2),calfun="logit")
> 
> dgreg<-calibrate(dkalton,~as.numeric(a)+as.numeric(b), gregpop)
> 
> 
> #table A
>  round(svytable(~a+b,dps)/xtabs(~a+b,kaltonsample),2)
   b
a      1    2    3
  1 4.00 1.00 1.38
  2 1.20 1.07 1.10
  3 1.70 1.20 4.00
  4 1.83 1.65 1.79
>  round(svytable(~a+b,dcalps)/xtabs(~a+b,kaltonsample),2)
   b
a      1    2    3
  1 4.00 1.00 1.37
  2 1.20 1.07 1.10
  3 1.70 1.20 4.00
  4 1.83 1.65 1.79
> 
> #table B
>  round(svytable(~a+b,drake)/xtabs(~a+b,kaltonsample),2)
   b
a      1    2    3
  1 1.81 1.45 2.02
  2 1.08 0.87 1.21
  3 2.20 1.76 2.45
  4 1.83 1.47 2.04
>  round(svytable(~a+b,dcalrake)/xtabs(~a+b,kaltonsample),2)
   b
a      1    2    3
  1 1.81 1.45 2.02
  2 1.08 0.87 1.21
  3 2.20 1.76 2.45
  4 1.83 1.47 2.04
> 
> #table C
> round(svytable(~a+b,dlinear)/xtabs(~a+b,kaltonsample),2)
   b
a      1    2    3
  1 1.82 1.50 1.97
  2 1.09 0.78 1.24
  3 2.19 1.88 2.34
  4 1.83 1.52 1.98
> 
> #table D
> round(svytable(~a+b,dgreg)/xtabs(~a+b,kaltonsample),2)
   b
a      1    2    3
  1 1.21 1.17 1.14
  2 1.43 1.40 1.36
  3 1.66 1.62 1.59
  4 1.88 1.85 1.81
> 
> #table G
> round(svytable(~a+b,dlogit)/xtabs(~a+b,kaltonsample),2)
   b
a      1    2    3
  1 1.87 1.46 1.98
  2 1.08 0.74 1.27
  3 2.17 2.09 2.18
  4 1.89 1.49 1.99
> 
> #table G
> round(svytable(~a+b,dtrunclinear)/xtabs(~a+b,kaltonsample),2)
   b
a      1    2    3
  1 1.81 1.48 1.99
  2 1.08 0.75 1.26
  3 2.20 2.00 2.20
  4 1.83 1.50 2.00
> 
> proc.time()
   user  system elapsed 
  0.248   0.027   0.282 
