Partial residual plots for linear model including an interaction term

后端 未结 2 1494
天涯浪人
天涯浪人 2021-01-14 10:48

My model includes one response variable, five predictors and one interaction term for predictor_1 and predictor_2. I would like to plot partial residual plots for every pred

相关标签:
2条回答
  • 2021-01-14 11:12

    Another way to do this is to put the interaction term in as a separate variable (which avoids hacking the code for crPlot(...)).

    df <- data.frame(R,P1,P2,P1.P2=P1*P2)
    lm.fit1 <- lm(R ~ ., df)
    summary(lm.fit1)
    crPlots(lm.fit1)
    

    Note that summary(lm.fit1) yeilds exactly the same result as summary(lm(R~P1*P2,df)).

    0 讨论(0)
  • 2021-01-14 11:15

    I must admit i'm not that familiar with partial residual plots so i'm not entirely sure what the proper interpretation of them should be given an interaction term. But basically, the equivalent of

    crPlot(lm.fit1, "P1")
    

    is

    x <- predict(lm.fit1, type="term", term="P1")
    y <- residuals(lm.fit1, type="partial")[,"P1"]
    plot(x, y)
    abline(lm(y~x), col="red", lty=2)
    loessLine(x,y,col="green3",log.x = FALSE, log.y = FALSE, smoother.args=list())
    

    so really, there's no real reason the same idea couldn't work with an interaction term as well. We just leave the partial contribution from a variable due to the interaction as a separate entity and just focus on the non-interaction contribution. So what i'm going to do is just take out the check for the interaction term and then we can use the function. Assuming that

    body(car:::crPlot.lm)[[11]]
    # if (any(attr(terms(model), "order") > 1)) {
    #     stop("C+R plots not available for models with interactions.")
    # }
    

    we can copy and modify to create a new function with out the check

    crPlot2 <- car:::crPlot.lm
    body(crPlot2) <- body(crPlot2)[-11]
    environment(crPlot2) <- asNamespace("car")
    

    And then we can run

    layout(matrix(1:2, ncol=2))
    crPlot2(lm.fit2, "P1")
    crPlot2(lm.fit2, "P2")
    

    to get

    enter image description here

    I'm sure the authors had a good reason for not incorporating models with interaction terms so use this hack at your own risk. It's just unclear to me what should happen to the residual from the interaction term when making the plot.

    0 讨论(0)
提交回复
热议问题