leeper / margins

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

Plotting marginal effects fails #147

Open mronkko opened 4 years ago

mronkko commented 4 years ago

Please specify whether your issue is about:

The problem seems to be that the "at" attribute of the margins object contains a column named index, but there is no corresponding column in the summary(margins) output.

Put your code here:

## load package
library("margins")

## code goes here
library(carData)

data(Prestige)

womenValues <- c(0, 25, 50, 75, 97.51)

m5 <- lm(income ~ education*women, data = Prestige)

mar5 <- margins(m5, variables = "education",
                at = list(women = womenValues))

plot(mar5)

10: stop("undefined columns selected")
9: `[.data.frame`(summ, , names(attributes(x)[["at"]]), drop = FALSE)
8: summ[, names(attributes(x)[["at"]]), drop = FALSE]
7: unique(summ[, names(attributes(x)[["at"]]), drop = FALSE])
6: plot.margins(mar5)
5: plot(mar5) at .active-rstudio-document#13
4: eval(ei, envir)
3: eval(ei, envir)
2: withVisible(eval(ei, envir))
1: source("~/.active-rstudio-document", echo = TRUE)

## session info for your system
sessionInfo()

R version 4.0.0 (2020-04-24) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS Catalina 10.15.4

Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages: [1] carData_3.0-4 margins_0.3.23 maxLik_1.3-8 miscTools_0.6-26

loaded via a namespace (and not attached): [1] Rcpp_1.0.4.6 pillar_1.4.4 compiler_4.0.0 tools_4.0.0 digest_0.6.25 lifecycle_0.2.0 tibble_3.0.1
[8] gtable_0.3.0 lattice_0.20-41 pkgconfig_2.0.3 rlang_0.4.6 Matrix_1.2-18 cli_2.0.2 rstudioapi_0.11
[15] dplyr_0.8.5 vctrs_0.3.0 grid_4.0.0 tidyselect_1.1.0 glue_1.4.1 data.table_1.12.8 R6_2.4.1
[22] fansi_0.4.1 prediction_0.3.14 ggplot2_3.3.0 purrr_0.3.4 magrittr_1.5 scales_1.1.1 ellipsis_0.3.1
[29] MASS_7.3-51.6 assertthat_0.2.1 colorspace_1.4-1 sandwich_2.5-1 munsell_0.5.0 crayon_1.3.4 zoo_1.8-8

vincentarelbundock commented 4 years ago

Are you sure you shouldn't be using the cplot command instead? It looks like you care about variation in the marginal effect of education for different values of women, which is exactly what cplot is for.

duckmayr commented 2 years ago

@vincentarelbundock It's probably safe to close this issue... I had the exact same issue as the original poster, and investigating the problem led me to this issue page, and you were 100% right, the problem was just needing to use cplot() rather than plot.margins()

vincentarelbundock commented 2 years ago

@duckmayr thanks, but I didn't open this issue and I don't manage the repository, so I can't close the issue. Maybe the confusion from the fact that I maintain a package with a very similar name ;)