Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
ahs
  • Loading branch information
ajdamico committed Apr 16, 2018
1 parent d647f8e commit d52d580
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 94 deletions.
12 changes: 9 additions & 3 deletions 07-acs.Rmd
Expand Up @@ -324,11 +324,17 @@ stopifnot( round( coef( svytotal( ~ one , acs_design ) ) , 0 ) == 4863300 )
Compute the population by age:

```{r eval = FALSE , results = "hide" }
pums_estimate <- c( 285681 , 314701 , 300814 , 334318 , 327896 , 629329 , 599719 , 644212 , 342205 , 300254 , 464893 , 231293 , 87985 )
pums_estimate <-
c( 285681 , 314701 , 300814 , 334318 , 327896 , 629329 , 599719 , 644212 ,
342205 , 300254 , 464893 , 231293 , 87985 )
pums_standard_error <- c( 2888 , 5168 , 5009 , 3673 , 3521 , 4825 , 4088 , 4398 , 5329 , 5389 , 1938 , 3214 , 2950 )
pums_standard_error <-
c( 2888 , 5168 , 5009 , 3673 , 3521 , 4825 , 4088 ,
4398 , 5329 , 5389 , 1938 , 3214 , 2950 )
pums_margin_of_error <- c( 4751 , 8501 , 8240 , 6043 , 5792 , 7937 , 6725 , 7234 , 8767 , 8865 , 3188 , 5287 , 4853 )
pums_margin_of_error <-
c( 4751 , 8501 , 8240 , 6043 , 5792 , 7937 , 6725 ,
7234 , 8767 , 8865 , 3188 , 5287 , 4853 )
results <-
svytotal(
Expand Down
111 changes: 76 additions & 35 deletions 10-ahs.Rmd
Expand Up @@ -30,8 +30,8 @@ ahs_cat <-
get_catalog( "ahs" ,
output_dir = file.path( path.expand( "~" ) , "AHS" ) )
# 2015 only
ahs_cat <- subset( ahs_cat , year == 2015 )
# 2013 only
ahs_cat <- subset( ahs_cat , year == 2013 )
# download the microdata to your local computer
ahs_cat <- lodown( "ahs" , ahs_cat )
```
Expand All @@ -50,13 +50,13 @@ library(survey)
ahs_df <-
readRDS(
file.path( path.expand( "~" ) , "AHS" ,
"2015/national_v2.1/household.rds"
"2013/national_v1.2/newhouse_repwgt.rds"
)
)
ahs_design <-
svrepdesign(
weights = ~weight,
weights = ~wgt90geo,
repweights = "repwgt[1-9]" ,
type = "Fay" ,
rho = ( 1 - 1 / sqrt( 4 ) ) ,
Expand All @@ -73,20 +73,31 @@ ahs_design <-
update(
ahs_design ,
occupant =
ifelse( tenure == 1 , "owner" ,
ifelse( tenure %in% 2:3 , "renter" ,
"not occupied" ) ) ,
tenure =
factor(
ifelse( is.na( tenure ) , 4 , tenure ) ,
levels = 1:4 ,
labels =
c( 'Owned or being bought' ,
'Rented for cash rent' ,
'Occupied without payment of cash rent' ,
'Not occupied' )
) ,
lotsize =
factor( lotsize , levels = 1:7 ,
factor(
1 + findInterval( lot ,
c( 5500 , 11000 , 22000 ,
44000 , 220000 , 440000 ) ) ,
levels = 1:7 ,
labels = c( "Less then 1/8 acre" ,
"1/8 up to 1/4 acre" , "1/4 up to 1/2 acre" ,
"1/2 up to 1 acre" , "1 up to 5 acres" ,
"5 up to 10 acres" , "10 acres or more" ) ) ,
below_poverty = as.numeric( perpovlvl < 100 )
below_poverty = as.numeric( poor < 100 )
)
```
Expand All @@ -97,54 +108,54 @@ Count the unweighted number of records in the survey sample, overall and by grou
```{r eval = FALSE , results = "hide" }
sum( weights( ahs_design , "sampling" ) != 0 )
svyby( ~ one , ~ occupant , ahs_design , unwtd.count )
svyby( ~ one , ~ tenure , ahs_design , unwtd.count )
```

### Weighted Counts {-}
Count the weighted size of the generalizable population, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ one , ahs_design )
svyby( ~ one , ~ occupant , ahs_design , svytotal )
svyby( ~ one , ~ tenure , ahs_design , svytotal )
```

### Descriptive Statistics {-}

Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svymean( ~ totrooms , ahs_design )
svymean( ~ rooms , ahs_design )
svyby( ~ totrooms , ~ occupant , ahs_design , svymean )
svyby( ~ rooms , ~ tenure , ahs_design , svymean )
```

Calculate the distribution of a categorical variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svymean( ~ lotsize , ahs_design , na.rm = TRUE )
svyby( ~ lotsize , ~ occupant , ahs_design , svymean , na.rm = TRUE )
svyby( ~ lotsize , ~ tenure , ahs_design , svymean , na.rm = TRUE )
```

Calculate the sum of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ totrooms , ahs_design )
svytotal( ~ rooms , ahs_design )
svyby( ~ totrooms , ~ occupant , ahs_design , svytotal )
svyby( ~ rooms , ~ tenure , ahs_design , svytotal )
```

Calculate the weighted sum of a categorical variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ lotsize , ahs_design , na.rm = TRUE )
svyby( ~ lotsize , ~ occupant , ahs_design , svytotal , na.rm = TRUE )
svyby( ~ lotsize , ~ tenure , ahs_design , svytotal , na.rm = TRUE )
```

Calculate the median (50th percentile) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svyquantile( ~ totrooms , ahs_design , 0.5 )
svyquantile( ~ rooms , ahs_design , 0.5 )
svyby(
~ totrooms ,
~ occupant ,
~ rooms ,
~ tenure ,
ahs_design ,
svyquantile ,
0.5 ,
Expand All @@ -156,7 +167,7 @@ svyby(
Estimate a ratio:
```{r eval = FALSE , results = "hide" }
svyratio(
numerator = ~ totrooms ,
numerator = ~ rooms ,
denominator = ~ rent ,
ahs_design ,
na.rm = TRUE
Expand All @@ -171,14 +182,14 @@ sub_ahs_design <- subset( ahs_design , garage == 1 )
```
Calculate the mean (average) of this subset:
```{r eval = FALSE , results = "hide" }
svymean( ~ totrooms , sub_ahs_design )
svymean( ~ rooms , sub_ahs_design )
```

### Measures of Uncertainty {-}

Extract the coefficient, standard error, confidence interval, and coefficient of variation from any descriptive statistics function result, overall and by groups:
```{r eval = FALSE , results = "hide" }
this_result <- svymean( ~ totrooms , ahs_design )
this_result <- svymean( ~ rooms , ahs_design )
coef( this_result )
SE( this_result )
Expand All @@ -187,8 +198,8 @@ cv( this_result )
grouped_result <-
svyby(
~ totrooms ,
~ occupant ,
~ rooms ,
~ tenure ,
ahs_design ,
svymean
)
Expand All @@ -206,16 +217,16 @@ degf( ahs_design )

Calculate the complex sample survey-adjusted variance of any statistic:
```{r eval = FALSE , results = "hide" }
svyvar( ~ totrooms , ahs_design )
svyvar( ~ rooms , ahs_design )
```

Include the complex sample design effect in the result for a specific statistic:
```{r eval = FALSE , results = "hide" }
# SRS without replacement
svymean( ~ totrooms , ahs_design , deff = TRUE )
svymean( ~ rooms , ahs_design , deff = TRUE )
# SRS with replacement
svymean( ~ totrooms , ahs_design , deff = "replace" )
svymean( ~ rooms , ahs_design , deff = "replace" )
```

Compute confidence intervals for proportions using methods that may be more accurate near 0 and 1. See `?svyciprop` for alternatives:
Expand All @@ -228,7 +239,7 @@ svyciprop( ~ below_poverty , ahs_design ,

Perform a design-based t-test:
```{r eval = FALSE , results = "hide" }
svyttest( totrooms ~ below_poverty , ahs_design )
svyttest( rooms ~ below_poverty , ahs_design )
```

Perform a chi-squared test of association for survey data:
Expand All @@ -243,7 +254,7 @@ Perform a survey-weighted generalized linear model:
```{r eval = FALSE , results = "hide" }
glm_result <-
svyglm(
totrooms ~ below_poverty + lotsize ,
rooms ~ below_poverty + lotsize ,
ahs_design
)
Expand All @@ -261,18 +272,48 @@ ahs_srvyr_design <- as_survey( ahs_design )
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
ahs_srvyr_design %>%
summarize( mean = survey_mean( totrooms ) )
summarize( mean = survey_mean( rooms ) )
ahs_srvyr_design %>%
group_by( occupant ) %>%
summarize( mean = survey_mean( totrooms ) )
group_by( tenure ) %>%
summarize( mean = survey_mean( rooms ) )
```

---

## Replication Example {-}

The example below matches statistics and standard errors from this table pulled from the US Census Bureau's [Quick Guide to Estimating Variance Using Replicate Weights](https://www.census.gov/content/dam/Census/programs-surveys/ahs/tech-documentation/2015/Quick%20Guide%20to%20Estimating%20Variance%20Using%20Replicate%20Weights%202009%20to%20Current.pdf):

`r knitr::include_graphics("images/ahs2013.png")`

Compute the statistics and standard errors for monthly housing costs by owner/renter status of the unit:

```{r eval = FALSE , results = "hide" }
means <- c( 1241.8890 , 972.6051 , 170.0121 )
std_err <- c( 7.3613 , 5.6956 , 6.1586 )
ci_lb <- c( 1227.3511 , 961.3569 , 157.8495 )
ci_ub <- c( 1256.4270 , 983.8532 , 182.1747 )
results <-
svyby(
~ zsmhc ,
~ tenure ,
ahs_design ,
svymean ,
na.rm = TRUE ,
na.rm.all = TRUE
)
ci_res <-
confint( results , df = degf( ahs_design ) + 1 )
stopifnot( all( round( coef( results ) , 4 ) == means ) )
stopifnot( all( round( SE( results ) , 4 ) == std_err ) )
stopifnot( all( round( ci_res[ , 1 ] , 4 ) == ci_lb ) )
stopifnot( all( round( ci_res[ , 2 ] , 4 ) == ci_ub ) )
```

12 changes: 9 additions & 3 deletions docs/american-community-survey-acs.html
Expand Up @@ -1186,11 +1186,17 @@ <h2>Replication Example</h2>
<p>Match the sum of the weights:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">stopifnot</span>( <span class="kw">round</span>( <span class="kw">coef</span>( <span class="kw">svytotal</span>( <span class="op">~</span><span class="st"> </span>one , acs_design ) ) , <span class="dv">0</span> ) <span class="op">==</span><span class="st"> </span><span class="dv">4863300</span> )</code></pre></div>
<p>Compute the population by age:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">pums_estimate &lt;-<span class="st"> </span><span class="kw">c</span>( <span class="dv">285681</span> , <span class="dv">314701</span> , <span class="dv">300814</span> , <span class="dv">334318</span> , <span class="dv">327896</span> , <span class="dv">629329</span> , <span class="dv">599719</span> , <span class="dv">644212</span> , <span class="dv">342205</span> , <span class="dv">300254</span> , <span class="dv">464893</span> , <span class="dv">231293</span> , <span class="dv">87985</span> )
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">pums_estimate &lt;-<span class="st"> </span>
<span class="st"> </span><span class="kw">c</span>( <span class="dv">285681</span> , <span class="dv">314701</span> , <span class="dv">300814</span> , <span class="dv">334318</span> , <span class="dv">327896</span> , <span class="dv">629329</span> , <span class="dv">599719</span> , <span class="dv">644212</span> ,
<span class="dv">342205</span> , <span class="dv">300254</span> , <span class="dv">464893</span> , <span class="dv">231293</span> , <span class="dv">87985</span> )

pums_standard_error &lt;-<span class="st"> </span><span class="kw">c</span>( <span class="dv">2888</span> , <span class="dv">5168</span> , <span class="dv">5009</span> , <span class="dv">3673</span> , <span class="dv">3521</span> , <span class="dv">4825</span> , <span class="dv">4088</span> , <span class="dv">4398</span> , <span class="dv">5329</span> , <span class="dv">5389</span> , <span class="dv">1938</span> , <span class="dv">3214</span> , <span class="dv">2950</span> )
pums_standard_error &lt;-<span class="st"> </span>
<span class="st"> </span><span class="kw">c</span>( <span class="dv">2888</span> , <span class="dv">5168</span> , <span class="dv">5009</span> , <span class="dv">3673</span> , <span class="dv">3521</span> , <span class="dv">4825</span> , <span class="dv">4088</span> ,
<span class="dv">4398</span> , <span class="dv">5329</span> , <span class="dv">5389</span> , <span class="dv">1938</span> , <span class="dv">3214</span> , <span class="dv">2950</span> )

pums_margin_of_error &lt;-<span class="st"> </span><span class="kw">c</span>( <span class="dv">4751</span> , <span class="dv">8501</span> , <span class="dv">8240</span> , <span class="dv">6043</span> , <span class="dv">5792</span> , <span class="dv">7937</span> , <span class="dv">6725</span> , <span class="dv">7234</span> , <span class="dv">8767</span> , <span class="dv">8865</span> , <span class="dv">3188</span> , <span class="dv">5287</span> , <span class="dv">4853</span> )
pums_margin_of_error &lt;-<span class="st"> </span>
<span class="st"> </span><span class="kw">c</span>( <span class="dv">4751</span> , <span class="dv">8501</span> , <span class="dv">8240</span> , <span class="dv">6043</span> , <span class="dv">5792</span> , <span class="dv">7937</span> , <span class="dv">6725</span> ,
<span class="dv">7234</span> , <span class="dv">8767</span> , <span class="dv">8865</span> , <span class="dv">3188</span> , <span class="dv">5287</span> , <span class="dv">4853</span> )

results &lt;-
<span class="st"> </span><span class="kw">svytotal</span>(
Expand Down

0 comments on commit d52d580

Please sign in to comment.