How to create a graph showing the predictive model, data and residuals in R

前端 未结 2 808
再見小時候
再見小時候 2021-01-12 14:26

Given two variables, x and y, I run a dynlm regression on the variables and would like to plot the fitted model against one of the variables and th

2条回答
  •  感情败类
    2021-01-12 15:19

    what you're looking for is resid(model). Try this:

    library(dynlm)
    x <- 10+rnorm(100)
    y <- 10+rnorm(100)
    model <- dynlm(x ~ y)
    
    plot(x, type="l", col="red", ylim=c(min(c(x,y,resid(model))), max(c(x,y,resid(model)))))
    lines(y, type="l", col="green")
    lines(resid(model), type="l", col="blue")
    

    enter image description here

提交回复
热议问题