# Thread: R: plotting values a regression model produces

1. ## R: plotting values a regression model produces

I have a regression model in R, and I want to make a plot of the actual values against the values the model yields. For example, if my real data is 12 at data point 1 but the model yields a value of 13.5 at data point 1, I want to overlay those two plots together, so I can visually see how close to the real data my model is.

I know I've done this before, but I can't remember how to do it.
Getting my real data is easy, via something like
ts.plot(x) or plot(x)
but I can't figure out how to overlay the regression model.

I realize I can create a vector by putting each x value through my model and getting what it would yield, but it would be a real pain to do that (at least using any methods I'm aware of), since I have both the month of the year and the week of the month as factors.
Which makes me want to ask: is there an easy way to tell R "Here is my model and my x-values. Tell me what my model says for these X values."

R and SAS are the only languages I have access to for this. I'd take an answer in SAS, but R is preferable as that's where I derived my model. I tried Google, but the answers I'm getting aren't to the question I intend to ask.

2. ## Re: R: plotting values a regression model produces

Originally Posted by JeenLeen
I have a regression model in R, and I want to make a plot of the actual values against the values the model yields. For example, if my real data is 12 at data point 1 but the model yields a value of 13.5 at data point 1, I want to overlay those two plots together, so I can visually see how close to the real data my model is.

I know I've done this before, but I can't remember how to do it.
Getting my real data is easy, via something like
ts.plot(x) or plot(x)
but I can't figure out how to overlay the regression model.

I realize I can create a vector by putting each x value through my model and getting what it would yield, but it would be a real pain to do that (at least using any methods I'm aware of), since I have both the month of the year and the week of the month as factors.
Which makes me want to ask: is there an easy way to tell R "Here is my model and my x-values. Tell me what my model says for these X values."

R and SAS are the only languages I have access to for this. I'd take an answer in SAS, but R is preferable as that's where I derived my model. I tried Google, but the answers I'm getting aren't to the question I intend to ask.
plot(x)
abline(LinearModel_object) reference
Not going to type out argurments for curve(...) on my phone, but that should get you started.

3. ## Re: R: plotting values a regression model produces

Thanks, but I'm still not getting it to work. Let me lay out what I have.

My model, called timeReg, is made by:
timeReg=lm(d ~ as.factor(week) + as.factor(month) + dummy -1, data=combo)

where week is the week number (1-5) in a given month, month is the month number (1-12) in the year, and dummy is to compensate for an outlier (it is 1 at the outlier, 0 otherwise). All are vectors combined into one time series with the raw data, called d.

ts.plot(d) gives me the original data.
But plot(timeReg) gives diagnostics, curve gives error message, and abline(timeReg) gives a horizontal line.

For curve, I tried curve(timeReg), curve(timeReg\$fit), and curve(timeReg\$model).

HOWEVER, between starting this post and now I went through some notes and found the code I used previously, which works.
Code:
```> ts.plot(d, col='red')
> lines(as.vector(time(d)), fitted(timeReg, col='blue'))```
Thanks for your help, though. I wonder if it's something about the time series nature that's disrupting the functions that usually work.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•