Closed ajdamico closed 8 years ago
Hi, I'm out of town. I'll be back on wednesday. Have a wonderful 2016!
Em 2 de jan de 2016, às 18:19, Anthony Damico notifications@github.com escreveu:
djalma, i made some changes to the main script to get started--
https://githubcom/ajdamico/asdfree/commit/e7d9b6eb5985b5203b7b40d3ccb06142bca2888d
--but the dom table never gets used, and i believe it should be merged onto the pes table before the survey design gets created? that way, it is a rectangular file (like the censo demografico gets dom and pes merged) what merge fields should be used? so that i can merge on the household-level information
using the current version of the import file, the intersecting fields are--
intersect(names(dom),names(pes)) [1] "v0001" "v0024" "upa_pns" "v0006_pns" "upa" "v0028" "v0029" "v00281" "v00291" "v00282" "v00292" "v00283" "v00293" thanks!
— Reply to this email directly or view it on GitHub.
To merge "dom" and "pes" please use the variables:
V0001 (State), V0024 (Stratum), UPA_PNS (PSU) and V0006_PNS (household order number in the PNS).
Note that for people it is needed to work with two different designs:
people with age >18 answering the long questionnaire. It is defined in the the data subset:
!is.na(M001)&M001=="1"
Please define them separately and poststratify each one
In the script I sent before there are examples of estimates defined for the the two cases.
To estimate a household characteristic please use the
design:
dom_des <- subset (pes_des, C001== "01")
where "pes_des" is the design for the whole population of people (2).
This defines the data relative to the head of the household (one for each household), eliminating household data duplication for people living in the same household in the merged file.
On Sun, Jan 3, 2016 at 11:05 AM, Djalma Pessoa pessoad@gmail.com wrote:
Hi, I'm out of town. I'll be back on wednesday. Have a wonderful 2016!
Em 2 de jan de 2016, às 18:19, Anthony Damico notifications@github.com escreveu:
djalma, i made some changes to the main script to get started--
https://githubcom/ajdamico/asdfree/commit/e7d9b6eb5985b5203b7b40d3ccb06142bca2888d
--but the dom table never gets used, and i believe it should be merged onto the pes table before the survey design gets created? that way, it is a rectangular file (like the censo demografico gets dom and pes merged) what merge fields should be used? so that i can merge on the household-level information
using the current version of the import file, the intersecting fields are--
intersect(names(dom),names(pes)) [1] "v0001" "v0024" "upa_pns" "v0006_pns" "upa" "v0028" "v0029" "v00281" "v00291" "v00282" "v00292" "v00283" "v00293"
thanks!
— Reply to this email directly or view it on GitHub https://github.com/ajdamico/asdfree/issues/71.
new script with changes after merging dom and pes.
On Thu, Jan 7, 2016 at 5:12 PM, Djalma Pessoa pessoad@gmail.com wrote:
To merge "dom" and "pes" please use the variables:
V0001 (State), V0024 (Stratum), UPA_PNS (PSU) and V0006_PNS (household order number in the PNS).
Note that for people it is needed to work with two different designs:
people with age >18 answering the long questionnaire. It is defined in the the data subset:
!is.na(M001)&M001=="1"
- For people in the whole population.
Please define them separately and poststratify each one
In the script I sent before there are examples of estimates defined for the the two cases.
To estimate a household characteristic please use the
design:
dom_des <- subset (pes_des, C001== "01")
where "pes_des" is the design for the whole population of people (2).
This defines the data relative to the head of the household (one for each household), eliminating household data duplication for people living in the same household in the merged file.
On Sun, Jan 3, 2016 at 11:05 AM, Djalma Pessoa pessoad@gmail.com wrote:
Hi, I'm out of town. I'll be back on wednesday. Have a wonderful 2016!
Em 2 de jan de 2016, às 18:19, Anthony Damico notifications@github.com escreveu:
djalma, i made some changes to the main script to get started--
https://githubcom/ajdamico/asdfree/commit/e7d9b6eb5985b5203b7b40d3ccb06142bca2888d
--but the dom table never gets used, and i believe it should be merged onto the pes table before the survey design gets created? that way, it is a rectangular file (like the censo demografico gets dom and pes merged) what merge fields should be used? so that i can merge on the household-level information
using the current version of the import file, the intersecting fields are--
intersect(names(dom),names(pes)) [1] "v0001" "v0024" "upa_pns" "v0006_pns" "upa" "v0028" "v0029" "v00281" "v00291" "v00282" "v00292" "v00283" "v00293"
thanks!
— Reply to this email directly or view it on GitHub https://github.com/ajdamico/asdfree/issues/71.
setwd( "C:/Djalma/PNS/" )
load("dom.rda") load("pes.rda")
DomPes <- merge(dom,pes, by.x=c("V0001","V0024", "UPA_PNS", "V0006_PNS"), by.y= c("V0001","V0024", "UPA_PNS", "V0006_PNS") )
save(DomPes,file="DomPes.rda")
##################################################################
############################################################# pes_sel<-subset(DomPes,!is.na(M001)&M001=="1")
pes_sel<-transform(pes_sel,saude.b.mb=ifelse(N001%in%c("1","2"),1,0))
pes_sel<-transform(pes_sel,situ=substring(V0024,7,7)) pes_sel$situ<-factor(pes_sel$situ,labels=c("urbano","rural"))
pes_sel$C006<-factor(pes_sel$C006,labels=c("masculino","feminino"))
estado.names <- c( "Rondônia" , "Acre" , "Amazonas" , "Roraima" , "Pará" , "Amapá" , "Tocantins" , "Maranhão" , "Piauí" , "Ceará" , "Rio Grande do Norte" , "Paraíba" , "Pernambuco" , "Alagoas" , "Sergipe" , "Bahia" , "Minas Gerais" , "Espírito Santo" , "Rio de Janeiro" , "São Paulo" , "Paraná" , "Santa Catarina" , "Rio Grande do Sul" , "Mato Grosso do Sul" , "Mato Grosso" , "Goiás" , "Distrito Federal" ) pes_sel$UF<-factor(pes_sel$V0001,labels=estado.names)
region.names<-c("Norte","Nordeste","Sudeste","Sul","Centro-Oeste") pes_sel$Region<-substring(pes_sel$V0001,1,1) pes_sel$Region<-factor(pes_sel$Region,labels=region.names) table(pes_sel$Region)
pes_sel$P04101<- as.numeric(pes_sel$P04101) pes_sel$P04102<- as.numeric(pes_sel$P04102) pes_sel$P04301<- as.numeric(pes_sel$P04301) pes_sel$P04302<- as.numeric(pes_sel$P04302)
pes_sel<-transform(pes_sel,tempo_desl_trab=ifelse(is.na(P04101),0,P04101 * 60 + P04102)) pes_sel<-transform(pes_sel,tempo_desl_athab =ifelse(is.na(P04301),0,P04301 * 60 + P04302)) pes_sel<-transform(pes_sel,tempo_desl = tempo_desl_trab + tempo_desl_athab) pes_sel<-transform(pes_sel,atfi04=ifelse(tempo_desl >= 30,1,0))
pes_sel$one<-1
library(survey) pes.sel.des<-svydesign(ids=~UPA_PNS,strata=~V0024,data=pes_sel,weights=~V0029.y,nest=TRUE)
post.pop<-unique(subset(pes_sel,select=c(V00293.y,V00292.y))) names(post.pop)<-c("V00293.y","Freq")
pes.sel.des.pos<-postStratify(pes.sel.des,~V00293.y,post.pop)
############################################################
#################################################################
saudbr<-svymean(~saude.b.mb, design=pes.sel.des.pos) c(round(100_coef(saudbr),1),round(100_confint(saudbr)[1,],1))
saudbrsex<-svyby(~saude.b.mb,~C006, design= pes.sel.des.pos, vartype="ci", level = 0.95, svymean) cbind(saudbrsex[,1],round(100*saudbrsex[,2:4],1))
saudsitu<-svyby(~saude.b.mb,~situ, design= pes.sel.des.pos, vartype="ci", level = 0.95, svymean) cbind(saudsitu[,1],round(100*saudsitu[,2:4],1))
saudsitusex<-svyby(~saude.b.mb,~situ+C006, design= pes.sel.des.pos, vartype="ci", level = 0.95, svymean) cbind(saudsitusex[,1:2],round(100*saudsitusex[,3:5],1))
sauduf<-svyby(~saude.b.mb,~UF, design= pes.sel.des.pos, vartype="ci", level = 0.95, svymean) cbind(sauduf[,1],round(100*sauduf[,2:4],1))
saudufsex<-svyby(~saude.b.mb,~UF+C006, design= pes.sel.des.pos, vartype="ci", level = 0.95, svymean) cbind(saudufsex[,1:2],round(100*saudufsex[,3:5],1))
###########################################################################################
####################################################
atfibr<-svymean(~atfi04, design=pes.sel.des.pos) svymean(~atfi04, design=pes.sel.des.pos,vartype="ci", level = 0.95) c(round(100_coef(atfibr),1),round(100_confint(atfibr)[1,],1))
atfisitu<-svyby(~atfi04, ~situ, design= pes.sel.des.pos, vartype="ci", level = 0.95, svymean,na.rm=T) cbind(atfisitu[,1],round(100*atfisitu[,2:4],1))
atfireg<-svyby(~atfi04, ~Region, design= pes.sel.des.pos, vartype="ci", level = 0.95, svymean) cbind(atfireg[,1],round(100*atfireg[,2:4],1))
atfirace<-svyby(~atfi04, ~C009, design= pes.sel.des.pos, vartype="ci", level = 0.95, svymean) cbind(atfirace[-6,1],round(100*atfirace[-6,2:4],1))
atfiuf<-svyby(~atfi04,~UF, design= pes.sel.des.pos, vartype="ci", level = 0.95, svymean) cbind(atfiuf[,1],round(100*atfiuf[,2:4],1))
atfiufsex<-svyby(~atfi04,~UF+C006, design= pes.sel.des.pos, vartype="ci", level = 0.95, svymean) cbind(atfiufsex[,1:2],round(100*atfiufsex[,3:5],1))
###############################################################
############################################################
DomPes$C008<-as.numeric(DomPes$C008) DomPes$age.cat<-cut(DomPes$C008,c(0,18,30,40,60,150),right=FALSE,include.lowest=TRUE)
DomPes$raca<-DomPes$C009 DomPes$raca[DomPes$raca%in%c("3","5","9")]<-NA DomPes$raca<-factor(DomPes$raca,labels=c("Branca","Preta","Parda"))
DomPes$educ<-DomPes$VDD004 DomPes$educ[DomPes$educ==" "]<-NA DomPes$educ[DomPes$educ%in%c("1","2")]<-"1" DomPes$educ[DomPes$educ%in%c("3","4")]<-"2" DomPes$educ[DomPes$educ%in%c("5","6")]<-"3" DomPes$educ<-factor(DomPes$educ,labels=c("SinstFundi","FundcMedi","MedcSupi","Supc"))
DomPes$Region<-substring(DomPes$V0001,1,1) DomPes$Region<-factor(DomPes$Region,labels=region.names)
DomPes$C001<- as.numeric(DomPes$C001)
pes.all.des<-svydesign(ids=~UPA_PNS,strata=~V0024,data=DomPes,weights=~V0028.y,nest=TRUE)
post.pop.all<-subset(DomPes,select=c(V00283.y,V00282.y)) post.pop.all<-unique(post.pop.all) names(post.pop.all)<-c("V00283.y","Freq") pes.all.des.pos<-postStratify(pes.all.des,~V00283.y,post.pop.all)
svymean(~VDD004,pes.all.des.pos)
svymean(~I001,pes.all.des.pos,na.rm=TRUE)
plansex<-svyby(~I001,~C006, design=pes.all.des.pos,vartype="ci", level = 0.95, svymean,na.rm=TRUE) plansex.res<-data.frame(Sex=c("Male","Female"),round(100*plansex[,c(2,4,6)],1))
library(ggplot2) ggplot(plansex.res,aes(x=Sex,y=I0011,fill=Sex))+ geom_bar( stat="identity", width=0.9) + geom_errorbar(aes(ymin=ci_l.I0011, ymax=ci_u.I0011))+ xlab("Sex")+ylab("% Health Insurance")+ ggtitle("Proportion of people having health insurance by sex")+ theme_bw()
planage<-svyby(~I001,~age.cat, design=pes.all.des.pos,vartype="ci", level = 0.95, svymean,na.rm=TRUE) planage.res<-data.frame(age.cat=planage[,1],round(100*planage[,c(2,4,6)],1))
ggplot(planage.res,aes(x=age.cat,y=I0011,fill=age.cat))+ geom_bar( stat="identity") + geom_errorbar(aes(ymin=ci_l.I0011, ymax=ci_u.I0011))+ xlab("Age")+ylab("% Health Insurance")+ ggtitle("Proportion of people having health insurance by age")+ theme_bw()
planeduc<-svyby(~I001,~educ, design=pes.all.des.pos,vartype="ci", level = 0.95, svymean,na.rm=TRUE) planeduc.res<-data.frame(educ=planeduc[,1],round(100*planeduc[,c(2,4,6)],1)) planeduc.res
ggplot(planeduc.res,aes(x=educ,y=I0011,fill=educ))+ geom_bar( stat="identity") + geom_errorbar(aes(ymin=ci_l.I0011, ymax=ci_u.I0011))+ xlab("Education")+ylab("% health insurance")+ ggtitle("Proportion of people having health insurance by education")+ theme_bw()
planraca<-svyby(~I001,~raca, design=pes.all.des.pos,vartype="ci", level = 0.95, svymean,na.rm=TRUE) planraca.res<-data.frame(race=planraca[,1],round(100*planraca[,c(2,4,6)],1)) planraca.res
ggplot(planraca.res,aes(x=race,y=I0011,fill=race))+ geom_bar( stat="identity") + geom_errorbar(aes(ymin=ci_l.I0011, ymax=ci_u.I0011))+ xlab("Education")+ylab("% health insurance")+ ggtitle("Proportion of people having health insurance by race")+ theme_bw()
library(survey)
pes_sel<-subset(pes,!is.na(M001)&M001=="1")
pes.peso0<-subset(pes_sel,is.na(V0029))
pes_sel0<-subset(pes_sel,!is.na(V0029))
pes_sel0<-transform(pes_sel0,saude.b.mb=ifelse(N001%in%c("1","2"),1,0))
pes_sel0<-transform(pes_sel0,situ=substring(V0024,7,7)) pes_sel0$situ<-factor(pes_sel0$situ,labels=c("Urbano","Rural"))
pes_sel0$C006<-factor(pes_sel0$C006,labels=c("Masculino","Feminino"))
estado.names <- c( "Rondônia" , "Acre" , "Amazonas" , "Roraima" , "Pará" , "Amapá" , "Tocantins" , "Maranhão" , "Piauí" , "Ceará" , "Rio Grande do Norte" , "Paraíba" , "Pernambuco" , "Alagoas" , "Sergipe" , "Bahia" , "Minas Gerais" , "Espírito Santo" , "Rio de Janeiro" , "São Paulo" , "Paraná" , "Santa Catarina" , "Rio Grande do Sul" , "Mato Grosso do Sul" , "Mato Grosso" , "Goiás" , "Distrito Federal" ) pes_sel0$V0001<-factor(pes_sel0$V0001,labels=estado.names)
library(survey) pes.sel.des<-svydesign(ids=~UPA_PNS,strata=~V0024,data=pes_sel0,weights=~V0029,nest=TRUE)
post.pop<-unique(subset(pes_sel0,select=c(V00293,V00292))) names(post.pop)<-c("V00293","Freq")
pes.sel.des.pos<-postStratify(pes.sel.des,~V00293,post.pop)
####################Example 1#############################################
tab5.1.1.1.BR<-svymean(~saude.b.mb,pes.sel.des.pos)
round(100_c(coef(tab5.1.1.1.BR),coef(tab5.1.1.1.BR)-2_SE(tab5.1.1.1.BR), coef(tab5.1.1.1.BR)+2*SE(tab5.1.1.1.BR)),1)
tab5111<-function(svyby.obj,nomevar){ LI<-coef(svyby.obj)-2_SE(svyby.obj) LS<-coef(svyby.obj)+2_SE(svyby.obj) result<-data.frame(svyby.obj[,1:length(nomevar)],saude.b.mb=round(100_coef(svyby.obj),1), LI=round(100_LI,1),LS=round(100*LS,1)) names(result)[1:length(nomevar)]<-nomevar rownames(result)<-NULL result }
tab5.1.1.1_BR_SEXO<-svyby(~saude.b.mb,~C006,pes.sel.des.pos,svymean) tab5111(tab5.1.1.1_BR_SEXO,"sexo")
tab5.1.1.1_SITU<-svyby(~saude.b.mb,~situ,pes.sel.des.pos,svymean) tab5111(tab5.1.1.1_SITU,"situação")
tab5.1.1.1_SITU_SEXO<-svyby(~saude.b.mb,~situ+C006,pes.sel.des.pos,svymean) tab5111(tab5.1.1.1_SITU_SEXO,c("situação","sexo"))
tab5.1.1.1_UF<-svyby(~saude.b.mb,~V0001,pes.sel.des.pos,svymean) tab5111(tab5.1.1.1_UF,"UF")
tab5.1.1.1_UF_SEXO<-svyby(~saude.b.mb,~V0001+C006,pes.sel.des.pos,svymean) tab5111(tab5.1.1.1_UF_SEXO,c("UF","sexo")) ###########################End of Example 1 ########################################################3
pes.all.des<-svydesign(ids=~UPA_PNS,strata=~V0024,data=pes,weights=~V0028,nest=TRUE)
post.pop.all<-subset(pes,select=c(V00283,V00282)) post.pop.all<-unique(post.pop.all) names(post.pop.all)<-c("V00283","Freq") pes.all.des.pos<-postStratify(pes.sel.des,~V00283,post.pop.all)
svymean(~VDD004,pes.all.des.pos)
x<-subset(pes.all.des.pos,C008>=5) svymean(~VDD004,x)
dom01<-subset(dom,V0015=="01")
pes_sel<-subset(pes,!is.na(M001)&M001=="1")
pes.peso0<-subset(pes_sel,is.na(V0029))
pes_sel0<-subset(pes_sel,!is.na(V0029))
pes_sel0<-transform(pes_sel0,saude.b.mb=ifelse(N001%in%c("1","2"),1,0))
pes_sel0<-transform(pes_sel0,situ=substring(V0024,7,7))
library(survey) pes.sel.des<-svydesign(ids=~UPA_PNS,strata=~V0024,data=pes_sel0,weights=~V0029,nest=TRUE)
post.pop<-unique(subset(pes_sel0,select=c(V00293,V00292))) names(post.pop)<-c("V00293","Freq")
pes.sel.des.pos<-postStratify(pes.sel.des,~V00293,post.pop)
tab5.1.1.1.BR<-svymean(~saude.b.mb,pes.sel.des.pos) class(tab5.1.1.1.BR) round(100_c(coef(tab5.1.1.1.BR),coef(tab5.1.1.1.BR)-2_SE(tab5.1.1.1.BR), coef(tab5.1.1.1.BR)+2*SE(tab5.1.1.1.BR)),1)
tab5.1.1.1_BR_SEXO<-svyby(~saude.b.mb,~C006,pes.sel.des.pos,svymean) tab5.1.1.1_BR_SEXO<-transform(tab5.1.1.1_BR_SEXO,LI=saude.b.mb-2_se,LS=saude.b.mb+2_se)
cbind(tab5.1.1.1_BR_SEXO[1,1],round(100*tab5.1.1.1_BR_SEXO[1,c(2,4,5)],1))
cbind(tab5.1.1.1_BR_SEXO[2,1],round(100*tab5.1.1.1_BR_SEXO[2,c(2,4,5)],1))
tab5.1.1.1_SITU<-svyby(~saude.b.mb,~situ,pes.sel.des.pos,svymean) tab5.1.1.1_SITU<-transform(tab5.1.1.1_SITU,LI=saude.b.mb-2_se,LS=saude.b.mb+2_se) cbind(tab5.1.1.1_SITU[,1],round(100*tab5.1.1.1_SITU[,c(2,4,5)],1))
tab5.1.1.1_SITU_SEXO<-svyby(~saude.b.mb,~situ+C006,pes.sel.des.pos,svymean)
tab5.1.1.1_SITU_SEXO<-transform(tab5.1.1.1_SITU_SEXO,LI=saude.b.mb-2_se,LS=saude.b.mb+2_se) tab5.1.1.1_SITU_MASC<-subset(tab5.1.1.1_SITU_SEXO,C006==1) tab5.1.1.1_SITU_FEM<-subset(tab5.1.1.1_SITU_SEXO,C006==2)
cbind(tab5.1.1.1_SITU_MASC[,1],round(100*tab5.1.1.1_SITU_MASC[,c(3,5,6)],1))
cbind(tab5.1.1.1_SITU_FEM[,1],round(100*tab5.1.1.1_SITU_FEM[,c(3,5,6)],1))
tab5.1.1.1_UF<-svyby(~saude.b.mb,~V0001,pes.sel.des.pos,svymean)
tab5.1.1.1_UF<-transform(tab5.1.1.1_UF,LI=saude.b.mb-2_se,LS=saude.b.mb+2_se) cbind(tab5.1.1.1_UF$V0001,round(100*tab5.1.1.1_UF[,c(2,4,5)],1))
tab5.1.1.1_UF_SEXO<-svyby(~saude.b.mb,~V0001+C006,pes.sel.des.pos,svymean)
tab5.1.1.1_UF_SEXO<-transform(tab5.1.1.1_UF_SEXO,LI=saude.b.mb-2_se,LS=saude.b.mb+2_se)
tab5.1.1.1_UF_MASC<-subset(tab5.1.1.1_UF_SEXO,C006==1) cbind(tab5.1.1.1_UF_MASC[,1],round(100*tab5.1.1.1_UF_MASC[,c(3,5,6)],1))
tab5.1.1.1_UF_FEM<-subset(tab5.1.1.1_UF_SEXO,C006==2) cbind(tab5.1.1.1_UF_FEM[,1],round(100*tab5.1.1.1_UF_FEM[,c(3,5,6)],1))
#################################################################
#######################################################
dom_des <- subset(pes.all.des.pos,C004=="01" )
svymean(~~C001,dom_des )
svyby(~C001,~Region, dom_des,svymean)
dom
table now incorporated
djalma, i made some changes to the main script to get started--
https://github.com/ajdamico/asdfree/commit/e7d9b6eb5985b5203b7b40d3ccb06142bca2888d
--but the
dom
table never gets used, and i believe it should be merged onto thepes
table before the survey design gets created? that way, it is a rectangular file (like the censo demografico gets dom and pes merged). what merge fields should be used? so that i can merge on the household-level informationusing the current version of the import file, the intersecting fields are--
thanks!