error using R survey for urban sanitation [message #13348] |
Thu, 26 October 2017 11:42 |
dhswes
Messages: 6 Registered: June 2017
|
Member |
|
|
Hello:
I would like to calculate the % of the urban population using a flush toilet connected to a sewer. For most countries, I can easily calculate this using the R Survey package, after inputting the .dta files. Here is the code:
country_recode <- read.dta(filename)
country_survey <- svydesign(id = country_recode$hv021, strata= country_recode$hv023, weights = country_recode$hv005/1000000, nest = TRUE, data=country_recode)
svyby(~hv205, ~hv025, country_survey, svymean)
For some .dta files, I receive this error:
"Error in sum(sapply(stratvars, function(m) !any(is.na(m)))) :
invalid 'type' (list) of argument"
Can anyone advise on what is the problem? For the countries I receive this error, such as Gabon, the recode file has values for hv025 and hv205.
Thanks,
Michael
|
|
|
Re: error using R survey for urban sanitation [message #13396 is a reply to message #13348] |
Tue, 31 October 2017 14:29 |
dhswes
Messages: 6 Registered: June 2017
|
Member |
|
|
As a follow-up, I think the problem is with the stratification. For the 2012 household recode file for Gabon, for instance, hv023 has only NA values. So, how does one calculate survey means for Gabon? I have been using the R survey package with this code:
country_recode <- read.dta(filename)
country_survey <- svydesign(id = country_recode$hv021, strata= country_recode$hv023, weights = country_recode$hv005/1000000, nest = TRUE, data=country_recode)
svyby(~hv205, ~hv025, country_survey, svymean)
I see results in the Stat Compiler for Gabon.
Thanks,
Michael
|
|
|
Re: error using R survey for urban sanitation [message #13514 is a reply to message #13396] |
Mon, 13 November 2017 08:57 |
Liz-DHS
Messages: 1516 Registered: February 2013
|
Senior Member |
|
|
A response from Dr. Shireen Assaf:
Quote:
Dear Michael,
Your code is correct. Not all surveys have the strata values stored in hv023, especially earlier surveys. For some surveys you need to construct the strata variable based on the stratification design used for that survey (usually by combining region and urban/rural place of residence). For some surveys the strata variable is stored in hv022 and not in hv023 and in some surveys the strata values are in both variables. For Gabon please use hv022 for the strata.
Thank you.
Shireen Assaf
|
|
|