Example: National Ambulatory Medical Care Survey (NAMCS) tables
Source:vignettes/Example-National-Ambulatory-Medical-Care-Survey-NAMCS-tables.Rmd
Example-National-Ambulatory-Medical-Care-Survey-NAMCS-tables.Rmd
This example uses the National Ambulatory Medical Care Survey (NAMCS) 2019 Public Use File (PUF) to replicate certain tables from the National Ambulatory Medical Care Survey: 2019 National Summary Tables. NAMCS is “an annual nationally representative sample survey of visits to non-federal office-based patient care physicians, excluding anesthesiologists, radiologists, and pathologists.” Note that the unit of observation is visits, not patients – this distinction is important since a single patient can make multiple visits.
Selected variables from NAMCS 2019 come with the
surveytable
package, for use in examples, in an object
called namcs2019sv
.
Begin
Begin by loading the surveytable
package.
Now, specify the survey that you’d like to analyze.
set_survey(namcs2019sv)
Survey info {NAMCS 2019 PUF} | ||
Variables | Observations | Design |
---|---|---|
Check the survey name, survey design variables, and the number of observations to verify that it all looks correct.
For this example, we do want to turn on certain NCHS-specific options, such as identifying low-precision estimates. If you do not care about identifying low-precision estimates, you can skip this command. To turn on the NCHS-specific options:
set_opts(mode = "NCHS")
## * Mode: NCHS.
Table 1
Counts and percentages
This table shows the overall estimated count as well as the counts and percentages by type of doctor, physician specialty, and metropolitan statistical area.
The variables that are necessary for creating this table are already in the survey, making the commands very straightforward.
total()
Total {NAMCS 2019 PUF} | ||||
n | Number (000) | SE (000) | LL (000) | UL (000) |
---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
tab("MDDO", "SPECCAT", "MSA")
Type of doctor (MD or DO) {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
Type of specialty (Primary, Medical, Surgical) {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
Metropolitan Statistical Area Status of physician location {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
Rates
The published table also shows several rates. To calculate rates, in
addition to the survey, we need a source of information with population
estimates. You would typically use a function such as
read.csv()
to load the population estimates and get them
into the correct format. The surveytable
package comes with
an object called uspop2019
that contains several population
estimates for use in these examples.
class(uspop2019)
## [1] "list"
names(uspop2019)
## [1] "total" "MSA" "AGER" "Age group" "SEX"
## [6] "AGER x SEX" "Age group 5"
Here is the overall population estimate:
uspop2019$total
## [1] 323186697
Once we have the overall population estimate, the overall rate is:
total_rate(uspop2019$total)
Total (rate per 100 population) {NAMCS 2019 PUF} | ||||
n | Rate | SE | LL | UL |
---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
To calculate the rates for a particular variable, we need to provide
a data frame with a variable called Level
that matches the
levels of the variable in the survey, and a variable called
Population
that gives the population size (which is assumed
to be a constant rather than a random variable).
For MSA
, we can see the levels of the variables just by
using the tab()
command, just as we did above. Thus, to
calculate rates, we need a data frame as follows:
uspop2019$MSA
## Level Population
## 1 MSA (Metropolitan Statistical Area) 277229518
## 2 Non-MSA 45957179
Now that we have the appropriate population estimates, the rate is:
tab_rate("MSA", uspop2019$MSA)
Metropolitan Statistical Area Status of physician location (rate per 100 population) {NAMCS 2019 PUF} | |||||
Level | n | Rate | SE | LL | UL |
---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
We can also calculate rates of a specific variable based on the entire population:
tab_rate("MDDO", uspop2019$total)
## * Rate based on the entire population.
Type of doctor (MD or DO) (rate per 100 population) {NAMCS 2019 PUF} | |||||
Level | n | Rate | SE | LL | UL |
---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
tab_rate("SPECCAT", uspop2019$total)
## * Rate based on the entire population.
Type of specialty (Primary, Medical, Surgical) (rate per 100 population) {NAMCS 2019 PUF} | |||||
Level | n | Rate | SE | LL | UL |
---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
Table 3
Counts and percentages
This table presents estimates for each age group, as well as for each age group by sex.
var_list("age")
Variables beginning with ‘age’ {NAMCS 2019 PUF} | ||
Variable | Class | Long name |
---|---|---|
The survey has a couple of relevant age-related variables.
AGE
is the patient age in years. AGER
is a
categorical variable based on AGE
. However, for this table,
in addition to AGER
, we need another age group variable,
with different age categories. We create it using the
var_cut
function.
var_cut("Age group", "AGE"
, c(-Inf, 0, 4, 14, 64, Inf)
, c("Under 1", "1-4", "5-14", "15-64", "65 and over") )
Now that we’ve created the Age group
variable, we can
create the tables:
tab("AGER", "Age group", "SEX")
Patient age recode {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
Age group {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
Patient sex {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
tab_cross("AGER", "SEX")
(Patient age recode) x (Patient sex) {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
Rates
tab_rate("AGER", uspop2019$AGER)
Patient age recode (rate per 100 population) {NAMCS 2019 PUF} | |||||
Level | n | Rate | SE | LL | UL |
---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
tab_rate("Age group", uspop2019$`Age group`)
## * Population for some levels not defined: 15-64
Age group (rate per 100 population) {NAMCS 2019 PUF} | |||||
Level | n | Rate | SE | LL | UL |
---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
tab_rate("SEX", uspop2019$SEX)
Patient sex (rate per 100 population) {NAMCS 2019 PUF} | |||||
Level | n | Rate | SE | LL | UL |
---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
To calculate the rates for one variable (AGER
) by
another variable (SEX
), we need population estimates in the
following format:
uspop2019$`AGER x SEX`
## Level Subset Population
## 1 Under 15 years Female 29604762
## 2 15-24 years Female 20730118
## 3 25-44 years Female 43192143
## 4 45-64 years Female 42508901
## 5 65-74 years Female 16673240
## 6 75 years and over Female 12421444
## 7 Under 15 years Male 30921894
## 8 15-24 years Male 20988582
## 9 25-44 years Male 42407267
## 10 45-64 years Male 40053148
## 11 65-74 years Male 14586962
## 12 75 years and over Male 9098236
Once we have these population estimates, the rates are:
tab_subset_rate("AGER", "SEX", uspop2019$`AGER x SEX`)
Patient age recode (Patient sex = Female) (rate per 100 population) {NAMCS 2019 PUF} | |||||
Level | n | Rate | SE | LL | UL |
---|---|---|---|---|---|
N = 4609. Checked NCHS presentation standards. Nothing to report. |
Patient age recode (Patient sex = Male) (rate per 100 population) {NAMCS 2019 PUF} | |||||
Level | n | Rate | SE | LL | UL |
---|---|---|---|---|---|
N = 3641. Checked NCHS presentation standards. Nothing to report. |
Table 5
This table gives the expected sources of payment. We use the
PAY*
variables to create several new variables that are
required by the table. Note that the PAY*
variables are
logical (TRUE
or FALSE
), which simplifies the
workflow. (The survey was imported into R using the
importsurvey
package, which automatically detects binary
variables and imports them as logical variables.)
#
var_all("Medicare and Medicaid", c("PAYMCARE", "PAYMCAID"))
#
var_any("Payment used", c("PAYPRIV", "PAYMCARE", "PAYMCAID"
, "PAYWKCMP", "PAYOTH", "PAYDK"))
var_not("No other payment used", "Payment used")
var_all("Self-pay", c("PAYSELF", "No other payment used"))
var_all("No charge", c("PAYNOCHG", "No other payment used"))
var_any("No insurance", c("Self-pay", "No charge"))
#
var_case("No pay", "NOPAY", "No categories marked")
var_any("Unknown or blank", c("PAYDK", "No pay"))
##
tab("PAYPRIV", "PAYMCARE", "PAYMCAID", "Medicare and Medicaid"
, "No insurance", "Self-pay", "No charge"
, "PAYWKCMP", "PAYOTH", "Unknown or blank")
Expected source of payment for visit: Private insurance {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
Expected source of payment for visit: Medicare {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
Expected source of payment for visit: Medicaid or CHIP or other state-based program {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
Medicare and Medicaid {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
No insurance {NAMCS 2019 PUF} | ||||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL | Flags |
---|---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards: Cx: suppress count (and rate); Px: suppress percent; Pc: footnote percent - complement. |
Self-pay {NAMCS 2019 PUF} | ||||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL | Flags |
---|---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards: Cx: suppress count (and rate); Px: suppress percent; Pc: footnote percent - complement. |
No charge {NAMCS 2019 PUF} | ||||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL | Flags |
---|---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards: Cx: suppress count (and rate). |
Expected source of payment for visit: Workers Compensation {NAMCS 2019 PUF} | ||||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL | Flags |
---|---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards: Cx: suppress count (and rate); Px: suppress percent; Pc: footnote percent - complement. |
Expected source of payment for visit: Other {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
Unknown or blank {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
Check the presentation standards flags! Under NCHS presentation standards rules, some of these estimates should not be shown.
Table 6
This table shows the primary care provider and referral status, by prior-visit status.
In the table, the “Unknown” and “Blank” values are collapsed into a
single value. We can collapse two or more levels of a factor into a
single level using the var_collapse
function.
var_collapse("PRIMCARE", "Unknown if PCP", c("Unknown", "Blank"))
var_collapse("REFER", "Unknown if referred", c("Unknown", "Blank"))
Now, for the table:
tab("PRIMCARE", "REFER", "SENBEFOR")
Are you the patient’s primary care provider? {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
Was patient referred for visit? {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
Has this patient been seen in your practice before? {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
The percentages within each subset that is defined by
SENBEFOR
add up to 100% – for this reason, we want to use
tab_subset()
, not tab_cross()
.
tab_subset("PRIMCARE", "SENBEFOR")
Are you the patient’s primary care provider? (Has this patient been seen in your practice before? = Yes, established patient) {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 6771. Checked NCHS presentation standards. Nothing to report. |
Are you the patient’s primary care provider? (Has this patient been seen in your practice before? = No, new patient) {NAMCS 2019 PUF} | ||||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL | Flags |
---|---|---|---|---|---|---|---|---|---|---|
N = 1479. Checked NCHS presentation standards: Cx: suppress count (and rate); Px: suppress percent. |
tab_subset("REFER", "SENBEFOR")
Was patient referred for visit? (Has this patient been seen in your practice before? = Yes, established patient) {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 6771. Checked NCHS presentation standards. Nothing to report. |
Was patient referred for visit? (Has this patient been seen in your practice before? = No, new patient) {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 1479. Checked NCHS presentation standards. Nothing to report. |
Table 11
This table shows the same information as Table 3, but only for preventive care visits. That is, estimates for each age group, as well as for each age group by sex, but only for preventive care visits.
Let’s create Age group
from AGE
and cross
AGER
and SEX
to create a variable called
Age x Sex
:
var_cut("Age group", "AGE"
, c(-Inf, 0, 4, 14, 64, Inf)
, c("Under 1", "1-4", "5-14", "15-64", "65 and over") )
## Warning in var_cut("Age group", "AGE", c(-Inf, 0, 4, 14, 64, Inf), c("Under 1",
## : Age group: overwriting a variable that already exists.
var_cross("Age x Sex", "AGER", "SEX")
To see the possible values of MAJOR
(Major reason for
this visit), and to estimate the total count for preventive care
visits:
tab("MAJOR")
Major reason for this visit {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 8250. Checked NCHS presentation standards. Nothing to report. |
To create the tables of age, sex, and their interaction, and limit them to only the preventive care visits:
tab_subset("AGER", "MAJOR", "Preventive care")
Patient age recode (Major reason for this visit = Preventive care) {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 1568. Checked NCHS presentation standards. Nothing to report. |
tab_subset("Age group", "MAJOR", "Preventive care")
Age group (Major reason for this visit = Preventive care) {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 1568. Checked NCHS presentation standards. Nothing to report. |
tab_subset("SEX", "MAJOR", "Preventive care")
Patient sex (Major reason for this visit = Preventive care) {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 1568. Checked NCHS presentation standards. Nothing to report. |
tab_subset("Age x Sex", "MAJOR", "Preventive care")
Age x Sex (Major reason for this visit = Preventive care) {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 1568. Checked NCHS presentation standards. Nothing to report. |
As each of the above commands is similar, and differs only in the
first variable that is passed to the tab_subset()
function,
this code can be streamlined with a for
loop:
for (vr in c("AGER", "Age group", "SEX", "Age x Sex")) {
print( tab_subset(vr, "MAJOR", "Preventive care") )
}
Patient age recode (Major reason for this visit = Preventive care) {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 1568. Checked NCHS presentation standards. Nothing to report. |
Age group (Major reason for this visit = Preventive care) {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 1568. Checked NCHS presentation standards. Nothing to report. |
Patient sex (Major reason for this visit = Preventive care) {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 1568. Checked NCHS presentation standards. Nothing to report. |
Age x Sex (Major reason for this visit = Preventive care) {NAMCS 2019 PUF} | |||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL |
---|---|---|---|---|---|---|---|---|---|
N = 1568. Checked NCHS presentation standards. Nothing to report. |
Note that when called from inside a for
loop, the
print()
function needs to be called explicitly.
More advanced coding
In addition, for each age-sex category, the published table shows the percentage of preventive care visits made to primary care physicians.
To calculate these percentages, a slightly more involved
for
loop is needed. Below is the code, followed by an
explanation:
set_opts(output = "csv", file = "my_output.csv")
## * Printing to CSV file my_output.csv.
for (vr in c("AGER", "Age group", "SEX", "Age x Sex")) {
var_cross("tmp", "MAJOR", vr)
for (lvl in levels(surveytable:::env$survey$variables[,vr])) {
print( tab_subset("SPECCAT", "tmp", paste0("Preventive care: ", lvl)) )
}
}
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: Under 15 years) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 15-24 years) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 25-44 years) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 45-64 years) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 65-74 years) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 75 years and over) {NAMCS 2019 PUF} to CSV file my_output.csv.
## Warning in var_cross("tmp", "MAJOR", vr): tmp: overwriting a variable that
## already exists.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: Under 1) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 1-4) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 5-14) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 15-64) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 65 and over) {NAMCS 2019 PUF} to CSV file my_output.csv.
## Warning in var_cross("tmp", "MAJOR", vr): tmp: overwriting a variable that
## already exists.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: Female) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: Male) {NAMCS 2019 PUF} to CSV file my_output.csv.
## Warning in var_cross("tmp", "MAJOR", vr): tmp: overwriting a variable that
## already exists.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: Under 15 years: Female) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 15-24 years: Female) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 25-44 years: Female) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 45-64 years: Female) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 65-74 years: Female) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 75 years and over: Female) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: Under 15 years: Male) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 15-24 years: Male) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 25-44 years: Male) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 45-64 years: Male) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 65-74 years: Male) {NAMCS 2019 PUF} to CSV file my_output.csv.
## * Printing Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: 75 years and over: Male) {NAMCS 2019 PUF} to CSV file my_output.csv.
set_opts(output = "auto")
## * Printing with huxtable for screen, gt for HTML, or kableExtra for PDF.
- Because so many tables will be printed, we don’t want to print them
to the screen. Rather, we print them to a CSV file called
my_output.csv
. - Since
tab_subset()
is called from within afor
loop, to print the tables, we need to call theprint()
function explicitly. - As before, the loop goes through the age, sex, and age / sex
interaction variables, calling each of these variables
vr
. - MAJOR and
vr
are crossed, with the result stored in a variable calledtmp
. - Next, the inner loop goes through all levels of
vr
, calling each of these levelslvl
. - The code tabulates
SPECCAT
(Type of specialty – Primary, Medical, Surgical) on a subset in whichtmp
(which isMAJOR
crossed withvr
) is restricted to"Preventive care: "
followed bylvl
, which is some level ofvr
, such as “Under 15 years” forAGER
. - Finally, CSV output is turned off.
If you run this code, all of the tables should be stored in the CSV file. To give you an idea of what the tables should look like, here is just one of the tables:
vr = "AGER"
var_cross("tmp", "MAJOR", vr)
## Warning in var_cross("tmp", "MAJOR", vr): tmp: overwriting a variable that
## already exists.
lvl = levels(surveytable:::env$survey$variables[,vr])[1]
tab_subset("SPECCAT", "tmp", paste0("Preventive care: ", lvl))
Type of specialty (Primary, Medical, Surgical) (tmp = Preventive care: Under 15 years) {NAMCS 2019 PUF} | ||||||||||
Level | n | Number (000) | SE (000) | LL (000) | UL (000) | Percent | SE | LL | UL | Flags |
---|---|---|---|---|---|---|---|---|---|---|
N = 300. Checked NCHS presentation standards: R: If the data is confidential, suppress all estimates, SE’s, CI’s, etc.; Cx: suppress count (and rate); Px: suppress percent; Pc: footnote percent - complement. |
To match the percentage in the published table, see the “Primary care specialty” row. Be sure to check the presentation standards flags.