├── inst
└── extdata
│ ├── input_example.txt
│ ├── deflatorexample.xls
│ └── dictionaryexample.xls
├── NAMESPACE
├── README.md
├── MD5
├── man
├── pns_example.Rd
├── read_pns.Rd
├── pns_labeller.Rd
├── pns_design.Rd
├── pns_deflator.Rd
└── get_pns.Rd
├── DESCRIPTION
└── R
├── example.R
├── read_pns.R
├── pns_deflator.R
├── pns_design.R
├── pns_labeller.R
└── get_pns.R
/inst/extdata/input_example.txt:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gabriel-Assuncao/PNSIBGE/HEAD/inst/extdata/input_example.txt
--------------------------------------------------------------------------------
/inst/extdata/deflatorexample.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gabriel-Assuncao/PNSIBGE/HEAD/inst/extdata/deflatorexample.xls
--------------------------------------------------------------------------------
/inst/extdata/dictionaryexample.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gabriel-Assuncao/PNSIBGE/HEAD/inst/extdata/dictionaryexample.xls
--------------------------------------------------------------------------------
/NAMESPACE:
--------------------------------------------------------------------------------
1 | # Generated by roxygen2: do not edit by hand
2 |
3 | export(get_pns)
4 | export(pns_deflator)
5 | export(pns_design)
6 | export(pns_example)
7 | export(pns_labeller)
8 | export(read_pns)
9 | import(RCurl)
10 | import(dplyr)
11 | import(httr)
12 | import(magrittr)
13 | import(projmgr)
14 | import(readr)
15 | import(readxl)
16 | import(survey)
17 | import(tibble)
18 | import(timeDate)
19 | import(utils)
20 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PNSIBGE
2 | ## Downloading, Reading and Analyzing PNS Microdata
3 |
4 | This R package provides tools for download, read, and analyze the National Survey of Health - PNS from Brazilian Institute of Geography and Statistics - IBGE.
5 |
6 | ## Baixe, Leia e Analise os Microdados da PNS
7 |
8 | Pacote em R com ferramentas para download, leitura e análise da Pesquisa Nacional de Saúde (PNS) do Instituto Brasileiro de Geografia e Estatística (IBGE).
9 |
10 | [Manual em Português Disponível](https://rpubs.com/gabriel-assuncao-ibge/pns)
11 |
12 | [Pacotes das Pesquisas do SIPD](https://cran.r-project.org/package=SIPDIBGE)
13 |
14 | [](https://cran.r-project.org/package=PNSIBGE) [](https://cran.r-project.org/package=PNSIBGE)
15 |
--------------------------------------------------------------------------------
/MD5:
--------------------------------------------------------------------------------
1 | 40c73674b13e6748a06a62ea474bd293 *DESCRIPTION
2 | 0e60945450611daf64d3dd1753f773a3 *NAMESPACE
3 | b4c18be1a5e20b8e7de9796322b066fe *R/example.R
4 | 0b0ba642cbdaaa0ebbd2aab283cbbf35 *R/get_pns.R
5 | df2b8ca5ef49130c0867779d56bc99e4 *R/pns_deflator.R
6 | 4106ec772f10fa8aea8a9a7e3d54e26d *R/pns_design.R
7 | 47640e5eeceac6474a5fa0101be007c2 *R/pns_labeller.R
8 | 79c23133a09edb6bd82ff6a327d3bacf *R/read_pns.R
9 | 314deeb17971b25024c824b094617216 *README.md
10 | e1581ed83835707b1bce53c028a4fbe3 *inst/extdata/deflatorexample.xls
11 | b9efe888f6c4717d1f0857cc93bc2d2a *inst/extdata/dictionaryexample.xls
12 | addbd7edf3a1df3d7b722fdbfd55c7a3 *inst/extdata/exampledata.txt
13 | 3d0f8407268dc2105ea644d865e08e6d *inst/extdata/input_example.txt
14 | 5081036958525a805c623fced214a1d2 *man/get_pns.Rd
15 | 271864903f7575027e76da02d58ec165 *man/pns_deflator.Rd
16 | 91335f136202d0d9ea6ed5f45f34ddd6 *man/pns_design.Rd
17 | c7b8ac5d528a207f9d68fa87ee48a5f8 *man/pns_example.Rd
18 | 80f353153e893ea9906e13d0c3c53d15 *man/pns_labeller.Rd
19 | e517290c9e0e09d2abebf82334567a95 *man/read_pns.Rd
20 |
--------------------------------------------------------------------------------
/man/pns_example.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/example.R
3 | \name{pns_example}
4 | \alias{pns_example}
5 | \title{Get the path of the PNS toy example files}
6 | \usage{
7 | pns_example(path = NULL)
8 | }
9 | \arguments{
10 | \item{path}{Name of file. If \code{NULL}, the PNS toy example files names will be listed.}
11 | }
12 | \value{
13 | A vector with names of all the available PNS toy example files or the path for specific requested PNS toy example file.
14 | }
15 | \description{
16 | This function provides the path of the microdata from year 2019 of the PNS toy example files, loaded with this package.
17 | }
18 | \note{
19 | For more information, visit the survey official website <\url{https://www.ibge.gov.br/estatisticas/sociais/saude/9160-pesquisa-nacional-de-saude.html?=&t=o-que-e}> and consult the other functions of this package, described below.
20 | }
21 | \examples{
22 | pns_example()
23 | pns_example(path="exampledata.txt")
24 | pns_example(path="input_example.txt")
25 | pns_example(path="dictionaryexample.xls")
26 | pns_example(path="deflatorexample.xls")
27 | }
28 | \seealso{
29 | \link[PNSIBGE]{get_pns} for downloading, labeling, deflating and creating survey design object for PNS microdata.\cr \link[PNSIBGE]{read_pns} for reading PNS microdata.\cr \link[PNSIBGE]{pns_labeller} for labeling categorical variables from PNS microdata.\cr \link[PNSIBGE]{pns_deflator} for adding deflator variables to PNS microdata.\cr \link[PNSIBGE]{pns_design} for creating PNS survey design object.
30 | }
31 |
--------------------------------------------------------------------------------
/DESCRIPTION:
--------------------------------------------------------------------------------
1 | Package: PNSIBGE
2 | Type: Package
3 | Title: Downloading, Reading and Analyzing PNS Microdata
4 | Version: 0.2.1
5 | Authors@R: c(person("Gabriel", "Assuncao", email = "pacotesipd@ibge.gov.br",
6 | role = c("aut", "cre")),
7 | person("Luna", "Hidalgo", email = "pacotesipd@ibge.gov.br",
8 | role = c("aut")),
9 | person("Douglas", "Braga", email = "pacotesipd@ibge.gov.br",
10 | role = c("ctb")),
11 | person("Viviane", "Quintaes", email = "pacotesipd@ibge.gov.br",
12 | role = c("ctb")))
13 | Description: Provides tools for downloading, reading and analyzing the National
14 | Survey of Health - PNS, a household survey from Brazilian Institute
15 | of Geography and Statistics - IBGE. The data must be downloaded from the official
16 | website . Further analysis must be made using package 'survey'.
17 | Depends: R (>= 3.2.0)
18 | Imports: dplyr, httr, magrittr, projmgr, RCurl, readr, readxl, survey,
19 | tibble, timeDate, utils
20 | Suggests: convey, SIPDIBGE, srvyr
21 | License: GPL-3
22 | Encoding: UTF-8
23 | RoxygenNote: 7.1.2
24 | NeedsCompilation: no
25 | Author: Gabriel Assuncao [aut, cre],
26 | Luna Hidalgo [aut],
27 | Douglas Braga [ctb],
28 | Viviane Quintaes [ctb]
29 | Maintainer: Gabriel Assuncao
30 | BugReports: https://github.com/Gabriel-Assuncao/PNSIBGE/issues
31 | Packaged: 2024-02-01 14:21:54 UTC; gabriel.assuncao
32 | Repository: CRAN
33 | Date/Publication: 2024-02-01 15:40:02 UTC
34 |
--------------------------------------------------------------------------------
/R/example.R:
--------------------------------------------------------------------------------
1 | #' Get the path of the PNS toy example files
2 | #' @description This function provides the path of the microdata from year 2019 of the PNS toy example files, loaded with this package.
3 | #' @import dplyr httr magrittr projmgr RCurl readr readxl survey tibble timeDate utils
4 | #' @param path Name of file. If \code{NULL}, the PNS toy example files names will be listed.
5 | #' @return A vector with names of all the available PNS toy example files or the path for specific requested PNS toy example file.
6 | #' @note For more information, visit the survey official website <\url{https://www.ibge.gov.br/estatisticas/sociais/saude/9160-pesquisa-nacional-de-saude.html?=&t=o-que-e}> and consult the other functions of this package, described below.
7 | #' @seealso \link[PNSIBGE]{get_pns} for downloading, labeling, deflating and creating survey design object for PNS microdata.\cr \link[PNSIBGE]{read_pns} for reading PNS microdata.\cr \link[PNSIBGE]{pns_labeller} for labeling categorical variables from PNS microdata.\cr \link[PNSIBGE]{pns_deflator} for adding deflator variables to PNS microdata.\cr \link[PNSIBGE]{pns_design} for creating PNS survey design object.
8 | #' @examples
9 | #' pns_example()
10 | #' pns_example(path="exampledata.txt")
11 | #' pns_example(path="input_example.txt")
12 | #' pns_example(path="dictionaryexample.xls")
13 | #' pns_example(path="deflatorexample.xls")
14 | #' @export
15 |
16 | pns_example <- function(path = NULL) {
17 | if (is.null(path)) {
18 | dir(system.file("extdata", package="PNSIBGE"))
19 | }
20 | else {
21 | system.file("extdata", path, package="PNSIBGE", mustWork=TRUE)
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/man/read_pns.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/read_pns.R
3 | \name{read_pns}
4 | \alias{read_pns}
5 | \title{Read PNS microdata}
6 | \usage{
7 | read_pns(microdata, input_txt, vars = NULL)
8 | }
9 | \arguments{
10 | \item{microdata}{A text file containing microdata from PNS survey, available on official website: (select a microdata file, according to the appropriated year, microdata folder and then, inside, data) - \url{https://ftp.ibge.gov.br/PNS/}.}
11 |
12 | \item{input_txt}{A text file, related to the microdata, containing the input script for SAS, available on official website: (select the dictionary and input zip file, according to the appropriated year, microdata folder and then, inside, documentation) - \url{https://ftp.ibge.gov.br/PNS/}.}
13 |
14 | \item{vars}{Vector of variable names to be kept for analysis. Default is to keep all variables.}
15 | }
16 | \value{
17 | A tibble with selected variables of the microdata, including the necessary survey design ones.
18 | }
19 | \description{
20 | This function reads PNS microdata.
21 | }
22 | \note{
23 | For more information, visit the survey official website <\url{https://www.ibge.gov.br/estatisticas/sociais/saude/9160-pesquisa-nacional-de-saude.html?=&t=o-que-e}> and consult the other functions of this package, described below.
24 | }
25 | \examples{
26 | input_path <- pns_example(path="input_example.txt")
27 | data_path <- pns_example(path="exampledata.txt")
28 | pns.df <- read_pns(microdata=data_path, input_txt=input_path, vars=c("J007","J009"))
29 | }
30 | \seealso{
31 | \link[PNSIBGE]{get_pns} for downloading, labeling, deflating and creating survey design object for PNS microdata.\cr \link[PNSIBGE]{pns_labeller} for labeling categorical variables from PNS microdata.\cr \link[PNSIBGE]{pns_deflator} for adding deflator variables to PNS microdata.\cr \link[PNSIBGE]{pns_design} for creating PNS survey design object.\cr \link[PNSIBGE]{pns_example} for getting the path of the PNS toy example files.
32 | }
33 |
--------------------------------------------------------------------------------
/man/pns_labeller.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/pns_labeller.R
3 | \name{pns_labeller}
4 | \alias{pns_labeller}
5 | \title{Label categorical variables from PNS microdata}
6 | \usage{
7 | pns_labeller(data_pns, dictionary.file)
8 | }
9 | \arguments{
10 | \item{data_pns}{A tibble of PNS microdata read with \code{read_pns} function.}
11 |
12 | \item{dictionary.file}{The dictionary file for selected survey available on official website: (select the dictionary and input zip file, according to the appropriated year, microdata folder and then, inside, documentation) - \url{https://ftp.ibge.gov.br/PNS/}.}
13 | }
14 | \value{
15 | A tibble with the data provided from PNS survey and its categorical variables as factors with related labels.
16 | }
17 | \description{
18 | This function labels categorical variables from PNS microdata.
19 | }
20 | \note{
21 | For more information, visit the survey official website <\url{https://www.ibge.gov.br/estatisticas/sociais/saude/9160-pesquisa-nacional-de-saude.html?=&t=o-que-e}> and consult the other functions of this package, described below.
22 | }
23 | \examples{
24 | # Using data read from disk
25 | input_path <- pns_example(path="input_example.txt")
26 | data_path <- pns_example(path="exampledata.txt")
27 | dictionary.path <- pns_example(path="dictionaryexample.xls")
28 | pns.df <- read_pns(microdata=data_path, input_txt=input_path, vars=c("J007","J009"))
29 | pns.df <- pns_labeller(data_pns=pns.df, dictionary.file=dictionary.path)
30 | \donttest{
31 | # Downloading data
32 | pns.df2 <- get_pns(year=2019, selected=FALSE, anthropometry=FALSE, vars=c("J007","J009"),
33 | labels=FALSE, deflator=FALSE, design=FALSE,
34 | reload=TRUE, curlopts=list(), savedir=tempdir())
35 | dictionary.path2 <- pns_example(path="dictionaryexample.xls")
36 | pns.df2 <- pns_labeller(data_pns=pns.df2, dictionary.file=dictionary.path2)}
37 | }
38 | \seealso{
39 | \link[PNSIBGE]{get_pns} for downloading, labeling, deflating and creating survey design object for PNS microdata.\cr \link[PNSIBGE]{read_pns} for reading PNS microdata.\cr \link[PNSIBGE]{pns_deflator} for adding deflator variables to PNS microdata.\cr \link[PNSIBGE]{pns_design} for creating PNS survey design object.\cr \link[PNSIBGE]{pns_example} for getting the path of the PNS toy example files.
40 | }
41 |
--------------------------------------------------------------------------------
/man/pns_design.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/pns_design.R
3 | \name{pns_design}
4 | \alias{pns_design}
5 | \title{Create PNS survey object with its sample design}
6 | \usage{
7 | pns_design(data_pns)
8 | }
9 | \arguments{
10 | \item{data_pns}{A tibble of PNS microdata read with \code{read_pns} function.}
11 | }
12 | \value{
13 | An object of class \code{survey.design} or \code{svyrep.design} with the data from PNS and its sample design.
14 | }
15 | \description{
16 | This function creates PNS survey object with its sample design for analysis using \code{survey} package functions.
17 | }
18 | \note{
19 | For more information, visit the survey official website <\url{https://www.ibge.gov.br/estatisticas/sociais/saude/9160-pesquisa-nacional-de-saude.html?=&t=o-que-e}> and consult the other functions of this package, described below.
20 | }
21 | \examples{
22 | # Using data read from disk
23 | input_path <- pns_example(path="input_example.txt")
24 | data_path <- pns_example(path="exampledata.txt")
25 | dictionary.path <- pns_example(path="dictionaryexample.xls")
26 | deflator.path <- pns_example(path="deflatorexample.xls")
27 | pns.df <- read_pns(microdata=data_path, input_txt=input_path, vars=c("J007","J009"))
28 | pns.df <- pns_labeller(data_pns=pns.df, dictionary.file=dictionary.path)
29 | pns.df <- pns_deflator(data_pns=pns.df, deflator.file=deflator.path)
30 | \donttest{
31 | pns.svy <- pns_design(data_pns=pns.df)
32 | # Calculating proportion of people diagnosed with chronic diseases
33 | if (!is.null(pns.svy)) survey::svymean(x=~J007, design=pns.svy, na.rm=TRUE)}
34 | \donttest{
35 | # Downloading data
36 | pns.df2 <- get_pns(year=2019, selected=FALSE, anthropometry=FALSE, vars=c("J007","J009"),
37 | labels=TRUE, deflator=TRUE, design=FALSE,
38 | reload=TRUE, curlopts=list(), savedir=tempdir())
39 | pns.svy2 <- pns_design(data_pns=pns.df2)
40 | # Calculating proportion of people diagnosed with chronic diseases
41 | if (!is.null(pns.svy2)) survey::svymean(x=~J007, design=pns.svy2, na.rm=TRUE)}
42 | }
43 | \seealso{
44 | \link[PNSIBGE]{get_pns} for downloading, labeling, deflating and creating survey design object for PNS microdata.\cr \link[PNSIBGE]{read_pns} for reading PNS microdata.\cr \link[PNSIBGE]{pns_labeller} for labeling categorical variables from PNS microdata.\cr \link[PNSIBGE]{pns_deflator} for adding deflator variables to PNS microdata.\cr \link[PNSIBGE]{pns_example} for getting the path of the PNS toy example files.
45 | }
46 |
--------------------------------------------------------------------------------
/man/pns_deflator.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/pns_deflator.R
3 | \name{pns_deflator}
4 | \alias{pns_deflator}
5 | \title{Add deflator variables to PNS microdata}
6 | \usage{
7 | pns_deflator(data_pns, deflator.file)
8 | }
9 | \arguments{
10 | \item{data_pns}{A tibble of PNS microdata read with \code{read_pns} function.}
11 |
12 | \item{deflator.file}{The deflator file for selected survey available on official website: (select the deflator zip file) - \url{https://ftp.ibge.gov.br/PNS/Documentacao_Geral/}.}
13 | }
14 | \value{
15 | A tibble with the data provided from PNS survey and the deflator variables added for use.
16 | }
17 | \description{
18 | This function adds deflator variables to PNS microdata. For deflation of income variables, the documentation provided through the following address must be used: \url{https://ftp.ibge.gov.br/PNS/Documentacao_Geral/PNSIBGE_Deflator.pdf}.
19 | }
20 | \note{
21 | For more information, visit the survey official website <\url{https://www.ibge.gov.br/estatisticas/sociais/saude/9160-pesquisa-nacional-de-saude.html?=&t=o-que-e}> and consult the other functions of this package, described below.
22 | }
23 | \examples{
24 | # Using data read from disk
25 | input_path <- pns_example(path="input_example.txt")
26 | data_path <- pns_example(path="exampledata.txt")
27 | dictionary.path <- pns_example(path="dictionaryexample.xls")
28 | deflator.path <- pns_example(path="deflatorexample.xls")
29 | pns.df <- read_pns(microdata=data_path, input_txt=input_path, vars=c("J007","J009"))
30 | pns.df <- pns_labeller(data_pns=pns.df, dictionary.file=dictionary.path)
31 | pns.df <- pns_deflator(data_pns=pns.df, deflator.file=deflator.path)
32 | \donttest{
33 | # Downloading data
34 | pns.df2 <- get_pns(year=2019, selected=FALSE, anthropometry=FALSE, vars=c("J007","J009"),
35 | labels=TRUE, deflator=FALSE, design=FALSE,
36 | reload=TRUE, curlopts=list(), savedir=tempdir())
37 | deflator.path2 <- pns_example(path="deflatorexample.xls")
38 | pns.df2 <- pns_deflator(data_pns=pns.df2, deflator.file=deflator.path2)}
39 | }
40 | \seealso{
41 | \link[PNSIBGE]{get_pns} for downloading, labeling, deflating and creating survey design object for PNS microdata.\cr \link[PNSIBGE]{read_pns} for reading PNS microdata.\cr \link[PNSIBGE]{pns_labeller} for labeling categorical variables from PNS microdata.\cr \link[PNSIBGE]{pns_design} for creating PNS survey design object.\cr \link[PNSIBGE]{pns_example} for getting the path of the PNS toy example files.
42 | }
43 |
--------------------------------------------------------------------------------
/R/read_pns.R:
--------------------------------------------------------------------------------
1 | #' Read PNS microdata
2 | #' @description This function reads PNS microdata.
3 | #' @import dplyr httr magrittr projmgr RCurl readr readxl survey tibble timeDate utils
4 | #' @param microdata A text file containing microdata from PNS survey, available on official website: (select a microdata file, according to the appropriated year, microdata folder and then, inside, data) - \url{https://ftp.ibge.gov.br/PNS/}.
5 | #' @param input_txt A text file, related to the microdata, containing the input script for SAS, available on official website: (select the dictionary and input zip file, according to the appropriated year, microdata folder and then, inside, documentation) - \url{https://ftp.ibge.gov.br/PNS/}.
6 | #' @param vars Vector of variable names to be kept for analysis. Default is to keep all variables.
7 | #' @return A tibble with selected variables of the microdata, including the necessary survey design ones.
8 | #' @note For more information, visit the survey official website <\url{https://www.ibge.gov.br/estatisticas/sociais/saude/9160-pesquisa-nacional-de-saude.html?=&t=o-que-e}> and consult the other functions of this package, described below.
9 | #' @seealso \link[PNSIBGE]{get_pns} for downloading, labeling, deflating and creating survey design object for PNS microdata.\cr \link[PNSIBGE]{pns_labeller} for labeling categorical variables from PNS microdata.\cr \link[PNSIBGE]{pns_deflator} for adding deflator variables to PNS microdata.\cr \link[PNSIBGE]{pns_design} for creating PNS survey design object.\cr \link[PNSIBGE]{pns_example} for getting the path of the PNS toy example files.
10 | #' @examples
11 | #' input_path <- pns_example(path="input_example.txt")
12 | #' data_path <- pns_example(path="exampledata.txt")
13 | #' pns.df <- read_pns(microdata=data_path, input_txt=input_path, vars=c("J007","J009"))
14 | #' @export
15 |
16 | read_pns <- function(microdata, input_txt, vars = NULL) {
17 | X1 = X2 = X3 = start = end = NULL
18 | input <- suppressWarnings(suppressMessages({readr::read_table(input_txt, col_names=FALSE) %>% subset(substr(X1, 1, 1) == "@") %>%
19 | dplyr::mutate(type=ifelse(substr(X3, 1, 1) == "$","c","d"), start=as.numeric(gsub("@", "", X1)), X3=as.integer(chartr("$", " ", X3)), end=start+X3-1)}))
20 | if (!is.null(vars)) {
21 | if (any(!(vars %in% input$X2))) {
22 | missvar <- vars[!(vars %in% input$X2)]
23 | message(paste("Variables", paste(missvar, collapse=", "), "not present in microdata.\n"))
24 | }
25 | input %<>% subset(X2 %in% c("V0001", "UPA_PNS", "ID_DOMICILIO", "V0006_PNS", "V0015", "V0020", "V0024", "V0028", "V00281", "V00282", "V00283", "V0029", "V00291", "V00292", "V00293", "V0030", "V00301", "V00302", "V00303", "C00301", "M001", "W001", vars))
26 | }
27 | columns <- input %$% readr::fwf_positions(start, end, X2)
28 | data_pns <- suppressWarnings(readr::read_fwf(microdata, columns, col_types=paste0(input$type, collapse="")))
29 | data_pns <- dplyr::mutate(data_pns, ID_DOMICILIO=paste0(data_pns$UPA_PNS, data_pns$V0006_PNS))
30 | data_pns <- data_pns[(data_pns$V0015 == "01" & !is.na(data_pns$V0015)),]
31 | return(data_pns)
32 | }
33 |
--------------------------------------------------------------------------------
/R/pns_deflator.R:
--------------------------------------------------------------------------------
1 | #' Add deflator variables to PNS microdata
2 | #' @description This function adds deflator variables to PNS microdata. For deflation of income variables, the documentation provided through the following address must be used: \url{https://ftp.ibge.gov.br/PNS/Documentacao_Geral/PNSIBGE_Deflator.pdf}.
3 | #' @import dplyr httr magrittr projmgr RCurl readr readxl survey tibble timeDate utils
4 | #' @param data_pns A tibble of PNS microdata read with \code{read_pns} function.
5 | #' @param deflator.file The deflator file for selected survey available on official website: (select the deflator zip file) - \url{https://ftp.ibge.gov.br/PNS/Documentacao_Geral/}.
6 | #' @return A tibble with the data provided from PNS survey and the deflator variables added for use.
7 | #' @note For more information, visit the survey official website <\url{https://www.ibge.gov.br/estatisticas/sociais/saude/9160-pesquisa-nacional-de-saude.html?=&t=o-que-e}> and consult the other functions of this package, described below.
8 | #' @seealso \link[PNSIBGE]{get_pns} for downloading, labeling, deflating and creating survey design object for PNS microdata.\cr \link[PNSIBGE]{read_pns} for reading PNS microdata.\cr \link[PNSIBGE]{pns_labeller} for labeling categorical variables from PNS microdata.\cr \link[PNSIBGE]{pns_design} for creating PNS survey design object.\cr \link[PNSIBGE]{pns_example} for getting the path of the PNS toy example files.
9 | #' @examples
10 | #' # Using data read from disk
11 | #' input_path <- pns_example(path="input_example.txt")
12 | #' data_path <- pns_example(path="exampledata.txt")
13 | #' dictionary.path <- pns_example(path="dictionaryexample.xls")
14 | #' deflator.path <- pns_example(path="deflatorexample.xls")
15 | #' pns.df <- read_pns(microdata=data_path, input_txt=input_path, vars=c("J007","J009"))
16 | #' pns.df <- pns_labeller(data_pns=pns.df, dictionary.file=dictionary.path)
17 | #' pns.df <- pns_deflator(data_pns=pns.df, deflator.file=deflator.path)
18 | #' \donttest{
19 | #' # Downloading data
20 | #' pns.df2 <- get_pns(year=2019, selected=FALSE, anthropometry=FALSE, vars=c("J007","J009"),
21 | #' labels=TRUE, deflator=FALSE, design=FALSE,
22 | #' reload=TRUE, curlopts=list(), savedir=tempdir())
23 | #' deflator.path2 <- pns_example(path="deflatorexample.xls")
24 | #' pns.df2 <- pns_deflator(data_pns=pns.df2, deflator.file=deflator.path2)}
25 | #' @export
26 |
27 | pns_deflator <- function(data_pns, deflator.file) {
28 | if (sum(class(data_pns) == "tbl_df") > 0) {
29 | if (!(FALSE %in% (c("V0020", "V0001") %in% names(data_pns)))) {
30 | data_pns <- data_pns[, !names(data_pns) %in% c("Deflator"), drop=FALSE]
31 | deflator <- suppressMessages(readxl::read_excel(deflator.file))
32 | colnames(deflator)[c(1:2)] <- c("V0020", "V0001")
33 | deflator$V0001 <- as.factor(deflator$V0001)
34 | if (identical(intersect(levels(deflator$V0001), levels(as.factor(data_pns$V0001))), character(0)) & length(levels(deflator$V0001)) == length(levels(as.factor(data_pns$V0001)))) {
35 | levels(deflator$V0001) <- levels(as.factor(data_pns$V0001))
36 | }
37 | data_pns <- merge(x=data_pns, y=deflator, by.x=c("V0020", "V0001"), by.y=c("V0020", "V0001"), all.x=TRUE, all.y=FALSE)
38 | if (!(FALSE %in% (c("ID_DOMICILIO") %in% names(data_pns)))) {
39 | data_pns <- data_pns[order(data_pns$V0024, data_pns$ID_DOMICILIO, data_pns$C00301),]
40 | }
41 | else {
42 | data_pns <- data_pns[order(data_pns$V0024, data_pns$UPA_PNS, data_pns$V0006_PNS, data_pns$C00301),]
43 | }
44 | data_pns <- tibble::as_tibble(data_pns)
45 | }
46 | else {
47 | message("Merge variables required for adding deflator variables are missing.\n")
48 | }
49 | }
50 | else {
51 | message("The microdata object is not of the tibble class or sample design was already defined for microdata, so adding deflator variables is not possible.\n")
52 | }
53 | return(data_pns)
54 | }
55 |
--------------------------------------------------------------------------------
/man/get_pns.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/get_pns.R
3 | \name{get_pns}
4 | \alias{get_pns}
5 | \title{Download, label, deflate and create survey design object for PNS microdata}
6 | \usage{
7 | get_pns(
8 | year,
9 | selected = FALSE,
10 | anthropometry = FALSE,
11 | vars = NULL,
12 | labels = TRUE,
13 | deflator = TRUE,
14 | design = TRUE,
15 | reload = TRUE,
16 | curlopts = list(),
17 | savedir = tempdir()
18 | )
19 | }
20 | \arguments{
21 | \item{year}{The year of the data to be downloaded. Must be a number equal to 2013 or 2019. Vector not accepted.}
22 |
23 | \item{selected}{Logical value. If \code{TRUE}, the specific questionnaire for selected resident will be used. If \code{FALSE}, the basic questionnaire for household and residents will be used.}
24 |
25 | \item{anthropometry}{Logical value. If \code{TRUE}, the specific questionnaire for the anthropometry module of the selected resident will be used. If \code{FALSE}, the questionnaire defined by the \code{selected} argument of this function will be used. This argument will be used only if \code{year} is equal to 2019.}
26 |
27 | \item{vars}{Vector of variable names to be kept for analysis. Default is to keep all variables.}
28 |
29 | \item{labels}{Logical value. If \code{TRUE}, categorical variables will presented as factors with labels corresponding to the survey's dictionary.}
30 |
31 | \item{deflator}{Logical value. If \code{TRUE}, deflator variables will be available for use in the microdata.}
32 |
33 | \item{design}{Logical value. If \code{TRUE}, will return an object of class \code{survey.design} or \code{svyrep.design}. It is strongly recommended to keep this parameter as \code{TRUE} for further analysis. If \code{FALSE}, only the microdata will be returned.}
34 |
35 | \item{reload}{Logical value. If \code{TRUE}, will re-download the files even if they already exist in the save directory. If \code{FALSE}, will be checked if the files already exist in the save directory and the download will not be performed repeatedly, be careful with coinciding names of microdata files.}
36 |
37 | \item{curlopts}{A named list object identifying the curl options for the handle when using functions from \code{RCurl} package.}
38 |
39 | \item{savedir}{Directory to save the downloaded data. Default is to use a temporary directory.}
40 | }
41 | \value{
42 | An object of class \code{survey.design} or \code{svyrep.design} with the data from PNS and its sample design, or a tibble with selected variables of the microdata, including the necessary survey design ones.
43 | }
44 | \description{
45 | Core function of package. With this function only, the user can download a PNS microdata from a year and get a sample design object ready to use with \code{survey} package functions.
46 | }
47 | \note{
48 | For more information, visit the survey official website <\url{https://www.ibge.gov.br/estatisticas/sociais/saude/9160-pesquisa-nacional-de-saude.html?=&t=o-que-e}> and consult the other functions of this package, described below.
49 | }
50 | \examples{
51 | \donttest{
52 | pns.svy <- get_pns(year=2019, selected=FALSE, anthropometry=FALSE, vars=c("J007","J009"),
53 | labels=TRUE, deflator=TRUE, design=TRUE,
54 | reload=TRUE, curlopts=list(), savedir=tempdir())
55 | # Calculating proportion of people diagnosed with chronic diseases
56 | if (!is.null(pns.svy)) survey::svymean(x=~J007, design=pns.svy, na.rm=TRUE)
57 | pns.svy2 <- get_pns(year=2019, selected=TRUE, anthropometry=FALSE, vars=c("N001","N00101"),
58 | labels=TRUE, deflator=TRUE, design=TRUE,
59 | reload=TRUE, curlopts=list(), savedir=tempdir())
60 | # Calculating proportion of people's self-rated health
61 | if (!is.null(pns.svy2)) survey::svymean(x=~N001, design=pns.svy2, na.rm=TRUE)
62 | pns.svy3 <- get_pns(year=2019, selected=FALSE, anthropometry=TRUE, vars=c("W00101","W00201"),
63 | labels=TRUE, deflator=TRUE, design=TRUE,
64 | reload=TRUE, curlopts=list(), savedir=tempdir())
65 | # Calculating the average weight of people
66 | if (!is.null(pns.svy3)) survey::svymean(x=~W00101, design=pns.svy3, na.rm=TRUE)}
67 | }
68 | \seealso{
69 | \link[PNSIBGE]{read_pns} for reading PNS microdata.\cr \link[PNSIBGE]{pns_labeller} for labeling categorical variables from PNS microdata.\cr \link[PNSIBGE]{pns_deflator} for adding deflator variables to PNS microdata.\cr \link[PNSIBGE]{pns_design} for creating PNS survey design object.\cr \link[PNSIBGE]{pns_example} for getting the path of the PNS toy example files.
70 | }
71 |
--------------------------------------------------------------------------------
/R/pns_design.R:
--------------------------------------------------------------------------------
1 | #' Create PNS survey object with its sample design
2 | #' @description This function creates PNS survey object with its sample design for analysis using \code{survey} package functions.
3 | #' @import dplyr httr magrittr projmgr RCurl readr readxl survey tibble timeDate utils
4 | #' @param data_pns A tibble of PNS microdata read with \code{read_pns} function.
5 | #' @return An object of class \code{survey.design} or \code{svyrep.design} with the data from PNS and its sample design.
6 | #' @note For more information, visit the survey official website <\url{https://www.ibge.gov.br/estatisticas/sociais/saude/9160-pesquisa-nacional-de-saude.html?=&t=o-que-e}> and consult the other functions of this package, described below.
7 | #' @seealso \link[PNSIBGE]{get_pns} for downloading, labeling, deflating and creating survey design object for PNS microdata.\cr \link[PNSIBGE]{read_pns} for reading PNS microdata.\cr \link[PNSIBGE]{pns_labeller} for labeling categorical variables from PNS microdata.\cr \link[PNSIBGE]{pns_deflator} for adding deflator variables to PNS microdata.\cr \link[PNSIBGE]{pns_example} for getting the path of the PNS toy example files.
8 | #' @examples
9 | #' # Using data read from disk
10 | #' input_path <- pns_example(path="input_example.txt")
11 | #' data_path <- pns_example(path="exampledata.txt")
12 | #' dictionary.path <- pns_example(path="dictionaryexample.xls")
13 | #' deflator.path <- pns_example(path="deflatorexample.xls")
14 | #' pns.df <- read_pns(microdata=data_path, input_txt=input_path, vars=c("J007","J009"))
15 | #' pns.df <- pns_labeller(data_pns=pns.df, dictionary.file=dictionary.path)
16 | #' pns.df <- pns_deflator(data_pns=pns.df, deflator.file=deflator.path)
17 | #' \donttest{
18 | #' pns.svy <- pns_design(data_pns=pns.df)
19 | #' # Calculating proportion of people diagnosed with chronic diseases
20 | #' if (!is.null(pns.svy)) survey::svymean(x=~J007, design=pns.svy, na.rm=TRUE)}
21 | #' \donttest{
22 | #' # Downloading data
23 | #' pns.df2 <- get_pns(year=2019, selected=FALSE, anthropometry=FALSE, vars=c("J007","J009"),
24 | #' labels=TRUE, deflator=TRUE, design=FALSE,
25 | #' reload=TRUE, curlopts=list(), savedir=tempdir())
26 | #' pns.svy2 <- pns_design(data_pns=pns.df2)
27 | #' # Calculating proportion of people diagnosed with chronic diseases
28 | #' if (!is.null(pns.svy2)) survey::svymean(x=~J007, design=pns.svy2, na.rm=TRUE)}
29 | #' @export
30 |
31 | pns_design <- function(data_pns) {
32 | if (sum(class(data_pns) == "tbl_df") > 0) {
33 | if (!(FALSE %in% (c("UPA_PNS", "ID_DOMICILIO", "V0024", "V0028", "V00281", "V00282", "V00283") %in% names(data_pns))) |
34 | !(FALSE %in% (c("UPA_PNS", "ID_DOMICILIO", "V0024", "V0029", "V00291", "V00292", "V00293") %in% names(data_pns))) |
35 | !(FALSE %in% (c("UPA_PNS", "ID_DOMICILIO", "V0024", "V0030", "V00301", "V00302", "V00303") %in% names(data_pns)))) {
36 | options(survey.lonely.psu="adjust")
37 | options(survey.adjust.domain.lonely=TRUE)
38 | if (!(FALSE %in% (c("UPA_PNS", "ID_DOMICILIO", "V0024", "V0028", "V00281", "V00282", "V00283") %in% names(data_pns)))) {
39 | data_prior <- survey::svydesign(ids=~UPA_PNS, strata=~V0024, data=data_pns, weights=~V0028, nest=TRUE)
40 | popc.types <- data.frame(V00283=as.character(unique(data_pns$V00283)), Freq=as.numeric(unique(data_pns$V00282)))
41 | popc.types <- popc.types[order(popc.types$V00283),]
42 | data_posterior <- survey::postStratify(design=data_prior, strata=~V00283, population=popc.types)
43 | }
44 | else if (!(FALSE %in% (c("UPA_PNS", "ID_DOMICILIO", "V0024", "V0029", "V00291", "V00292", "V00293") %in% names(data_pns)))) {
45 | data_prior <- survey::svydesign(ids=~UPA_PNS, strata=~V0024, data=data_pns, weights=~V0029, nest=TRUE)
46 | popc.types <- data.frame(V00293=as.character(unique(data_pns$V00293)), Freq=as.numeric(unique(data_pns$V00292)))
47 | popc.types <- popc.types[order(popc.types$V00293),]
48 | data_posterior <- survey::postStratify(design=data_prior, strata=~V00293, population=popc.types)
49 | }
50 | else {
51 | data_prior <- survey::svydesign(ids=~UPA_PNS, strata=~V0024, data=data_pns, weights=~V0030, nest=TRUE)
52 | popc.types <- data.frame(V00303=as.character(unique(data_pns$V00303)), Freq=as.numeric(unique(data_pns$V00302)))
53 | popc.types <- popc.types[order(popc.types$V00303),]
54 | data_posterior <- survey::postStratify(design=data_prior, strata=~V00303, population=popc.types)
55 | }
56 | }
57 | else {
58 | message("Weight variables required for sample design are missing.\n")
59 | data_posterior <- data_pns
60 | }
61 | }
62 | else {
63 | message("The microdata object is not of the tibble class or sample design was already defined for microdata, so applying another design is not possible.\n")
64 | data_posterior <- data_pns
65 | }
66 | return(data_posterior)
67 | }
68 |
--------------------------------------------------------------------------------
/R/pns_labeller.R:
--------------------------------------------------------------------------------
1 | #' Label categorical variables from PNS microdata
2 | #' @description This function labels categorical variables from PNS microdata.
3 | #' @import dplyr httr magrittr projmgr RCurl readr readxl survey tibble timeDate utils
4 | #' @param data_pns A tibble of PNS microdata read with \code{read_pns} function.
5 | #' @param dictionary.file The dictionary file for selected survey available on official website: (select the dictionary and input zip file, according to the appropriated year, microdata folder and then, inside, documentation) - \url{https://ftp.ibge.gov.br/PNS/}.
6 | #' @return A tibble with the data provided from PNS survey and its categorical variables as factors with related labels.
7 | #' @note For more information, visit the survey official website <\url{https://www.ibge.gov.br/estatisticas/sociais/saude/9160-pesquisa-nacional-de-saude.html?=&t=o-que-e}> and consult the other functions of this package, described below.
8 | #' @seealso \link[PNSIBGE]{get_pns} for downloading, labeling, deflating and creating survey design object for PNS microdata.\cr \link[PNSIBGE]{read_pns} for reading PNS microdata.\cr \link[PNSIBGE]{pns_deflator} for adding deflator variables to PNS microdata.\cr \link[PNSIBGE]{pns_design} for creating PNS survey design object.\cr \link[PNSIBGE]{pns_example} for getting the path of the PNS toy example files.
9 | #' @examples
10 | #' # Using data read from disk
11 | #' input_path <- pns_example(path="input_example.txt")
12 | #' data_path <- pns_example(path="exampledata.txt")
13 | #' dictionary.path <- pns_example(path="dictionaryexample.xls")
14 | #' pns.df <- read_pns(microdata=data_path, input_txt=input_path, vars=c("J007","J009"))
15 | #' pns.df <- pns_labeller(data_pns=pns.df, dictionary.file=dictionary.path)
16 | #' \donttest{
17 | #' # Downloading data
18 | #' pns.df2 <- get_pns(year=2019, selected=FALSE, anthropometry=FALSE, vars=c("J007","J009"),
19 | #' labels=FALSE, deflator=FALSE, design=FALSE,
20 | #' reload=TRUE, curlopts=list(), savedir=tempdir())
21 | #' dictionary.path2 <- pns_example(path="dictionaryexample.xls")
22 | #' pns.df2 <- pns_labeller(data_pns=pns.df2, dictionary.file=dictionary.path2)}
23 | #' @export
24 |
25 | pns_labeller <- function(data_pns, dictionary.file) {
26 | if (sum(class(data_pns) == "tbl_df") > 0) {
27 | dictionary <- suppressMessages(readxl::read_excel(dictionary.file))
28 | X__3 = X__6 = X__7 = NULL
29 | colnames(dictionary) <- paste0("X__",1:dim(dictionary)[2])
30 | dictionary %<>% subset(!is.na(X__6))
31 | codcurrent <- dictionary$X__3
32 | for (i in 1:dim(dictionary)[1]) {
33 | if (is.na(dictionary$X__3[i])) {
34 | dictionary$X__3[i] <- codcurrent
35 | }
36 | else {
37 | codcurrent <- dictionary$X__3[i]
38 | }
39 | }
40 | notlabel <- c("UPA_PNS", "UPA", "ID_DOMICILIO", "V0006_PNS", "V0020", "V0022", "V0024",
41 | "V0028", "V00281", "V00282", "V00283",
42 | "V0029", "V00291", "V00292", "V00293",
43 | "V0030", "V00301", "V00302", "V00303",
44 | "A010", "A01001", "A011", "A014", "A01401", "A01402",
45 | "A018012", "A018014", "A018016", "A018018",
46 | "A018020", "A018022", "A018024", "A018026", "A018028",
47 | "A01802", "A01804", "A01806", "A01808", "A01810",
48 | "A01812", "A01813", "A01814", "A01816", "A01818", "A01819",
49 | "A020", "A02102", "A02301", "A02302", "A02303", "A02304",
50 | "A02305", "A02306", "A02307", "A02308", "A02401", "A02402",
51 | "VDC001", "VDC002", "VDC003", "VDF002", "VDF003", "VDF00102",
52 | "C001", "C00301", "C00701", "C00702", "C00703", "C008", "C01801",
53 | "E010011", "E010012", "E010013", "E01201", "E01501",
54 | "E01602", "E01604", "E017", "E01802", "E01804", "E019",
55 | "E024021", "E02501", "E02502", "E02503", "E033",
56 | "F001021", "F007021", "F008021", "F010021", "F011021",
57 | "F012021", "F013021", "F014021", "I001021", "I00701",
58 | "J003", "J006", "J012", "J019", "J038", "J04001", "J04002",
59 | "K04302", "M00001", "M00002", "M00003", "M00303", "M00401",
60 | "M00402", "M005011", "O00901", "O02101", "P00103", "P00104",
61 | "P00403", "P00404", "P006", "P00901", "P01101", "P013", "P015",
62 | "P02001", "P01601", "P018", "P02002", "P023", "P02501", "P02602",
63 | "P02801", "P029", "P03202", "P035", "P03701", "P03702", "P03904",
64 | "P03905", "P03906", "P04001", "P04101", "P04102", "P042", "P04301",
65 | "P04302", "P04401", "P04405", "P04406", "P053", "P05402", "P05403",
66 | "P05405", "P05406", "P05408", "P05409", "P05411", "P05412",
67 | "P05414", "P05415", "P05417", "P05418", "P05421", "P05422",
68 | "P05601", "P05602", "P05603", "P05604", "P05605", "P057", "P5701",
69 | "P05801", "P05802", "P05901", "P05902", "P05903", "P05904",
70 | "Q003", "Q031", "Q061", "Q064", "Q070", "Q075", "Q080", "Q085",
71 | "Q08901", "Q09301", "Q111", "Q11701", "Q12201", "Q125", "Q133",
72 | "R012", "R025", "R027", "S066", "S06701", "S06702", "S06703",
73 | "S06901", "S06902", "S099", "S09901", "S11001", "S11801",
74 | "U02303", "U02403", "Z00101", "Z00102", "Z002", "Z003", "Z01401",
75 | "Z01402", "Y00101", "AA00701", "AA00702", "AA00801", "AA00802",
76 | "AA02001", "AA02002", "AA02101", "AA02102", "AA02103", "AA02104",
77 | "AA022", "AA02601", "AA02602", "AA02701", "AA02702", "AA03601",
78 | "AA03602", "AA03701", "AA03702", "AA03703", "AA03704", "AA038",
79 | "W00201", "W00101", "W00202", "W00102", "W00203", "W00103",
80 | "Deflator")
81 | vars <- names(data_pns)
82 | varsc <- vars[sapply(data_pns, class) == "character"]
83 | varsf <- setdiff(varsc, notlabel)
84 | for (i in 1:length(varsf)) {
85 | if (i > 0 & varsf[i] %in% (dictionary$X__3)) {
86 | data_pns[varsf[i]] <- factor(suppressWarnings(as.numeric(unlist(data_pns[varsf[i]]))),
87 | levels=suppressWarnings(as.numeric(unlist(dictionary %>% subset(X__3 == varsf[i]) %>% select(X__6)))),
88 | labels=unlist(dictionary %>% subset(X__3 == varsf[i]) %>% select(X__7)))
89 | }
90 | }
91 | }
92 | else {
93 | message("The microdata object is not of the tibble class or sample design was already defined for microdata, so labeling categorical variables is not possible.\n")
94 | }
95 | return(data_pns)
96 | }
97 |
--------------------------------------------------------------------------------
/R/get_pns.R:
--------------------------------------------------------------------------------
1 | #' Download, label, deflate and create survey design object for PNS microdata
2 | #' @description Core function of package. With this function only, the user can download a PNS microdata from a year and get a sample design object ready to use with \code{survey} package functions.
3 | #' @import dplyr httr magrittr projmgr RCurl readr readxl survey tibble timeDate utils
4 | #' @param year The year of the data to be downloaded. Must be a number equal to 2013 or 2019. Vector not accepted.
5 | #' @param selected Logical value. If \code{TRUE}, the specific questionnaire for selected resident will be used. If \code{FALSE}, the basic questionnaire for household and residents will be used.
6 | #' @param anthropometry Logical value. If \code{TRUE}, the specific questionnaire for the anthropometry module of the selected resident will be used. If \code{FALSE}, the questionnaire defined by the \code{selected} argument of this function will be used. This argument will be used only if \code{year} is equal to 2019.
7 | #' @param vars Vector of variable names to be kept for analysis. Default is to keep all variables.
8 | #' @param labels Logical value. If \code{TRUE}, categorical variables will presented as factors with labels corresponding to the survey's dictionary.
9 | #' @param deflator Logical value. If \code{TRUE}, deflator variables will be available for use in the microdata.
10 | #' @param design Logical value. If \code{TRUE}, will return an object of class \code{survey.design} or \code{svyrep.design}. It is strongly recommended to keep this parameter as \code{TRUE} for further analysis. If \code{FALSE}, only the microdata will be returned.
11 | #' @param reload Logical value. If \code{TRUE}, will re-download the files even if they already exist in the save directory. If \code{FALSE}, will be checked if the files already exist in the save directory and the download will not be performed repeatedly, be careful with coinciding names of microdata files.
12 | #' @param curlopts A named list object identifying the curl options for the handle when using functions from \code{RCurl} package.
13 | #' @param savedir Directory to save the downloaded data. Default is to use a temporary directory.
14 | #' @return An object of class \code{survey.design} or \code{svyrep.design} with the data from PNS and its sample design, or a tibble with selected variables of the microdata, including the necessary survey design ones.
15 | #' @note For more information, visit the survey official website <\url{https://www.ibge.gov.br/estatisticas/sociais/saude/9160-pesquisa-nacional-de-saude.html?=&t=o-que-e}> and consult the other functions of this package, described below.
16 | #' @seealso \link[PNSIBGE]{read_pns} for reading PNS microdata.\cr \link[PNSIBGE]{pns_labeller} for labeling categorical variables from PNS microdata.\cr \link[PNSIBGE]{pns_deflator} for adding deflator variables to PNS microdata.\cr \link[PNSIBGE]{pns_design} for creating PNS survey design object.\cr \link[PNSIBGE]{pns_example} for getting the path of the PNS toy example files.
17 | #' @examples
18 | #' \donttest{
19 | #' pns.svy <- get_pns(year=2019, selected=FALSE, anthropometry=FALSE, vars=c("J007","J009"),
20 | #' labels=TRUE, deflator=TRUE, design=TRUE,
21 | #' reload=TRUE, curlopts=list(), savedir=tempdir())
22 | #' # Calculating proportion of people diagnosed with chronic diseases
23 | #' if (!is.null(pns.svy)) survey::svymean(x=~J007, design=pns.svy, na.rm=TRUE)
24 | #' pns.svy2 <- get_pns(year=2019, selected=TRUE, anthropometry=FALSE, vars=c("N001","N00101"),
25 | #' labels=TRUE, deflator=TRUE, design=TRUE,
26 | #' reload=TRUE, curlopts=list(), savedir=tempdir())
27 | #' # Calculating proportion of people's self-rated health
28 | #' if (!is.null(pns.svy2)) survey::svymean(x=~N001, design=pns.svy2, na.rm=TRUE)
29 | #' pns.svy3 <- get_pns(year=2019, selected=FALSE, anthropometry=TRUE, vars=c("W00101","W00201"),
30 | #' labels=TRUE, deflator=TRUE, design=TRUE,
31 | #' reload=TRUE, curlopts=list(), savedir=tempdir())
32 | #' # Calculating the average weight of people
33 | #' if (!is.null(pns.svy3)) survey::svymean(x=~W00101, design=pns.svy3, na.rm=TRUE)}
34 | #' @export
35 |
36 | get_pns <- function(year, selected = FALSE, anthropometry = FALSE, vars = NULL,
37 | labels = TRUE, deflator = TRUE, design = TRUE, reload = TRUE, curlopts = list(), savedir = tempdir())
38 | {
39 | if (year != 2013 & year != 2019) {
40 | message("Year must be equal to 2013 or 2019.\n")
41 | return(NULL)
42 | }
43 | if (!(selected %in% c(TRUE, FALSE))) {
44 | selected <- FALSE
45 | message("Invalid value provided for selected argument, so default value FALSE was set to this argument.\n")
46 | }
47 | if (!(anthropometry %in% c(TRUE, FALSE))) {
48 | anthropometry <- FALSE
49 | message("Invalid value provided for anthropometry argument, so default value FALSE was set to this argument.\n")
50 | }
51 | if (!(labels %in% c(TRUE, FALSE))) {
52 | labels <- TRUE
53 | message("Invalid value provided for labels argument, so default value TRUE was set to this argument.\n")
54 | }
55 | if (!(deflator %in% c(TRUE, FALSE))) {
56 | deflator <- TRUE
57 | message("Invalid value provided for deflator argument, so default value TRUE was set to this argument.\n")
58 | }
59 | if (!(design %in% c(TRUE, FALSE))) {
60 | design <- TRUE
61 | message("Invalid value provided for design argument, so default value TRUE was set to this argument.\n")
62 | }
63 | if (!(reload %in% c(TRUE, FALSE))) {
64 | reload <- TRUE
65 | message("Invalid value provided for reload argument, so default value TRUE was set to this argument.\n")
66 | }
67 | if (!is.list(curlopts)) {
68 | curlopts <- list()
69 | message("Invalid value provided for curlopts argument, as the value of this argument needs to be a list, so the value provided will be ignored.\n")
70 | }
71 | if (!dir.exists(savedir)) {
72 | savedir <- tempdir()
73 | message(paste0("The directory provided does not exist, so the directory was set to '", savedir), "'.\n")
74 | }
75 | if (savedir != tempdir()) {
76 | printpath <- TRUE
77 | }
78 | else {
79 | printpath <- FALSE
80 | }
81 | if (substr(savedir, nchar(savedir), nchar(savedir)) == "/" | substr(savedir, nchar(savedir), nchar(savedir)) == "\\") {
82 | savedir <- substr(savedir, 1, nchar(savedir)-1)
83 | }
84 | ftpdir <- paste0("https://ftp.ibge.gov.br/PNS/", year, "/Microdados/")
85 | if (!projmgr::check_internet()) {
86 | message("The internet connection is unavailable.\n")
87 | return(NULL)
88 | }
89 | if (httr::http_error(httr::GET(ftpdir, httr::timeout(60)))) {
90 | message("The microdata server is unavailable.\n")
91 | return(NULL)
92 | }
93 | restime <- getOption("timeout")
94 | on.exit(options(timeout=restime))
95 | options(timeout=max(600, restime))
96 | ftpdata <- paste0(ftpdir, "Dados/")
97 | datayear <- unlist(strsplit(unlist(strsplit(unlist(strsplit(gsub("\r\n", "\n", RCurl::getURL(ftpdata, dirlistonly=TRUE, .opts=curlopts)), "\n")), " 1) {
104 | message("There is more than one file available for the requested microdata, please contact the package maintainer.\n")
105 | return(NULL)
106 | }
107 | else {
108 | dataname <- paste0(dataname, ".zip")
109 | }
110 | if (reload == FALSE & file.exists(paste0(savedir, "/", dataname))) {
111 | message("The reload argument was defined as FALSE and the file of microdata was already downloaded, so the download process will not execute again.\n")
112 | }
113 | else {
114 | utils::download.file(url=paste0(ftpdata, dataname), destfile=paste0(savedir, "/", dataname), mode="wb")
115 | if (suppressWarnings(class(try(utils::unzip(zipfile=paste0(savedir, "/", dataname), exdir=savedir), silent=TRUE)) == "try-error")) {
116 | message("The directory defined to save the downloaded data is denied permission to overwrite the existing files, please clear or change this directory.\n")
117 | return(NULL)
118 | }
119 | if (reload == FALSE) {
120 | message("The definition of FALSE for the reload argument will be ignored, since the file of microdata was not downloaded yet.\n")
121 | }
122 | }
123 | utils::unzip(zipfile=paste0(savedir, "/", dataname), exdir=savedir)
124 | docfiles <- unlist(strsplit(unlist(strsplit(unlist(strsplit(gsub("\r\n", "\n", RCurl::getURL(paste0(ftpdir, "Documentacao/"), dirlistonly=TRUE, .opts=curlopts)), "\n")), "