leeper / margins

An R Port of Stata's 'margins' Command
https://cloud.r-project.org/package=margins
Other
260 stars 39 forks source link

at=list error "undefined columns selected" #173

Closed mariebk closed 3 years ago

mariebk commented 3 years ago

Hello community, I am a R beginner! Maybe there is an easy solution and you can help? Sorry for not providing a dataset, every single step in R is hard for me. Thanks for your help!

Please specify whether your issue is about:

If you are reporting (1) a bug or (2) a question about code, please supply:

Put your code here:


## load package
library("margins")

## code goes here

reg_detachment_wtcbreaks_model4 <- lm(formula = detachment ~ dummy1 + dummy2 + dummy3 + dummy4  + dummy5 + dummy6 + continuous + dummy7 + age + wtc_breaks + age*wtc_breaks, data=data_final) # all dummies are factors, the others are dbl+lbl

margins::margins(reg_detachment_wtcbreaks_model4, variables="wtc_breaks", at=list(age=c(23,24,25))) # works fine if I run it without the at-argument

## error message

> traceback()
8: stop("undefined columns selected")
7: `[.data.frame`(dat, , not_numeric, drop = FALSE)
6: dat[, not_numeric, drop = FALSE]
5: check_values(data, at)
4: check_at(data, at)
3: build_datalist(data, at = at)
2: margins.lm(reg_detachment_wtcbreaks_model4, variables = "wtc_breaks", 
       at = list(age = c(23, 24, 25)))
1: margins::margins(reg_detachment_wtcbreaks_model4, variables = "wtc_breaks", 
       at = list(age = c(23, 24, 25)))

## sessionInfo

R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C                    LC_TIME=German_Germany.1252    

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

other attached packages:
 [1] margins_0.3.26  Hmisc_4.5-0     ggplot2_3.3.3   Formula_1.2-4   survival_3.2-11 lattice_0.20-44 sjPlot_2.8.8   
 [8] ggeffects_1.1.0 lavaan_0.6-8    horst_0.1       dplyr_1.0.6     haven_2.4.1     psych_2.1.6    

loaded via a namespace (and not attached):
  [1] nlme_3.1-152        fs_1.5.0            usethis_2.0.1       devtools_2.4.2      insight_0.14.2     
  [6] RColorBrewer_1.1-2  rprojroot_2.0.2     tools_4.1.0         backports_1.2.1     utf8_1.2.1         
 [11] R6_2.5.0            sjlabelled_1.1.8    rpart_4.1-15        DBI_1.1.1           colorspace_2.0-1   
 [16] nnet_7.3-16         withr_2.4.2         gridExtra_2.3       tidyselect_1.1.1    prettyunits_1.1.1  
 [21] mnormt_2.0.2        processx_3.5.2      emmeans_1.6.1       compiler_4.1.0      performance_0.7.2  
 [26] cli_2.5.0           htmlTable_2.2.1     desc_1.3.0          bayestestR_0.10.0   checkmate_2.0.0    
 [31] scales_1.1.1        mvtnorm_1.1-2       readr_1.4.0         callr_3.7.0         stringr_1.4.0      
 [36] digest_0.6.27       pbivnorm_0.6.0      foreign_0.8-81      minqa_1.2.4         htmltools_0.5.1.1  
 [41] jpeg_0.1-8.1        base64enc_0.1-3     pkgconfig_2.0.3     lme4_1.1-27         sessioninfo_1.1.1  
 [46] fastmap_1.1.0       htmlwidgets_1.5.3   rlang_0.4.11        rstudioapi_0.13     generics_0.1.0     
 [51] magrittr_2.0.1      parameters_0.14.0   Matrix_1.3-3        Rcpp_1.0.6          munsell_0.5.0      
 [56] fansi_0.4.2         prediction_0.3.14   lifecycle_1.0.0     stringi_1.6.1       MASS_7.3-54        
 [61] pkgbuild_1.2.0      grid_4.1.0          parallel_4.1.0      sjmisc_2.8.7        forcats_0.5.1      
 [66] crayon_1.4.1        splines_4.1.0       sjstats_0.18.1      hms_1.1.0           tmvnsim_1.0-2      
 [71] knitr_1.33          ps_1.6.0            pillar_1.6.1        boot_1.3-28         estimability_1.3   
 [76] effectsize_0.4.5    stats4_4.1.0        pkgload_1.2.1       glue_1.4.2          latticeExtra_0.6-29
 [81] data.table_1.14.0   remotes_2.4.0       modelr_0.1.8        vctrs_0.3.8         png_0.1-7          
 [86] nloptr_1.2.2.2      testthat_3.0.3      gtable_0.3.0        purrr_0.3.4         tidyr_1.1.3        
 [91] assertthat_0.2.1    cachem_1.0.5        xfun_0.23           xtable_1.8-4        broom_0.7.6        
 [96] tibble_3.1.1        tinytex_0.31        memoise_2.0.0       cluster_2.1.2       ellipsis_0.3.2     
> 
mariebk commented 3 years ago

Solved, age was dbl+lbl. It works when I transform age to numeric!