Skip to content

Commit

Permalink
nychvs
Browse files Browse the repository at this point in the history
  • Loading branch information
ajdamico committed Nov 25, 2023
0 parents commit c33c89e
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
^.*\.Rproj$
^\.Rproj\.user$
^\.travis\.yml
^appveyor\.yml
^r\.yml

56 changes: 56 additions & 0 deletions .github/workflows/r.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Workflow derived from https://github.com/r-lib/actions/tree/master/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: R-CMD-check

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: macOS-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- {os: ubuntu-latest, r: 'release'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: rcmdcheck

- uses: r-lib/actions/check-r-package@v2

- name: Show testthat output
if: always()
run: find check -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload check results
if: failure()
uses: actions/upload-artifact@main
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: check
11 changes: 11 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Package: nychvs
Type: Package
Title: nychvs
Version: 0.1.0
Author: Anthony Joseph Damico
Maintainer: Anthony Joseph Damico <ajdamico@gmail.com>
Description: A continuous integration dummy package
License: GPL-3
Imports: survey, srvyr
Encoding: UTF-8
LazyData: true
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exportPattern("^[[:alpha:]]+")
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="https://github.com/asdfree/nychvs/actions"><img src="https://github.com/asdfree/nychvs/actions/workflows/r.yml/badge.svg" alt="Github Actions Badge"></a>
162 changes: 162 additions & 0 deletions tests/setup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# all i want is a
# room somewhere / with clawfoot tub
# and a frigidaire
nychvs_csv_import <-
function( this_url ){

tf <- tempfile()

download.file( this_url , tf , mode = 'wb' )

this_df <- read.csv( tf )

names( this_df ) <- tolower( names( this_df ) )

this_df
}
all_units_df <- nychvs_csv_import( "https://www2.census.gov/programs-surveys/nychvs/datasets/2021/microdata/allunits_puf_21.csv" )

occupied_units_df <- nychvs_csv_import( "https://www2.census.gov/programs-surveys/nychvs/datasets/2021/microdata/occupied_puf_21.csv" )

person_df <- nychvs_csv_import( "https://www2.census.gov/programs-surveys/nychvs/datasets/2021/microdata/person_puf_21.csv" )

vacant_units_df <- nychvs_csv_import( "https://www2.census.gov/programs-surveys/nychvs/datasets/2021/microdata/vacant_puf_21.csv" )

stopifnot( nrow( all_units_df ) == nrow( occupied_units_df ) + nrow( vacant_units_df ) )
before_nrow <- nrow( occupied_units_df )

occupied_units_df <- merge( all_units_df , occupied_units_df )

stopifnot( nrow( occupied_units_df ) == before_nrow )

before_nrow <- nrow( vacant_units_df )

vacant_units_df <- merge( all_units_df , vacant_units_df )

stopifnot( nrow( vacant_units_df ) == before_nrow )

stopifnot( nrow( nychvs_df ) == nrow( nychvs_df_person ) )

nychvs_df[ , 'one' ] <- 1
# nychvs_fn <- file.path( path.expand( "~" ) , "NYCHVS" , "this_file.rds" )
# saveRDS( nychvs_df , file = nychvs_fn , compress = FALSE )
# nychvs_df <- readRDS( nychvs_fn )
library(survey)

nychvs_design <-
svrepdesign(
weight = ~fw ,
repweights = 'fw[0-9]+' ,
scale = 4 / 80 ,
rscales = rep( 1 , 80 ) ,
mse = TRUE ,
type = 'JK1' ,
data = nychvs_df
)
nychvs_design <-
update(
nychvs_design ,

one = 1 ,

home_owners = as.numeric( sc115 == 1 ) ,

yearly_household_income = ifelse( uf42 == 9999999 , 0 , as.numeric( uf42 ) ) ,

gross_monthly_rent = ifelse( uf17 == 99999 , NA , as.numeric( uf17 ) ) ,

borough =
factor( boro , levels = 1:5 , labels =
c( 'Bronx' , 'Brooklyn' , 'Manhattan' ,
'Queens' , 'Staten Island' )
) ,

householder_sex = factor( hhr2 , labels = c( 'male' , 'female' ) )

)
sum( weights( nychvs_design , "sampling" ) != 0 )

svyby( ~ one , ~ borough , nychvs_design , unwtd.count )
svytotal( ~ one , nychvs_design )

svyby( ~ one , ~ borough , nychvs_design , svytotal )
svymean( ~ yearly_household_income , nychvs_design , na.rm = TRUE )

svyby( ~ yearly_household_income , ~ borough , nychvs_design , svymean , na.rm = TRUE )
svymean( ~ householder_sex , nychvs_design )

svyby( ~ householder_sex , ~ borough , nychvs_design , svymean )
svytotal( ~ yearly_household_income , nychvs_design , na.rm = TRUE )

svyby( ~ yearly_household_income , ~ borough , nychvs_design , svytotal , na.rm = TRUE )
svytotal( ~ householder_sex , nychvs_design )

svyby( ~ householder_sex , ~ borough , nychvs_design , svytotal )
svyquantile( ~ yearly_household_income , nychvs_design , 0.5 , na.rm = TRUE )

svyby(
~ yearly_household_income ,
~ borough ,
nychvs_design ,
svyquantile ,
0.5 ,
ci = TRUE , na.rm = TRUE
)
svyratio(
numerator = ~ gross_monthly_rent ,
denominator = ~ yearly_household_income ,
nychvs_design ,
na.rm = TRUE
)
sub_nychvs_design <- subset( nychvs_design , boro == 3 )
svymean( ~ yearly_household_income , sub_nychvs_design , na.rm = TRUE )
this_result <- svymean( ~ yearly_household_income , nychvs_design , na.rm = TRUE )

coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )

grouped_result <-
svyby(
~ yearly_household_income ,
~ borough ,
nychvs_design ,
svymean ,
na.rm = TRUE
)

coef( grouped_result )
SE( grouped_result )
confint( grouped_result )
cv( grouped_result )
degf( nychvs_design )
svyvar( ~ yearly_household_income , nychvs_design , na.rm = TRUE )
# SRS without replacement
svymean( ~ yearly_household_income , nychvs_design , na.rm = TRUE , deff = TRUE )

# SRS with replacement
svymean( ~ yearly_household_income , nychvs_design , na.rm = TRUE , deff = "replace" )
svyciprop( ~ home_owners , nychvs_design ,
method = "likelihood" )
svyttest( yearly_household_income ~ home_owners , nychvs_design )
svychisq(
~ home_owners + householder_sex ,
nychvs_design
)
glm_result <-
svyglm(
yearly_household_income ~ home_owners + householder_sex ,
nychvs_design
)

summary( glm_result )

library(srvyr)
nychvs_srvyr_design <- as_survey( nychvs_design )
nychvs_srvyr_design %>%
summarize( mean = survey_mean( yearly_household_income , na.rm = TRUE ) )

nychvs_srvyr_design %>%
group_by( borough ) %>%
summarize( mean = survey_mean( yearly_household_income , na.rm = TRUE ) )

0 comments on commit c33c89e

Please sign in to comment.