'Using ggpredict after feglm
I want to make a fixed-effects glm model with feglm and want to use ggpredict afterward. Yet, somehow I always get errors (see below). Is it possible to use ggpredict after feglm at all, or am I doing something wrong here?
# Make example data set
obsnum <- c(1:8000)
DF <- data.frame(obsnum) # Dataset with 8000 observations
DF$help1 <- 0
DF$help1[((DF$obsnum-1) %% 80) == 0] <- 1
DF$participantID <- cumsum(DF$help1) # Make participant var (cluster)
DF$itemNr <- data.table::rowid(DF$participantID) # ordered item numbers within participants
DF$V1 <- stats::rbinom(8000, 1, 0.5)
DF$V2 <- stats::rbinom(8000, 1, 0.5)
DF$V3 <- sample(1:6, 8000, replace = TRUE)
# Create random effect
DF$clustRandEffect <- rep(rnorm(100,0,0.05),each=80)
# First make percent with intercept ####
p_0 <- 0.7
p_V1 <- p_0 + 0.1
p_V2 <- p_0 - 0.1
p_V1V2 <- p_0 + 0.05
p_V3 <- p_0 + 0.05
# Then logs of odds ratios ####
fu_odds <- function(p) {
odds <- p/(1-p)
return (odds)
}
fu_oddR <- function(o){
oR <- o/fu_odds(p_0)
return(oR)
}
b0 <- log(fu_oddR(fu_odds(p_0)))
b_V1 <- log(fu_oddR(fu_odds(p_V1)))
b_V2 <- log(fu_oddR(fu_odds( p_V2)))
b_V1V2 <- log(fu_oddR(fu_odds(p_V1V2)))
b_V2 <- log(fu_oddR(fu_odds(p_V3)))
# y probability
DF$prop = stats::plogis((b0+b_V1*DF$V1+b_V2*DF$V2
+b_V1V2*DF$V1*DF$V2 + b_V2*DF$V3 + DF$clustRandEffect))
# Make Y ####
DF$Y <- rbinom(n= 8000, size = 1, DF$prop)
# Make model
# Version 1
model1 <- fixest::feglm(Y ~ V1 * V2 + V3 | participantID + itemNr, DF)
ggeffects::ggpredict(
model = model1,
terms = c("V1", "V2"),
cluster = DF$participantID, # Same error with or without cluster setting
ci.lvl = 0.90
) #Error in tapply(x$predicted, list(x$group), NULL) : Argumente müssen die selbe Länge haben
# Yet all have the same length
length(DF$Y) # 8000
length(DF$V1) # 8000
length(DF$V2) # 8000
# Version 2
model2 <- fixest::feglm(Y ~ V1 * V2 + V3 | participantID, DF)
ggeffects::ggpredict(
model = model2,
terms = c("V1", "V2"),
cluster = DF$participantID,
ci.lvl = 0.90
) # Error in tapply(x$predicted, list(x$group), NULL) : Argumente müssen die selbe Länge haben
ggeffects::ggpredict(
model = summary(model2),
terms = c("V1", "V2"),
cluster = DF$participantID,
ci.lvl = 0.90
) # Error in tapply(x$predicted, list(x$group), NULL) : Argumente müssen die selbe Länge haben
# Version 3
model3 <- fixest::feglm(Y ~ 0 + V1 * V2 + V3 | participantID, DF)
ggeffects::ggpredict(
model = model3,
terms = c("V1", "V2"),
cluster = DF$participantID,
ci.lvl = 0.90
) # Error in tapply(x$predicted, list(x$group), NULL) : Argumente müssen die selbe Länge haben
# Version 4 - Set DF as data frame again
DF <- as.data.frame(DF)
class(DF)
model4 <- fixest::feglm(Y ~ V1 * V2 + V3 | participantID, DF)
ggeffects::ggpredict(
model = model4,
terms = c("V1", "V2"),
# cluster = DF$participantID,
ci.lvl = 0.90
) # Error in tapply(x$predicted, list(x$group), NULL) : Argumente müssen die selbe Länge haben
PS: I have asked a similar question on ggpredict after bife here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
