Obter os mesmos valores de consumo de alimentos disponíveis no SIDRA/IBGE
Baseado em:
https://gist.github.com/arthurwelle/a53499c5d33fa7d9aa75d7ece8f7d84d
https://rpubs.com/amrofi/microdados_pof
# Pacotes
library(dplyr) # data wrangler
library(survey) # survey
library(srvyr) # survey
library(readxl) # leitura de .xls
# lê o arquivo de pos-estratificação e projeção da população
poststr <- readxl::read_excel(
path = "pos_estratos_totais.xlsx",
sheet = "Planilha1",
skip = 5)
poststr <- data.table::as.data.table( poststr )
data.table::setnames( poststr , "COD_UPA(UF+SEQ+DV)" , "COD_UPA" )
colnames(poststr)[4] <-"UF"
CONSUMO_ALIMENTAR <- readRDS("/mnt/DADOS/Data Science/POF/CONSUMO_ALIMENTAR.rds")
MORADOR <- readRDS("/mnt/DADOS/Data Science/POF/MORADOR.rds")
CARACTERISTICAS_DIETA <- readRDS("/mnt/DADOS/Data Science/POF/CARACTERISTICAS_DIETA.rds")
#Diminuindo dimensões em CONSUMO
CONSUMO <- CONSUMO_ALIMENTAR[c("UF","ESTRATO_POF","COD_UPA","NUM_DOM", "NUM_UC","COD_INFOR.MANTE","QTD","V9001", "COD_TBCA","PESO_FINAL")]
colnames(CONSUMO)[6] <-"COD_INFORMANTE"
#Diminuindo dimensões em MORADOR
MORADOR2 <- MORADOR[c("UF","ESTRATO_POF","COD_UPA","NUM_DOM","NUM_UC","COD_INFORMANTE","INSTRUCAO","V0403","V0306")]
#MERGE do Consumo com os postrato com os pos stratos:
CONSUMO <- merge( CONSUMO , poststr , by = intersect( colnames( CONSUMO ) , colnames( poststr ) ) , all.x = TRUE )
#Merge Morador e Consumo
#OBS: Existem mais Moradores do que selecionados para a entrevista de consumo então "all.y=FALSE"
merge1 <- merge( CONSUMO , MORADOR2 ,
by = intersect( colnames( MORADOR2 ) , colnames( CONSUMO ) ),
all.x = TRUE,
all.y = FALSE )
# cria o objeto de pré-estratificação
pre_stratified_design <-
survey::svydesign(
id = ~COD_UPA ,
strata = ~ESTRATO_POF ,
weights = ~PESO_FINAL ,
data = merge1,
nest = TRUE
)
# total população por estrato
population_totals <-
data.frame(
pos_estrato = unique( merge1$pos_estrato ) ,
Freq = unique( merge1$TOTAL_PESSOAS_REFERENCIA )
)
# cria o objeto de desenho amostral com estratificação da POF
d <-
survey::postStratify(
design = pre_stratified_design ,
strata = ~pos_estrato ,
population = population_totals
)
# apaga objetos que não usaremos mais
#rm(pre_stratified_design, population_totals )
Alimentos <- read_excel("Cadastro.xls")
#Disponível na documentação
Alimentos[grep("BATATA",Alimentos$`DESCRIÇÃO DO ALIMENTO`),]
## # A tibble: 42 × 2
## `CÓDIGO DO ALIMENTO` `DESCRIÇÃO DO ALIMENTO`
## <dbl> <chr>
## 1 6400101 BATATA INGLESA
## 2 6400303 MANDIOQUINHA SALSA (BATATA BAROA)
## 3 6400304 CENOURA AMARELA (BATATA BAROA)
## 4 6400401 BATATA DOCE
## 5 6400802 BATATA (NAO ESPECIFICADA)
## 6 6503501 PURE DE BATATA
## 7 8002255 SALGADINHO BATATA CHIPS (TIPO RUFFLES)
## 8 8002801 BOLO DE BATATA DOCE
## 9 8011701 PAO DE BATATA
## 10 8011801 PAO DE BATATA RECHEADO
## # … with 32 more rows
# lê o arquivo criado acima
#MORADOR <- base::readRDS(file = "../Arquivos de dados/MORADOR.RDS" )
# selecionando somente A BATATA
CONSUMO %>%
dplyr::filter(V9001 == 6400101) %>%
stats::weighted.mean(x = .$QTD,
w = .$PESO_FINAL)*180/1000
## [1] 21.18143
# ajuste
options( survey.lonely.psu = "adjust" )
# faz a análise
survey::svymean(~QTD,
design = subset(d, (V9001 == 6400101 & V0306 == 1 ) ) ,
na.rm = TRUE,
level = 0.95)*180/1000 #Anualizando e convertendo para kg
## mean SE
## QTD 21.294 6.2436
BATATA: O SIDRA reporta 4 kg achei 21
???