[英]What is the best way in R to plot predicted values and compare interaction terms in a random effects plm model?
我有一個plm隨機效應模型,該模型包括連續變量和分類變量之間的相互作用(出於說明目的,假設分類變量具有很強的宗教信仰,而連續變量則是對慈善事業的貢獻)。
這是示例數據(IRL,數據大得多)
ratio contrib relig np_score ID year
.4 3 1 11 1 1990
0 7 0 8 2 1990
.9 7 1 6 1 1992
.7 6 1 10 1 1994
.1 2 0 4 2 1992
.3 9 0 8 2 1994
我想隨着貢獻的增加來繪制預期的結果,這取決於受訪者是否認同強烈的宗教信仰。
m1 <- plm(ratio ~ contrib + relig + np_score + contrib*relig,
data = panel_dat,
index = c("ID", "year"),
model = "random", random.method = "amemiya")
我已經嘗試了interaction.plot,如下所示:
interaction.plot(panel_dat$contrib, panel_dat$relig, predict(m1), col = 2:3, lty = 1)
但是我得到了錯誤術語:
Error in tapply(response, list(x.factor, trace.factor), fun) :
arguments must have same length
我最終寧願使用ggplot2做到這一點。 有什么建議么? 似乎應該有一個簡單的答案...
此示例代碼生成下面的多線圖(在ggplot中)。 不確定軸是否正確...您可以根據需要進行調整。
# Create sample dataframe
df <- data.frame(
id = c(1, 1, 1, 2, 2, 2),
year = c(2000, 2001, 2002, 2000, 2001, 2002),
ratio = c(0.3, 0.6, 0.1, 0.4, 0.5, 0.6),
contrib = c(310, 220, 230, 140, 0, 10),
relig = c(1, 1, 1, 3, 3, 3)
)
# Build model and predictions
library(plm)
m1 <- plm(ratio ~ contrib + relig + contrib*relig, data = df, index = c('id','year'))
df$p_ratio <- predict(m1)
# Create plot
library(ggplot2)
ggplot(data=df, aes(x=contrib, y=p_ratio, group=relig))+geom_line(size=2, aes(color=relig))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.