I encountered a problem in R when trying to bootstrap my multiple regression with a continous variable as outcome and several variables as predictors (factors and numeric). When running , R throws the error
Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels
The data, however, seem to be okay , also I don’t have any NA in my dataset.
tibble [61 × 13] (S3: tbl_df/tbl/data.frame)
$ PTBS_Gesamt : num [1:61] 29 20 28 32 54 27 22 25 31 26 …
$ PHQ4_Gesamt : num [1:61] 10 5 6 7 7 10 5 5 7 7 …
$ Wohlbefinden_Gesamt : num [1:61] 26 32 24 31 23 28 33 31 31 24 …
$ Erwartungen_Geburt_Gesamt : num [1:61] -5 4 1 -5 2 2 2 9 -9 -1 …
$ Erwartungen_Zeit_nach_Geburt_Gesamt: num [1:61] 58 -6 9 21 -3 30 12 18 7 26 …
$ Objektive_Geburtserfahrung : num [1:61] 1 0 1 2 2 0 0 1 1 0 …
$ Differenz_Geburtsort : Factor w/ 2 levels “Keine Übereinstimmung”,..: 1 2 2 2 2 2 2 2 2 2 …
$ DE09 : num [1:61] 4 4 1 4 1 2 5 2 5 5 …
$ DE06 : Factor w/ 2 levels “Ja1″,”Ja2”: 2 2 2 2 1 2 2 2 2 2 …
$ DE07 : Factor w/ 2 levels “Ja1″,”Ja2”: 1 2 2 1 1 2 2 2 2 2 …
$ KV02 : Factor w/ 2 levels “Ja1″,”Ja2”: 1 2 2 1 1 1 2 1 1 1 …
$ Wohlbefinden_Baseline : num [1:61] 25 28 25 26 28 22 28 25 27 28 …
$ PHQ4_Baseline : num [1:61] 11 5 5 5 8 7 5 4 6 5 …
But still it doesnt work…
set.seed(12345)
model_PTBS_mk_b <- lm(data = joined_data_relevant, PTBS_Gesamt ~ Erwartungen_Geburt_Gesamt + Erwartungen_Zeit_nach_Geburt_Gesamt+ Objektive_Geburtserfahrung + Differenz_Geburtsort + DE09 + KV02 + DE06 + DE07)
fit_PTBS_mk_b <- Boot(model_PTBS_mk_b, R = 5000)
Fehler incontrasts<-
(*tmp*
, value = contr.funs[1 + isOF[nn]]) :
Kontraste können nur auf Faktoren mit 2 oder mehr Stufen angewendet werden
Any ideas what goes wrong here?
and my session info:
sessionInfo()
R version 4.3.0 (2023-04-21 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22631)
Matrix products: default
locale:
[1] LC_COLLATE=German_Germany.utf8 LC_CTYPE=German_Germany.utf8 LC_MONETARY=German_Germany.utf8
[4] LC_NUMERIC=C LC_TIME=German_Germany.utf8
time zone: Europe/Berlin
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lmtest_0.9-40 zoo_1.8-12 boot_1.3-28.1 car_3.1-2 carData_3.0-5 psych_2.3.3 effectsize_0.8.6
[8] ggpubr_0.6.0 lubridate_1.9.2 forcats_1.0.0 stringr_1.5.0 purrr_1.0.1 readr_2.1.4 tibble_3.2.1
[15] ggplot2_3.4.4 tidyverse_2.0.0 dplyr_1.1.2 tidyr_1.3.0
loaded via a namespace (and not attached):
[1] gtable_0.3.3 xfun_0.39 bayestestR_0.13.1 insight_0.19.7 rstatix_0.7.2 lattice_0.21-8
[7] tzdb_0.4.0 vctrs_0.6.2 tools_4.3.0 generics_0.1.3 datawizard_0.9.0 parallel_4.3.0
[13] fansi_1.0.4 pkgconfig_2.0.3 lifecycle_1.0.3 compiler_4.3.0 munsell_0.5.0 mnormt_2.1.1
[19] htmltools_0.5.5 yaml_2.3.7 crayon_1.5.2 pillar_1.9.0 abind_1.4-5 nlme_3.1-162
[25] tidyselect_1.2.0 digest_0.6.31 mvtnorm_1.2-4 stringi_1.7.12 fastmap_1.1.1 grid_4.3.0
[31] colorspace_2.1-0 cli_3.6.1 magrittr_2.0.3 utf8_1.2.3 broom_1.0.4 withr_2.5.0
[37] scales_1.2.1 backports_1.4.1 bit64_4.0.5 timechange_0.2.0 estimability_1.4.1 rmarkdown_2.25
[43] emmeans_1.8.9 bit_4.0.5 ggsignif_0.6.4 hms_1.1.3 evaluate_0.21 knitr_1.43
[49] parameters_0.21.1 rlang_1.1.1 glue_1.6.2 rstudioapi_0.14 vroom_1.6.3 R6_2.5.1
I already bootstrapped the regression only with my main variables (the others are controlvariables)
there it worked just finde
model_PTBS_mk_b <- lm(data = joined_data_relevant, PTBS_Gesamt ~ Erwartungen_Geburt_Gesamt + Erwartungen_Zeit_nach_Geburt_Gesamt)
Anna Meyer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.