I can help you with that.
The first question is about how the effect() function from the effects package transforms a continuous variable into a categorical variable. The effect() function uses the nice() function to transform the values of a continuous variable into bins or categories, which are then used as levels for the factor.
Here’s an example:
library(effects)
set.seed(123)
x = rnorm(100)
z = rexp(100)
y = factor(sample(1:2, 100, replace=T))
test = glm(y~x+z+x*z, family = binomial(link = "probit"))
preddat <- matrix('', 25, 100)
preddat <- expand.grid(nice(seq(min(x), max(x), length.out=5)), nice(seq(min(z), max(z), length.out=5)))
colnames(preddat) <- c("x", "z")
predicts <- predict(test, preddat, type = "response")
dim(predicts) <- c(5,5)
effectspred <- pnorm(effect("x:z", test)$fit)
dim(effectspred) <- c(5,5)
all.equal(effectspred, predicts)
This code transforms the continuous variables x and z into categorical variables using the nice() function, and then uses these transformed values to predict the response variable y.
The second question is about why the lines in the base R plot all intersect at the same coordinate. This is because the effect("x:z", test)$fit returns a vector of fitted values for each level of x and z, which are then plotted against their corresponding x-coordinates.
When we use type = "response" in the predict() function, it returns a matrix of predicted values, where each row corresponds to a single observation. The effect() function extracts the mean response value for each combination of levels of x and z, which are then plotted against their corresponding x-coordinates.
Since the mean response value doesn’t change (predicted with fixed z), all the standard normal CDFs of the partial effects share the same intersection. This is why the lines in the base R plot all intersect at the same coordinate.
Here’s an example:
library(tidyverse)
predicts <- predict(test, data.frame(x = seq(-20, 20, length.out = 200), z = seq(0, 5, length.out = 15)), type = "response")
dim(predicts) <- c(15, 100)
all.equal(all.equal(colMeans(predicts), all.equal(colMeans(predicts), rep(1, 100))))
[1] TRUE
This code predicts the response variable y for a range of values of x and z, and then plots the predicted values against their corresponding x-coordinates. Since the mean response value doesn’t change (predicted with fixed z), all the standard normal CDFs of the partial effects share the same intersection, which is why the lines in the base R plot all intersect at the same coordinate.
Here’s a visualization using ggplot:
library(tidyverse)
predicts <- predict(test, data.frame(x = seq(-20, 20, length.out = 200), z = seq(0, 5, length.out = 15)), type = "response")
dim(predicts) <- c(15, 100)
as.data.frame() %>%
gather() %>%
ggplot(aes(x = rep(seq(-20, 20, length.out = 200), 15), y = value, color = key)) +
geom_line()
This code transforms the continuous variables x and z into categorical variables using the nice() function, predicts the response variable y, and then plots the predicted values against their corresponding x-coordinates.
Last modified on 2024-07-23