How to plot a comparisson of two fixed categorical values for linear regression of another continuous variable

前端 未结 3 1190
情书的邮戳
情书的邮戳 2021-01-25 01:42

So I want to plot this:

lmfit = lm (y ~ a + b)

but, \"b\" only has the values of zero and one. So, I want to plot two separate regre

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-25 02:25

    Given you have an additive lm model to begin with, drawing the lines is pretty straightforward, even though not completely intuitive. I tested it with the following simulated data:

    y <- rnorm(30)
    a <- rep(1:10,times=3)
    b <- rep(c(1,0),each=15)
    
    LM <- lm(y~a+b)
    

    You have to access the coefficient values in the lm. Its is:

    LM$coefficients
    

    Here comes the tricky part, you have to assign the coefficients for each line.

    The first one is easy:

    abline(LM$coef[1],LM$coef[2])
    

    The other one is a bit more complicated, given R works with additive coefficients, so for the second line you have:

    abline(LM$coef[1]+LM$coef[3],LM$coef[2])
    

    I hope this is what you was expecting

提交回复
热议问题