strengejacke / sjPlot

sjPlot - Data Visualization for Statistics in Social Science
https://strengejacke.github.io/sjPlot
609 stars 93 forks source link

Matrix Issue with Centered Predictor in plot_model #489

Closed bbjonz closed 5 years ago

bbjonz commented 5 years ago

Had a weird error message from plot_model using a centered predictor in a logistic regression. Here's the code:

admit.dat <- read.csv("https://stats.idre.ucla.edu/stat/data/binary.csv")
admit.dat$rank.f <- factor(admit.dat$rank, levels = c(1,2,3,4), labels = c("Top","Hi Middle","Low Middle", "Bottom"))
admit.dat$admit.f <- factor(admit.dat$admit, levels = c(0,1), labels = c("Deny","Admit"))

#center the gre variable
admit.dat$gre.c <- as.numeric(scale(admit.dat$gre, center = TRUE, scale = FALSE))

#full model, no centering
full.model <- glm(admit.f ~ rank.f*gre, data=admit.dat, family = "binomial")

#plot works fine
sjPlot::plot_model(full.model, type = "pred", terms = c("gre", "rank.f")) 

#full model, gre centered
full.c.model <- glm(admit.f ~ rank.f*gre.c, data=admit.dat, family = "binomial")

#plot command
sjPlot::plot_model(full.c.model, type = "pred", terms = c("gre.c", "rank.f")) 

The error for the last plot command reads:

Error: variable 'gre.c' was fitted with type "nmatrix.1" but type "numeric" was supplied

Thoughts? Thanks in advance.

Joe

 

strengejacke commented 5 years ago

Not sure what scale() does here, but try sjmisc::center() to center your variables. Does that help?

bbjonz commented 5 years ago

Hi Daniel,

It did help, thanks. But not sure why. The scale command is base R version of standardizing/centering and it produces exactly the same values and structure (dbl) that sjmisc::center() does. Strange one...

strengejacke commented 5 years ago

Are all packages up to date? For me, all your examples run fine w/o any error... I'm using R 3.6, and the latest sjPlot / ggeffects /insigh packages.

bbjonz commented 5 years ago

Yes, and have the devel version of sjPlot. I'm sure you know that R 3.6.0 dropped a few days ago, so I had to update/reinstall all packages after the update.

strengejacke commented 5 years ago

Which OS are you using?

strengejacke commented 5 years ago

bump

bbjonz commented 5 years ago

Sorry. Busy time. Mac 10.14.4. Same problem on Linux 18.04

strengejacke commented 5 years ago

I can test only on Windows, but your example here works fine. As sjmisc::center() currently solves your problem, I'l close this issue. Maybe we can re-open if we have better possibilities to check what is going on.

gjoue commented 3 years ago

Hi,

I just wanted to add a workaround solution to this thread. I ran into the same problem doing the same thing (adding a scaled version of a column to a data frame -- either by doing df$x_sc = scale(df$x), df['x_sc'] = scale(df$x), or df %>% mutate(x_sc = scale(x)) -- and fitting my model using that new column, and then when I run predict() or anything that calls it, I get the same error that Joe got about fitted with type "nmatrix.1" but type "numeric" was supplied.

It seems like a legacy of how new columns are quickly added to data frames -- as a complete matrix (see this forum post), so I didn't trigger the error when I did my scaling on the fly when I was calling my model fitting function. My workaround is to add my new column this way:

df['x_sc'] = as.data.frame(scale(df$x))

This, by the way, did not work: df$x_sc = as.data.frame(scale(df$x))

Hope that helps others out there, Gina