jinseob2kim / jstable

Regression Tables from 'GLM', 'GEE', 'GLMM', 'Cox' and 'survey' Results.
https://jinseob2kim.github.io/jstable/
Apache License 2.0
23 stars 13 forks source link

Error in UseMethod("svyglm", design) no applicable method for 'svyglm' applied to an object of class "function" #17

Closed weisx2022 closed 8 months ago

weisx2022 commented 9 months ago

Dear author.thanks for your excellent working, but i have some questions: I am analyzing weighted data from NHANES. I get such an error when I performed a subgroup analysis using weighted data by survey package.

library(tidyverse) library(data.table) library(survey) library(dplyr) library(gtsummary) library(flextable) library(jstable) load("./test.RData")

df <- svydesign(data = df.model.type, id = ~SDMVPSU,strata = ~SDMVSTRA,weights = ~weight, nest = T) svyglm(dc ~ gender,df) time.sub.res <- TableSubgroupGLM(dc ~ gender,var_subgroup = "bmi", data = df, family = "binomial") Error in UseMethod("svyglm", design) : no applicable method for 'svyglm' applied to an object of class "function"

But when I copy the code of TableSubgroupGLM to the external environment and it wroks fine. Could you please provide guidance or suggestions on how to resolve it? I appreciate your help!

This is my data: [Uploading test.csv…]()

This is my environment:

