Skip to content

Commit

Permalink
Merge pull request #325 from PSLmodels/pr-initial-cut-at-state-targets
Browse files Browse the repository at this point in the history
PR initial cut at state targets
  • Loading branch information
donboyd5 authored Dec 20, 2024
2 parents 119fe6c + cec1278 commit b9c929b
Show file tree
Hide file tree
Showing 17 changed files with 793 additions and 75 deletions.
46 changes: 23 additions & 23 deletions tmd/areas/targets/prepare/prepare_cds/renv.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"R": {
"Version": "4.4.1",
"Version": "4.4.2",
"Repositories": [
{
"Name": "CRAN",
Expand Down Expand Up @@ -166,13 +166,13 @@
},
"bit": {
"Package": "bit",
"Version": "4.5.0",
"Version": "4.5.0.1",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R"
],
"Hash": "5dc7b2677d65d0e874fc4aaf0e879987"
"Hash": "f89f074e0e49bf1dbe3eba0a15a91476"
},
"bit64": {
"Package": "bit64",
Expand Down Expand Up @@ -371,13 +371,13 @@
},
"cpp11": {
"Package": "cpp11",
"Version": "0.5.0",
"Version": "0.5.1",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R"
],
"Hash": "91570bba75d0c9d3f1040c835cee8fba"
"Hash": "9df43854f1c84685d095ed6270b52387"
},
"crayon": {
"Package": "crayon",
Expand Down Expand Up @@ -406,24 +406,24 @@
},
"curl": {
"Package": "curl",
"Version": "6.0.0",
"Version": "6.0.1",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R"
],
"Hash": "ff51697d9205fe715f29e7171e874c6e"
"Hash": "e8ba62486230951fcd2b881c5be23f96"
},
"data.table": {
"Package": "data.table",
"Version": "1.16.2",
"Version": "1.16.4",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R",
"methods"
],
"Hash": "2e00b378fc3be69c865120d9f313039a"
"Hash": "38bbf05fc2503143db4c734a7e5cab66"
},
"dbplyr": {
"Package": "dbplyr",
Expand Down Expand Up @@ -560,15 +560,15 @@
},
"fontawesome": {
"Package": "fontawesome",
"Version": "0.5.2",
"Version": "0.5.3",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R",
"htmltools",
"rlang"
],
"Hash": "c2efdd5f0bcd1ea861c2d4e2a883a67d"
"Hash": "bd1297f9b5b1fc1372d19e2c4cd82215"
},
"forcats": {
"Package": "forcats",
Expand Down Expand Up @@ -985,14 +985,14 @@
},
"later": {
"Package": "later",
"Version": "1.3.2",
"Version": "1.4.1",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"Rcpp",
"rlang"
],
"Hash": "a3e051d405326b8b0012377434c62b37"
"Hash": "501744395cac0bab0fbcfab9375ae92c"
},
"lattice": {
"Package": "lattice",
Expand Down Expand Up @@ -1034,7 +1034,7 @@
},
"lubridate": {
"Package": "lubridate",
"Version": "1.9.3",
"Version": "1.9.4",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
Expand All @@ -1043,7 +1043,7 @@
"methods",
"timechange"
],
"Hash": "680ad542fbcf801442c83a6ac5a2126c"
"Hash": "be38bc740fc51783a78edb5a157e4104"
},
"magrittr": {
"Package": "magrittr",
Expand Down Expand Up @@ -1137,7 +1137,7 @@
},
"nlme": {
"Package": "nlme",
"Version": "3.1-165",
"Version": "3.1-166",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
Expand All @@ -1147,17 +1147,17 @@
"stats",
"utils"
],
"Hash": "2769a88be217841b1f33ed469675c3cc"
"Hash": "ccbb8846be320b627e6aa2b4616a2ded"
},
"openssl": {
"Package": "openssl",
"Version": "2.2.2",
"Version": "2.3.0",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"askpass"
],
"Hash": "d413e0fef796c9401a4419485f709ca1"
"Hash": "5bfe2927efa9f87766ca9605301ea48f"
},
"pillar": {
"Package": "pillar",
Expand Down Expand Up @@ -1225,7 +1225,7 @@
},
"promises": {
"Package": "promises",
"Version": "1.3.0",
"Version": "1.3.2",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
Expand All @@ -1237,7 +1237,7 @@
"rlang",
"stats"
],
"Hash": "434cd5388a3979e74be5c219bcd6e77d"
"Hash": "c84fd4f75ea1f5434735e08b7f50fbca"
},
"proxy": {
"Package": "proxy",
Expand Down Expand Up @@ -1665,7 +1665,7 @@
},
"textshaping": {
"Package": "textshaping",
"Version": "0.4.0",
"Version": "0.4.1",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
Expand All @@ -1674,7 +1674,7 @@
"lifecycle",
"systemfonts"
],
"Hash": "5142f8bc78ed3d819d26461b641627ce"
"Hash": "573e0d015b7fc3e555f83e254cad7533"
},
"tibble": {
"Package": "tibble",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
---
output: html_document
editor_options:
chunk_output_type: console
---

# How to write target files

Previous code in this project culminated in creation of "enhanced_targets.csv", stored in the "data/intermediate/" folder. These data contain more than 1,800 potential targets for each state for 2021, based largely on SOI Historical Table 2 data for the state.

The next task, which this page discusses but does not implement, is to choose which potential targets to use, to map these potential targets to TMD variables, to choose which states to include, and to write \[xx\]\_targets.csv files for those states.

This page does not write those target files. Instead, a utility program that users can run from the command line, \`\`, reads a json file that defines variables and states to write targets for, and writes the state target files.

The remainder of this page (1) shows information about the enhanced targets file, and (2) explains how to run the utility program.

```{r}
#| label: setup
#| output: false
suppressPackageStartupMessages(source(here::here("R", "libraries.R")))
source(here::here("R", "constants.R"))
source(here::here("R", "functions.R"))
```

```{r}
#| label: get-etargets
#| output: false
etargets <- read_csv(fs::path(DINTERMEDIATE, "enhanced_targets.csv"))
```

## Show information about enhanced targets data

```{r}
#| label: show-etargets
cat('"glimpse" the structure of the data')
glimpse(etargets)
cat("summarize the data")
skim(etargets)
```

### Browse the data for a single state

The sortable and filter-able table below shows the 2021 potential-target data for one of the Phase 6 states, Minnesota. The purpose is simply to make clear the kind of information that is available for targeting. We include just one state to keep the demands on this web page minimal.

By selecting agistub 0, you can see one record per potential target. Putting 18400 in the upper right search box will show all entries related to e18400, "State and local income or sales tax",

::: {.cell-output-display style="font-size: 60%;"}
```{r}
#| label: etargets-table
#| column: page
etargets |>
filter(stabbr %in% c("MN")) |>
mutate(across(c(sort, count, scope, fstatus, agistub, basesoivname, soivname), factor)) |>
DT::datatable(rownames = FALSE,
caption = htmltools::tags$caption(
style = 'caption-side: top; text-align: center; color: black; font-size: 200%;',
"Potential targets for Minnesota"
),
options = list(order = list(0, "asc"), # use 1st column (0) for sorting
scrollX = TRUE, scrollY = TRUE,
paging = TRUE, pageLength = 20,
# autoWidth = TRUE,
columnDefs = list(list(width = '15px', targets = c("stabbr", "sort", "count",
"scope", "fstatus", "agistub")),
list(width = '15px', targets = c("soivname", "basesoivname")))),
filter="top",
escape = FALSE,
class = "compact") |> # A default DT class that makes the table more compact
formatRound(c("target"), digits = 0)
```
:::

### Reminder of what the target files will look like

The screenshot below shows the first few rows of a typical target file. The utility program described below will select targets and states defined in a json file and write a target file in that format.

![](images/clipboard-754780137.png)

## How to run the utility program that writes \[xx\]\_targets.csv files

It's a two-step process: (1) create a json file that defines what targets to write, and (2) run an R script that reads the json file and creates the desired target files.

### The json file

### Running the R script

From a terminal in the prepare_states folder enter:

`Rscript create_state_targets.R phase6.json`

## Additional notes

```{r}
#| label: notes
#| output: false
# documentation for the targets.csv data file
# sample file excerpt
# varname,count,scope,agilo,agihi,fstatus,target
# XTOT, 0, 0,-9e99, 9e99, 0, 33e6
# e00300, 0, 1,-9e99, 9e99, 0, 20e9
# e00900, 0, 1,-9e99, 9e99, 0, 30e9
# e00200, 0, 1,-9e99, 9e99, 0,1000e9
# e02000, 0, 1,-9e99, 9e99, 0, 30e9
# e02400, 0, 1,-9e99, 9e99, 0, 60e9
# varname: any Tax-Calculator input variable name plus any Tax-Calculator calculated variable in the list of cached variables in the tmd/storage/__init__.py file
# count: integer in [0,4] range:
# count==0 implies dollar total of varname is tabulated
# count==1 implies number of tax units with any value of varname is tabulated
# count==2 implies number of tax units with a nonzero value of varname is tabulated
# count==3 implies number of tax units with a positive value of varname is tabulated
# count==4 implies number of tax units with a negative value of varname is tabulated
# scope: integer in [0,2] range:
# scope==0 implies all tax units are tabulated
# scope==1 implies only PUF-derived filing units are tabulated
# scope==2 implies only CPS-derived filing units are tabulated
# agilo: float representing lower bound of the AGI range (which is included in the range) that is tabulated.
# agihi: float representing upper bound of the AGI range (which is excluded from the range) that is tabulated.
# fstatus: integer in [0,5] range:
# fstatus=0 implies all filing statuses are tabulated
# other fstatus values imply just the tax units with the Tax-Calculator MARS variable equal to fstatus are included in the tabulation
# target: target amount:
# dollars if count==0
# number of tax units if count>0
```
2 changes: 2 additions & 0 deletions tmd/areas/targets/prepare/prepare_states/R/constants.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# NJ, NM, VA, AK, MN
PHASE6_STATES <- c("AK", "MN", "NJ", "NM", "VA")

DRAW <- here::here("data", "data_raw")
DINTERMEDIATE <- here::here("data", "intermediate")
Expand Down
19 changes: 8 additions & 11 deletions tmd/areas/targets/prepare/prepare_states/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ project:


# site info:
# OLD Unique deploy URL: https://671e13320a7e7cfb68b1ba7d--tmd-areas-prepare-targets.netlify.app
# site ID: 9c6bf4ca-1b88-4735-9c29-ec5a41dab2ef
# url: https://tmd-areas-prepare-state-targets.netlify.app

# publishing with netlify cli:
Expand Down Expand Up @@ -43,21 +43,18 @@ book:
- download_soi_data.qmd
- construct_soi_documentation.qmd
- construct_long_soi_data_file.qmd
- part: "Analysis of SALT variables and other issues"
chapters:
- SALT_analysis.qmd
- part: "Create data from which to extract state target files"
chapters:
- create_state_targets_basefile.qmd
- create_additional_state_targets.qmd
- combine_base_and_additional_targets.qmd
# old files maybe use as base for new work
# - cd_create_variable_mapping.qmd
# - cd_compare_us_totals_tmd_vs_irs_published.qmd
# - cd_enhance_basefile_with_special_targets.qmd
# appendices:
# - cd_issues_and_TODOs.qmd
# - cd_IRS_documentation.qmd
- part: "Writing target files"
chapters:
- 50_state_data_underlying_target_files.qmd
appendices:
- notes_on_areas_and_targets.qmd
- compare_tmdsums_vs_soisums_keyvars.qmd
- SALT_analysis.qmd

format:
html:
Expand Down
Loading

0 comments on commit b9c929b

Please sign in to comment.