gabriel
gabriel

Reputation: 399

Visual Graph of Regression in R

I am trying to run a basic time series regression in R.

reg<-lm(y~x)
summary(reg)
Call:lm(formula = y ~ x)

Residuals:
     Min       1Q   Median       3Q      Max 
-100.188  -21.600    0.503   21.999   97.296 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 102.53835    4.66296  21.990   <2e-16 ***
x            -0.03687    0.04524  -0.815    0.415   

However I would also like a visual plot of the y variable, the model's projection and the residuals underneath. Is there a capability in R to have this kind of regression? Thank you!

Upvotes: 0

Views: 272

Answers (1)

Thilo
Thilo

Reputation: 9167

I'm not totally sure what you want to plot. The following gives you two plots - one with the data and the model prediction and one with the residuals.

par(mfrow=c(2,1))  # Two plots in one window

plot(x,y)                # Your datapoints
lines(x,predict(reg))    # The model prediction

plot(x, residuals(reg), ylab='Residuals')    # x vs. residuals

R's plotting facilities for linear models are fairly good. I strongly suggest you to have a look at the output of plot(reg), which probably gives you far more information than your plots if interpreted correctly.

Upvotes: 2

Related Questions