0

The command predict <- ggpredict(fit_tw1, terms = "pko_dummy") does not work and it gives me the following error. Do you know how to solve my problem?

Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : factor as.factor(pa_dummy) has new level 0.0906593406593407

Can you help me?

Model: (The Model has fixed effects for countries (cown) and years (year))

fit_tw1 <- lm(parl_wom.per ~ as.factor(pko_dummy)*as.factor(pa_dummy) + as.factor(cown) + as.factor(year) + female_pko.per + lf_wom.per + ss.per + fdi.per + jud_ind.per + polity + as.factor(intensity_level) + as.factor(cons_ref),
data = subset(data9, rownames!="639"))

Reproducible sample of the dataset

structure(list(cown = c(432, 432, 432, 432, 432, 432, 432, 432, 
432, 432, 432, 432, 432, 432, 432, 432, 432, 432, 432, 432), 
    year = c(1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 
    1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 
    2008, 2009), intensity_level = c("1", "1", "0", "0", "1", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "1", "1", "1"), pa_dummy = c(0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), pko_dummy = c(0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), parl_wom.per = c(NA, 
    NA, 0.023, 0.023, 0.023, 0.023, 0.023, 0.122449, 0.122449, 
    0.122449, 0.122449, 0.122449, 0.1020408, 0.1020408, 0.1020408, 
    0.1020408, 0.1020408, 0.1020408, 0.1020408, 0.1020408), exe_wom.per = c(0.0588235, 
    0.1052632, 0.0526316, 0.0952381, 0.1111111, 0.0555556, 0.125, 
    0.1176471, 0.2608696, 0.2727273, 0.45, 0.4210526, 0.15, 0.15, 
    0.15, 0.1923077, 0.1923077, 0.1923077, 0.1851852, 0.1785714
    ), gender_mean = c(0, 0, 1.75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0), gender_art = c(0, 0, 7, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), female_pko.per = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    lf_wom.per = c(0.60855, 0.60834, 0.6082, 0.60815, 0.6082, 
    0.60838, 0.60806, 0.60798, 0.60804, 0.60811, 0.60813, 0.60782, 
    0.60752, 0.60725, 0.60701, 0.60681, 0.60616, 0.60564, 0.60525, 
    0.60495), ss.per = c(0.0679798984527588, 0.0723097991943359, 
    0.0827134037017822, 0.0837932968139648, 0.0957365036010742, 
    0.107322397232056, 0.112752199172974, 0.122838802337646, 
    0.133676099777222, 0.151076498031616, 0.174537200927734, 
    NA, NA, 0.221253795623779, 0.239939594268799, 0.25832540512085, 
    0.277074604034424, 0.303055400848389, 0.33731990814209, 0.36671989440918
    ), fdi.per = c(0.0021364, 0.0004424, -0.0077276, 0.001441, 
    0.0083661, 0.0411724, 0.009786, 0.0275705, 0.0032724, 0.0090061, 
    0.0203215, 0.0602065, -0.0031506, 0.0153489, 0.015555, 0.0256452, 
    0.0214593, 0.0252638, 0.0270809, 0.0631946), ele.sy = c(1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 
    polity = c(-7, NA, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 
    7, 7, 7, 7, 7), mus.per = c(0.944, 0.944, 0.944, 0.944, 0.944, 
    0.944, 0.944, 0.944, 0.944, 0.944, 0.944, 0.944, 0.944, 0.944, 
    0.944, 0.944, 0.944, 0.944, 0.944, 0.944), cons_ref = c(0, 
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 
    jud_ind.per = c(0.476311308991478, 0.523786338536123, 0.557528417528326, 
    0.548066004702523, 0.548066004702523, 0.548066004702523, 
    0.548066004702523, 0.548066004702523, 0.548066004702523, 
    0.548066004702523, 0.548066004702523, 0.548066004702523, 
    0.548066004702523, 0.548066004702523, 0.548066004702523, 
    0.539288342106394, 0.539288342106394, 0.548066004702523, 
    0.539288342106394, 0.539288342106394)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))
  • 1
    I think you're going to need to give us some more observations - the model doesn't produce coefficients for all the variables because many of them are invariant in the subset you gave us. – DaveArmstrong May 11 '22 at 15:40
  • Based on the error message, I am 93% sure that `ggpredict` is trying to create a "representative grid" of regressor values on which to compute predictions. But it is setting those values at the mean, which causes problems for numeric variables that should be treated as factor. I dealt with the same problem [in the `marginaleffects` package](https://vincentarelbundock.github.io/marginaleffects/) and try to return informative error messages, but it's a trickier issue than it sounds. If I were you, I would try converting my variables to factors before fitting the model instead of in the formula. – Vincent May 11 '22 at 17:20
  • Okay, I have added more observations to the example provided. Is there another way to solve the problem without converting my variables into factors before fitting the model? – Andrea Fontana May 11 '22 at 18:33
  • The code in your example does not run, even after adding rows. Please make sure you provide a minimal WORKING example. I suggest you use the `reprex` package. Here are some additional tips: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 – Vincent May 11 '22 at 18:45

0 Answers0