tags:

views:

157

answers:

2

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the fitted model.

So I try something like:

model <- lm(x~y+z)
plot(x)
lines(fitted(model))

But this just plots x with lines.

Thanks

+3  A: 

I think you want abline(model) here as in this example from the help page:

 z <- lm(dist ~ speed, data = cars)
 plot(cars)
 abline(z) # equivalent to abline(reg = z) or
 abline(coef = coef(z))
Dirk Eddelbuettel
doesn't work. For one thing, I get an error saying that it can only use the first 2 regression coefficients (I have 4)
Karl
That's special case. Exactly how do you expect to plot a response over four dependents? Five-dimensional plots are hard... So what is often done is to fix n-1 of these at some value (say their mean) and then vary the n-th along with response variable. That way you get several charts of y versus the different x_i.
Dirk Eddelbuettel
I don't really need all that, all I need is for the vector of fitted values (which I already have) to be plotted on a line.So I suppose I can just say that what I am asking is, if I have time series x, and time series y, then how do I plot both on the same graph, where x is a scatter plot, and y is a line graph.
Karl
plot( fitted(mymodel), type='l') -- if you don't provide an x vector, the sequence is used. If you have an x vector, use plot(x, fitted(mymodel), type='l')
Dirk Eddelbuettel
+2  A: 

`x<- rnorm(100)

y <- rnorm(100)

z<- rnorm(100)

model <- lm(x~y+z)

plot(x,type="l",col="green")

lines(fitted(model),col="blue") `

I tried this and it seems to work

harshsinghal