我创建了下面的图来可视化交互,但我想知道我是否可以自定义图中“教育=大学教育=研究生教育=高中”的部分。
代码:
# Generate example data
set.seed(123)
education <- factor(rep(c("High School", "College", "Graduate"), each = 30))
study_hours <- rep(c(10, 20, 30), each = 10, times = 3)
exam_scores <- rnorm(90, mean = 70, sd = 10)
# Create dataframe
example_df <- data.frame(education, study_hours, exam_scores)
# Fit the model with interaction term
library(glmmTMB)
models_example <- glmmTMB(exam_scores ~ education * study_hours, data = example_df)
# Generate the effects plot for the interaction term
library(effects)
effects_plot_example <- plot(allEffects(models_example, data = example_df))
# Display the plot
print(effects_plot_example)
我尝试过的示例:
Educational Level
,但空间给了我这个错误Error in parse(text = paste("~", paste(extras, collapse = "+"))) :
<text>:1:28: unexpected symbol
如果直接使用
effects:::plot.eff()
,它将生成一个plot.eff
对象,您可以修改该对象的strip
函数,该函数用于创建要更改的标签。
library(lattice)
# save the plot object
ef.plot <- effects:::plot.eff(allEffects(models_example, data = example_df)[[1]])
# change the function which draws strips at the top of the panels to a custom one
ef.plot$strip <- strip.custom(
var.name="Educational Level",
factor.levels=c("College", "Graduate", "High School"),
strip.names=c(T, T),
strip.levels=c(T, T),
sep=': ')
# draw the plot with the new strip function
ef.plot