sessionInfo() R version 4.2.2 (2022-10-31 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale: [1] LC_COLLATE=Chinese (Simplified)_China.utf8 LC_CTYPE=Chinese (Simplified)_China.utf8 LC_MONETARY=Chinese (Simplified)_China.utf8 [4] LC_NUMERIC=C LC_TIME=Chinese (Simplified)_China.utf8

attached base packages: [1] grid stats graphics grDevices utils datasets methods base

other attached packages: [1] jstable_1.1.5 flextable_0.9.4 gtsummary_1.7.2 survey_4.2-1 survival_3.4-0 Matrix_1.6-1.1 rstudioapi_0.15.0 [8] rstudiothemes_0.1 rlist_0.4.6.2 TwoSampleMR_0.5.6 data.table_1.14.8 lubridate_1.9.2 forcats_1.0.0 stringr_1.5.0
[15] dplyr_1.1.2 purrr_1.0.2 readr_2.1.4 tidyr_1.3.0 tibble_3.2.1 ggplot2_3.4.4 tidyverse_2.0.0

loaded via a namespace (and not attached): [1] uuid_1.1-0 backports_1.4.1 Hmisc_5.0-1 systemfonts_1.0.4 plyr_1.8.8
[6] splines_4.2.2 mr.raps_0.2 usethis_2.1.6 TH.data_1.1-1 digest_0.6.31
[11] foreach_1.5.2 htmltools_0.5.4 fansi_1.0.4 magrittr_2.0.3 checkmate_2.1.0
[16] memoise_2.0.1 cluster_2.1.4 tzdb_0.3.0 openxlsx_4.2.5.2 remotes_2.4.2
[21] R.utils_2.12.2 officer_0.6.2 vroom_1.6.1 sandwich_3.0-2 bdsmatrix_1.3-6
[26] askpass_1.1 timechange_0.2.0 gfonts_0.2.0 prettyunits_1.1.1 colorspace_2.1-0
[31] mitools_2.4 haven_2.5.2 textshaping_0.3.6 xfun_0.37 callr_3.7.3
[36] crayon_1.5.2 jsonlite_1.8.7 lme4_1.1-31 geepack_1.3.10 zoo_1.8-11
[41] iterators_1.0.14 glue_1.6.2 gtable_0.3.3 MatrixModels_0.5-1 pkgbuild_1.4.0
[46] shape_1.4.6 rms_6.5-0 SparseM_1.81 scales_1.2.1 fontquiver_0.2.1
[51] mvtnorm_1.1-3 DBI_1.1.3 miniUI_0.1.1.1 Rcpp_1.0.11 xtable_1.8-4
[56] htmlTable_2.4.1 foreign_0.8-83 bit_4.0.5 Formula_1.2-5 fontLiberation_0.1.0
[61] glmnet_4.1-6 profvis_0.3.7 htmlwidgets_1.6.1 ellipsis_0.3.2 urlchecker_1.0.1
[66] pkgconfig_2.0.3 R.methodsS3_1.8.2 nnet_7.3-18 utf8_1.2.3 crul_1.3
[71] tidyselect_1.2.0 rlang_1.1.1 later_1.3.0 munsell_0.5.0 tools_4.2.2
[76] cachem_1.0.7 cli_3.6.2 generics_0.1.3 broom_1.0.5 devtools_2.4.5
[81] evaluate_0.20 fastmap_1.1.1 ragg_1.2.5 processx_3.8.0 knitr_1.42
[86] bit64_4.0.5 fs_1.6.1 zip_2.2.2 nlme_3.1-160 mime_0.12
[91] quantreg_5.94 R.oo_1.25.0 xml2_1.3.3 compiler_4.2.2 curl_5.0.0
[96] gt_0.9.0 broom.helpers_1.13.0 stringi_1.7.12 ps_1.7.2 gdtools_0.3.3
[101] lattice_0.20-45 nloptr_2.0.3 fontBitstreamVera_0.1.1 vctrs_0.6.5 pillar_1.9.0
[106] lifecycle_1.0.3 ieugwasr_0.1.5 httpuv_1.6.9 patchwork_1.1.2 R6_2.5.1
[111] promises_1.2.0.1 gridExtra_2.3 sessioninfo_1.2.2 codetools_0.2-18 polspline_1.1.22
[116] boot_1.3-28 MASS_7.3-58.1 pkgload_1.3.2 openssl_2.0.6 withr_2.5.1
[121] nortest_1.0-4 httpcode_0.3.0 coxme_2.2-18.1 multcomp_1.4-23 parallel_4.2.2
[126] hms_1.1.2 tableone_0.13.2 labelled_2.10.0 rpart_4.1.19 minqa_1.2.5
[131] rmarkdown_2.20 shiny_1.7.4 base64enc_0.1-3

jinseob2kim commented 9 months ago

@cyk0315 Please review the error

weisx2022 commented 9 months ago

Can you give me some specific instructions? I think it's a file format problem. when I trace the error,

pv_anova <- anova(model.int, method = "Wald")

it shows the above code may be the source of the error. I think the format of model.int is wrong, but in the function environment the class of model.int is svyglm.

debug(TableSubgroupGLM) time.sub.res <- TableSubgroupGLM(dc ~ gender,var_subgroup = "bmi", data = nhanes.design, family = "binomial")

Browse[2]> class(model.int) [1] "svyglm" "glm" "lm"

Why is model.int considered a class of function ?

cyk0315 commented 9 months ago

@weisx2022 Can you upload the data to reproduce?

weisx2022 commented 9 months ago

@weisx2022 Can you upload the data to reproduce?

This is my test data test.csv Thank you for your attention

cyk0315 commented 9 months ago

@weisx2022 Hello, I ran it with your data and found that the code below also has a problem. This was confirmed by the update function in the oneanova.svyglm function and the error in the update function I'm sorry I couldn't give you an exact answer

test<-read_csv("Downloads/test.csv")

df <- svydesign(data = test,
                id = ~SDMVPSU,strata = ~SDMVSTRA,weights = ~weight,
                nest = T)
model.int<-svyglm(dc ~ gender * bmi, design = df, family = quasibinomial())
anova(model.int, method = "Wald")
oneanova.svyglm<-function(object,test,method){
  tt<-terms(object)
  tlbls<-attr(tt,"term.labels")
  nt<-length(tlbls)
  if (nt<2) return(NULL)
  seqtests<-vector("list",nt)
  if(test=="F") ddf<-NULL else ddf<-Inf
  thismodel<-object
  if (!("formula") %in% names(thismodel$call)) 
    names(thismodel$call)[[2]] <- "formula"
  for(i in nt:1){
    thisterm<-tlbls[i]
    seqtests[[i]]<-regTermTest(thismodel,tlbls[i],method=method,df=ddf)
    thisformula<-make.formula(thisterm)[[2]]
    thismodel <- eval(bquote(update(thismodel, . ~ . - (.(thisformula)))))
  }
  class(seqtests)<-"seqanova.svyglm"
  attr(seqtests,"method")<-method
  attr(seqtests,"test")<-test
  seqtests
}
weisx2022 commented 9 months ago

@weisx2022 Hello, I ran it with your data and found that the code below also has a problem. This was confirmed by the update function in the oneanova.svyglm function and the error in the update function I'm sorry I couldn't give you an exact answer

test<-read_csv("Downloads/test.csv")

df <- svydesign(data = test,
                id = ~SDMVPSU,strata = ~SDMVSTRA,weights = ~weight,
                nest = T)
model.int<-svyglm(dc ~ gender * bmi, design = df, family = quasibinomial())
anova(model.int, method = "Wald")
oneanova.svyglm<-function(object,test,method){
  tt<-terms(object)
  tlbls<-attr(tt,"term.labels")
  nt<-length(tlbls)
  if (nt<2) return(NULL)
  seqtests<-vector("list",nt)
  if(test=="F") ddf<-NULL else ddf<-Inf
  thismodel<-object
  if (!("formula") %in% names(thismodel$call)) 
    names(thismodel$call)[[2]] <- "formula"
  for(i in nt:1){
    thisterm<-tlbls[i]
    seqtests[[i]]<-regTermTest(thismodel,tlbls[i],method=method,df=ddf)
    thisformula<-make.formula(thisterm)[[2]]
    thismodel <- eval(bquote(update(thismodel, . ~ . - (.(thisformula)))))
  }
  class(seqtests)<-"seqanova.svyglm"
  attr(seqtests,"method")<-method
  attr(seqtests,"test")<-test
  seqtests
}

thank you very much

cyk0315 commented 9 months ago

@weisx2022 I fix the problem. If you install the latest version, it will work.

remotes::install_github("jinseob2kim/jstable")

When it comes to errors in the anova function, it seems better to define the variable name as something other than df. Below is an example

testdata<-read_csv("Downloads/test.csv")

data.design<- svydesign(data = testdata,
                id = ~SDMVPSU,strata = ~SDMVSTRA,weights = ~weight,
                nest = T)
TableSubgroupGLM(dc ~ gender,var_subgroup = "bmi", data = data.design, family = "binomial")
model.int<-svyglm(dc ~ gender * bmi, design = data.design, family = quasibinomial())
anova(model.int, method = "Wald")