1

I have a plm random effects model that includes an interaction between a continuous and a categorical variable (for illustrative purposes, let's say the categorical variable is strong religiosity and the continuous is contribution to charity).

Here is sample data (IRL, data much larger)

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

I want to plot the predicted outcomes as contribution increases depending on whether the respondent does or does not identify as strongly religious.

m1 <- plm(ratio ~ contrib + relig + np_score + contrib*relig, 
            data = panel_dat, 
            index = c("ID", "year"),
            model = "random", random.method = "amemiya")

I have tried interaction.plot as follows:

interaction.plot(panel_dat$contrib, panel_dat$relig, predict(m1), col = 2:3, lty = 1)

But I get the error term:

Error in tapply(response, list(x.factor, trace.factor), fun) : 
  arguments must have same length

I would ultimately rather do this with ggplot2. Any suggestions? Seems like there should be a simple answer...

user3385922
  • 179
  • 1
  • 1
  • 9
  • 1
    [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data and all necessary code. – camille Aug 13 '19 at 18:39
  • Also check out [glmmFAQ](http://bbolker.github.io/mixedmodels-misc/glmmFAQ.html#) by B. Bolker and possibly the [DHARMa](https://cran.r-project.org/web/packages/DHARMa/index.html) package. The latter is currently lacking scientific argumentation (although it seems somewhat promising). – Oliver Aug 13 '19 at 18:57

1 Answers1

0

This sample code produces the multiline plot below (in ggplot). Not sure if the axes are correct ... you can adjust as needed.

# 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))

enter image description here

Monk
  • 407
  • 3
  • 8