【问题标题】:Plotting SPSS style moderation (interaction) example绘制 SPSS 样式调节(交互)示例
【发布时间】:2016-05-14 21:25:40
【问题描述】:

我正在尝试在 R 中绘制类似 SPSS 样式调节图 here (bottom image) 的东西。我尝试使用 QuantPsyc 包但完全失败了。有人可以帮我正确绘制吗?我不期望图像中的确切数据,这只是我想要的格式。我在这个例子中使用data(tra)。我试过了:

data(tra)
lm.mod1 <- moderate.lm(beliefs, values, attitudes, tra)
ss.mod1 <- sim.slopes(lm.mod1, tra$values)
## requires user interaction 
graph.mod(ss.mod1,beliefs,attitudes,tra,"Interaction Example")

这根本不是我想要的。感谢我找到 this post 的 cmets,这让我找到了这段代码:

library(effects)
data(Prestige)
mod5 <- lm(prestige ~ income*type + education, data=Prestige)
eff_cf <- effect("income*type", mod5)
print(plot(eff_cf, multiline=TRUE))

更接近,但仍然没有正确的格式,具体来说:

  • 绘制的数据不限于高于和低于平均值的一个标准差(我相信在示例中就是这样)

  • x 轴被标记(它不在 SPSS 中)

【问题讨论】:

标签: r statistics


【解决方案1】:

您可以通过预测值来相对轻松地手动执行此操作。我猜您的帖子中有某种错字,因为“类型”是 Prestige 数据集中的分类预测器。我假设您想要“收入*教育”。对于绘图,我使用 ggplot2 包只是因为我比基本图形更熟悉它,但那里可能也有解决方案。我在这里的绘图细节已经过火了,但是您似乎真的想完全模仿您链接的示例,所以我就是这样做的。

# model
mod5 <- lm(prestige ~ income * education, data = Prestige)

# first, create data frame to store predictions
m <- mean(Prestige$education) # mean of education
s <- sd(Prestige$education) # sd of education
newdata <- data.frame(education = c(rep(m - s, 2), rep(m, 2), rep(m + s, 2)), # add in +/- 1 SD and mean
    income = with(Prestige, rep(range(income), 3))) # range of income values

# predict new values using the predict() function
newdata$prestige.predicted <- predict(mod5, newdata)

# plot
library(ggplot2)
ggplot(newdata, aes(x = income, y = prestige.predicted, linetype = factor(education))) +
    geom_line() +
    scale_linetype_manual(values = c("dotted", "solid", "dashed"), name = "Education", labels = c("1 SD Below Mean", "Mean", "1 SD Above Mean")) +
    theme_bw() +
    theme(axis.text.x = element_blank(), # get rid of axis labels
          axis.ticks.x = element_blank(),
          panel.grid.major.x = element_blank(), # get rid of vertical lines
          panel.grid.minor.x = element_blank(),
          panel.grid.major.y = element_line(color = "black"), # make horizontal gridlines black
          panel.grid.minor.y = element_blank())

Resulting graph

【讨论】:

    猜你喜欢
    • 2016-05-08
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    相关资源
    最近更新 更多