Skip to content

Commit 74a3bbb

Browse files
committed
km
1 parent 916f182 commit 74a3bbb

14 files changed

Lines changed: 762 additions & 77 deletions

.Rhistory

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,3 @@
1-
for (var in table_vars) {
2-
var_formula <- stats::as.formula(paste0("~", var))
3-
strata_formula <- stats::as.formula(paste0("~", strata_var))
4-
var_header_row <- as.data.frame(setNames(as.list(c(var, rep("", ncol(header_row) - 1))), names(header_row)))
5-
results_list[[length(results_list) + 1]] <- var_header_row
6-
if (is.factor(df[[var]])) {
7-
df[[var]] <- droplevels(df[[var]])
8-
unweighted_counts_overall <- table(df[[var]])
9-
unweighted_pcts_overall <- stats::prop.table(unweighted_counts_overall) * 100
10-
unweighted_counts_strata <- table(df[[var]], df[[strata_var]])
11-
unweighted_pcts_strata <- stats::prop.table(unweighted_counts_strata, margin = 2) * 100
12-
if (mode %in% c("weighted", "mixed")) {
13-
weighted_counts_overall <- svytable(var_formula, design)
14-
weighted_pcts_overall <- svymean(var_formula, design, na.rm = TRUE) * 100
15-
weighted_counts_strata <- svytable(stats::as.formula(paste0("~", var, "+", strata_var)), design)
16-
weighted_pcts_strata <- svyby(var_formula, strata_formula, design, svymean, na.rm = TRUE)
17-
}
18-
for (lvl in levels(df[[var]])) {
19-
row_data <- data.frame(Variable = "", Level = lvl, stringsAsFactors = FALSE)
20-
if (mode == "mixed") {
21-
val <- sprintf("%s (%.1f%%)", format_num(unweighted_counts_overall[lvl], FALSE),
22-
weighted_pcts_overall[paste0(var, lvl)])
23-
} else if (mode == "weighted") {
24-
val <- sprintf("%s (%.1f%%)", format_num(weighted_counts_overall[lvl], TRUE),
25-
weighted_pcts_overall[paste0(var, lvl)])
26-
} else {
27-
val <- sprintf("%s (%.1f%%)", format_num(unweighted_counts_overall[lvl], FALSE),
28-
unweighted_pcts_overall[lvl])
29-
}
30-
row_data$Overall <- val
31-
for (s_lvl in strata_levels) {
32-
if (mode == "mixed") {
33-
val <- sprintf("%s (%.1f%%)", format_num(unweighted_counts_strata[lvl, s_lvl], FALSE),
34-
weighted_pcts_strata[s_lvl, paste0(var, lvl)] * 100)
35-
} else if (mode == "weighted") {
36-
val <- sprintf("%s (%.1f%%)", format_num(unweighted_counts_strata[lvl, s_lvl], TRUE),
37-
weighted_pcts_strata[s_lvl, paste0(var, lvl)] * 100)
38-
} else {
39-
val <- sprintf("%s (%.1f%%)", format_num(unweighted_counts_strata[lvl, s_lvl], FALSE),
401
unweighted_pcts_strata[lvl, s_lvl])
412
}
423
row_data[[s_lvl]] <- val
@@ -510,3 +471,42 @@ rhub::rhub_doctor()
510471
rhub::rhub_check()
511472
rhub::rhub_check()
512473
devtools::check_win_devel()
474+
# 1. SETUP (do this first, once per session)
475+
library(devtools)
476+
library(roxygen2)
477+
# 3. UPDATE DOCUMENTATION
478+
devtools::document() # This runs roxygen2::roxygenise()
479+
# 3. UPDATE DOCUMENTATION
480+
devtools::document() # This runs roxygen2::roxygenise()
481+
help(package = "utils")
482+
?tempfile
483+
# 3. UPDATE DOCUMENTATION
484+
devtools::document() # This runs roxygen2::roxygenise()
485+
# 4. REMOVE OLD VERSION (if testing locally)
486+
remove.packages("svyTable1")
487+
# 5. TEST YOUR CHANGES
488+
devtools::test() # Run unit tests
489+
# 6. INSTALL LOCALLY TO TEST
490+
devtools::install() # Install and load to test interactively
491+
# 7. COMPREHENSIVE PACKAGE CHECK
492+
devtools::check() # Full check with vignettes
493+
tools::showNonASCIIfile("R/reportint.R")
494+
# 7. COMPREHENSIVE PACKAGE CHECK
495+
devtools::check() # Full check with vignettes
496+
# 1. SETUP (do this first, once per session)
497+
library(devtools)
498+
library(roxygen2)
499+
# 3. UPDATE DOCUMENTATION
500+
devtools::document() # This runs roxygen2::roxygenise()
501+
# 4. REMOVE OLD VERSION (if testing locally)
502+
remove.packages("svyTable1")
503+
# 5. TEST YOUR CHANGES
504+
devtools::test() # Run unit tests
505+
# 6. INSTALL LOCALLY TO TEST
506+
devtools::install() # Install and load to test interactively
507+
# 7. COMPREHENSIVE PACKAGE CHECK
508+
devtools::check() # Full check with vignettes
509+
# 7. COMPREHENSIVE PACKAGE CHECK
510+
devtools::check() # Full check with vignettes
511+
# 8. IF VIGNETTES ARE IMPORTANT:
512+
devtools::build_vignettes()

DESCRIPTION

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: svyTable1
22
Title: Create Survey-Weighted Descriptive Statistics and Diagnostic Tables
3-
Version: 0.9.0
3+
Version: 0.10.0
44
Authors@R: c(person("Ehsan", "Karim",
55
email = "ehsan.karim@gmail.com",
66
role = c("aut", "cre")),
@@ -10,33 +10,40 @@ Authors@R: c(person("Ehsan", "Karim",
1010
Description: A tool to create publication-ready tables from complex
1111
survey data, including descriptive summaries and multi-panel
1212
interaction reports. It also provides a suite of functions to
13-
evaluate survey-weighted regression models, including coefficient
14-
diagnostics, goodness-of-fit tests, and design-correct AUC
15-
calculations.
13+
evaluate survey-weighted regression models and survey-weighted
14+
survival plots, including coefficient diagnostics,
15+
goodness-of-fit tests, and design-correct AUC calculations.
1616
License: MIT + file LICENSE
1717
Encoding: UTF-8
1818
RoxygenNote: 7.3.3
1919
Imports:
2020
dplyr,
21+
ggplot2,
2122
kableExtra,
2223
knitr,
2324
magrittr,
2425
msm,
26+
patchwork,
2527
Publish,
28+
purrr,
2629
rlang,
2730
rmarkdown,
2831
rstudioapi,
32+
scales,
2933
stats,
3034
stringr,
35+
survival,
3136
survey,
3237
tibble,
3338
tidyr,
3439
utils,
3540
WeightedROC
3641
Suggests:
37-
ggplot2,
3842
mice,
3943
NHANES,
4044
testthat (>= 3.0.0)
4145
VignetteBuilder: knitr
4246
Config/testthat/edition: 3
47+
Depends:
48+
R (>= 3.5)
49+
LazyData: true

NAMESPACE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export(reportint)
99
export(svyAUC)
1010
export(svydiag)
1111
export(svygof)
12+
export(svykmplot)
1213
export(svypooled)
1314
export(svytable1)
1415
import(stats)
@@ -19,12 +20,33 @@ importFrom(dplyr,"%>%")
1920
importFrom(dplyr,across)
2021
importFrom(dplyr,bind_rows)
2122
importFrom(dplyr,case_when)
23+
importFrom(dplyr,count)
24+
importFrom(dplyr,group_by)
2225
importFrom(dplyr,if_else)
2326
importFrom(dplyr,left_join)
2427
importFrom(dplyr,mutate)
2528
importFrom(dplyr,relocate)
2629
importFrom(dplyr,rename)
2730
importFrom(dplyr,select)
31+
importFrom(dplyr,sym)
32+
importFrom(dplyr,ungroup)
33+
importFrom(ggplot2,aes)
34+
importFrom(ggplot2,annotate)
35+
importFrom(ggplot2,coord_cartesian)
36+
importFrom(ggplot2,element_blank)
37+
importFrom(ggplot2,element_text)
38+
importFrom(ggplot2,geom_point)
39+
importFrom(ggplot2,geom_ribbon)
40+
importFrom(ggplot2,geom_step)
41+
importFrom(ggplot2,geom_text)
42+
importFrom(ggplot2,ggplot)
43+
importFrom(ggplot2,labs)
44+
importFrom(ggplot2,scale_color_manual)
45+
importFrom(ggplot2,scale_fill_manual)
46+
importFrom(ggplot2,scale_x_continuous)
47+
importFrom(ggplot2,scale_y_discrete)
48+
importFrom(ggplot2,theme)
49+
importFrom(ggplot2,theme_classic)
2850
importFrom(graphics,abline)
2951
importFrom(graphics,plot)
3052
importFrom(graphics,title)
@@ -34,12 +56,15 @@ importFrom(kableExtra,pack_rows)
3456
importFrom(knitr,kable)
3557
importFrom(magrittr,"%>%")
3658
importFrom(msm,deltamethod)
59+
importFrom(patchwork,plot_layout)
60+
importFrom(purrr,map_dfr)
3761
importFrom(rlang,":=")
3862
importFrom(rlang,.data)
3963
importFrom(rlang,sym)
4064
importFrom(rmarkdown,render)
4165
importFrom(rstudioapi,isAvailable)
4266
importFrom(rstudioapi,viewer)
67+
importFrom(scales,comma)
4368
importFrom(stats,coef)
4469
importFrom(stats,confint)
4570
importFrom(stats,fitted)
@@ -64,12 +89,16 @@ importFrom(survey,svyby)
6489
importFrom(survey,svyciprop)
6590
importFrom(survey,svydesign)
6691
importFrom(survey,svyglm)
92+
importFrom(survey,svykm)
93+
importFrom(survey,svylogrank)
6794
importFrom(survey,svymean)
6895
importFrom(survey,svytable)
6996
importFrom(survey,svyvar)
7097
importFrom(survey,withReplicates)
98+
importFrom(survival,Surv)
7199
importFrom(tibble,as_tibble)
72100
importFrom(tibble,tibble)
73101
importFrom(tidyr,pivot_longer)
74102
importFrom(tidyr,pivot_wider)
103+
importFrom(tools,toTitleCase)
75104
importFrom(utils,browseURL)

R/data.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#' Processed NHANES Mortality Data for Diabetic Adults
2+
#'
3+
#' A subset of the NHANES 1999-2010 data, filtered for adults (>=18)
4+
#' with diabetes and plausible caloric intake (500-3500 kcal).
5+
#' This data is prepared for survival analysis on the relationship
6+
#' between caffeine consumption and mortality.
7+
#'
8+
#' @format A data frame with 3780 rows and 23 variables:
9+
#' \describe{
10+
#' \item{id}{Respondent sequence/ID number}
11+
#' \item{survey.weight}{Full sample 2-year weights}
12+
#' \item{psu}{Masked pseudo-PSU}
13+
#' \item{strata}{Masked pseudo-stratum}
14+
#' \item{caff}{Caffeine consumption (Factor: "No consumption", "<100 mg/day", etc.)}
15+
#' \item{stime}{Follow-up time in months}
16+
#' \item{status}{Mortality status (0=Censored, 1=Deceased)}
17+
#' \item{sex}{Sex (Factor: "Male", "Female")}
18+
#' \item{age}{Age in years at interview}
19+
#' \item{race}{Race/ethnicity (Factor)}
20+
#' \item{smoking}{Smoking status (Factor)}
21+
#' \item{bmi.cat}{BMI category (Factor)}
22+
#' \item{education}{Education level (Factor)}
23+
#' \item{carbohyd}{Carbohydrate in gm}
24+
#' \item{alcohol}{Alcohol consumption (Factor)}
25+
#' \item{htn}{Hypertension status (Factor)}
26+
#' \item{macrovascular}{Macrovascular complications (Character: "No", "Yes")}
27+
#' \item{insulin}{Insulin use (Factor: "No", "Yes")}
28+
#' \item{survey.cycle}{Survey cycle (Character: "1999-00", etc.)}
29+
#' \item{physical.activity}{Physical activity level (Factor)}
30+
#' \item{diabetes}{Diabetes status (Factor, all "Yes")}
31+
#' \item{cal.total}{Total calories in kcal}
32+
#' \item{survey_weight}{Adjusted 6-cycle survey weight (survey.weight / 6)}
33+
#' }
34+
#' @source Data originally from NHANES and processed according to the
35+
#' "Survival analysis: NHANES" tutorial.
36+
"nhanes_mortality"

R/globals.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
utils::globalVariables(c(
2-
".", "term", "estimate", "conf.low", "conf.high", "group",
3-
"Characteristic", "Estimate_CI", "p_value_formatted"
4-
))
1+
utils::globalVariables(c("."))

R/svydiag.R

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#' @importFrom dplyr mutate select
4444
#' @importFrom tibble tibble
4545
#' @importFrom stats confint coef vcov
46+
#' @importFrom rlang .data
4647
#'
4748
#' @export
4849
#'
@@ -122,23 +123,23 @@ svydiag <- function(fit, p_threshold = 0.05, rse_threshold = 30) {
122123
# 3. Calculate derived metrics, add flags, and finalize the output
123124
reliability_df <- reliability_df %>%
124125
dplyr::mutate(
125-
RSE_percent = (SE / abs(Estimate)) * 100,
126-
CI_Width = CI_Upper - CI_Lower,
127-
is_significant = p.value < p_threshold,
128-
is_rse_high = RSE_percent >= rse_threshold
126+
RSE_percent = (.data$SE / abs(.data$Estimate)) * 100,
127+
CI_Width = .data$CI_Upper - .data$CI_Lower,
128+
is_significant = .data$p.value < p_threshold,
129+
is_rse_high = .data$RSE_percent >= rse_threshold
129130
) %>%
130131
# Reorder and select the final columns for a clean output
131132
dplyr::select(
132-
Term,
133-
Estimate,
134-
SE,
135-
p.value,
136-
is_significant,
137-
CI_Lower,
138-
CI_Upper,
139-
CI_Width,
140-
RSE_percent,
141-
is_rse_high
133+
.data$Term,
134+
.data$Estimate,
135+
.data$SE,
136+
.data$p.value,
137+
.data$is_significant,
138+
.data$CI_Lower,
139+
.data$CI_Upper,
140+
.data$CI_Width,
141+
.data$RSE_percent,
142+
.data$is_rse_high
142143
)
143144

144145
return(reliability_df)

0 commit comments

Comments
 (0)