Results 1 to 3 of 3

20171205, 11:36 PM (ISO 8601)
 Join Date
 Jan 2009
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 xvalues. 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.

20171206, 12:06 AM (ISO 8601)
 Join Date
 Apr 2010
 Location
 Night Vale
 Gender
Re: R: plotting values a regression model produces
plot(x)
abline(LinearModel_object) reference
Not going to type out argurments for curve(...) on my phone, but that should get you started.Avatar by TheGiant
Longform Sig

20171206, 10:13 PM (ISO 8601)
 Join Date
 Jan 2009
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 (15) in a given month, month is the month number (112) 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'))
Last edited by JeenLeen; 20171206 at 10:13 PM.