#population
df<-data.frame(x=rnorm(1000),z=rep(0:4,200))
df$y<-with(df, 3+3*x*z)
#sampling fraction
df$p<-with(df, exp(x)/(1+exp(x)))
#sample
xi<-rbinom(1000,1,df$p)
sdf<-df[xi==1,]
#survey design object: independent sampling,
dxi<-svydesign(~0,~p,data=sdf)
dxi
mean(df$x) #right
mean(sdf$x) #wrong
svymean(sdf$x,dxi) #right
var(df$x) #right
var(sdf$x) #wrong
svyvar(sdf$x,dxi) #right
quantile(df$x,c(0.025,0.5,0.975)) #right
quantile(sdf$x,c(0.025,0.5,0.975)) #wrong
svyquantile(sdf$x,design=dxi,c(0.025,0.5,0.975)) #right
table(sdf$z) # sample table
svytable(~z, dxi, round=TRUE) # estimated population table
Run the code above in your browser using DataLab