Skip to content

Commit 80a16e2

Browse files
committed
1v
1 parent 05f0d65 commit 80a16e2

16 files changed

+3076
-71
lines changed

.Rhistory

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,3 @@
1-
unweighted_pcts_strata[lvl, s_lvl])
2-
}
3-
row_data[[s_lvl]] <- val
4-
}
5-
results_list[[length(results_list) + 1]] <- row_data
6-
}
7-
} else if (is.numeric(df[[var]])) {
8-
unweighted_mean_overall <- mean(df[[var]], na.rm = TRUE)
9-
unweighted_sd_overall <- stats::sd(df[[var]], na.rm = TRUE)
10-
unweighted_mean_strata <- tapply(df[[var]], df[[strata_var]], mean, na.rm = TRUE)
11-
unweighted_sd_strata <- tapply(df[[var]], df[[strata_var]], stats::sd, na.rm = TRUE)
12-
if (mode %in% c("weighted", "mixed")) {
13-
weighted_mean_overall <- svymean(var_formula, design, na.rm = TRUE)
14-
weighted_var_overall <- svyvar(var_formula, design, na.rm = TRUE)
15-
weighted_stats_strata <- svyby(var_formula, strata_formula, design, svymean, na.rm = TRUE)
16-
weighted_var_strata <- svyby(var_formula, strata_formula, design, svyvar, na.rm = TRUE)
17-
}
18-
row_data <- data.frame(Variable = "", Level = "Mean (SD)", stringsAsFactors = FALSE)
19-
if (mode %in% c("mixed", "weighted")) {
20-
val <- sprintf("%.2f (%.2f)", weighted_mean_overall, sqrt(weighted_var_overall))
21-
} else {
22-
val <- sprintf("%.2f (%.2f)", unweighted_mean_overall, unweighted_sd_overall)
23-
}
24-
row_data$Overall <- val
25-
for (i in seq_along(strata_levels)) {
26-
s_lvl <- strata_levels[i]
27-
if (mode %in% c("mixed", "weighted")) {
28-
mean_val <- weighted_stats_strata[i, var]
29-
sd_val <- sqrt(weighted_var_strata[i, var])
30-
val <- sprintf("%.2f (%.2f)", mean_val, sd_val)
31-
} else {
32-
val <- sprintf("%.2f (%.2f)", unweighted_mean_strata[s_lvl], unweighted_sd_strata[s_lvl])
33-
}
34-
row_data[[s_lvl]] <- val
35-
}
36-
results_list[[length(results_list) + 1]] <- row_data
37-
}
38-
}
39-
final_table <- do.call(rbind, results_list)
40-
return(final_table)
41-
}
42-
# --- 3. Running Example: Flegal (2016) NHANES Data ---
43-
# --- Data Preparation ---
44-
# Load the dataset from a working URL.
45-
load(url("https://raw.githubusercontent.com/epiverse-trace/datasets/raw/main/RData/Flegal2016.RData"))
46-
load("E:/GitHub/EpiMethods/Data/surveydata/Flegal2016.RData")
47-
# --- 1. Load Required Libraries ---
48-
# This script requires the 'survey' and 'dplyr' packages.
49-
# If you don't have them, run: install.packages(c("survey", "dplyr"))
50-
library(survey)
51-
library(dplyr)
52-
# --- 2. The Final, Self-Contained svytable1 Function ---
53-
# This function is robust and handles survey complexities internally.
54-
svytable1 <- function(design, strata_var, table_vars,
55-
mode = "mixed", commas = TRUE) {
56-
# --- Lonely PSU Handling ---
57-
old_option <- getOption("survey.lonely.psu")
58-
options(survey.lonely.psu = "adjust")
59-
on.exit(options(survey.lonely.psu = old_option))
60-
# --- Input Validation ---
61-
df <- design$variables
62-
all_vars <- c(strata_var, table_vars)
63-
missing_vars <- all_vars[!all_vars %in% names(df)]
64-
if (length(missing_vars) > 0) {
65-
stop(paste("The following variables were not found in the data:",
66-
paste(missing_vars, collapse = ", ")))
67-
}
68-
# --- Helper function for formatting ---
691
format_num <- function(n, is_weighted) {
702
if (is_weighted) n <- round(n)
713
if (commas) return(format(n, big.mark = ","))
@@ -510,3 +442,71 @@ devtools::check() # Full check with vignettes
510442
devtools::check() # Full check with vignettes
511443
# 8. IF VIGNETTES ARE IMPORTANT:
512444
devtools::build_vignettes()
445+
# 1. SETUP (do this first, once per session)
446+
library(devtools)
447+
library(roxygen2)
448+
# 3. UPDATE DOCUMENTATION
449+
devtools::document() # This runs roxygen2::roxygenise()
450+
# 4. REMOVE OLD VERSION (if testing locally)
451+
remove.packages("svyTable1")
452+
# 5. TEST YOUR CHANGES
453+
devtools::test() # Run unit tests
454+
# 6. INSTALL LOCALLY TO TEST
455+
devtools::install() # Install and load to test interactively
456+
# 6. INSTALL LOCALLY TO TEST
457+
devtools::install() # Install and load to test interactively
458+
# 7. COMPREHENSIVE PACKAGE CHECK
459+
devtools::check() # Full check with vignettes
460+
# 1. SETUP (do this first, once per session)
461+
library(devtools)
462+
library(roxygen2)
463+
# 2. MAKE YOUR CODE CHANGES first (edit R/ files)
464+
# ... edit your functions, add features, fix bugs ...
465+
# 3. UPDATE DOCUMENTATION
466+
devtools::document() # This runs roxygen2::roxygenise()
467+
# 4. REMOVE OLD VERSION (if testing locally)
468+
remove.packages("svyTable1")
469+
# 5. TEST YOUR CHANGES
470+
devtools::test() # Run unit tests
471+
# 6. INSTALL LOCALLY TO TEST
472+
devtools::install() # Install and load to test interactively
473+
# 7. COMPREHENSIVE PACKAGE CHECK
474+
devtools::check() # Full check with vignettes
475+
# 1. SETUP (do this first, once per session)
476+
library(devtools)
477+
library(roxygen2)
478+
# 3. UPDATE DOCUMENTATION
479+
devtools::document() # This runs roxygen2::roxygenise()
480+
# 4. REMOVE OLD VERSION (if testing locally)
481+
remove.packages("svyTable1")
482+
# 5. TEST YOUR CHANGES
483+
devtools::test() # Run unit tests
484+
# 6. INSTALL LOCALLY TO TEST
485+
devtools::install() # Install and load to test interactively
486+
devtools::check(build_args = "--no-build-vignettes")
487+
devtools::document()
488+
devtools::check(build_args = "--no-build-vignettes")
489+
devtools::document()
490+
devtools::check(build_args = "--no-build-vignettes")
491+
# 1. SETUP (do this first, once per session)
492+
library(devtools)
493+
library(roxygen2)
494+
# 2. MAKE YOUR CODE CHANGES first (edit R/ files)
495+
# ... edit your functions, add features, fix bugs ...
496+
# 3. UPDATE DOCUMENTATION
497+
devtools::document() # This runs roxygen2::roxygenise()
498+
# 4. REMOVE OLD VERSION (if testing locally)
499+
remove.packages("svyTable1")
500+
# 5. TEST YOUR CHANGES
501+
devtools::test() # Run unit tests
502+
# 6. INSTALL LOCALLY TO TEST
503+
devtools::install()
504+
devtools::check(build_args = "--no-build-vignettes")
505+
devtools::document()
506+
devtools::check(build_args = "--no-build-vignettes")
507+
# 8. IF VIGNETTES ARE IMPORTANT:
508+
devtools::build_vignettes()
509+
# 3. UPDATE DOCUMENTATION
510+
devtools::document() # This runs roxygen2::roxygenise()
511+
# 8. IF VIGNETTES ARE IMPORTANT:
512+
devtools::build_vignettes()

DESCRIPTION

Lines changed: 7 additions & 3 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.10.1
3+
Version: 0.12.0
44
Authors@R: c(person("Ehsan", "Karim",
55
email = "[email protected]",
66
role = c("aut", "cre")),
@@ -12,16 +12,20 @@ Description: A tool to create publication-ready tables from complex
1212
interaction reports. It also provides a suite of functions to
1313
evaluate survey-weighted regression models and survey-weighted
1414
survival plots, including coefficient diagnostics,
15-
goodness-of-fit tests, and design-correct AUC calculations.
15+
goodness-of-fit tests, design-correct AUC calculations,
16+
proportional hazards assumption testing, and visualization
17+
of interaction effects.
1618
License: MIT + file LICENSE
1719
Encoding: UTF-8
1820
RoxygenNote: 7.3.3
1921
Imports:
2022
dplyr,
23+
emmeans,
2124
ggplot2,
2225
kableExtra,
2326
knitr,
2427
magrittr,
28+
mice,
2529
msm,
2630
patchwork,
2731
Publish,
@@ -40,8 +44,8 @@ Imports:
4044
WeightedROC,
4145
broom
4246
Suggests:
43-
mice,
4447
NHANES,
48+
mitools,
4549
testthat (>= 3.0.0)
4650
VignetteBuilder: knitr
4751
Config/testthat/edition: 3

NAMESPACE

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ export(addint)
55
export(addintlist)
66
export(inteffects)
77
export(jointeffects)
8+
export(plotint)
89
export(reportint)
910
export(svyAUC)
11+
export(svycoxph_CE)
12+
export(svycoxph_CE_mi)
1013
export(svydiag)
1114
export(svygof)
1215
export(svykmplot)
1316
export(svypooled)
1417
export(svytable1)
18+
import(emmeans)
19+
import(ggplot2)
1520
import(stats)
1621
importFrom(Publish,publish)
1722
importFrom(WeightedROC,WeightedAUC)
@@ -26,9 +31,11 @@ importFrom(dplyr,group_by)
2631
importFrom(dplyr,if_else)
2732
importFrom(dplyr,left_join)
2833
importFrom(dplyr,mutate)
34+
importFrom(dplyr,n)
2935
importFrom(dplyr,relocate)
3036
importFrom(dplyr,rename)
3137
importFrom(dplyr,select)
38+
importFrom(dplyr,setdiff)
3239
importFrom(dplyr,sym)
3340
importFrom(dplyr,tibble)
3441
importFrom(dplyr,ungroup)
@@ -57,6 +64,7 @@ importFrom(kableExtra,kable_styling)
5764
importFrom(kableExtra,pack_rows)
5865
importFrom(knitr,kable)
5966
importFrom(magrittr,"%>%")
67+
importFrom(mice,pool)
6068
importFrom(msm,deltamethod)
6169
importFrom(patchwork,plot_layout)
6270
importFrom(purrr,map_dfr)
@@ -89,6 +97,7 @@ importFrom(survey,degf)
8997
importFrom(survey,regTermTest)
9098
importFrom(survey,svyby)
9199
importFrom(survey,svyciprop)
100+
importFrom(survey,svycoxph)
92101
importFrom(survey,svydesign)
93102
importFrom(survey,svyglm)
94103
importFrom(survey,svykm)
@@ -98,6 +107,7 @@ importFrom(survey,svytable)
98107
importFrom(survey,svyvar)
99108
importFrom(survey,withReplicates)
100109
importFrom(survival,Surv)
110+
importFrom(survival,survSplit)
101111
importFrom(tibble,as_tibble)
102112
importFrom(tibble,tibble)
103113
importFrom(tidyr,pivot_longer)

0 commit comments

Comments
 (0)