Mediation and binary logistic regression - is this even possible with R?

46 views Asked by At

I am a beginner and have my term paper due next week. As I said, I'm carrying out a binary logistic regression with Mediator. I wanted to calculate the effect using mediate(), but I get an error:

Error in mediate(Model2, Model3, treat = "Sat_dem", mediator = "Corona", : number of observations do not match between mediator and outcome models

To understand, I did this:

Model1 <- glm(data$second_vote ~ data$Sat_dem, data = data, family = "binomial")
Model2 <- glm(data$Corona ~ data$Sat_dem + data$Gender + data$Income + data$Education +     data$Religion, data = data)
Model3 <- glm(data$second_vote ~ data$Sat_dem + data$Corona + data$Gender + data$Income + data$Education + data$Religion, data = data, family = "binomial")
results <- mediate(Model2, Model3, treat = "Sat_dem", mediator = "Corona", covariates = c("Gender", "Income", "Education", "Religion") ~ NULL)

All packages are available. I first tried swapping Model2 and Model3 into the term, which didn't work. Then I tried to split the file:

set.seed(42)
split <- sample.split(data, SplitRatio = 0.8)
train_data <- subset(data, split == TRUE)
test_data <- subset(data, split == FALSE)

First came this:

Error in quantile.default(y, probs = seq(0, 1, length = groups)) : missing values and NaN's not allowed if 'na.rm' is FALSE

I really tried to fix this problem somehow, but it just didn't work. Then I tried again manually:

set.seed(42)
split_manual <- sample(2, nrow(data), replace = TRUE, prob = c(0.8, 0.2))
train_data_manual <- data[split_manual == 1, ]

and only Model2 shows Error:

Error in glm.fit(x = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, :
NA/NaN/Inf in 'y'
In addition: Warning messages:
1: In Ops.factor(y, mu) : ‘-’ not meaningful for factors
2: In Ops.factor(eta, offset) : ‘-’ not meaningful for factors
3: In Ops.factor(y, mu) : ‘-’ not meaningful for factors

Even if I use the original variables, I get an error. Now I'm wondering if this is even possible in the way I imagined?

0

There are 0 answers